function init() {
	if($$('nav')) nav_init()
	if($$('.collapsomatic')) collapsomatic_init()
	if($$('#reservations .scal')) reservations_init()
	if($$('form')) form_init()
}

function arrange_zindexes(){
	var blocks = $$('div')
	var zi = 1000
	
	if(blocks){
	    blocks.each(function(block, i) {
	        block.style.zIndex = zi--
	    });
	}
}


/* ============== */
/* = NAVIGATION = */
/* ============== */

function nav_init(){
	$$('#nav ul ul').each(function(e){
		e.hide()
	})
	
	/*if(BrowserDetect.browser == "Explorer"){
		arrange_zindexes()
	}*/
}

/* ================= */
/* = COLLAPSOMATIC = */
/* ================= */

function collapsomatic_init(){
	$$('.collapsomatic').each(function(e){
		var height = e.getHeight() + 'px';
		e.setStyle({'height':height})
		e.setStyle({'display':'none'})
		e.previousSiblings().first().innerHTML += ' <span class="trigger down"><img src="images/blank.gif" width="9" alt=""/></span>'
	})
	
	$$('.trigger').each(function(e){
		//e.setStyle({'cursor':'pointer'})
		//Event.observe(e, 'click', toggle)

		// make the title also clickable
		e.ancestors().first().setStyle({'cursor' : 'pointer'})
		Event.observe(e.ancestors().first(), 'click', toggle)
		e.ancestors().first().addClassName("off");
	})
}

function toggle(e){
	var t;
	var block;
	if (this.hasClassName("on"))
	{
		this.removeClassName("on")
		this.addClassName("off")

	}
	else if (this.hasClassName("off"))
	{
		this.removeClassName("off")
		this.addClassName("on")
	}
	if(this.hasClassName("trigger")){
		t = this
		block = this.ancestors().first().nextSiblings().first()
	}else{
		t = this.childElements().first()
		block = this.nextSiblings().first()
	}
	
	Effect.toggle(block, 'blind', {duration: 0.3});
	
	/*
	if(t.hasClassName("up")){
		Effect.BlindUp(block)
		t.removeClassName("up")
		t.addClassName("down")
	}else{
		Effect.BlindDown(block)
		t.removeClassName("down")
		t.addClassName("up")
	}
	*/
}

/* ========= */
/* = FORMS = */
/* ========= */

function form_init(){
	$('error-text').setStyle({'display':'none'})
	$$('form input').each(function(e) {
		if(e.type == "text"){
			label = e.previousSiblings()[0]
			label.setStyle({'display':'none'})
			Event.observe(e, 'focus', focus_field)
			Event.observe(e, 'blur', blur_field)
		}
	})
	$$('form textarea').each(function(e) {
		Event.observe(e, 'focus', focus_field)
		Event.observe(e, 'blur', blur_field)
	})
}

function focus_field(){
	label = this.previousSiblings()[0]
	if (this.value == label.innerHTML) {
			this.value = ""
	}
}

function change_field(){
	alert(this.value)
}

function blur_field(){
	field = this.id
	this.previousSiblings()[0].setStyle({'display':'none'})
	if($(field).value == ""){
		$(field).value = this.previousSiblings()[0].innerHTML
	}
}

function matches_label(e){
	return e.getValue()==e.previousSiblings()[0].innerHTML
}

function check_form(id){
	form = $(id)
		
	var emailRegex = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
	var postalRegex = /^([ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]\d[ABCEGHJKLMNPRSTVWXYZabceghjklmnprstvxy])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZabceghjklmnprstvxy]\d)$/
	var phoneRegex = /^([(]?[2-9]\d{2}[)]?[-| |.]?\d{3}[-| |.]?\d{4}((( x| ext)\d{1,5})|))*$/
	
	var error = false
	var missing = false
	
	$('error-text').setStyle({'display' : 'none'})
	switch (id){
		case 'reservationform': form.getInputs('text').each(function(e){ e.setStyle({'border' : '1px solid #ccc'})}); break
		default: form.getInputs('text').each(function(e){ e.setStyle({'border' : 'none'})})
	}
	
	$('error-text').innerHTML = "&nbsp;"
	if(form.getInputs('text', 'firstname').first().value == "" || form.getInputs('text', 'firstname').first().value == undefined || matches_label(form.getInputs('text', 'firstname').first())){
		form.getInputs('text', 'firstname').first().setStyle({'border' : '2px solid #d5222e'})
		missing = true
	}
	
	if(form.getInputs('text', 'email').first().value == "" || form.getInputs('text', 'email').first().value == undefined){
		form.getInputs('text', 'email').first().setStyle({'border' : '2px solid #d5222e'})
		missing = true
	}else if(!(form.getInputs('text', 'email').first().value.match(emailRegex))){
		//$('error-text').innerHTML += "Please enter a valid email address."
		form.getInputs('text', 'email').first().setStyle({'border' : '2px solid #d5222e'})
		//error = true
		missing = true
	}
	
	if(form.getInputs('text', 'phone').first().value == "" || form.getInputs('text', 'phone').first().value == undefined || matches_label(form.getInputs('text', 'phone').first())){
		form.getInputs('text', 'phone').first().setStyle({'border' : '2px solid #d5222e'})
		missing = true
	}else if(!(form.getInputs('text', 'phone').first().value.match(phoneRegex))){
		//$('error-text').innerHTML += "Please enter a valid phone number."
		form.getInputs('text', 'phone').first().setStyle({'border' : '2px solid #d5222e'})
		//error = true
		missing = true
	}
		
	if(id == 'reservationform'){
		if(form.getInputs('text', 'city').first().value == "" || form.getInputs('text', 'city').first().value == undefined || matches_label(form.getInputs('text', 'city').first())){
			form.getInputs('text', 'city').first().setStyle({'border' : '2px solid #d5222e'})
			missing = true
		}
		if(form.getInputs('text', 'startdate').first().value == "" || form.getInputs('text', 'startdate').first().value == undefined || matches_label(form.getInputs('text', 'startdate').first())){
			form.getInputs('text', 'startdate').first().setStyle({'border' : '2px solid #d5222e'})
			missing = true
		}
		if(form.getInputs('text', 'enddate').first().value == "" || form.getInputs('text', 'enddate').first().value == undefined || matches_label(form.getInputs('text', 'enddate').first())){
			form.getInputs('text', 'enddate').first().setStyle({'border' : '2px solid #d5222e'})
			missing = true
		}
	}
	
	//Humans won't fill this in, spambots will
	if(form.getInputs('hidden', 'nospam').first().value){
		$('error-text').innerHTML += "Spambot validation failed."
		error = true
	}
	
	if(!error && !missing){
		form.submit()
	} else {
		if(missing){ $('error-text').innerHTML += "Please fill in all required fields." } 
		$('error-text').setStyle({'display' : 'block'})
	}
}

/* ============= */
/* = CALENDARS = */
/* ============= */

function reservations_init(){
	$$(".scal").each(function(e) {
		e.hide()
	})
	
	$$(".calpick").each(function(e) {
		Event.observe(e, 'click', toggle_cal)
	})
}

function toggle_cal(){
	this.ancestors().first().nextSiblings().first().toggle()
}

function hide_cal(id){
	$(id).ancestors().first().nextSiblings().first().hide()
}

document.observe('dom:loaded', init)
