//SET variables
var currentURL;
var currentLangNum;		//number of language Array
var menuActive;   //variable to set the current menu item for JS/Flash communication


//CHANGEABLE Variables
var currentLang = "nl";  //set standard language
lang = new Array("nl","eng"); //languages available

var label_more = new Array("meer","more"); 		//Label detail button - closed
var label_less = new Array("minder","less");	//Label detail button - opened


//CONTACT FORM ARRAYS
errorMsg = new Array("Alle ingekleurden vakken moeten ingevuld worden.","Please fill in all colored fields.");
successMsg = new Array("Bericht verstuurd!","Email sent!");
storedValues = new Array;
mailMsg = new Array("Geen geldig email adres.","No valid email adress.");
var errorColor = "#cc00ff" ;
var color;
sessionArray = new Array("&name=","&surname=","&email=","&tel=","&company=","&comment="); //ARRAY WHICH WILL BE USED AS PAREMETERS FOR THE SESION





function setupxmlHttp(){ //function to setup Ajax request
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
		if(xmlHttp.overrideMimeType){               
	        xmlHttp.overrideMimeType('text/html; charset=ISO-8859-1');
		}
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}


function swapImage(url,id){ //Function to swap object images
	document.images[id].src = url;
}


function setCSS(){ //CSS setup to hide layers in order to be recognized for if statements
	var divs=document.getElementsByTagName('div');
	for (var i=0;i<divs.length;i++) {
		if (document.getElementsByTagName('div')[i].className == "wrapper_more"){
				document.getElementsByTagName('div')[i].style.visibility='hidden';
				document.getElementsByTagName('div')[i].style.overflow='hidden';
				document.getElementsByTagName('div')[i].style.height='0px';
		}
	}
}

function swapVisibility(divID){ //swap visibility of wrapper more -> details in objects and architecture
	var changeTo;
	var divs=document.getElementsByTagName('div');
	if (document.getElementById(divID).style.visibility=='visible'){
		changeTo = 'hidden';
		linkType = label_more[currentLangNum];
		document.getElementById(divID).style.overflow='hidden';
		document.getElementById(divID).style.height='0px';
	}else{
		changeTo = 'visible';
		linkType = label_less[currentLangNum];
		document.getElementById(divID).style.height='100%';
	}
	for (var i=0;i<divs.length;i++) {
		if (document.getElementsByTagName('div')[i].className == "wrapper_more"){		
			if (document.getElementsByTagName('div')[i].style.visibility=='visible'){
				document.getElementsByTagName('div')[i].style.visibility='hidden';
				document.getElementsByTagName('div')[i].style.overflow='hidden';
				document.getElementsByTagName('div')[i].style.height='0px';
			}						
		}
	}
	document.getElementById(divID).style.visibility=changeTo;
	document.getElementById("link_"+divID).firstChild.nodeValue = "[" + linkType + "]"; //switch label_more
}



function ajaxFill(req, lang, anchorName, title) {   //function to fill content
	var xmlHttp =  setupxmlHttp();
	if (req != null){
		currentURL = req;
	}
	// build target query
	if (lang != null){
		currentLang = lang;
		currentQuery = currentURL + '?l=' + lang;
	} else{
		currentURL = req;
		currentQuery = req + '?l=' + currentLang;
	}
	
	xmlHttp.open('POST', currentQuery, true);
	document.getElementById("container_content").innerHTML = "<div class='loader'><img src='img/loader.gif'/></div>";
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			var result = xmlHttp.responseText;
			document.getElementById("container_content").innerHTML = result;
			if(anchorName){
				window.location.href = "#" + escape(anchorName);
			}
			if(title){
				currentTitle = title;
			}
			window.document.title = "LED-Art - " + currentTitle;
			switch (currentLang) {
				case "nl": 
					currentLangNum=0;  //index number of "nl"
					break;
				case "eng": 
					currentLangNum=1; //index number of "eng"
					break;		
			}	
			switch (currentURL) {
				case "home.php": 
					menuActive=1;
					break;
					
				case "objects.php": 
					Shadowbox.setup(); 
					menuActive=2;
					setCSS();
					break;	 
					
				case "architecture.php": 
					Shadowbox.setup(); 
					menuActive=3;
					setCSS();
					break;
			 
				case "about":
					menuActive=4;
					break;
				
				case "contact.php": 
					color = document.getElementsByTagName('input')[1].style.backgroundColor;
					document.getElementById('comment').value="";
					menuActive=5;
					break;
					
		
				default: menuActive=0;	
			}
 
			if(currentURL=="home.php" || currentURL=="about.php" || currentURL=="contact.php" || currentURL=="disclaimer.php" ){ //if one of these pages is active, show slideshow
				document.getElementById("slideshow").style.visibility='visible';
				document.getElementById("slideshow").style.height='135px';
				document.getElementById("container_header").style.height='135px';
				document.getElementById("container_header").style.margin='0 0 20px 0';
			}else{
				document.getElementById("slideshow").style.visibility='hidden';
				document.getElementById("slideshow").style.height='0px';
				document.getElementById("container_header").style.height='0px';
				document.getElementById("container_header").style.margin='0 0 0px 0';
			}
		}
	}
	xmlHttp.send(null);	
}

