$(document).ready(function(){
	$('a[rel=external]').each(function(){
		$(this).attr('target','_blank');
	});
	
	//set cycle on references
	if($('.slideshow').length>0){
		$('.slideshow').cycle();
	}
	
 }); 
 


////////////////////////////////////////////////////// PUBLIC VARIABLES  ////////////////////////////////////////////////////// 

var slideshow_int;
var thumbs;
var minx;
var ttl;
var info_arr = new Array();
var photo_arr = new Array();
var photo_max_arr = new Array();
var photo_active = 0;
  
  
 


////////////////////////////////////////////////////// TOOLBOX ////////////////////////////////////////////////////// 

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 changeImgLnk(id, img){
	id.childNodes[0].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;
}


//snippet for fade in / out

//tween opacity
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



////////////////////////////////////////////////////// FONT AND MENU REPLACEMENT ////////////////////////////////////////////////////// 

function replaceMenu(){
	//
	//replace menu
	var xml = document.getElementById('menu');
	//remove whitespace
	xml = removeWhiteSpace(xml);
	//set variables for menu
	var tempvars = "";
	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+"'");
		tempvars += "txt"+(i+1)+": txt"+(i+1)+", url"+(i+1)+": url"+(i+1)+", ";
	}
	var cnt = xml.childNodes.length;
	//write menu
	xml.innerHTML = "<div id='menu_container'></div>";
	eval("var flashvars = {"+tempvars+"cnt:"+cnt+"}");
	//var params = {wmode: "transparent"};
	var params = {};
	swfobject.embedSWF("/media/swf/aqualityprotect_menu.swf", "menu_container", "100%", "100%", "9.0.0", "media/swf/expressInstall.swf", flashvars, params);
}

function replaceSubmenu(){
	//
	//replace menu
	var xml = document.getElementById('submenu');
	//remove whitespace
	xml = removeWhiteSpace(xml);
	//set variables for menu
	var tempvars = "";
	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+"'");
		tempvars += "txt"+(i+1)+": txt"+(i+1)+", url"+(i+1)+": url"+(i+1)+", ";
	}
	var cnt = xml.childNodes.length;
	//write menu
	xml.innerHTML = "<div id='submenu_container'></div>";
	eval("var flashvars = {"+tempvars+"cnt:"+cnt+"}");
	var params = {wmode: "transparent"};
	//var params = {};
	swfobject.embedSWF("/media/swf/aqualityprotect_submenu.swf", "submenu_container", "100%", "250", "9.0.0", "media/swf/expressInstall.swf", flashvars, params);
	//set padding
	document.getElementById('submenu').style.padding = "0px 0px 0px 0px";
}

function replacettl(txt, tgt){
	var flashvars = {txt: txt, tgt: tgt};
	var params = {wmode: "transparent"};
	swfobject.embedSWF("/media/swf/fontreplacer_frutigercondensed.swf", tgt, "100%", "24", "9.0.0", "media/swf/expressInstall.swf", flashvars, params);
	//set margin
	document.getElementById(tgt).parentNode.style.margin = "0px 0px 5px 0px";
}

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

////////////////////////////////////////////////////// FORMS ////////////////////////////////////////////////////// 

//gratis diagnose module check

function checkDiagnoseModule(lang, loc){
	var module = document.getElementById("formdiagnose");
	var values = module.getElementsByTagName("input");
	var ok = false;
	/*//check if at least one of the checkboxes is checked
	for(i=0; i<values.length; i++){
		if(values[i].checked){
			ok = true;
		}
	}
	//alert if not
	if(!ok){
		switch(lang){
			case "nl":
				alert("Gelieve één of meerdere symptomen in het formulier aan te duiden.");
			break;
			case "fr":
				alert("Indiquer un ou plusieurs des symptômes dans la forme s'il vous plaît.");
			break;
			case "en":
				alert("Please check one or more symptoms in the form.");
			break;
			default:
			break;
		}
	} else {
		window.location=loc;
		$('#formdiagnose').submit();
	}*/
	window.location=loc;
	$('#formdiagnose').submit();
}

