$(document).ready(
	function()
	{
		installValidationOverride();
		//$('#overlay').after('<iframe frameborder="0" scrolling="no" id="overlayIframe"></iframe>');
		$('dl.faq dt').bind('click', switchQuestion);
		$('dl.example dt').bind('click', switchExample);
		$('#homepageSubmit').bind(
			'click',
			function(e) {
				this.parentNode.parentNode.submit();
				return false;
			}
		);
		//$('#overlayIframe').css('opacity', 0);
		$('#overlay').css('opacity', 0.6);
		$('#box003, #contactOptionsCallBack').bind('click', showContact);
		$('#boxSendemail, #contactOptionsSendEmail').bind('click', showEmail);
		$('#contactInfo .x').bind('click', closeContact);
		
		var triggerLink = $('#ratetableTrigger').bind('click', triggerRateTable);
		if (triggerLink.size() == 1) {
			//setTimeout(closeRateTable, 5000);
			switch(landingPage) {
				case 'leningoversluiten':
				case 'maandlastenverlagen':
				case 'maximaallenen':
					closeRateTable();
				break;
			}
		}
		$('#linkToOffer').bind('mouseover', function(){
			$('img', this).attr('src', 'resources/images/media/button_vraag2.jpg')
		}).bind('mouseout', function(){
			$('img', this).attr('src', 'resources/images/media/button_vraag.jpg')
		})
	}
);
var rateTableVisible = true;
var rateTableWasOpened = true;
var triggerRateTable = function(e) {
	if (rateTableVisible == false) {
		$('img', this).attr('src', 'resources/images/media/button_voorbeeld2.jpg');
		rateTableVisible = true;
		$('#rateTable').show();
		if (rateTableWasOpened == false)
			setTimeout(closeRateTable, 5000);
	} else {
		closeRateTable();
	}
	this.blur();
	return false;
};
var closeRateTable = function() {
			$('#rateTable').hide();
			rateTableVisible = false;
			rateTableWasOpened = true;
			$('#ratetableTrigger img').attr('src', 'resources/images/media/button_voorbeeld.jpg');
};

var showEmail = function(e) {
	showOverlay();
	window.scrollTo(0,0);
	$.iUtil.centerEl($('#contactInfo')
		.find('h4').html('stuur een e-mail')
		.end()
		.find('div').html($('#sendEmailContent').html())
		.end()
		.fadeIn(500)[0], 'horizontally');
	$('#contactInfo button').bind('click', closeContact);
	this.blur();
	return false;
};
var closeContact = function(e) {
	$('#contactInfo').fadeOut(500, hideOverlay);
	this.blur();
	return false;
}
var showContact = function(e) {
	showOverlay();
	window.scrollTo(0,0);
	$.iUtil.centerEl($('#contactInfo')
		.find('h4').html('bel me terug')
		.end()
		.find('div').html($('#callMeBackContent').html())
		.end()
		.fadeIn(500)[0], 'horizontally');
	$('#contactInfo button').bind('click', closeContact);
	this.blur();
	return false;
};
var currentQuestion = null;
var switchQuestion = function(e)
{
	if(currentQuestion) {
		$(currentQuestion).removeClass('orange');
		nextEl = currentQuestion.nextSibling;
		while (!nextEl.tagName || nextEl.tagName.toUpperCase() != 'DD') {
			nextEl = nextEl.nextSibling;
		}
		$(nextEl).hide();
	}
	
	currentQuestion = this;
	$(currentQuestion).addClass('orange');
	nextEl = currentQuestion.nextSibling;
	while (!nextEl.tagName || nextEl.tagName.toUpperCase() != 'DD') {
		nextEl = nextEl.nextSibling;
	}
		$(nextEl).show();
}

var switchExample = function(e)
{
	var currentExampleHeader = this;
	currentExampleDescription = currentExampleHeader.nextSibling;
	while (!currentExampleDescription.tagName || 
			currentExampleDescription.tagName.toUpperCase() != 'DD') {
		currentExampleDescription = currentExampleDescription.nextSibling;
	}
		
	if (currentExampleDescription.style.display == "none"){
		currentExampleDescription.style.display="";
	} else {
		currentExampleDescription.style.display="none";
	}
}

var showOverlay = function()
{
	var clientScroll = $.iUtil.getScroll();
	$('#overlay')
		.css (
			{
				height: Math.max(clientScroll.h,clientScroll.ih) + 'px',
				display: 'block'
			}
		);
	if($.browser.msie) {
		$('#overlayIframe')
			.css (
				{
					height: Math.max(clientScroll.h,clientScroll.ih) + 'px',
					display: 'block'
				}
			);
	}
}
var hideOverlay = function()
{
	$('#overlay').hide();
	if($.browser.msie) {
		$('#overlayIframe').hide();
	}
}

function writeWebtrendsVisitorId()
{
	var visitorId = getVisitorId();
	if (visitorId != null)
		document.write("<meta name=\"DCSext.visitor_ID\" content=\""+visitorId+"\"/>");
}

function getVisitorId()
{
	return readCookie("WEBTRENDS_ID");
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/*
	Focus the first field of the first form in the document.
	When aType is specified (e.g. "text") the first field of that type will get the focus.
*/
function focusFirstField(aType) {
	try {
		var vElements = document.forms[0].elements;
		for (var i=0; i<vElements.length; i++) {
			if ((typeof(aType) == 'undefined' || vElements[i].type == aType)
						&& (vElements[i].type != "hidden" && !vElements[i].disabled && !vElements[i].readOnly)) {
				vElements[i].focus();
				break;
			}
		}
	} catch (e) {}
}

var _validateFormOverride = function(form, source)
{
	//alert("Client side validation starts...");
	var result = window._validateFormTrinidad(form, source);
	if (result) {
		//$('#validationErrorBox').hide();
	} else {
		$('#validationErrorBox').show();
		$('#errorMessages').hide();
		window.location="#top";
	}
	//alert("Client side validation ends.");
	return result;
}

function installValidationOverride()
{
 	var trinidadValidateFormFn = window["_validateForm"];
 	if (typeof trinidadValidateFormFn == "function") {
 		window["_validateFormTrinidad"] = trinidadValidateFormFn;
 		window["_validateForm"] = _validateFormOverride;
	}
}

function saveFocussedElementId( event )
{
  var elementId = event.target.id;
  if (typeof elementId !== "undefined" && elementId !== "") {
    savedFocussedElementId = elementId;
  } else {
    clearFocussedElementId();
  }
}

function clearFocussedElementId( event )
{
  savedFocussedElementId = undefined;
}			
function saveFocussedElementIdOnFucus() {
	$(":input").each(function (i) {
		var elementId = this.id;
		if (typeof elementId !== "undefined" && elementId !== "") {
			$(this).focus( saveFocussedElementId );
			$(this).blur( clearFocussedElementId );
		}
	});
}

function restoreFocus() {
	if (typeof savedFocussedElementId != "undefined") {
		$(savedFocussedElementId).focus();
	} else {
		focusFirstField();
	}
}
