if(typeof HTMLElement!='undefined'&&!HTMLElement.prototype.click)
HTMLElement.prototype.click=function(){
var evt = this.ownerDocument.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
this.dispatchEvent(evt);
}

function init() {
	if($('nav')) nav_init()
	if($('contactForm')) contact_init()
	if($$('.collapsomatic')) collapsomatic_init()
}

/* ========= */
/* = FORMS = */
/* ========= */

function contact_init(){
	$('contactForm').getInputs('radio', 'interest').each(function(e){
		Event.observe(e, 'click', test)
	})
}

function test(e){
	if(this.value == "24824226"){ // = Camping
		// since they want camping-interested people to have their emails sent to an address, let's change the form action)
		$('contactForm').setAttribute("action", "/contact.html")
	}else{
		$('contactForm').setAttribute("action", "http://leisureclub.relenta.com/api/httpform")
		//$('contactForm').setAttribute("action", "/thankyou.html")
	}
}

function send(id){

	form = $(id)
	
	if(form.getInputs('hidden', 'address').length){
		// address is stored as one line in Relenta
		var address = form.getInputs('text', 'street').first().value + '\n' + form.getInputs('text', 'city').first().value + " " + form.getInputs('text', 'province').first().value + " " + form.getInputs('text', 'postal').first().value
		form.getInputs('hidden', 'address').first().value = address
	}
	
	if(form.getInputs('radio', 'interest').length){
		var interest = form.getInputs('radio', 'interest').find(function(e){
			return e.checked
		})
		form.getInputs('hidden', 'contact_groups').first().value = (interest == undefined) ? "24918305" : interest.value
	}
	
	if(form.getInputs('hidden', 'custom_field(Documents)').length){
		// documents is stored as one line in Relenta
		form.getInputs('hidden', 'custom_field(Documents)').first().value = form.getInputs('checkbox', 'docs').collect(function(e){
			return e.checked ? e.value : null;
		}).compact()
	}
	
	if(form.getInputs('hidden', 'custom_field(Comments)').length){
		form.getInputs('hidden', 'custom_field(Comments)').first().value = $('comments').value
	}
	
	if(form.getInputs('hidden', 'custom_field(Referral)').length){
		form.getInputs('hidden', 'custom_field(Referral)').first().value = $('referral').value
	}
	
	form.submit()
}

function check_form(id){
	form = $(id)

	var error = false
	$('error').setStyle({'display' : 'none'})
	
	$('error-text').innerHTML = "&nbsp;"
	if(form.getInputs('text', 'first_name').first().value == "" || form.getInputs('text', 'first_name').first().value == undefined){
		$('error-text').innerHTML += "<li>Please enter your first name.</li>"
		error = true
	}
	
	if(form.getInputs('text', 'last_name').first().value == "" || form.getInputs('text', 'last_name').first().value == undefined){
		$('error-text').innerHTML += "<li>Please enter your last name.</li>"
		error = true
	}
	
	if(form.getInputs('text', 'email').first().value == "" || form.getInputs('text', 'email').first().value == undefined){
		$('error-text').innerHTML += "<li>Please enter your email address.</li>"
		error = true
	}else if((form.getInputs('text', 'email').first().value.indexOf('@') == -1) || (form.getInputs('text', 'email').first().value.indexOf('.') == -1)){
		$('error-text').innerHTML += "<li>Please enter a valid email address.</li>"
		error = true
	}
	
	if(form.getInputs('hidden', 'nospam').first().value){
		$('error-text').innerHTML += "<li>Spambot validation failed.</li>"
		error = true
	}
	
	if(!error){
		send(id)
	} else {
		$('error').setStyle({'display' : 'block'})
	}
}

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){
		e.setStyle({'display':'none'})
		e.previousSiblings().first().innerHTML += ' <span class="trigger down"><img src="/images/blank.gif" width="10" height="10"></span>'
	})
	
	$$('.trigger').each(function(e){
		e.setStyle({'cursor':'pointer'})
		Event.observe(e, 'click', toggle)
		e.previousSiblings().first().setStyle({'cursor' : 'pointer'})
		Event.observe(e.previousSiblings().first(), 'click', toggle)
	})
}

function toggle(e){
	var block = this.ancestors().first().nextSiblings().first()
	
	var t;
	if(this.hasClassName("trigger")){
		t = this
	}else{
		t = this.nextSiblings().first()
	}
	
	if(t.hasClassName("up")){
		Effect.BlindUp(block)
		t.removeClassName("up")
		t.addClassName("down")
	}else{
		Effect.BlindDown(block)
		t.removeClassName("down")
		t.addClassName("up")
	}
}

document.observe('dom:loaded', init)
