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 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 show_error(errorbox, message){
	$(errorbox).innerHTML = message
	$(errorbox).show()
}

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})|))*$/
	
	if([$('name'), $('email')].findAll(matches_label).length == 0){
		if($('email').getValue().match(emailRegex)){
			form.submit()
		}else{
			show_error('error-text', 'Please enter a valid email address.')
		}
	}else{
		show_error('error-text', 'Please fill in all fields.')
	}
}

/* ============= */
/* = 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)
