
//// set up xmlhttp instance
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (E) {
		xmlhttp = false;
	}
}
@end @*/

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}

function showElem(elemID, visibility)
{
        var elem = document.getElementById(elemID);
        elem.className = (visibility) ? "show" : "hide";
}
function toggleElem(elemID)
{
        var elem = document.getElementById(elemID);
        //elem.className = ( elem.className=="show" ) ? "hide" : "show";
		if (elem.className=="show") {
			elem.className = "hide";
		} else {
			elem.className = "show";
			urchinTracker('/view/timelapse/toggle_'+elemID);
		}
}

function setElemHtml( elem, html )
{
        elem.innerHTML = html;
}

function setCondition( key, val )
{
	var vals = "";
	if( val == "" || val[0]=="" )
	{
		vals = "unavailable";
	}
	else
	{
		switch( key )
		{
			case "wind":
				var spd = val[0];
				var dir = val[1];
				vals = spd + "mph " + dir;
				break;
			case "temp":
				vals = Math.round(((val-32) * 5/9)*10)/10  + "&deg; C ("+val+" &deg;F)";
				break;
			case "press":
				vals = Math.round(25.4*val*10)/10 + " mmHg";
				break;
			case "hum":
				vals = val + "%";
				break;
			default:
				vals = val;
		}
	}
	setElemHtml( document.getElementById(key), vals );
}

function reloadImg()
{
        //var imgelem = document.getElementById("viewimage");
        //var dateStr = (new Date()).getTime();
        //imgelem.src = "images/newview.jpg?" + dateStr;
		reloadImageByID( "viewimage", "images/newview.jpg" );
}

function reloadGraphs()
{
    reloadImageByID( "tempGraph", "images/temperature.gif" );
	reloadImageByID( "pressGraph", "images/pressure.gif" );
	reloadImageByID( "humidGraph", "images/humidity.gif" );
}

function reloadImageByID( elemID, imgSrc )
{
	var dateStr = (new Date()).getTime();
	var imgelem = document.getElementById(elemID);
	imgelem.src = imgSrc + "?" + dateStr;
}

function reloadConditions()
{
	var url = "weather-lhsal.txt";
        var dateStr = (new Date()).getTime();
	url += "?" + dateStr;
	if( ! xmlhttp )
	{
		return;
	}

	xmlhttp.open( "GET", url, true );
	xmlhttp.onreadystatechange = function()
	{
		if( xmlhttp.readyState == 4 )
		{
			parseConditions( xmlhttp.responseText );
		}
	}
	xmlhttp.send(null);
}

function parseConditions( conds )
{
	var pairs = conds.split("|");
	var wind = Array();
	for( var p in pairs )
	{
		var s = pairs[p].split("=");
		if( s[0] == "winds" )
			wind[0] = s[1];
		else if( s[0] == "windd" )
			wind[1] = s[1];
		else
			setCondition( s[0], s[1] );
	}
	setCondition("wind", wind );
}

function showArchiveList()
{
	if( document.getElementById("archiveTableHolder").innerHTML == "Loading data" )
		loadArchiveList();
	toggleElem( "archiveList" );
}

function loadArchiveList()
{
	var url = "timelapse_archive.txt";
	if( !xmlhttp )
		return;
	
	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange = function()
	{
		if( xmlhttp.readyState == 4 )
		{
			parseTimelapseArchive( xmlhttp.responseText );
		}
	}
	xmlhttp.send(null);
}

function parseTimelapseArchive( archive )
{
	var html = "<table id=\"archiveTable\">";
	html += "<tr class=\"nohover\"><th>date</th><th>description</th><th>movie size</th><tr>";
	var rows = archive.split("~");
	for( var r in rows )
	{
		var s = rows[r].split("|");
		var date = s[0];
		var desc = s[1];
		var size = s[2];
		var url = s[3];
		url = url.replace(/\n/g, '');
		var rowHTML = "<tr onclick=\"showArchive('"+url+"');\"><td>"+date+"</td><td>"+desc+"</td><td>"+size+" MB</td></tr>";
		html += rowHTML;
	}
	html += "</table>";
	setElemHtml( document.getElementById("archiveTableHolder"), html );
}

function showArchive( url )
{
	window.location = url;
}

// for timelapse movie display
function showTimelapse()
{
	toggleElem("yesterdayHolder");
}

function init()
{
	reloadConditions();
        var conds = new Object();
        reloadImg();
        setInterval("reloadImg(); reloadConditions()", 60000);
		setInterval("reloadGraphs();", 360000);
}

function fixRollTrans()
{
	var img = document.getElementById("viewlabelimg");
	img.src = "images/transparent.gif";
	var div = document.getElementById("viewlabels");
	div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/LOCATIONSover.png', sizingMethod='scale')";
	img = document.getElementById("lhsrollimg");
	img.src = "images/transparent.gif";
}

window.onload = init;
