$(document).ready(function() {
//external to blank
			$('a[rel=external]').each(function(){
				$(this).attr('target','_blank');
			});
			$('#hidden').hide();
			$('#route').click(function(){$('#hidden').show('slow');});
});


//variables
var slide_int;
var thumbs;
var minx;
var ttl;
var photo_arr = new Array();
var thumb_arr = new Array();
var photo_active = 0;
var thumb_active = 0;
var slideshow_int;
//functions  
function setResolution() {
w = window.screen.availWidth;
h = window.screen.availHeight;
window.resizeTo(w,h);
window.moveTo(0,0);
window.focus();
}

function writeEmailAddress(nme, dom, cou, cla){
	   document.write("<a href='mailto:"+nme+"@"+dom+"."+cou+"' class="+cla+" >"+nme+"@"+dom+"."+cou+"</a>");
}

function changeTitle(tit){
		document.getElementById("changetitle").innerHTML = tit;
		document.title = document.getElementById("changetitle").innerHTML;
		document.getElementById("changetitle").innerHTML = "";
}


function hideDiv(id){
document.getElementById(id).style.display="none";
}

function showDiv(id){
document.getElementById(id).style.display="block";
}

function changeImg(id, img){
document.getElementById(id).src = img;
}
	
function removeWhiteSpace(id){
	//remove whitespace (FF sees whitespace as extra nodes)
	for(j=0; j<id.childNodes.length; j++){
		if(!id.childNodes[j].innerHTML){
			id.removeChild(id.childNodes[j]);
		}
	}
	return id;
}	

function replaceMenu(){
	//
	//replace menu
	var xml = document.getElementById('menu');
	//remove whitespace
	xml = removeWhiteSpace(xml);
	//set variables for menu
	for(i=0; i<xml.childNodes.length; i++){
		var temptxt = xml.childNodes[i].childNodes[0].innerHTML;
		var templnk = xml.childNodes[i].childNodes[0].href;
		eval("txt"+(i+1)+" = '"+temptxt+"'");
		eval("url"+(i+1)+" = '"+templnk+"'");
	}
	//write menu
	xml.innerHTML = "<div id='menu_container'></div>";
	var flashvars = {txt1: txt1, txt2: txt2, txt3: txt3, txt4: txt4, txt5: txt5, txt6: txt6, txt7: txt7, url1: url1, url2: url2, url3: url3, url4: url4, url5: url5, url6: url6, url7: url7};
	var params = {wmode: "transparent"};
	swfobject.embedSWF("../media/swf/decota_menu.swf", "menu_container", "100%", "100%", "9.0.0", "../media/swf/expressInstall.swf", flashvars, params);
}

function replacettl(txt, tgt, setting){
	if(setting == "ttl1" || setting == "null" || setting == ""){
		var flashvars = {txt: txt, tgt: tgt};
	} else if(setting == "ttl2"){
		var flashvars = {txt: txt, tgt: tgt};
	} else if(setting == "ttl3"){
		var flashvars = {txt: txt, tgt: tgt};
	} else if(setting == "ttl4"){
		var flashvars = {txt: txt, tgt: tgt};
	}
	var params = {wmode: "transparent"};
	swfobject.embedSWF("../media/swf/fontreplacer_futuralightcondensed.swf", tgt, "100%", "22", "9.0.0", "../media/swf/expressInstall.swf", flashvars, params);
	//set margin
	document.getElementById(tgt).parentNode.style.margin = "0px 0px 10px 0px";
}

function setHeight(tgt, h){
	document.getElementById(tgt).style.height=(h-5)+"px";
	//set margin double check for IE
	document.getElementById(tgt).parentNode.style.margin = "0px 0px 0px 0px";
}

function replaceNextTitle(){
	if(ttl<ttl_xml.length){
		var temptxt = ttl_xml[ttl].innerHTML;
		var tempset = ttl_xml[ttl].className;
		ttl_xml[ttl].innerHTML = '<div id="ttl'+ttl+'">'+temptxt+'</div>';
		replacettl(temptxt, 'ttl'+ttl, tempset);
		ttl++;
	} else {
		clearInterval(replace_int);
	}
}

function fontReplacement(){
	//
	//replace all titles (use interval to avoid badly loaded titles in IE and Opera)
	ttl_xml = document.getElementsByTagName("h1");
	ttl = 0;
	replace_int = setInterval("replaceNextTitle();", 5);
}