function ajaxFillMenu(lang, firstStart, menuItem) { //Function to fill or swith PHP menu
	var xmlHttp =  setupxmlHttp();
	if (menuItem){
		if (lang != null){
			currentQuery = 'menu.php?l=' + lang + '&item=' + menuItem;
		}else{
			currentQuery = 'menu.php?l=' + currentLang + '&item=' + menuItem;
		}
		xmlHttp.open("POST", currentQuery, true);
		
		xmlHttp.onreadystatechange=function() {
			//document.getElementById("menu_main").innerHTML = "<div class='loader'><img src='img/loader_flash.gif'/></div>";   //CHEK - PRELOADER menu
			if(xmlHttp.readyState==4) {
				var result = xmlHttp.responseText;
				if (document.getElementById("menu_main")){
					document.getElementById("menu_main").innerHTML = result;
				}else{ //if menu_main has been overwritten by swfobject rebuild the wrapper
					//var $content = "<p> Flash needed. <a href='http://www.adobe.com/go/getflashplayer'> download </a></p>  <div id='menu_main'> </div>  "; 
					var $content =  "<div id='menu_main'> </div>  ";
					if (document.getElementById("navigation").innerHTML == $content){
						document.getElementById("navigation").innerHTML = $content;
					}
				}
				if (firstStart==true){
					flashFillMenu(currentLang);
				}
			}
		}
		xmlHttp.send(null);
	}
}
	
function ajaxFillMenuBottom(lang, firstStart, menuItem) { //Function to fill or swith PHP menu
	var xmlHttp =  setupxmlHttp();
	if (menuItem){
		if (lang != null){
			currentQuery = 'menu_bottom.php?l=' + lang;
		}else{
			currentQuery = 'menu_bottom.php?l=' + currentLang;
		}
		xmlHttp.open("POST", currentQuery, true);
		xmlHttp.onreadystatechange=function() {
			//document.getElementById("menu_main").innerHTML = "<div class='loader'><img src='img/loader_flash.gif'/></div>";   //CHEK - PRELOADER menu
			if(xmlHttp.readyState==4) {
				var result = xmlHttp.responseText;
				if (document.getElementById("wrapper_menu")){
					document.getElementById("wrapper_menu").innerHTML = result;
				}else{ //if menu_main has been overwritten by swfobject rebuild the wrapper
					//var $content = "<p> Flash needed. <a href='http://www.adobe.com/go/getflashplayer'> download </a></p>  <div id='menu_main'> </div>  "; 
					var $content =  "<div id='wrapper_menu'> </div>  ";
					if (document.getElementById("wrapper_menu").innerHTML == $content){
						document.getElementById("wrapper_menu").innerHTML = $content;
					}
				}
				
			}
		}
		xmlHttp.send(null);
	}
}

function ajaxFillLang(req, lang) { //Function to fill or swith PHP language file
	var xmlHttp =  setupxmlHttp();
	
	if (lang != null){
		//test = "?l=" + lang + "&mode=script";
		xmlHttp.open('GET', 'language.php?l=' + lang + "&mode=script", true); //IMPROVE
	}else{
		//test = "?l=" + currentLang + "&mode=script";
		xmlHttp.open('GET', 'language.php?l=' + currentLang + "&mode=script", true); //IMPROVE
	}
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			var result = xmlHttp.responseText;
			document.getElementById("wrapper_lang").innerHTML = result;	
			
		}
	}
	xmlHttp.send(null);
}		