//contact
function checkContactValues(lang){
	
	//define fields
	var firstname = document.getElementById('firstname');
	var lastname = document.getElementById('lastname');
	var address = document.getElementById('address');
	var zipcode = document.getElementById('zipcode');
	var city = document.getElementById('city');
	var tel = document.getElementById('tel');
	var email = document.getElementById('email');
	
	var ok = true;
	var contactform = document.getElementById("formcontact");
	
	//define fields array
	var fields = new Array(firstname,lastname,address,zipcode,city,tel,email);
	
	//comments in different languages
	var commentnl = new Array("Gelieve uw voornaam in te vullen.","Gelieve uw familienaam in te vullen.","Gelieve uw adres in te vullen.", "Gelieve uw postcode in te vullen.", "Gelieve uw stad in te vullen.", "Gelieve uw telefoonnummer in te vullen.", "Gelieve een correct e-mailadres in te vullen.");
	var commentfr = new Array ("Entrez votre prénom, s'il vous plaît.", "Entrez votre nom de famille, s'il vous plaît.", "Remplissez votre adresse, s'il vous plaît.", "Remplissez votre code postal, s'il vous plaît.", "Entrez votre ville, s'il vous plaît.","Entrez votre numéro de téléphone, s'il vous plaît.","Entrez une adresse e-mail correcte, s'il vous plaît.");
	var commenten = new Array("Please fill in your first name.", "Please enter your surname.", "Please fill in your address.", "Please fill in your postal code.", "Please enter your city.", "Please enter your telephone number.", "Please enter a correct e-mail address.");
	
	//check language and set comment
	eval("var comment = comment"+lang);
	
	//comment if needed
	for(i=0; i<fields.length; i++){
		if(fields[i] != email){
			if(fields[i].value.length==0){alert(comment[i]); ok=false; break;}
		} else {
			if(!validMail(email.value)){alert(comment[i]); ok=false; break;}
		}
	}
	
	//if ok
	if(ok){
		contactform.submit();
	}
}
//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;		
}


////////////////////////////////////////////////////// MEDIA POPUP ////////////////////////////////////////////////////// 

//load video  in media
function loadVideo(file){
	//set class & dimensions
	var media = document.getElementById('mediacontainer');
	media.className = "contvideo";
	media.style.width = (480+120)+"px";
	media.style.height = (270+120+document.getElementById('mediamenu').clientHeight)+"px";
	//show video
	openMedia();
	centerMedia();
	//create holder
	var object = document.getElementById('media');
	object.innerHTML = '<span><div id="videoholder"></div></span>';
	//write video
	setTimeout('swfobject.embedSWF("'+file+'", "videoholder", "480", "270", "9.0.0", "/media/swf/expressInstall.swf")', 250);
}

//load image in media
function loadPhoto(file){
	//set class
	document.getElementById('mediacontainer').className = "contphoto";
	//show div before checking width
	showDiv('overlay');
	//load photo
	var object = document.getElementById('media');
	object.innerHTML = '<div><img src="/media/img/preloader.gif" alt="loading" title="loading" /><img src="'+file+'" alt="" id="mediaphoto" /></div>';
	var pic=new Image();
	pic.src= file;
	if(pic.complete){
		showMediaPhoto();
	}else {
		pic.onload= showMediaPhoto;
	}
}

//show image in media
function showMediaPhoto(){
	resizePhotoToWindow("mediaphoto",160); 
	collapseMediaToPhoto("mediaphoto"); 
	openMedia(); 
	centerMedia();
}

//collapse media container to photo width
function collapseMediaToPhoto(id){
	var object = document.getElementById(id);
	var nh = object.clientHeight+document.getElementById('mediamenu').clientHeight;
	var nw = object.clientWidth;
	var media = document.getElementById('mediacontainer');
	media.style.width = nw+"px";
	media.style.height = nh+"px";
}