function hideContent(){
	//get content
	var cont = document.getElementById('content');
	//hide container
	hideDiv('container');
	//set nav bottom
	document.getElementById('navbottom').style.padding = "0px";
	document.getElementById('navbottom').style.styleFloat = "right";
	//set content
	cont.style.width = "auto";
	cont.style.background = "transparent";
	cont.style.border = "0px";
	cont.style.padding = "1px 1px 0px 0px";
	//set buttons
	showDiv('info');
	hideDiv('close');
}

function showContent(w){
	//get thumbs
	var cont = document.getElementById('content');
	//show container
	showDiv('container');

	//set nav bottom
	document.getElementById('navbottom').style.padding = "15px 0px 1px 1px";
	document.getElementById('navbottom').style.styleFloat = "left";
	//set content
	cont.style.width = w+"px";
	cont.style.background = "#ffffff";
	cont.style.border = "1px solid #eeeeee";
	cont.style.padding = "0px";
	//set buttons
	hideDiv('info');
	showDiv('close');
}

function loadThumbs(){
	var temp = "";
	for(var i=0; i<thumb_arr.length; i++){
		temp += '<a href="javascript:activateThumb('+i+'); swapPhoto(0);" class="thumb" ><img src="'+thumb_arr[i]+'" alt="" /></a>';
	}
	document.getElementById("thumbs").innerHTML = temp;
	activateThumb(0);
}

function hideThumbs(){
	//get container
	var cont = document.getElementById('thumbcontainer');
	//hide container
	hideDiv('thumbmask');
	hideDiv('navbtn_back');
	hideDiv('navbtn_forward');
	//set container
	cont.style.height = "auto";
	cont.style.width = "949px";
	cont.style.background = "transparent";
	cont.style.border = "0px";
	cont.style.padding = "0px 0px 2px 0px";
	//set buttons
	hideDiv('thumbclose');
	showDiv('thumbopen');
}

function showThumbs(){
	//get container
	var cont = document.getElementById('thumbcontainer');
	//show container
	showDiv('thumbmask');
	showDiv('navbtn_back');
	showDiv('navbtn_forward');
	//set container
	cont.style.height = "98px";
	cont.style.width = "948px";
	cont.style.background = "#ffffff";
	cont.style.border = "1px solid #eeeeee";
	cont.style.padding = "0px";
	//set buttons
	showDiv('thumbclose');
	hideDiv('thumbopen');
}

function slideThumbs(lr){
	//get vars
	thumbs = document.getElementById('thumbs');
	var mw = document.getElementById('thumbmask').clientWidth;
	//calculate width of thumbs
	var thumbs_xml = removeWhiteSpace(thumbs);
	//var thw = (thumbs_xml.childNodes.length*72)-5;
	var thw = (thumb_arr.length*72)-5;
	//
	minx = mw-thw;
	var speed = 0.02;
	//check if thumbs is wider than container (check for scroll)
	if(minx<=0){
		//activate navbuttons
		activate('navbtn_forward');
		activate('navbtn_back');
		//check left or right slide 
		if(lr =="l"){
			slideStop();
			slideLeft();
			slide_int=window.setInterval("slideLeft()",speed*1000);
		} else if(lr=="r"){
			slideStop();
			slideRight();
			slide_int=window.setInterval("slideRight()",speed*1000);
		} else if(lr="n"){
			//check to deactivate
			slideStop();
			if(thumbs.offsetLeft==0){
				deactivate('navbtn_back');
			} else if(thumbs.offsetLeft==minx){
				deactivate('navbtn_forward');
			}
		}
	} else {
		//deactivate navbuttons
		deactivate('navbtn_forward');
		deactivate('navbtn_back');
	}
}

function activateThumb(id){
	//get vars
	thumbs = document.getElementById('thumbs');
	var thumbs_xml = removeWhiteSpace(thumbs);
	//deactivate old
	thumbs_xml.childNodes[thumb_active].className="thumb";
	//activate
	thumb_active = id;
	thumbs_xml.childNodes[thumb_active].className="thumbactive";
	
	//change text
	setContent(textarray[id]);
}


function slideLeft(){
	var xPos = thumbs.offsetLeft;
	if(xPos>minx){
		thumbs.style.left = (xPos-5)+"px";
	} else {
		slideStop();
		deactivate('navbtn_forward');
	}
}

