var timeOutID = -1;

$(function(){
	
	// Flash:
    var flashvars = {xmlFile: "/navigation.xml"};
	var params = {};
	var attributes = {};
	// swfobject.embedSWF("/assets/templates/ics/swf/navigation.swf", "navigationContainer", "777", "96", "9.0.0", false, flashvars, params, attributes);

	//History
	// $.historyInit(pageload, "jquery_history.html");
	
	runScripts();
	$('video').mediaelementplayer({loop: true, features: []});
	$("img").error(function(){
		$(this).hide();
	});

    $("div.thumbs a").lightBox();
});

function updateNavigationHeights(delta){
	$("#top").css("margin-bottom",36+delta);
	$('html').css("background-position", "0 -" + (104-delta) + "px");
	$('#navigationContainer').css("height", 349+delta);
	$('#star').css("marginTop", 193+delta);
	//alert(103-delta);
}

function navigateToUrl(url){
	//$("#contentPanel").load(url + " #contentPanel>*","",runScripts);
	$.historyLoad(url);
}

function restoreNavigation()
{
    $("#navigationContainer li ul").hide();
    $("#navigationContainer li.active ul").show();
}

function runScripts (responseText, textStatus, XMLHttpRequest) {
	// alert('runScript');
	// alert(responseText);
	$("input[name=searchString]").focus(function(){
		$(this).val('');
	});
	$("input[name=searchString]").blur(function(){
		if($(this).val()==''){
			$(this).val('Zoeken...');
		}
	});
	$("form.search").submit(function(){
		if($("input[name=searchString]").val()!='' && $("input[name=searchString]").val()!='Zoeken...') {
			// pageload("33", {searchString:$("input[name=searchString]").val()});
			// return false;
            return true;
		} else {
			return false;
		}
	});

    /*
	sIFR.replaceElement(named({sSelector:"#content h1", sFlashSrc:"/assets/templates/ics/swf/vistaSansLight.swf", sColor:"#0099a9", sBgColor:"#EFEFEF", sWmode: "transparent"}));
	sIFR.replaceElement(named({sSelector:"#content h2", sFlashSrc:"/assets/templates/ics/swf/vistaSansLight.swf", sColor:"#0099a9", sBgColor:"#EFEFEF", sWmode: "transparent"}));
	sIFR.replaceElement(named({sSelector:".box h3", sFlashSrc:"/assets/templates/ics/swf/vistaSansLight.swf", sColor:"#135f6d", sBgColor:"#EFEFEF", sWmode: "transparent"}));
	sIFR.replaceElement(named({sSelector:"#content h3", sFlashSrc:"/assets/templates/ics/swf/vistaSansLight.swf", sColor:"#6f6f6f", sBgColor:"#EFEFEF", sWmode: "transparent"}));
	*/

    Cufon.replace('#content h1, #content h2, .box h3, #content h3');
    Cufon.replace('#navigation a', { hover: true });

    $("#navigationContainer a").removeAttr("title");

    $("#navigationContainer > ul > li").hover(function(){
        // clearTimeout(timeOutID);
        $("#navigationContainer li ul").hide();
        $("ul", this).show();
    }, function(){

    });

    $("#navigation").mouseleave(function(){
        clearTimeout(timeOutID);
        timeOutID = setTimeout("restoreNavigation()", 500);
        // restoreNavigation();
    }).mouseenter(function(){
        clearTimeout(timeOutID);
    });

    /*
	$("#content a:not([href^=mailto])").click(function(){
		navigateToUrl($(this).attr("href"));
		return false;
	});
	*/

	/* E-mail beschermen tegen spambot: */
	$("#email").html('<a href="mailto:info'+'@'+'icsgroep.nl">info'+'@'+'icsgroep.nl</a>');
	
	/* Contactformulier: */
	$("form.contact").submit(function(){
		var ok = true;
		$(".required", this).each(function(){
			if($(this).val()=='') {
				ok = false;
			}
		});
		if(!ok) {
			alert('Niet alle verplichte velden zijn ingevuld! Velden met een uitroepteken zijn verplicht!');
			return false;
		} else {
			return true;
		}
	});
}




//TEST

// PageLoad function
// This function is called when:
// 1. after calling $.historyInit();
// 2. after calling $.historyLoad();
// 3. after pushing "Go Back" button of a browser
function pageload(hash,data) {
	// alert("pageload: " + hash);
	// hash doesn't contain the first # character.
	
	if(hash) {
		// restore ajax loaded state
		if($.browser.msie) {
			// jquery's $.load() function does't work when hash include special characters like aao.
			// alert(hash);
			// hash = hash.substr(1);
			// hash = encodeURIComponent(hash);
			// alert(hash);
			
		}		
		if(data){
			// $("#content").load(hash + " #content>*", data, runScripts);
			$("#content").load('index.php?id=' + hash + " #content>*", data, runScripts);
		}else{
			// alert('voor load');
			// $("#content").load(hash + " #content>*", runScripts);
			$("#content").load('index.php?id=' + hash + " #content>*", runScripts);
			// alert('na load');
			// $("#content").load("/random.php", runScripts);
			// $("#content").load(hash + "", runScripts);
			// $("#content").load(hash);
		}
	} else {
		// start page
		$("#content").empty();
	}


}
/*
$(document).ready(function(){
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload, "jquery_history.html");
	
	// set onlick event for buttons
	$("a[rel='history']").click(function(){
		// 
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		// moves to a new page. 
		// pageload is called at once. 
		// hash don't contain "#", "?"
		$.historyLoad(hash);
		return false;
	});
});
	*/