//collapse photo to window with margin
function resizePhotoToWindow(id, mrg){
	var object = document.getElementById(id);
	var ww = document.body.clientWidth;
	var wh = document.body.clientHeight;
	var mw = ww-mrg;
	var mh = wh-mrg;
	var pw = object.clientWidth;	
	var ph = object.clientHeight;
	if(ph+122+mrg>wh){
		var nh = wh-122-mrg;
		var sc = nh/ph;
		var nw = pw*sc;
	} else if(pw+122+mrg>ww){
		var nw = ww-122-mrg;
		var sc = nw/pw;
		var nh = ph*sc;
	} else {
		var nw = pw;
		var nh = ph;
	}
	object.style.width = nw+"px";
	object.style.height = nh+"px";
}

//check if image is loaded
function checkLoaded(id){
	var w = document.getElementById(id).clientWidth;
	alert(w);
	if(w>0){
		return true;
	} else {
		return false;
	}
}

//center mediacontainer
function centerMedia(){
	var media = document.getElementById('mediacontainer');
	var wh = document.body.clientHeight;
	var mh = media.clientHeight;
	var cy = Math.round((wh-mh)/2);
	media.style.margin = cy+"px auto 30px auto";
}

//open media
function openMedia(){
	opacity('overlay', 0, 100, 250);
	showDiv('overlay');
	//give overlay height of content
	var object = document.getElementById("overlay");
	object.style.height = document.getElementById("container").clientHeight+"px";

}

//close media
function closeMedia(){
	document.getElementById("media").innerHTML = "";
	opacity('overlay', 100, 0, 250);
	setTimeout('hideDiv("overlay");', 250);
}


//snippet for mouseposition

function mouseX(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
   return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
else return null;
}
function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
   return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
else return null;
}
function scrollY(){
	var scrollTop = document.body.scrollTop;
	if (scrollTop == 0){
		if (window.pageYOffset){
			scrollTop = window.pageYOffset;
		}else{
			scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}
	}
	return scrollTop;
}

//end snippet

//close media if mouse out of mediacontainer
function checkCloseMedia(e){
	var media = document.getElementById('mediacontainer');
	var ww = document.body.clientWidth;
	var wh = document.body.clientHeight;
	var mw = media.clientWidth;
	var mh = media.clientHeight;
	var mx1 = (ww-mw)/2;
	var mx2 = mx1+mw;
	var my1 = (wh-mh)/2-scrollY();
	var my2 = my1+mh-scrollY();
	var x = mouseX(e);
	var y = mouseY(e);
	if(x<mx1 || x>mx2){
		closeMedia();
	} else if(y<my1 || y>my2){
		closeMedia();
	}
}



////////////////////////////////////////////////////// SLIDESHOW ////////////////////////////////////////////////////// 

function swapPhoto(id){
	hideDiv('photo');
	changeImg('photo',photo_arr[id]);
	opacity('photo', 0, 100, 500);
	setTimeout("showDiv('photo');",10);
	photo_active = id;
	setTimeout("changeImg('photocopy',photo_arr[photo_active]);",500);
}
function nextPhoto(){
	if(photo_active+1>photo_arr.length-1){
		photo_active = 0;
	} else {
		photo_active++;
	}
	swapPhoto(photo_active);
}
function previousPhoto(){
	if(photo_active-1<0){
		photo_active = photo_arr.length-1;
	} else {
		photo_active--;
	}
	swapPhoto(photo_active);
}
function startSlideShow(speed,info){
	stopSlideShow();
	swapPhoto(photo_active);
	setSlideInfo();
	if(info){
		slideshow_int=window.setInterval("nextPhoto(); setSlideInfo();",speed*1000);
	} else{
		slideshow_int=window.setInterval("nextPhoto();",speed*1000);
	}
}
function stopSlideShow(){
	clearInterval(slideshow_int);
}
function setSlideInfo(){
	document.getElementById('slidertxt').innerHTML = '<div>'+info_arr[photo_active]+'</div>';
}


