/*******************************************************************************
	AUTHOR:		Timothy Higgins
	CONTACT:	timothymichaelhiggins@gmail.com
	
	Utility functions for TravelSmiths Sandals/Beaches subsite.
*******************************************************************************/

	//Define variables.
	var preloads=new Array();
	var splashColorIncrement=5;
	
	/***************************************************************************
		Move to the next splash image.
	***************************************************************************/
	function nextSplash(){
	
		//Advance to the next photo.
		if(i>=photos.length-1)
			i=0;
		else
			i++;
			
		//Preload the next photo if it has not yet been loaded.
		if(preloads.length<photos.length){
			var preload=new Image();
			preload.src="image/splash.php?resort="+resort+"&photo="+photos[i];
			preloads.push(preload);
		}
		
		//Get the dominant color of the next photo.
		var request;
		if(window.XMLHttpRequest)
			request=new XMLHttpRequest();
		else
			request=new ActiveXObject("Microsoft.XMLHTTP");
			
		request.open("POST","?",true);
		request.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded")
		request.send("action=color&resort="+resort+"&photo="+photos[i]);

		request.onreadystatechange=function(){
			if(request.readyState==4){
				var rgb=request.responseText.split(",");
				if(rgb.length==3)
					transitionSplash(rgb[0]-rgb[0]%splashColorIncrement,
						rgb[1]-rgb[1]%splashColorIncrement,
						rgb[2]-rgb[2]%splashColorIncrement);
				else
					transitionSplash(0,0,0);
			}
		};
	}
	
	/***************************************************************************
		Transition to the next splash color.
	***************************************************************************/
	function transitionSplash(r,g,b){

		//Get the current background color.
		var rgb=document.getElementById('splash').style.backgroundColor.
			replace(/[^0-9,]*/g,"").split(",");
		if(rgb.length==3){
			rgb[0]=parseInt(rgb[0]);
			rgb[1]=parseInt(rgb[1]);
			rgb[2]=parseInt(rgb[2]);
		}
		else{
			rgb=new Array(0,0,0);
			document.getElementById('splash').style.backgroundColor="rgb(0,0,0)";
		}
			
		//Continue fading the current background color to the desired color.
		if(rgb[0]<r) rgb[0]+=splashColorIncrement;
			else if(rgb[0]>r) rgb[0]-=splashColorIncrement;
		if(rgb[1]<g) rgb[1]+=splashColorIncrement;
			else if(rgb[1]>g) rgb[1]-=splashColorIncrement;
		if(rgb[2]<b) rgb[2]+=splashColorIncrement;
			else if(rgb[2]>b) rgb[2]-=splashColorIncrement;
		document.getElementById("splash").style.backgroundColor="rgb("+rgb[0]+
			","+rgb[1]+","+rgb[2]+")";
		
		//If the color fade is complete.
		if(rgb[0]==r&&rgb[1]==g&&rgb[2]==b){
			document.getElementById("splash").getElementsByTagName("div")[0].
				style.backgroundImage="url('image/splash.php?resort="+resort+
				"&photo="+photos[i]+"')";
			return true;
		}
		
		//Otherwise continue to fade.
		setTimeout("transitionSplash("+r+","+g+","+b+")",100);
	}
	
	/***************************************************************************
		Get search results for public_html/search.php.
	***************************************************************************/
	function getResults(){
			
		//Get all search parameters.
		var query="ajax=1";
		for(var i=0;i<document.getElementsByTagName("input").length;i++){
			if(document.getElementsByTagName("input")[i].id.indexOf("feature_")==0)
				query+="&"+document.getElementsByTagName("input")[i].id+"="+
					(document.getElementsByTagName("input")[i].checked?1:0);			
		}
		
		
		//Prepare serach parameters.
		var parameters=Array("feature","mood","location");
		var query="ajax=1";
		for(key in parameters){
			var values="";
			for(var i=0;i<document.getElementsByTagName("input").length;i++){
				if(document.getElementsByTagName("input")[i].id.indexOf(parameters[key])==0){
					if(document.getElementsByTagName("input")[i].checked){
						values+=values?",":"";
						values+=document.getElementsByTagName("input")[i].id.substring(parameters[key].length+1);
					}
				}
			}
			if(values)
				query+="&"+parameters[key]+"s="+values;
		}

		//AJAX.
		var request;
		if(window.XMLHttpRequest)
			request=new XMLHttpRequest();
		else
			request=new ActiveXObject("Microsoft.XMLHTTP");
		
		request.open("POST","?",true);
		request.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded")
		request.send(query);

		request.onreadystatechange=function(){
			if(request.readyState==4)
				document.getElementById("results").innerHTML=request.responseText;
		};
		return true;
	}
