//Blipmap Animation Script
//This page copyright Morgan Sandercock 2008
//http://www.sandercock.com/
//mailto:morgan@sandercock.com
//May be distributed and modified by other RASP users
//This file would not normally be modified when creating a new installation
//updated June 2008 for NSW server's current+1 forecasts
//Update Feb 2009 - Latest version of Opera (9.63) no longer inverts the mouse wheel
//Update March 09 - current+2 forecasts now available for NSW

var TheTimeout;
var RetryCount = 0;
var RetryMax = 5; //during animation, wait 5 times for image to load then give up and go to next
var DragStart;
var DragStep = 30; //number of pixels in each "step" of the slider movements
var DragOffset = 20;
var Lim1Offset = 10;
var Lim2Offset = 60;
var Shade1Offset = 10;
var Shade2Offset = 15;
//var WheelDeltaStep = 120; //mouse wheel movements are reported in steps of 120 (pixels?)

//JScript doesn't have a normal number-formatting function
//add a new format function to the Number object prototype
Number.prototype.format4 = function () {
	if (this < 1000)
	{
		return "0" + this.toString();
	} else
	{
		return this.toString();
	}
}

function ChangeParam() {
	//Fired when the parameter drop-down changes or the curent/previous option changes
	var CurrPrev;
	var MidFix;
	var Suffix = "lst.d2.png";
	if (document.getElementById('OptCurrent').checked) CurrPrev = document.getElementById('OptCurrent').value; 
	if (document.getElementById('OptPrevious').checked) CurrPrev = document.getElementById('OptPrevious').value; 
	if (document.getElementById('OptPlusOne').checked) {
		CurrPrev = document.getElementById('OptPlusOne').value; 
		MidFix = ".curr+1.";
		//root_href = "http://blipmap.walsys.net/NEWSOUTHWALES";
	} else {
		if (document.getElementById('OptPlusTwo').checked) {
			CurrPrev = document.getElementById('OptPlusTwo').value; 
			MidFix = ".curr+2.";
		} else {
			MidFix = ".curr.";
		}
	}
	RetryCount = 0;
	document.getElementById('LoadingImg').style.visibility = "visible";
	top.frames[1].document.getElementById('TheMap').src = root_href + CurrPrev + document.getElementById('Param').value + MidFix + iTime.format4() + Suffix;
	//if animation is not running, save all params in a cookie and in the "add-favourite" link
	if(paused) SaveParams();
}

	function animate() {
    	if (!paused)
			if (document.getElementById('LoadingImg').style.visibility == "hidden" || RetryCount >= RetryMax)
			{
				if(!MoveNext())
				{
					iTime = Lim1Time - TimeStep;
					MoveNext();
				}
			}
			else
			{ 
				RetryCount++;
			}
		    TheTimeout = setTimeout('animate()', delay);
	}

	function Speed_Up() {
		delay = delay/2;
		SaveParams();
	}
	function Speed_Down() {
		delay = delay * 2;
		SaveParams();
	}

	function DoButtons() {
		//Set the up/down status of the buttons
		if (paused) {
			document.getElementById('btnPlay').disabled = false;
			document.getElementById('btnPause').disabled = true;
		} else {
			document.getElementById('btnPlay').disabled = true;
			document.getElementById('btnPause').disabled = false;
		} 
	}

	function Pause_Anim() {
       if (!paused)
	   {
		   clearTimeout(TheTimeout);
		   paused=true;
		   SaveParams();
		   DoButtons();
	   }
	}

	function Play_Anim() {
	   paused = false; 
	   document.getElementById('LoadingImg').style.visibility == "hidden";
	   SaveParams();
	   animate();
	   DoButtons();
	}

	function Show_Prev() {
		//Show the previous frame (pause if necessary)
		//wrap to end if necessary
		if (!paused) { 
			paused = true;
			clearTimeout(TheTimeout);
			document.getElementById('LoadingImg').style.visibility = "hidden";
		}
	   MovePrev();
	   DoButtons();
	}

	function Show_Next() {
		//Show the next frame (pause if necessary)
		//wrap to begining
		if (!paused) { 
			paused = true;
			clearTimeout(TheTimeout);
			document.getElementById('LoadingImg').style.visibility = "hidden";
		}
		MoveNext();
		DoButtons();
	}


function SliderMouseWheel() {
	//Like the mouse movements, this may report several "clicks" on the wheel
	//in one firing of the event - ignore this and allow the wheel to "slip."
	//
	//Returning false will cancel the event and stop it scrolling the window
	//
	//Firefox doesn't seem to have mousewheel events.
	
	//Ignore mouse wheel movements if the animation is running (not paused)
	if (paused) 
	{
		//Previous (reversed mousewheel in Opera): if (navigator.appName=='Opera'?event.wheelDelta > 0 : event.wheelDelta < 0) 
		if (event.wheelDelta < 0) 
		{ 
			//the MoveNext will return true if it is valid to move to the next image (and it will do the move)
			return ! MoveNext();
		} else { 
			//the Moveprev will return true if it is valid to move to the previous image (and it will do the move)
			return ! MovePrev();
		} 
	}
	return true;
}//slider mouse wheel

