var windowOpen = false;
var currentCity = '';
var currentDate = '';

function showDetails(city, admitted, target) {
	if (windowOpen == false) {
		ajaxwin = dhtmlwindow.open("ajaxbox", "inline", 
			"<html><body><table width='100%' height='100%'><tr><td><center><img src='/images/loading.gif' width='32' height='32'></center></td></tr></table></body></html>", "Details", 
			"width=550px,height=240px,left=300px,top=100px,resize=1,scrolling=1");

		ajaxwin.load("ajax", "details.php?city=" + city + "&date=" + admitted + "&target=" + target, "Details");

		ajaxwin.moveTo('middle', 'middle');

		windowOpen = true;
	} else {
		ajaxwin.load("inline", "<html><body><table width='100%' height='100%'><tr><td><center><img src='/images/loading.gif' width='32' height='32'></center></td></tr></table></body></html>", "Details");
		ajaxwin.load("ajax", "details.php?city=" + city + "&date=" + admitted + "&target=" + target, "Details");

		// show window if it has been closed (hidden)
		if (ajaxwin.style.display == 'none') {
			ajaxwin.show();
			ajaxwin.moveTo('middle', 'middle');
		}
	}
}

function changeDate(admitted) {
	if (windowOpen) {
		ajaxwin.style.display='none';
	}

	document.getElementById('photos').innerHTML = "<br><br><br><br><br><br><br><br><br><br><br><br><br><center><img src='/images/loading.gif' width='32' height='32'></center>";
	makeRequest("photos.php?city=" + currentCity + "&date=" + admitted);
	currentDate = admitted;

	document.getElementById('linktome').href = '/?city=' + currentCity + '&date=' + admitted;
}

function changeCity(city) {
	if (windowOpen) {
		ajaxwin.style.display='none';
	}

	document.getElementById('photos').innerHTML = "<br><br><br><br><br><br><br><br><br><br><br><br><br><center><img src='/images/loading.gif' width='32' height='32'></center>";
	makeRequest("photos.php?city=" + city + "&date=" + currentDate);
	currentCity = city;

	document.getElementById('linktome').href = '/?city=' + city + '&date=' + currentDate;
}

function loadPhotos(city, admitted) {
	document.getElementById('photos').innerHTML = "<br><br><br><br><br><br><br><br><br><br><br><br><br><center><img src='/images/loading.gif' width='32' height='32'></center>";
	makeRequest("photos.php?city=" + city + "&date=" + currentDate);

	document.getElementById('linktome').href = '/?city=' + city + '&date=' + admitted;
}

function onLoad(city, admitted, target) {
	showDetails(city, admitted, target);

	if (city == '') {
		loadPhotos('All', admitted);
	} else {
		loadPhotos(city, admitted);
	}

	currentCity = city;
	currentDate = admitted;
}

function makeRequest(url) {
	var httpRequest;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 

			catch (e) {}
		}
	}

	if (!httpRequest) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}

	httpRequest.onreadystatechange = function() { responseReceived(httpRequest); };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

function responseReceived(httpRequest) {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			document.getElementById('photos').innerHTML = httpRequest.responseText;;
		} else {
			alert('There was a problem with the request.');
		}
	}
}

