var root = '/';

function fnTriggerPlayer(mode){
	//If there is a player, hide it so we can show the overlay
	var playerframe = document.getElementById('playerIframe');
	if(playerframe){
		if(mode=='hide')
			playerframe.style.visibility = 'hidden';
		else
			playerframe.style.visibility = 'visible';
	}
}

/* -- Begin: Login Functions -- */


	//Show the login form
	function fnShowScreen(methode){
		getXmlHttp();
		fnHideOverlay();
		
		var url=methode+".php?r="+Math.random();
		xmlHttp.onreadystatechange= function(){
			if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
				result = xmlHttp.responseText;
				
				if(result){
					fnTriggerPlayer('hide');
					
					var oDiv     		= document.createElement('div');
						oDiv.id  		= "overlay";
						oDiv.innerHTML 	= result;
					
					//Append the div to the body
					document.getElementsByTagName('body')[0].appendChild(oDiv);
					
					if(document.getElementById('formUsername'))
						document.getElementById('formUsername').focus();
					else if(document.getElementById('formEmail'))
						document.getElementById('formEmail').focus();	
				}
			} 	
		}
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
	
	//Hides the login screen
	function fnHideOverlay(){
		var overlay = document.getElementById('overlay');
		if(overlay){
			document.getElementsByTagName('body')[0].removeChild(overlay);
			fnTriggerPlayer('show');
		}
	}
	
	
	//Submit the login form and check values
	function fnSubmitLogin(methode){
		var methode  = methode;
		if(document.getElementById('formEmail')) 		var email 	  = document.getElementById('formEmail').value;
		if(document.getElementById('formUsername')) 	var username  = document.getElementById('formUsername').value;
		if(document.getElementById('formPassword')) 	var password  = document.getElementById('formPassword').value;
		if(document.getElementById('formPassword2')) 	var password2 = document.getElementById('formPassword2').value;
		
		//Check Username
		if(document.getElementById('formUsername')){
			if(!username){
				alert('Vul eerst je gebruikersnaam in!');
				document.getElementById('formUsername').focus();
				return false;
			}else if(username.length < 3){
				alert('Je Gebruikersnaam moet minimaal 3 karakters bevatten!');
				document.getElementById('formUsername').focus();
				return false;
			}
		}
		
		if(document.getElementById('formEmail')){
			if(!email){
				alert('Vul eerst je e-mail adres in!');
				document.getElementById('formEmail').focus();
				return false;
			}else if(email.length < 3){
				alert('Je e-mail adres moet minimaal 3 karakters bevatten!');
				document.getElementById('formEmail').focus();
				return false;
			}else{
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				if(reg.test(email) == false) {
					alert('Ongeldig e-mail adres!');
					document.getElementById('formEmail').focus();
					return false;  
				}
			}
		}
		
		//Check Password
		if(document.getElementById('formPassword')){
			if(!password){
				alert('Vul eerst je wachtwoord in!');
				return false;
			}else if(password.length < 3){
				alert('Je wachtwoord moet minimaal 3 karakters bevatten!');
				return false;
			}else if(document.getElementById('formPassword2')){
				if(password!=password2){
					alert('De wachtwoorden komen niet overeen!');
					return false;
				}	
			}
		}
		
		//Do the action here
		switch(methode){
			case 'login':		fnLogin(email,password);				break;
			case 'recover':		fnRecover(email);						break;	
			case 'register':	fnRegister(username,password,email);	break;	
		}
	}
	
	//Function to login a user
	function fnLogin(email,password){
		getXmlHttp();
		
		var url="modules/auth.php";
			url=url+"?action=login";
			url=url+"&email="+email;
			url=url+"&password="+password;
			
			
		xmlHttp.onreadystatechange= function(){
			if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
				result = xmlHttp.responseText.split('|');
				
				if(result[0]=='OKAY'){
					window.location.reload(true);
				}else if(result[0]=='ERROR'){
					alert(result[1]);
				}
			} 
		}
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
	
	//Function to register a user
	function fnRegister(username,password,email){
		getXmlHttp();
		
		var url="modules/auth.php";
			url=url+"?action=register";
			url=url+"&username="+username;
			url=url+"&password="+password;
			url=url+"&email="+email;
			
		xmlHttp.onreadystatechange= function(){
			if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
				result = xmlHttp.responseText.split('|');;
				
				if(result[0]=='OKAY'){
					alert(result[1]);	
					window.location.reload(true);
				}else if(result[0]=='ERROR'){
					alert(result[1]);
				}
			} 
		}
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
	
	//Function to recover a users email address
	function fnRecover(email){
		getXmlHttp();
		
		var url="modules/auth.php";
			url=url+"?action=recover";
			url=url+"&email="+email;
			
		xmlHttp.onreadystatechange= function(){
			if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
				result = xmlHttp.responseText.split('|');;
				
				if(result[0]=='OKAY'){
					alert(result[1]);
					window.location.reload(true);
				}else if(result[0]=='ERROR'){
					alert(result[1]);
				}
			} 
		}
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
/* -- End: Login Functions -- */