function MoveNext() {
	if ((iTime < imageMax) && (iTime < Lim2Time))
	{
		iTime += TimeStep;
		//Firefox doesn't support pixelTop and doesn't assume that an unadorned number means pixels
		//document.getElementById('SliderArrow').style.pixelTop = ((iTime-imageMin)/TimeStep) * DragStep + DragOffset;
		document.getElementById('SliderArrow').style.top = (((iTime-imageMin)/TimeStep) * DragStep + DragOffset) +'px';
		ChangeParam();
		return true;
	} else {
		return false;
	}
}//move next
function MovePrev() {
	if ((iTime > imageMin) && (iTime > Lim1Time))
	{
		iTime -= TimeStep;
		document.getElementById('SliderArrow').style.top = (((iTime-imageMin)/TimeStep) * DragStep + DragOffset) + 'px';
		ChangeParam();
		return true;
	} else {
		return false;
	}
}//move prev


function MoveLim1Next() {
	if ((Lim1Time < imageMax) && (Lim1Time < (Lim2Time-TimeStep)))
	{
		Lim1Time += TimeStep;
		document.getElementById('TopSlider').style.top = (((Lim1Time-imageMin)/TimeStep) * DragStep + Lim1Offset) + 'px';
		document.getElementById('TopShade').style.height = (((Lim1Time-imageMin)/TimeStep) * DragStep + Shade1Offset) + 'px';
		while (iTime < Lim1Time) MoveNext();
		   SaveParams();
		return true;
	} else {
		return false;
	}
}//move Limit 1 next
function MoveLim1Prev() {
	if (Lim1Time > imageMin)
	{
		Lim1Time -= TimeStep;
		document.getElementById('TopSlider').style.top = (((Lim1Time-imageMin)/TimeStep) * DragStep + Lim1Offset) + 'px';
		document.getElementById('TopShade').style.height = (((Lim1Time-imageMin)/TimeStep) * DragStep + Shade1Offset) + 'px';
		   SaveParams();
		return true;
	} else {
		return false;
	}
}//move Limit 1 prev
function MoveLim2Next() {
	if (Lim2Time < imageMax)
	{
		Lim2Time += TimeStep;
		document.getElementById('BotSlider').style.top = (((Lim2Time-imageMin)/TimeStep) * DragStep + Lim2Offset) + 'px';
		document.getElementById('BotShade').style.height = (((imageMax-Lim2Time)/TimeStep) * DragStep + Shade2Offset) + 'px';
		   SaveParams();
		return true;
	} else {
		return false;
	}
}//move Limit 2 next
function MoveLim2Prev() {
	if ((Lim2Time > imageMin) && (Lim1Time < (Lim2Time-TimeStep)))
	{
		Lim2Time -= TimeStep;
		document.getElementById('BotSlider').style.top = (((Lim2Time-imageMin)/TimeStep) * DragStep + Lim2Offset) + 'px';
		document.getElementById('BotShade').style.height = (((imageMax-Lim2Time)/TimeStep) * DragStep + Shade2Offset) + 'px';
		while (iTime > Lim2Time) MovePrev();
		   SaveParams();
		return true;
	} else {
		return false;
	}
}//move Limit 2 prev


function BodyMouseMove(e) {
	if(document.getElementById('SliderArrow').HasCapture) {
		while ((e.screenY >= DragStart + DragStep) && MoveNext()) {DragStart += DragStep;}
		while ((e.screenY <= DragStart - DragStep) && MovePrev()) {DragStart -= DragStep;}
	} 
	if(document.getElementById('TopSlider').HasCapture) {
		while ((e.screenY >= DragStart + DragStep) && MoveLim1Next()) {DragStart += DragStep;}
		while ((e.screenY <= DragStart - DragStep) && MoveLim1Prev()) {DragStart -= DragStep;}
	} 
	if(document.getElementById('BotSlider').HasCapture) {
		while ((e.screenY >= DragStart + DragStep) && MoveLim2Next()) {DragStart += DragStep;}
		while ((e.screenY <= DragStart - DragStep) && MoveLim2Prev()) {DragStart -= DragStep;}
	}
}

function BodyMouseUp() {
	//mouse button released, drop the capture on all objects
	document.body.style.cursor="auto";
	document.getElementById('SliderArrow').HasCapture = false;
	document.getElementById('SliderArrow').src = "images/ArrowFlat.gif";
	document.getElementById('TopSlider').HasCapture = false;
	document.getElementById('BotSlider').HasCapture = false;
}

function SetCookie(sName, sValue)
{
  document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 2099 23:59:59 GMT;";
}

function RetrieveParams() {
  //If we have a cookie (implicitly saved last position) then restore the parameters from the cookie
  //If we got a query string (eg a saved favourite) then use the parameters in the string
  //(the query takes precedence over the cookie)
  if(window.parent.location.search.length > 1) {
    DecodeParamArray(window.parent.location.search.substr(1).split("&"));
	SaveParams();
  } else if (document.cookie.length > 1) {
	DecodeParamArray(document.cookie.split("; "));
  }

  DoButtons();
  if(Lim1Time > imageMax || Lim1Time < imageMin) Lim1Time = 1000;
  if(Lim2Time > imageMax || Lim2Time < imageMin) Lim2Time = 1600;
  if(iTime > imageMax || iTime < imageMin) iTime = Lim1Time;
  if(paused) ChangeParam(); else animate();
}

