var playID = null
var delay = 2500
var max_num_city = 13;

function findplay(action, id){
	if(action == 'pause'){
		document.getElementById('play').innerHTML='false';
	}
	else{
		document.getElementById('play').innerHTML='true';
	}
	startplay(action, id);
}

function stopplay(){
    clearTimeout(playID);
    document.getElementById('play').innerHTML='false';
}

function startplay(action, id){
	if(document.getElementById('play').innerHTML=='false'){
        stopplay();
    }
    else{
		if(action == 'autoplay-next'){
			id++;
		}
		else if(action == 'autoplay-back'){
			id--;
		}
		else if(action == 'playnext'){
			id++;
			document.getElementById('play').innerHTML='false';
		}
		else if(action == 'playback'){
			id--;
			document.getElementById('play').innerHTML='false';
		}
		if(id > max_num_city - 1){
			id = 0;
		}
		if(id < 0){
			id = max_num_city - 1;
		}
		
		findcity(action, id);
			
        playID = self.setTimeout("startplay('"+action+"', "+id+")", delay);
    }

}

function findcity(action, id){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="/wp-content/themes/conwayconfidential/findcity.php";
	url=url+"?action="+action+"&id="+id;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged(){ 
	if (xmlHttp.readyState==3){
		document.getElementById('city-wrapper').innerHTML='<div class="cityname"><a style="font-style:italic">Loading...</a></div>';
	}
	else if (xmlHttp.readyState==4)
	{ 
		document.getElementById('city-wrapper').innerHTML=xmlHttp.responseText;
	}
}

function getCityInfo(cat, subcat){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="http://conwayconfidential.com/wp-content/themes/conwayconfidential/findcity.php";
	url=url+"?cat="+cat+"&subcat="+subcat;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChangedInfo;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChangedInfo(){ 
	if (xmlHttp.readyState==3){
		document.getElementById('citymid-wrapper').innerHTML='<div class="citymid" style="text-align:center">Loading...</div>';
	}
	else if (xmlHttp.readyState==4)
	{ 
		document.getElementById('citymid-wrapper').innerHTML=xmlHttp.responseText;
	}
}

//Exception handling for the different browsers
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;
}

function changeButton(subject){
	document.getElementById('bed').className = '';
	document.getElementById('bliss').className = '';
	document.getElementById('dish').className = '';
	document.getElementById('littleones').className = '';
	document.getElementById('funstuff').className = '';
	document.getElementById('buythis').className = '';
	document.getElementById('beaches').className = '';
	document.getElementById('drinks').className = '';
	document.getElementById('confections').className = '';
	
	document.getElementById(subject).className = subject+'-active';
	
	return;
}