/* -- Begin: Favorites Functions -- */
function fnUserFav(methode,id){
	getXmlHttp();
	
	var url="modules/favorites.php";
		url=url+"?action="+methode;
		url=url+"&id="+id;
		
	xmlHttp.onreadystatechange= function(){
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			result = xmlHttp.responseText.split('|');;
			if(result[0]=='OKAY'){
				//alert(result[1]);
				window.location.reload(true);
			}else if(result[0]=='ERROR'){
				alert(result[1]);
			}
		} 
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}
/* -- End: Favorites Functions -- */


/* -- Begin: Rating Functions -- */
	var rater;
	function fnRating(oObj, elmID){
	
		var iRating = oObj.id;
	
		//Lets save the vote to the database
		getXmlHttp();
		
		var url="modules/save_vote.php"
		url=url+"?content_id="+elmID
		url=url+"&rating="+iRating
		
		xmlHttp.onreadystatechange= function(){
			if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
				result = xmlHttp.responseText;
				//Hier gaan we ook even een cookie setten voor de vote
				setCookie('vote'+elmID, iRating, 1)
				document.getElementById("ratingDiv"+elmID).innerHTML=result 
			}
		}
		
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
			
	function setCookie(c_name,value,expiredays) {
		var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	}
/* -- End: Rating Functions -- */



	/*
	function fnGetCatName(catID){
		getXmlHttp();
		var url="modules/get_catname.php"
		url=url+"?cat="+catID
		
		xmlHttp.onreadystatechange= function(){
			if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
				document.getElementById('searchCatName').innerHTML = xmlHttp.responseText;
			}
		}
		
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
	*/
	
	
/* -- Begin: DropDown Menu Functions -- */
	function fnTriggerList(mode){
		if(mode == 'show'){
			document.getElementById('searchList').style.display = 'block';
			disableSelection(document.getElementById('searchList'))
		}else{
			document.getElementById('searchList').style.display = 'none'	
		}
	}
	
	function fnSetCat(obj){
		var catID = obj.id.replace('c','');
		setCookie('searchcat', catID, 7);
		
		//Loop through all Li Items and unset selected class
		var liElements = document.getElementById('searchList').getElementsByTagName("li");
		for (var i = 0; i < liElements.length; i++) {
			var elem = liElements[i];
			
			elem.setAttribute("Class","");
		}
		//Set selected class
		document.getElementById(obj.id).setAttribute("Class","selected"); //For Most Browsers
		document.getElementById(obj.id).setAttribute("Class","selected"); //For IE; harmless to other browsers.
		
		document.getElementById('searchCat').value = catID;
		document.getElementById('searchCatName').innerHTML = obj.innerHTML;
		document.getElementById('searchList').style.display = 'none';
	}
	
	/* Disable Text Selection */
	function disableSelection(target){
		if (typeof target.onselectstart!="undefined") //IE route
			target.onselectstart=function(){return false}
		else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
			target.style.MozUserSelect="none"
		else //All other route (ie: Opera)
			target.onmousedown=function(){return false}
		target.style.cursor = "default"
	}
/* -- Begin: DropDown Menu Functions -- */
	
	
/* -- Begin: Search Rewrite Functions -- */
	function fnRewriteSearch(term,catID){
		
		if(term.length < 3){
			alert('Gebruik minimaal 3 tekens in je zoekopdracht');
			return;
		}else{
			//Lets save the vote to the database
			getXmlHttp();
			
			var url="modules/savesearch.php";
			var params = "search="+term;
			
			xmlHttp.onreadystatechange= function(){
				
				if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
					result = xmlHttp.responseText;
					
					//Hier gaan we ook even een cookie setten voor de vote
					if(result=='okay'){
						var catName  = document.getElementById('searchCatName').innerHTML.toLowerCase();
						var new_term = term.replace(/ /g, "_");
						if(catID!='' && catID != 'all'){
							new_path = root+new_term+"_c"+catID+"_"+catName+".html";
						}else{
							new_path = root+new_term+".html";
						}
						location.href = new_path;
					}
				}
			}
			
			xmlHttp.open("POST",url,true)
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", params.length);
			xmlHttp.setRequestHeader("Connection", "close");
			
			xmlHttp.send(params);
		}
	}
/* -- End: Search Rewrite Functions -- */





//Ajax callback
function GetXmlHttpObject(){
	var xmlHttp=null;
	try{ // Firefox, Opera 8.0+, Safari
 		xmlHttp=new XMLHttpRequest();
 	}catch (e){ //Internet Explorer
		try{
  			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
 	}
	return xmlHttp;
}

//Check if ajax is enabled by browser
function getXmlHttp(){
	xmlHttp=GetXmlHttpObject()
	if(xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
	}	
}