function slideRight(){
	var xPos = thumbs.offsetLeft;
	if(xPos<0){
		thumbs.style.left = (xPos+5)+"px";
	} else {
		slideStop();
		deactivate('navbtn_back');
	}
}

function deactivate(id){
	document.getElementById(id).className = "navbtn_inactive";
}

function activate(id){
	document.getElementById(id).className = "navbtn";
}

function slideStop(){
	window.clearInterval(slide_int);
}
//snippet for fade in / out
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
			
        } 
		
    }
}



//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 
//end snippet
function swapPhoto(id){
	hideDiv('photo');
	changeImg('photo',photo_arr[thumb_active][id]);
	opacity('photo', 0, 100, 500);
	setTimeout("showDiv('photo');",10);
	photo_active = id;
	setTimeout("changeImg('photocopy',photo_arr[thumb_active][photo_active]);",500);
}

function nextPhoto(){
	if(photo_active+1>photo_arr[thumb_active].length-1){
		photo_active = 0;
		if(thumb_arr.length>0){
			if(thumb_active+1>thumb_arr.length-1){
				activateThumb(0);
			} else {
				activateThumb(thumb_active+1);
			}
		}
	} else {
		photo_active++;
	}
	swapPhoto(photo_active);
}
function previousPhoto(){
	if(photo_active-1<0){
		if(thumb_active-1<0){
			activateThumb(thumb_arr.length-1);
			
		} else {
			activateThumb(thumb_active-1);
		}
		photo_active = photo_arr[thumb_active].length-1;
	} else {
		photo_active--;
	}
	swapPhoto(photo_active);
}
function startSlideShow(){
	var speed = 5;
	stopSlideShow();
	swapPhoto(photo_active);
	slideshow_int=window.setInterval("nextPhoto();",speed*1000);
}
function stopSlideShow(){
	clearInterval(slideshow_int);
}
//activate menu item
function activateMenu(item){
	var menu = removeWhiteSpace(document.getElementById("menu"));
	var name = menu.childNodes[item].childNodes[0].innerHTML;
	menu.childNodes[item].innerHTML = "<span>"+name+"</span>";
}
//change content
function setContent(html){
	var object = document.getElementById("container");
	object.innerHTML = "<div class='txtcontainer'>"+html+"</div>";
	if(document.getElementById('project_ttl')){
		replacettl(document.getElementById('project_ttl').innerHTML, 'project_ttl', '');
	}
	Cufon.replace('h1');
}

//contact
function checkContactValues(){
	
	var firstname = document.getElementById('firstname');
	var lastname = document.getElementById('lastname');
	var street = document.getElementById('street');
	var postalcode = document.getElementById('postalcode');
	var city = document.getElementById('city');
	var tel = document.getElementById('tel');
	var aangepastveld = document.getElementById('aangepastveld');

	if(firstname.value.length==0){window.alert("Gelieve uw voornaam in te vullen.");return false;}
	if(lastname.value.length==0){window.alert("Gelieve uw familienaam in te vullen.");return false;}
	if(street.value.length==0){window.alert("Gelieve uw adres in te vullen.");return false;}
	if(postalcode.value.length==0){window.alert("Gelieve uw postcode in te vullen.");return false;}
	if(city.value.length==0){window.alert("Gelieve uw woonplaats in te vullen.");return false;}		
	if(tel.value.length==0){window.alert("Gelieve uw telefoonnummer in te vullen.");return false;}
	if(!validMail(aangepastveld.value)){window.alert("Gelieve uw e-mailadres in te vullen.");return false;}

	return true;
}
//contact mail validate
function validMail(str){
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if(str.indexOf(at)==-1){return false;}
	if(str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){return false;}
	if(str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){return false;}
	if(str.indexOf(at,(lat+1))!=-1){return false;}
	if(str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){return false;}
	if(str.indexOf(dot,(lat+2))==-1){return false;}	
	if(str.indexOf(" ")!=-1){return false;}
	return true;		
}
//set signature
function setSignature(){
	var object = document.getElementById('boa');
	var sh = document.getElementById('centered').offsetHeight;
	var bh = document.body.offsetHeight;
	//alert(bh+" - "+sh);
	if(sh>bh){
		object.style.top = sh-40+'px';
	}
	
}