function DecodeParamArray(aCookie) {
  //sets global variables based on the array of parameters (derived from a string-split on a cookie or query string)
  for (var i=0; i < aCookie.length; i++)
  {
    // a name=value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");

	if(aCrumb[0] == "Param") {
		document.getElementById('Param').value = aCrumb[1];
	}

	if(aCrumb[0] == "CurrPrev") {
		if(aCrumb[1] == "current") {
			document.getElementById('OptCurrent').checked = true;
			document.getElementById('OptPrevious').checked = false;
			document.getElementById('OptPlusOne').checked = false;
			document.getElementById('OptPlusTwo').checked = false;
		} else if(aCrumb[1] == "previous") {
			document.getElementById('OptCurrent').checked = false;
			document.getElementById('OptPrevious').checked = true;
			document.getElementById('OptPlusOne').checked = false;
			document.getElementById('OptPlusTwo').checked = false;
		} else if(aCrumb[1] == "plusone") {
			document.getElementById('OptCurrent').checked = false;
			document.getElementById('OptPrevious').checked = false;
			document.getElementById('OptPlusOne').checked = true;
			document.getElementById('OptPlusTwo').checked = false;
		} else if(aCrumb[1] == "plustwo") {
			document.getElementById('OptCurrent').checked = false;
			document.getElementById('OptPrevious').checked = false;
			document.getElementById('OptPlusOne').checked = false;
			document.getElementById('OptPlusTwo').checked = true;
		}
	}

	if(aCrumb[0] == "Lim1Time") {
		Lim1Time = parseInt(aCrumb[1]);
		document.getElementById('TopSlider').style.top = (((Lim1Time-imageMin)/TimeStep) * DragStep + Lim1Offset) + 'px';
		document.getElementById('TopShade').style.height = (((Lim1Time-imageMin)/TimeStep) * DragStep + Shade1Offset) + 'px';
	}

	if(aCrumb[0] == "Lim2Time") {
		Lim2Time = parseInt(aCrumb[1]);
		document.getElementById('BotSlider').style.top = (((Lim2Time-imageMin)/TimeStep) * DragStep + Lim2Offset) + 'px';
		document.getElementById('BotShade').style.height = (((imageMax-Lim2Time)/TimeStep) * DragStep + Shade2Offset) + 'px';
	}

	if(aCrumb[0] == "delay") {
		delay = parseInt(aCrumb[1]);
	}

	if(aCrumb[0] == "iTime") {
		iTime = parseInt(aCrumb[1]);
		document.getElementById('SliderArrow').style.top = (((iTime-imageMin)/TimeStep) * DragStep + DragOffset) +'px';
	}

	if(aCrumb[0] == "paused") {
		paused = (aCrumb[1] == "true");
	}
  }
}

function SaveParams() {
	//Always saves the current parameters into cookies, every time something changes
	SetCookie("Param", document.getElementById('Param').value);
	SetCookie("CurrPrev", "");
	if (document.getElementById('OptCurrent').checked) SetCookie("CurrPrev", "current");
	if (document.getElementById('OptPrevious').checked) SetCookie("CurrPrev", "previous"); 
	if (document.getElementById('OptPlusOne').checked) SetCookie("CurrPrev", "plusone");
	if (document.getElementById('OptPlusTwo').checked) SetCookie("CurrPrev", "plustwo");
	SetCookie("Lim1Time", Lim1Time);
	SetCookie("Lim2Time", Lim2Time);
	SetCookie("delay", delay);
	SetCookie("iTime", iTime);
	SetCookie("paused", paused);

	//Save all parameters to the "save favourite" link
	document.getElementById('FavouriteLink').href = ConstructURL();
	document.getElementById('FavouriteLink').title = document.getElementById('Param').options[document.getElementById('Param').selectedIndex].text;
	document.getElementById('EmailLink').href="mailto:?subject=NSW Blipmap Animation: " + escape(document.getElementById('Param').options[document.getElementById('Param').selectedIndex].text) + "&body=" + escape(ConstructURL());
}

function ConstructURL(){
	//create a URL (to the top containing frame) for the currently-displayed chart parameters
	var str;
	with (window.parent.location) str = protocol + "//" + host + pathname;
	str += "?Param=" + document.getElementById('Param').value;
	if (document.getElementById('OptCurrent').checked) str += "&CurrPrev=" + "current";
	if (document.getElementById('OptPrevious').checked) str += "&CurrPrev=" + "previous"; 
	if (document.getElementById('OptPlusOne').checked) str += "&CurrPrev=" + "plusone";
	if (document.getElementById('OptPlusTwo').checked) str += "&CurrPrev=" + "plustwo";
	str += "&Lim1Time=" + Lim1Time + "&Lim2Time=" + Lim2Time + "&delay=" + delay + "&iTime=" + iTime + "&paused=" + paused ; 
	return str;
}
