// JavaScript Document

//	Define mode table.  Controls loop image path
//	Index values are:	0 = state, 1 = metro, 2 = nw, 3 = ne, 4 = sw, 5 = se
var viewsarray = [
	{ View: "state" },
	{ View: "metro" },
	{ View: "nw" },
	{ View: "ne" },
	{ View: "sw" },
	{ View: "se" }
];

var Views = new Spry.Data.DataSet();
Views.setDataFromArray(viewsarray);


var loopProcID;		//	Loop process ID - We need this in order to turn on and off the looping process
var loopStatus;		//	Loop status - We need this to track whether the loop should be 'on' or 'off'
var visibleRow = 0;	//	Visible radar frame
var imageXSize;		//  Must be set in main file.  Determines the size of the loaded images.

// Watch radar loop dataset for updates
Spry.Data.Region.addObserver("RadarLoopDiv", radarLoopObserver);

// Begin auto refresh of current image
setInterval('refreshRadar()', 120000);	// Call refresh every two minutes

function setImageXSize( XSize ) {
	//  Set XSize of image.  Used for path information
	imageXSize = XSize;
}

function refreshRadar() {	// Reload the currently selected radar image

	if (!imageXSize)
		alert("Image X Size not defined");

	// Get current view
	var currentRow = Views.getCurrentRow();
	
	if (document.radarImage) {
		document.radarImage.src = "wximages/wsi/radar" + currentRow['View'] + imageXSize + ".jpg?" + Math.random();	// Math.random forces image refresh
	}
}

function setRadar(buttonobject, mode) {	// Set radar image and selected button
	if (!imageXSize)
		alert("Image X Size not defined");

	// Get current view
	var currentRow = Views.getCurrentRow();
	// Reset currently selected button
	if ( ( (currentRow['View'] == 'state') || (currentRow['View'] == 'metro') ) && (imageXSize == 640) ) {
		MM_findObj( currentRow['View'] + 'but').className = 'RadarLgButton';
	} else {
		MM_findObj( currentRow['View'] + 'but').className = 'RadarSmButton';
	}
	
	// Set the requested view
	Views.setCurrentRow(mode);
	currentRow = Views.getCurrentRow();
	
	// Set the selected radar image
	document.radarImage.src = "wximages/wsi/radar" + currentRow['View'] + imageXSize + ".jpg?" + Math.random();	// Math.random forces image refresh
	document.areaMap.src = "wximages/wxrad/map" + currentRow['View'] + ".gif";

	// Set the loop
	loopimages.setURL("wximages/wsi/loops/getloop.php", { method: "POST", postData: "View=" + currentRow['View'] + "&Size=" + imageXSize});
	loopimages.loadData();

/*	var rgn = Spry.Data.getRegion('RadarLoopDiv');
	if (rgn) {
		rgn.updateContent();
	}
*/	

	// Set selected button to "down" image
	if ( ( (currentRow['View'] == 'state') || (currentRow['View'] == 'metro') ) && (imageXSize == 640) ) {
		MM_findObj( currentRow['View'] + 'but').className = 'RadarLgButtonSelected';
	} else {
		MM_findObj( currentRow['View'] + 'but').className = 'RadarSmButtonSelected';
	}
	
}

function changeButtonStyle( object, mode , classname ) {	// Protects a selected button
	//'statebut','state' ,'RadarLgButtonHover'
	if (Views.getCurrentRowNumber() != mode) {
		object.className = classname;
	}
}

function toggleRadar() {	// Toggles radar and product layer visibility.  Stops looping if needed.
	
	var radarLayer = MM_findObj('RadarDiv');
	var productLayer = MM_findObj('ProdDiv');

	if (radarLayer.style.visibility == 'hidden') {
		radarLayer.style.visibility = 'visible';
		productLayer.style.visibility = 'hidden';
		if (loopStatus == 'on') {
			startLoop('toggle');
		}
			
	} else {
		productLayer.style.visibility = 'visible';
		radarLayer.style.visibility = 'hidden';
		if (loopStatus == 'on') {
			stopLoop('toggle');
		}
	}
}

function radarLoopObserver(notificationType, notifier, data) {
	if ((notificationType == "onPostUpdate") && (!loopStatus)) {
		loopStatus = "off";
		startLoop();
	}
}

function startLoop(toggle) {
	// Start radar looping
	var rgn = Spry.Data.getRegion('RadarLoopDiv');

	if (rgn) {	// Find out if the frames are loaded
		var name = rgn.getState();

		if ((name == "ready") && ((loopStatus == "off") || (toggle != null))) {
			// Start looping
			loopProcID = setInterval(advanceRadarFrame, 250);
			loopStatus = "on";
			
			// Display looping div
			MM_findObj('RadarLoopDiv').style.visibility = 'visible';
		}
	}
}

function stopLoop(toggle) {
	// Hide radar loop layer
	MM_findObj('RadarLoopDiv').style.visibility = 'hidden';

	if ((loopStatus == "on") || (toggle != null)) {
		// Stop looping process
		clearInterval(loopProcID);
		if (toggle == null) {
			loopStatus = "off";
		}
	
		// Set up for next time - make sure first frame is ready to display
		if (visibleRow != 0) {
			MM_findObj('loopImage' + '0').style.visibility = 'inherit';	// Make first frame visible
			MM_findObj('loopImage' + visibleRow).style.visibility = 'hidden';	// Make current frame hidden
			visibleRow = 0;
		}
	}
}

function advanceRadarFrame() {
	// Each call displays the next radar frame in the sequence
	var numrows = loopimages.getRowCount();
	var lastrow = visibleRow;
	
	if (visibleRow < numrows - 1) {
		// Advance frame count
		visibleRow++;
	} else {
		// Reset frame count
		visibleRow = 0;
	}
	
	// Change visible layer
	MM_findObj('loopImage' + visibleRow).style.visibility = 'visible';
	MM_findObj('loopImage' + lastrow).style.visibility = 'hidden';
}

function popLargerRadar(URL, h) {
	stopLoop();
	// Need wider window until Stivers is gone.  Use this kludge until then...
	//window.open(URL,'radar','width=880,height=' + h);
	window.open(URL,'radar','width=1110,height=' + h);
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

