// JavaScript Document
function doShowDetails()
{
	showPhotoDetails();
	hideQuestion();
	hideIslandQuestion();
}

function doSelectYes()
{
	hideQuestion();
	var answer = getAnswer();
	
	if (answer == "M")
	{
		// correct
		//alert("Aloha: Correct");
		showCorrect();
		updateTotals("c1"); 
	}
	else
	{
		// show wrong island message
		showWrongIsland();
		updateTotals("w1"); 
	}
}

function doSelectNo()
{
	hideQuestion();	
	var answer = getAnswer();
	
	if (answer == "M")
	{
		// show wrong
		showWrong();
		updateTotals("w1"); 
	}
	else
	{
		// correct
		// show island question
		showIslandQuestion();
		updateTotals("c1"); 
	}
}

function doSelectIsland(s)
{
	hideIslandQuestion();
	//alert("Aloha: " + s);
	var answer = getAnswer();
	
	if (answer == s)
	{
		// show correct message
		showCorrectIsland();
		updateTotals("c2"); 
	}
	else
	{
		// wrong
		showWrongIsland();
		updateTotals("w2");
	}
}

function getAnswer()
{
	var hdn = document.getElementById("ca");
	return hdn.value;
}

function hideQuestion() { hideDiv("question1"); }
function hideIslandQuestion() { hideDiv("question2"); }
function showIslandQuestion() { showDiv("question2"); }
function showCorrect() { showDiv("correct1_maui"); showPhotoDetails(); }
function showWrong() { showDiv("wrong1_maui"); showPhotoDetails(); }
function showWrongIsland() { showDiv("wrong1_island"); showPhotoDetails(); }
function showPhotoDetails() { showDiv("photo-details"); }
function showCorrectIsland() { showDiv("correct2"); showPhotoDetails(); }

function hideDiv(name)
{
	var div = document.getElementById(name);
	setClass(div, "hide-div");	
}


function showDiv(name)
{
	var div = document.getElementById(name);
	setClass(div, "show-div");	
}

function updateTotals(n)
{
	var h = document.getElementById("dt");
	var d = h.value;
	url = "isit_data.php?d=" + d + "&" + n + "=1";
	//alert("Aloha: getPhotos for " + url);

	var httpreq = getHTTPObject();
	
	httpreq.open("GET", url, true);

	httpreq.onreadystatechange = function () {
			if (httpreq.readyState == 4)
			{
				
				//alert(httpreq.responseXML);
				if (httpreq.responseText)
				{
					/*last_text_response = httpreq.responseText;
					var func = new Function("return " + last_text_response);
					var obj  = func();
					photos = obj.photos;
					displayResults(photos);*/
				}
				else 
				{		
					//alert("Load Failed"); 
				}
			}
	}
	httpreq.send(null);		
}	

function getListMonth()
{
	var lst = document.getElementById("lstMonths");
	var opt = lst.options[lst.selectedIndex];
	return opt.value;
}
function doPhotoList()
{
	var d = 0; //getListMonth();

	url = "isit_data.php?d=" + d+ "&l=1";
	//alert("Aloha: getPhotos for " + url);

	var div = document.getElementById("month-list");
	div.innerHTML = "<h3>Loading...</h3>";

	var httpreq = getHTTPObject();
	
	httpreq.open("GET", url, true);

	httpreq.onreadystatechange = function () {
			if (httpreq.readyState == 4)
			{
				
				//alert(httpreq.responseXML);
				if (httpreq.responseText)
				{
					last_text_response = httpreq.responseText;
					var func = new Function("return " + last_text_response);
					var obj  = func();
					photos = obj.photos;
					displayResults(photos);
					//alert("Aloha: found results - " + photos.length);
					//format_map();
					//format_beachwalk();
				}
				else 
				{		
					div.innerHTML = "<p>Data not available, please try again later.</p>";
				}
			}
	}
	httpreq.send(null);		
}

function displayResults(photos)
{
	clearResults();
	var div = document.getElementById("month-list");
	//alert("Aloha: displayResults, " + photos.length);
	// loop through photos
	var ul = document.createElement("ul");
	
	for (i = 0; i <  photos.length; i++)
	{
		// create paragraph tag
		var li = document.createElement("li");
		
		// get photo
		var photo = photos[i];
		
		// show image first
		var lnk = document.createElement("a");
		lnk.setAttribute("href", "isitmaui.php?d=" + (i + 1));

		var img = document.createElement("img");
		img.setAttribute("src", photo.url);
		img.setAttribute("border", 0);
		lnk.appendChild(img);
		li.appendChild(lnk);
		
		li.appendChild(document.createElement("br"));
		
		lnk = document.createElement("a");
		lnk.setAttribute("href", "isitmaui.php?d=" + (i + 1));
		lnk.appendChild(document.createTextNode(photo.label));
		li.appendChild(lnk);

		// add to list
		ul.appendChild(li);
	}
	div.appendChild(ul);
}

function clearResults()
{
	var div = document.getElementById("month-list");
	div.innerHTML = "";
}


function doTodaysPhotoList()
{
	url = "today_data.php";
	//alert("Aloha: getPhotos for " + url);

	var div = document.getElementById("month-list");
	div.innerHTML = "<p>Loading...</p>";

	var httpreq = getHTTPObject();
	
	httpreq.open("GET", url, true);

	httpreq.onreadystatechange = function () {
			if (httpreq.readyState == 4)
			{
				
				//alert(httpreq.responseXML);
				if (httpreq.responseText)
				{
					last_text_response = httpreq.responseText;
					var func = new Function("return " + last_text_response);
					var obj  = func();
					photos = obj.photos;
					displayTodaysResults(photos);
					//alert("Aloha: found results - " + photos.length);
					//format_map();
					//format_beachwalk();
				}
				else 
				{		
					alert("Load Failed"); 
				}
			}
	}
	httpreq.send(null);		
}

function displayTodaysResults(photos)
{
	clearResults();
	var div = document.getElementById("month-list");
	//alert("Aloha: displayResults, " + photos.length);
	// loop through photos
	var ul = document.createElement("ul");
	ul.setAttribute("style", "width: 310px");
	for (i = 0; i <  photos.length; i++)
	{
		// create paragraph tag
		var li = document.createElement("li");
		
		// get photo
		var photo = photos[i];
		
		// show image first
		var lnk = document.createElement("a");
		lnk.setAttribute("href", photo.pageUrl);

		var img = document.createElement("img");
		img.setAttribute("src", photo.thumbUrl);
		img.setAttribute("border", 0);
		lnk.appendChild(img);
		li.appendChild(lnk);
		
		li.appendChild(document.createElement("br"));
		
		lnk = document.createElement("a");
		lnk.setAttribute("href", photo.pageUrl);
		lnk.appendChild(document.createTextNode(photo.id));
		li.appendChild(lnk);

		// add to list
		ul.appendChild(li);
		
	}
	div.appendChild(ul);
}