function checkFields(){ //function to check contact form fields
	error = false;
	var divs=document.getElementsByTagName('input');
	for (var i=0;i<divs.length;i++) {
		if(document.getElementsByTagName('input')[i].className == "inputContact_required") {
			if(document.getElementsByTagName('input')[i].value == "") {
				document.getElementsByTagName('input')[i].style.backgroundColor = errorColor ;	
				error = true;
			}else{
				document.getElementsByTagName('input')[i].style.backgroundColor = color;	
			}
			//CHECK FOR VALID EMAIL ADRESS
			if(document.getElementsByTagName('input')[i].name == "input_email") {
				if (checkEmail(document.getElementsByTagName('input')[i].value)==false){
					error = true;
					document.getElementsByTagName('input')[i].value = mailMsg[currentLangNum];
					document.getElementsByTagName('input')[i].style.backgroundColor = errorColor ;	
				}
			}
		}
	}

	if (document.getElementById('comment').value==""){
		error=true;
		document.getElementById('comment').style.backgroundColor = errorColor;		
	}else{
		document.getElementById('comment').style.backgroundColor = color;
	}
	if (error == false){
		var poststr="";
		for (var i=0;i<divs.length;i++) {
			
			if (document.getElementsByTagName('input')[i].className == "inputContact_required" || document.getElementsByTagName('input')[i].className == "inputContact_free"){
				poststr = poststr + sessionArray[i] + document.getElementsByTagName('input')[i].value;
			}
		}
		tempNum = (divs.length)-1;
		poststr = poststr + sessionArray[tempNum] + document.getElementById('comment').value;
		pushSession('mailer.php',poststr);
		resetFields();
		document.getElementById('info').innerHTML = successMsg[currentLangNum] ;
	}else{
		document.getElementById('info').innerHTML = errorMsg[currentLangNum];
	}
}

function resetFields(){
	var divs=document.getElementsByTagName('input');
	for (var i=0;i<divs.length;i++) {
		if (document.getElementsByTagName('input')[i].className == "inputContact_required" || document.getElementsByTagName('input')[i].className == "inputContact_free"){
				document.getElementsByTagName('input')[i].value = "" ;	
		}
	}
	document.getElementById('comment').value = "";
};


function checkEmail(s){
var a = false;
var res = false;
	if(typeof(RegExp) == 'function') {
		var b = new RegExp('abc');
		if(b.test('abc') == true){a = true;}
	}
	if(a == true){
		reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+
                   '(\\@)([a-zA-Z0-9\\-\\.]+)'+
                   '(\\.)([a-zA-Z]{2,4})$');
  		res = (reg.test(s));
 	}else{
		res = (s.search('@') >= 1 &&
        s.lastIndexOf('.') > s.search('@') &&
        s.lastIndexOf('.') >= s.length-5)
	}
 	return(res);
}

function pushSession(url,value){
	var xmlHttp =  setupxmlHttp();
	xmlHttp.open('POST', url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", value.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(value);

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			//alert (xmlHttp.responseText);
			//document.getElementById("wrapper_lang").innerHTML = value;
		}
	}
}
	
	



//FLASH STUFF
flashRunning = false;
		
function flashFillMenu(swf) {
	var flashvars = {};
	var params = {};
	params.wmode = "transparent";
	params.bgcolor = "#000000";
	params.bgcolor = "#000033";
	var attributes = {};
	//INFO -> swfobject.embedSWF (swfUrl, id, width, height, version, expressInstallSwfurl, flashvars, params, attributes) 
	swfobject.embedSWF("flash_nav.swf", "navigation", "100%", "120", "9.0.0", "expressinstall.swf", flashvars, params, attributes);
}		
		
function javascript_setStatus() { //FLASH CALLED FUNCTION -> setting var to recognize FLASH
	flashRunning = true;
	flash_switchLanguage(currentLang); //setup Flash menu with the standard language when first starting the page
}	

function javascript_openURL(url,title) { //FLASH CALLED FUNCTION -> translating Flash button to ajaxCALL
	ajaxFill(url,null,null,title);
	//alert(title);
}	

		
//JAVASCRIPT -> FLASH FUNCTIONS			
function getFlashMovie(movieName) { //ACTIVATE JAVASCRIPT -> FLASH connection
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
 	return (isIE) ? window[movieName] : document[movieName];
}

function flash_switchLanguage(lang) { //JAVASCRIPT -> FLASH FUNCTION -> Switch Flash menu language, excecuted by flags
	if (flashRunning == true){
		getFlashMovie("navigation").switchLanguage(lang);
	}
}

function flash_mouseClick(id) { //JAVASCRIPT -> FLASH FUNCTION -> Switch Flash menu items when content switches with JS - called by pictures on home.php
//alert("§CLICK");
	if (id == null){
		id = menuActive;
	}
	getFlashMovie("navigation").clickItem(id);
}
			
function showSlideShow(visible) {		
	if (visible == false){
		swfobject.removeSWF("slideshow");
	}else{	
		var c = document.getElementById("slideshow");
		if (!c) {
			var d = document.createElement("div");
			d.setAttribute("id", "slideshow");
			document.getElementById("container_header").appendChild(d);
		}
		swfobject.embedSWF("slideshow/led_art_slideshow.swf", "slideshow", "100%", "235px", "9.0.0", false);
	} 
}
		
function clearPage() {	//REMOVES ALL FLASH FILES AFTER LEAVING THE SITE WHEN FLASH IS ACTIVE
	if (flashRunning){;	
		swfobject.removeSWF("slideshow");
		swfobject.removeSWF("navigation");
	}
}