// JavaScript Document
var photos = null;
var map = null;
var timeoutId = null;

function doLoadBeachwalk(name)
{
	var filename = "/" + name + "_json.txt";
	var hdn = document.getElementById("photo-index");
	hdn.value = "";
	//alert("starting");
	load_beachwalk(filename);
}

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

	httpreq.onreadystatechange = function () {
			if (httpreq.readyState == 4)
			{
				//var div = document.getElementById(divName);
		
				//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;
					format_map();
					format_beachwalk();
				}
				else 
				{		
					alert("Load Failed"); 
				}
			}
	}
	httpreq.send(null);		
}	

function format_map()
{
	var index = getIndex();	

	// create map here
		map = new GMap2(document.getElementById("map"));
		//alert(map.getSize());
		map.setCenter(new GLatLng(photos[index].lat, photos[index].lng), 18);
		map.setMapType(G_SATELLITE_MAP);
		map.addControl(new GSmallMapControl());
}

function format_beachwalk()
{
	// check for photo index
	var index = getIndex();	
	// display photo
	displayBeachwalkPhoto(index);
}

function displayBeachwalkPhoto(index)
{
	var photo = photos[index];
	// set title and description
	doSetText("photo-title", photo.title);
	doSetText("photo-desc", photo.desc);
	
	// set photo img 
	var div = document.getElementById("photo-display");
	// clear existing photo
	div.innerHTML = "";//<img src='" + photo.url + "' />";
	
	var lnk = document.createElement("a");
	lnk.setAttribute("href", "/photo/" + photo.id + ".html");
	//lnk.setAttribute("onclick", "MM_openBrWindow('viewphoto.php?photoid=" + photo.id + "','fullphoto','status=yes,scrollbars=yes,resizable=yes,width=600,height=600')");
	lnk.setAttribute("title", photo.title);
	
	var img = document.createElement("img");
	img.setAttribute("src", photo.url);
	
	// add image to link
	lnk.appendChild(img);
	
	// add link to div
	div.appendChild(lnk);
	lnk.parentNode.innerHTML = lnk.parentNode.innerHTML;
	// clear existing markers
	map.clearOverlays();
	// move marker on map
	var pt = new GLatLng(photo.lat, photo.lng);
	map.addOverlay(new GMarker(pt));
	map.panTo(pt);	
	
	// preload next image
	img = null;
	if (index != photos.length - 1)
	{
			img = new Image;
			img.src = photos[index + 1].url;
	}

	// preload prev image
	if (index > 0)
	{
			img = new Image;
			img.src = photos[index - 1].url;
	}

}

function getIndex()
{
	var hdn = document.getElementById("photo-index");
	var index = hdn.value;
	if (index.length == 0)
	{
		index = 0;
		setIndex(0);
	}
	else
		index = Number(index);

	return index;
}

function setIndex(index)
{
	var hdn = document.getElementById("photo-index");
	hdn.value = index;
	
	// set counters
	doSetText("num", index + 1);
	
	// check count
	var sp = document.getElementById("count");
	if (sp.innerHTML.length == 0) doSetText("count", photos.length);
}

function doNextPhoto()
{
	// get index value
	var index = getIndex();
	
	// check if last photo, exit if true
	if (index == photos.length - 1) return;
	
	// increment index by 1
	index += 1;
	setIndex(index);
	
	// display photo
	displayBeachwalkPhoto(index);
}

function doPrevPhoto()
{
	doPause();

	// get index value
	var index = getIndex();
	
	// check if first photo, exit if true
	if (index == 0) return;
	
	// decrement index by 1
	index -= 1;
	setIndex(index);
	
	// display photo
	displayBeachwalkPhoto(index);
}

function doFirstPhoto()
{
	doPause();
	
	var index = getIndex();
	
	// exit if at first
	if (index == 0) return;
	
	// set index to 0
	setIndex(0);
	
	// display photo
	displayBeachwalkPhoto(0);
}

function doLastPhoto()
{
	doPause();
	
	var index = getIndex();
	
	// exit if at last already
	if (index == photos.length - 1) return;
	
	// set index to 0
	setIndex(photos.length - 1);
	
	// display photo
	displayBeachwalkPhoto(photos.length - 1);
}

function doSetText(id, txt)
{
	// get object by id
	var para = document.getElementById(id);
	// clear existing text
	para.innerHTML = "";
	// set new text
	para.appendChild(document.createTextNode(txt));
}

function doPlay()
{
	doNextPhoto();
	
	// check index
	var index = getIndex();
	
	// clear timeout if at end
	if (index == photos.length - 1) 
	{
		doPause();
		return;
	}
	
	// set timeout
	if (timeoutId == null) 
	{
		// show pause button
		doShowButton("btnPause");		
	}
	
	timeoutId = setTimeout("doPlay()", 5000);	
}

function doPause()
{
	if (timeoutId != null)
	{
		// clear timeout
		clearTimeout(timeoutId);
		
		// reset
		timeoutId = null;
		
		// show play button again
		doShowButton("btnPlay");
	}
}

function doShowButton(id)
{
	// check which button
	switch (id)
	{
		case "btnPlay":
			// show play button
			var btn1 = document.getElementById(id);
			doToggleDisplay(btn1, "inline");
			
			// hide pause button
			var btn2 = document.getElementById("btnPause");
			doToggleDisplay(btn2, "none");
			
			break;
		case "btnPause":
			// show pause button
			var btn1 = document.getElementById(id);
			doToggleDisplay(btn1, "inline");
			
			// hide play button
			var btn2 = document.getElementById("btnPlay");
			doToggleDisplay(btn2, "none");
				
			break;
			
		default:
			
			break;
	}

}

function doToggleDisplay(obj, display)
{
	// check for IE
	if (obj.style != null)
	{
		// set display as IE
		obj.style.display = display;
	}
	else
	{
		obj.setAttribute("style", "display: " + display + ";");
	}
}

function doLoadBeachwalkPath(name)
{
	var filename = "/" + name + "_json.txt";
	load_beachwalk_path(filename);
}

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

	httpreq.onreadystatechange = function () {
			if (httpreq.readyState == 4)
			{
				//var div = document.getElementById(divName);
		
				//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;
					format_beachwalk_path();
				}
				else 
				{		
					alert("Load Failed"); 
				}
			}
	}
	httpreq.send(null);		
}	

function format_beachwalk_path()
{
	// get map reference
	//var map = new GMap2(document.getElementById("map"));
	
	// draw points here
	//alert("aloha");
	var points = new Array(); // new GLatLng(lat, lng), new GLatLng(dest_lat, dest_lng));
	// add first 10 points
	for (i = 0; i < photos.length - 1; i++)
	{
		photo = photos[i];
		points[i] = new GLatLng(photo.lat, photo.lng);
	}
	//map.addOverlay(new GMarker(points[0]));
	//map.addOverlay(new GMarker(points[1]));
	var line = new GPolyline(points, "#FF0000");
	map.addOverlay(line);	
	
}
