
var pe = false;
var aImages = new Array();
var aIPs = new Array();

if (!cityClickedUrl) cityClickedUrl = '%%scripturl%%?forecast=zandh&state=[state]&place=[place]';


	function toggleAnimation(optInterval) {

		if (pe) {
			Element.hide('mapLoadingStatus');
			pe.stop();
			pe = false;


			$('opt_anim_link' ).innerHTML = 'Start';
		}
		else {
			$('opt_anim_link' ).innerHTML = 'Stop';
			$('mapLoadingStatus').innerHTML = '';

			if (aIPs['i' + mapInt]) {
				startAnimation(aIPs['i' + mapInt].aImages, aIPs['i' + mapInt].nLoaded);
			}
			else {
				Element.show('mapLoadingStatus');
				aIPs['i' + mapInt] = new ImagePreloader(aImages['i' + mapInt], startAnimation,'mapLoadingStatus');
			}
		}
	}

	function startAnimation(aImages, nLoaded) {
		Element.hide('mapLoadingStatus');
		$('opt_anim_link' ).innerHTML = 'Stop';


		pe = new  PeriodicalExecuter(
		function(tpe) {
			mapMsg++;
			if (mapMsg > nLoaded) mapMsg = 1;

			$('contourMap').src =  aImages[mapMsg-1].src;
			updateMapHighlight();


		}, animInterval);

	}

	function updateMapHighlight() {

		var sfEls = document.getElementById(mapIntervalName).getElementsByTagName("TD");
		for (var i=0; i<sfEls.length; i++) {
			if (sfEls[i].className == 'smallIntervalsSelected') {
				sfEls[i].className = 'smallIntervals';
			}
			else if (sfEls[i].className == 'largeIntervalsSelected') {
				sfEls[i].className = 'largeIntervals';
			}

		}


		var obj = $('opt_' + mapMsg + '_' + mapInt + 'hr');

		if (obj) {
			if (obj.className.indexOf('smallIntervals') >= 0) {
				obj.className = 'smallIntervalsSelected';
			}
			else if (obj.className.indexOf('largeIntervals') >= 0) {
				obj.className = 'largeIntervalsSelected';
			}

		}

	}

function updateMapState(mType){
	var bRedirect = false;
	if (mType != '' && mType != mapState) {
		mapState = mType;
		bRedirect = true;

	}
	updateMapImage(bRedirect);
	return false;
}
function updateMapInt(mType){
	if (mType != '') mapInt = mType;
	updateMapImage();
	return false;
}
function updateMapMsg(mType){
	mapMsg = mType;
	updateMapImage();
	return false;
}
function updateMapMsgInt(mMsg,mInt){
	if (mMsg != '') mapMsg = mMsg;
	if (mInt != '') mapInt = mInt;
	updateMapImage();
	return false;
}

function updateMapFill(mType) {
	if (mType != mapFill) {
		mapFill = mType;
		updateMapImage(true);
	}
	return false;
}





function  updateCities(bShowCities) {
	mapShowCities = bShowCities;
	if (mapShowCities ) Element.show('mapCities');
	else Element.hide('mapCities');

}
function  updateCounties(bShowCounties) {
	if (mapState == 'us') return;

	mapShowCounties = bShowCounties;

	if (mapShowCounties ) Element.show('mapCounties');
	else Element.hide('mapCounties');

}

function cityClicked(sCity, sState) {

	if (sCity && sState) {
		var turl = cityClickedUrl.replace('[state]', sState.toLowerCase());
		turl = turl.replace('[place]', sCity.toLowerCase().replace(' ','+'));
		location = turl;
	}
}



function ImagePreloader(images, callBack, statusDiv)

{
  // store the call-back
   this.callBack = callBack;
   this.statusDiv = statusDiv;
   // initialize internal state.

   this.nLoaded = 0;
   this.nProcessed = 0;
   this.aImages = new Array;

   // record the number of images.
   this.nImages = images.length;

   // for each image, call preload()
	if (this.statusDiv) $(this.statusDiv).innerHTML = '<div style="position: relative; top: 5px;">Loading 1 of ' + this.nImages + '</div>';
   for ( var i = 0; i < images.length; i++ )
      this.preload(images[i]);
}

ImagePreloader.prototype.preload = function(image)

{
   // create new Image object and add to array
   var oImage = new Image;
   this.aImages.push(oImage);

   // set up event handlers for the Image object
   oImage.onload = ImagePreloader.prototype.onload;
   oImage.onerror = ImagePreloader.prototype.onerror;
   oImage.onabort = ImagePreloader.prototype.onabort;

   // assign pointer back to this.
   oImage.oImagePreloader = this;
   oImage.bLoaded = false;

   // assign the .src property of the Image object
   oImage.src = image;

}



ImagePreloader.prototype.onComplete = function()

{
   this.nProcessed++;
   if (this.statusDiv) $(this.statusDiv).innerHTML = '<div style="position: relative; top: 5px;">Loading ' + this.nProcessed + ' of ' + this.nImages +'</div>';
   if ( this.nProcessed == this.nImages )
   {
      this.callBack(this.aImages, this.nLoaded);
   }

}

ImagePreloader.prototype.onload = function()
{
   this.bLoaded = true;
   this.oImagePreloader.nLoaded++;
   this.oImagePreloader.onComplete();

}

ImagePreloader.prototype.onerror = function()
{
   this.bError = true;
   this.oImagePreloader.onComplete();

}

ImagePreloader.prototype.onabort = function()
{
   this.bAbort = true;
   this.oImagePreloader.onComplete();

}

