////////////////////////////////////////////////////////////////////////////////
//
// GLOBAL VARIABLES
//
////////////////////////////////////////////////////////////////////////////////
var g_selectedTab               = "live";
var g_bodySize                  = "large";
var g_liveDataObject;
var g_preferencesObject;
var g_stateObject;
var g_forecastDataObject;
var g_cameraDataObject;
var g_radarDataObject;
var g_alertsDataObject;
var g_moreDataObject;
var g_vaneObject = new VaneObject();
var SideShowTypes = new SideShowDataType();
var g_sideShowEnabled = false;
var g_progressIndicator;
var g_designObject;
var g_clickDidHappen = false;
var g_backgroundImage = new BackgroundImageController();
var previousLocation = "";
var previousUnits = "";

var isFirstSideShowWrite = true;
// Variables For The SideShow Menu
var isSideShowForecastWritten = false;
var isSideShowCameraWritten = false;
var isSideShowRadarWritten = false;
var isSideShowLiveObsWritten = false;
var version = "1.0.0.6";
var name = "VistaGadgets_WeatherBugLocalWeatherGadget";
var unicodeDegreeSymbol = "\u00B0";
var zcode = "Z5483";
var userNotifiedOfUpdate = false;

var XMLConstants;
var URLParameters;

function VaneObject () {
	this.currentRotation = 0;
	this.startRotation = 0;
	this.endRotation = 0;
	this.isAnimatingClockwise = true;
	this.isAnimating = false;
	this.duration = 2000; // Time In Miliseconds
	this.startTime;
};

function SideShowDataType()
{
	this.sideShowEnabled = true;
    this.Live = "1";
    this.Forecast = "2";
    this.Radar = "3";
    this.Cams = "4";
    this.LiveString;
    this.ForecastString;
    this.RadarString;
    this.MenuString;
    this.CamsString;
}

function BackgroundImageController()
{
	this.bgimage;
	this.isInitialized = false;
	this.updateAvailable = false;
	this.isSetup = false;
	
	this.tabIsVisible = false;
	this.mouseIsOverTab = false;
	this.isLarge = true;
	this.dayOrNight = "day"; // day, night
	this.section = "current"; // current, cams, forecast, more, radar

	
	this.setImage = function ()
	{
		if(this.isInitialized == false) // show loading bg
			document.body.style.background = "url(../../../images/backgrounds/other/loading.png)";
		else if(this.updateAvailable == true) // show update bg
			document.body.style.background =  "url(../../../images/backgrounds/other/update.png)";
		else if(this.isSetup == false) // show setup bg
			document.body.style.background = "url(../../../images/backgrounds/other/setup.png)";
		else if(this.isLarge == false) // show mini bg
			document.body.style.background = "url(../../../images/backgrounds/mini/" + this.dayOrNight + "/" + this.getCurrentMouseState() + ")";
		else // show large bg
			document.body.style.background = "url(../../../images/backgrounds/" + this.section + "/" + this.getSubFolder() + this.getCurrentMouseState() + ")";
	}
	
	this.getSubFolder = function()
	{
		if(this.section == "current")
			return this.dayOrNight + "/";
		else
			return "";
	}
	
	this.getCurrentMouseState = function ()
	{
		if(this.tabIsVisible == true && this.mouiseIsOverTab == true)
			return "mo.png";
		else if(this.tabIsVisible == true)
			return "nmo.png";
		else
			return "off.png";
	}
}
////////////////////////////////////////////////////////////////////////////////
//
// WEATHERBUG GADGET
//
////////////////////////////////////////////////////////////////////////////////
function loadWeatherBug()
{
	alerts_tab.style.visibility = "hidden";
	udate.style.visibliity = "hidden";
	document.getElementById("radar_image").attachEvent("onclick",openRadarLink);
	document.getElementById("cams_image").attachEvent("onclick",openCamsLink);
	document.getElementById("live_icon").attachEvent("onclick",openForecastLink);
	document.getElementById("small_live_icon").attachEvent("onclick",openForecastLink);
    document.getElementById("fc_one_icon").attachEvent("onclick",openForecastLink);
    document.getElementById("fc_two_icon").attachEvent("onclick",openForecastLink);
    document.getElementById("fc_three_icon").attachEvent("onclick",openForecastLink);

	XMLConstants = new WBXMLConstants();
	URLParameters = new WBUrlParameters();

	if(g_preferencesObject == null)
		g_preferencesObject = new WBPreferences();
	
	g_preferencesObject.SetCurrentAppVersion(version);
	g_preferencesObject.SetCurrentAppName(name);
	g_preferencesObject.SetCurrentZCode(zcode);
	
	if(g_progressIndicator == null)
		g_progressIndicator = new WBProgressAnimator(document.getElementById("progress_indicator"),9,100,"../images/loader");
		
	g_selectedTab = "live";
	tab_title.innerHTML = rf_liveconditions;
	
	if(g_designObject == null)
	{
		g_designObject = new WBDataLocations(designLoaded,dataLoadingError,'g_designObject.load()',30000,1800000,"http://datafeed.weatherbug.com/Command/VistaGadget_v1004.aspx",g_preferencesObject,version);
		g_designObject.load();
		g_progressIndicator.start("g_progressIndicator.drawStep()");
		setTimeout("reloadDesignData()",20000);
	}
	else
		designLoaded();
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function designLoaded()
{
	g_progressIndicator.stop();
	g_backgroundImage.isInitialized = true;
	
	// Allow access to settings screen
	System.Gadget.settingsUI = "settings.html";
	System.Gadget.onShowSettings = createStationUrl;
	
	// Setup permanent params
	g_preferencesObject.SetCurrentAppName(g_designObject.appName);
	g_preferencesObject.SetCurrentPartnerId(g_designObject.pid);
	g_preferencesObject.SetCurrentUserId(g_preferencesObject.getGUID());
	g_preferencesObject.SetCurrentLanguageType(rf_languagecode);
	g_preferencesObject.SetCurrentShowPoints("1");
	g_preferencesObject.SetCurrentIconSet("1");
	
	if(g_designObject.sideshowenabled == "1")
	    g_sideShowEnabled = true;
	else
	    g_sideShowEnabled = false;
	
    if(g_designObject.updateAvailable == true && userNotifiedOfUpdate == false && checkIfVersionSkipped() == false)
	{
		udate.style.visibility = "visible";
		g_backgroundImage.updateAvailable = true;
		g_backgroundImage.setImage();
	}
	else if(g_preferencesObject.getZipCode() == "" && g_preferencesObject.getStationID() == "")
	{
		udate.style.visibility = "hidden";
		navigation.style.visibility = "visible";
		g_backgroundImage.setImage();
	}
	else
	{
		udate.style.visibility = "hidden";
		setup.style.visibility = "hidden";
		navigation.style.visibility = "visible";
		location_text.style.visibility = "visible";
		setUpInterfaceVisibility();
		g_backgroundImage.isSetup = true;
		g_backgroundImage.setImage();
		
		if(g_preferencesObject.getType() == false)
	    {
	        g_preferencesObject.SetCurrentZipCode(g_preferencesObject.getZipCode());
	        g_preferencesObject.SetCurrentCityCode("");
	    }
	    else
	    {
	        g_preferencesObject.SetCurrentZipCode("");
	        g_preferencesObject.SetCurrentCityCode(g_preferencesObject.getZipCode());
	    }
    	
	    g_preferencesObject.SetCurrentStationId(g_preferencesObject.getStationID());
	    g_preferencesObject.SetCurrentUnitType(g_preferencesObject.getUnits());
		
		if(g_stateObject == null)
		{
			g_stateObject = new WBState(
								new Array(g_designObject.liveDataUrl.minupdaterate,g_designObject.forecastDataUrl.minupdaterate,g_designObject.cameraDataUrl.minupdaterate,g_designObject.radarDataUrl.minupdaterate,g_designObject.moreDataUrl.minupdaterate,g_designObject.alertsDataUrl.minupdaterate),
								new Array(g_designObject.liveDataUrl.maxupdaterate,g_designObject.forecastDataUrl.maxupdaterate,g_designObject.cameraDataUrl.maxupdaterate,g_designObject.radarDataUrl.maxupdaterate,g_designObject.moreDataUrl.maxupdaterate,g_designObject.alertsDataUrl.maxupdaterate),
								g_designObject.activeToInactiveSwitchRate
							);
		}
		
		if(g_liveDataObject != null) { delete g_liveDataObject; }
		g_liveDataObject = new WBLiveData(liveDataLoaded,dataLoadingError,'g_liveDataObject.load()',g_designObject.shortErrorWaitTime,g_designObject.longErrorWaitTime,g_designObject.liveDataUrl,g_preferencesObject,g_designObject.liveMinUpdateRate,g_designObject.liveMaxUpdateRate,g_stateObject);
		if(g_forecastDataObject != null) { delete g_forecastDataObject; }
		g_forecastDataObject = new WBForecastData(forecastDataLoaded,dataLoadingError,'g_forecastDataObject.load()',g_designObject.shortErrorWaitTime,g_designObject.longErrorWaitTime,g_designObject.forecastDataUrl,g_preferencesObject,g_designObject.pid,g_designObject.forecastMinUpdateRate,g_designObject.forecastMaxUpdateRate,g_stateObject,"NWS");
		if(g_cameraDataObject != null) { delete g_cameraDataObject; }
		g_cameraDataObject = new WBCameraData(cameraDataLoaded,dataLoadingError,'g_cameraDataObject.load()',g_designObject.shortErrorWaitTime,g_designObject.longErrorWaitTime,g_designObject.cameraDataUrl,g_preferencesObject,g_designObject.pid,g_designObject.cameraMinUpdateRate,g_designObject.cameraMaxUpdateRate,g_stateObject);
		if(g_radarDataObject != null) { delete g_radarDataObject; }
		g_radarDataObject = new WBRadarData(radarDataLoaded,dataLoadingError,'g_radarDataObject.load()',g_designObject.shortErrorWaitTime,g_designObject.longErrorWaitTime,g_designObject.radarDataUrl,g_preferencesObject,g_designObject.pid,g_designObject.radarMinUpdateRate,g_designObject.radarMaxUpdateRate,g_stateObject);
		if(g_alertsDataObject != null) { delete g_alertsDataObject; }
		g_alertsDataObject = new WBAlertsData(alertsDataLoaded,dataLoadingError,'g_alertsDataObject.load()',g_designObject.shortErrorWaitTime,g_designObject.longErrorWaitTime,g_designObject.alertsDataUrl,g_preferencesObject,g_designObject.pid,g_designObject.alertMinUpdateRate,g_designObject.alertMaxUpdateRate,g_stateObject);
		if(g_moreDataObject != null) { delete g_moreDataObject; }
		g_moreDataObject = new WBMoreTabData(moreContentLoaded,dataLoadingError,'g_moreDataObject.load()',g_designObject.shortErrorWaitTime,g_designObject.longErrorWaitTime,g_designObject.moreDataUrl,g_preferencesObject,g_designObject.moreMinUpdateRate,g_designObject.moreMaxUpdateRate,g_stateObject);
        
		g_stateObject.setAllObjects( new Array(g_liveDataObject,g_forecastDataObject,g_cameraDataObject,g_radarDataObject,g_moreDataObject,g_alertsDataObject) );

		g_stateObject.setSuperReload();
	}

}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function beginVaneAnimation(newRotationValue)
{
	if(newRotationValue != g_vaneObject.currentRotation && g_vaneObject.isAnimating == false)
	{
		if( (newRotationValue - g_vaneObject.currentRotation) > 180)
			newRotationValue = -1*(360 - newRotationValue);
		else if( (g_vaneObject.currentRotation - newRotationValue) > 180)
			newRotationValue = 360 + newRotationValue;
		 		
		g_vaneObject.endRotation = newRotationValue;
		var newdate = new Date();
		g_vaneObject.startTime = newdate.getTime();
		g_vaneObject.startRotation = g_vaneObject.currentRotation;
		
		g_vaneObject.isAnimating = true;
		animateVaneStep();
	}
}
////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////
function getAnimationValue(timeDelta, animationDuration, startValue, endValue)
{
	var percentDone = timeDelta/animationDuration;
	var value = startValue + (((1 - Math.cos(percentDone*Math.PI))*(endValue - startValue))/2.0);
	return value;
}
////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////
function animateVaneStep()
{
	var newdate = new Date();
	if((newdate.getTime() - g_vaneObject.startTime) < g_vaneObject.duration)
	{	
		vane_arrow.Rotation = g_vaneObject.currentRotation = getAnimationValue(newdate.getTime() - g_vaneObject.startTime, g_vaneObject.duration, g_vaneObject.startRotation, g_vaneObject.endRotation);
    	setTimeout("animateVaneStep()", 50);
	}
	else
	{
		g_vaneObject.currentRotation = g_vaneObject.endRotation;
		
		if(g_vaneObject.currentRotation >= 360)
			g_vaneObject.currentRotation = g_vaneObject.currentRotation - 360;
		else if(g_vaneObject.currentRotaiton < 0)
			g_vaneObject.currentRotation = 360 + g_vaneObject.currentRotation;
		
		vane_arrow.Rotation = g_vaneObject.currentRotation;
		g_vaneObject.isAnimating = false;
	}
}
////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////
function navLeftClick()
{
	if( setup.style.visibility == "hidden" && udate.style.visibility == "hidden")
	{
		if( g_selectedTab == "live") // current, cams, forecast, more, radar
		{
			live.style.visibility = "hidden";
			additional_content.style.visibility = "visible";
			g_selectedTab = "additional_content";
			tab_title.innerHTML = rf_moreweather;
			g_backgroundImage.section = "more";
			g_backgroundImage.setImage();
			g_preferencesObject.SetCurrentHitType("4");
			g_moreDataObject.superReload();
		}
		else if( g_selectedTab == "additional_content")
		{
			additional_content.style.visibility = "hidden";
			cams.style.visibility = "visible";
			g_selectedTab = "cams";
			tab_title.innerHTML = rf_camera;
			g_backgroundImage.section = "cams";
			g_backgroundImage.setImage();
			g_preferencesObject.SetCurrentHitType("4");
			g_cameraDataObject.superReload();
		}
		else if( g_selectedTab == "cams")
		{
			cams.style.visibility = "hidden";
			radar.style.visibility = "visible";
			g_selectedTab = "radar";
			if(System.Gadget.Settings.read("type") == 0)
			{
				g_backgroundImage.section = "radar";
				g_backgroundImage.setImage();
				tab_title.innerHTML = rf_radar;
				g_preferencesObject.SetCurrentHitType("4");
			    g_radarDataObject.superReload();
			}
			else
			{
				g_backgroundImage.section = "more";
				g_backgroundImage.setImage();
				tab_title.innerHTML = rf_satellite;
				g_preferencesObject.SetCurrentHitType("4");
			    g_radarDataObject.superReload();
			}
		}
		else if( g_selectedTab == "radar")
		{
			radar.style.visibility = "hidden";
			g_backgroundImage.section = "forecast";
			g_backgroundImage.setImage();
			forecast.style.visibility = "visible";
			
			if(System.Gadget.Settings.read("type") == 0)
			    fc_three.style.visibility = "visible";
			else
			    fc_three.style.visibility = "hidden";

			g_selectedTab = "forecast";
			tab_title.innerHTML = rf_forecast;
			g_preferencesObject.SetCurrentHitType("4");
			g_forecastDataObject.superReload();
		}
		else if( g_selectedTab == "forecast")
		{
		    fc_three.style.visibility = "hidden";
			forecast.style.visibility = "hidden";
			setLiveInterfaceVisible();
			live.style.visibility = "visible";
			g_selectedTab = "live";
			tab_title.innerHTML = rf_liveconditions;
			g_preferencesObject.SetCurrentHitType("4");
			g_liveDataObject.superReload();
		}
	}
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function navRightClick()
{
	if( setup.style.visibility == "hidden" && udate.style.visibility == "hidden")
	{
		if( g_selectedTab == "live") // current, cams, forecast, more, radar
		{
			live.style.visibility = "hidden";
			g_backgroundImage.section = "forecast";
			g_backgroundImage.setImage();
			forecast.style.visibility = "visible";
			
			if(System.Gadget.Settings.read("type") == 0)
			    fc_three.style.visibility = "visible";
			else
			    fc_three.style.visibility = "hidden";
			
			g_selectedTab = "forecast";
			tab_title.innerHTML = rf_forecast;
			g_preferencesObject.SetCurrentHitType("4");
			g_forecastDataObject.superReload();
		}
		else if( g_selectedTab == "additional_content")
		{
			additional_content.style.visibility = "hidden";
			setLiveInterfaceVisible();
			live.style.visibility = "visible";
			g_selectedTab = "live";
			tab_title.innerHTML = rf_liveconditions;
			g_preferencesObject.SetCurrentHitType("4");
			g_liveDataObject.superReload();
		}
		else if( g_selectedTab == "cams")
		{
			cams.style.visibility = "hidden";
			g_backgroundImage.section = "more";
			g_backgroundImage.setImage();
			additional_content.style.visibility = "visible";
			g_selectedTab = "additional_content";
			tab_title.innerHTML = rf_moreweather;
			g_preferencesObject.SetCurrentHitType("4");
			g_moreDataObject.superReload();
		}
		else if( g_selectedTab == "radar")
		{
			radar.style.visibility = "hidden";
			g_backgroundImage.section = "cams";
			g_backgroundImage.setImage();
			cams.style.visibility = "visible";
			g_selectedTab = "cams";
			tab_title.innerHTML = rf_camera;
			g_preferencesObject.SetCurrentHitType("4");
			g_cameraDataObject.superReload();
		}
		else if( g_selectedTab == "forecast")
		{
		    fc_three.style.visibility = "hidden";
			forecast.style.visibility = "hidden";
			radar.style.visibility = "visible";
			g_selectedTab = "radar";
			if(System.Gadget.Settings.read("type") == 0)
			{
				g_backgroundImage.section = "radar";
				g_backgroundImage.setImage();
				tab_title.innerHTML = rf_radar;
				g_preferencesObject.SetCurrentHitType("4");
			    g_radarDataObject.superReload();
			}
			else
			{
				g_backgroundImage.section = "more";
				g_backgroundImage.setImage();
				tab_title.innerHTML = rf_satellite;
				g_preferencesObject.SetCurrentHitType("4");
			    g_radarDataObject.superReload();
			}
		}
	}
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function setLiveInterfaceVisible()
{
	g_backgroundImage.section = "current";

	if(g_forecastDataObject.imageTypeArray[0] == "0")
		g_backgroundImage.dayOrNight = "day";
	else
		g_backgroundImage.dayOrNight = "night";
		
	g_backgroundImage.setImage();
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function openRadarLink()
{
	if(g_clickDidHappen == false)
	{
		g_clickDidHappen = true;
		openLink('026');
		setTimeout('expireClick()',500);
	}
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function openCamsLink()
{
	if(g_clickDidHappen == false)
	{
		g_clickDidHappen = true;
		openLink('017');
		setTimeout('expireClick()',500);
	}
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function openForecastLink()
{
	if(g_clickDidHappen == false)
	{
		g_clickDidHappen = true;
		openLink('001');
		setTimeout('expireClick()',500);
	}
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function expireClick()
{
	g_clickDidHappen = false;
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function reloadDesignData()
{
	if(g_designObject.dataLoadedCorrectly == false)
	{
		g_progressIndicator.switchDirection();
		g_designObject.data.request.abort();
		g_designObject.data.request = null;
		g_designObject.load();
		setTimeout("reloadDesignData()",60000);
	}
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function checkIfVersionSkipped()
{
	if(System.Gadget.Settings.read("skippedVersionNumber") == g_designObject.newestVersionNumber)
		return true;
	else
		return false;
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function updateAction( actionNumber )
{
    if(actionNumber == "1") // No Thanks
    {
        System.Gadget.Settings.write("skippedVersionNumber",g_designObject.newestVersionNumber);
        g_preferencesObject.SetCurrentUpdateAction("2");
        g_preferencesObject.SetCurrentHitType("4");
        userNotifiedOfUpdate = true;
        g_backgroundImage.updateAvailable = false;
        g_designObject.superReload();
    }
    else if(actionNumber == "2") // Remind Me Later
    {
        g_preferencesObject.SetCurrentUpdateAction("3")
        g_preferencesObject.SetCurrentHitType("4");
        userNotifiedOfUpdate = true;
        g_backgroundImage.updateAvailable = false;
        g_designObject.superReload();
    }
    else if(actionNumber == "3") // OK
    {
		userNotifiedOfUpdate = true;
        openLink('025');
        g_backgroundImage.updateAvailable = false;
        loadWeatherBug();
    }
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function setUpInterfaceVisibility()
{
	if(g_bodySize == "large")
	{
		g_selectedTab = "live";
		live.style.visibility = "visible";
		
		additional_content.style.visibility = "hidden";
		cams.style.visibility = "hidden";
		radar.style.visibility = "hidden";
		forecast.style.visibility = "hidden";
	}
	
	if( g_preferencesObject.getTypeIntValue() == "0")
	{
		fc_link_one.innerHTML = rf_extforecast;
		fc_link_two.innerHTML = rf_weekendfc;
		fc_link_three.innerHTML = rf_fcmap;
		
		fc_one.style.left = "3px";
		fc_two.style.left = "43px";
		
		radar_link_one.innerHTML = rf_animate;
		radar_link_two.innerHTML = rf_enlarge;
		radar_link_three.innerHTML = rf_satelliteradar;
		radar_link_four.innerHTML = rf_nationalradar;
	}
	else
	{
		fc_link_one.innerHTML = rf_extforecast;
		fc_link_two.innerHTML = rf_currenttemps;
		fc_link_three.innerHTML = rf_hitomorrow;
		fc_one.style.left = "23px";
		fc_two.style.left = "63px";
		
		radar_link_one.innerHTML = "";
		radar_link_two.innerHTML = "";
		radar_link_three.innerHTML = rf_enlarge;
		radar_link_four.innerHTML = rf_precipitationmap;
	}
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function alertsDataLoaded()
{
	if(g_alertsDataObject.numberOfAlerts == 0)
		alerts_tab.style.visibility = "hidden";
	else
		alerts_tab.style.visibility = "visible";
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function getCurrentTime()
{
	var timeObject = new Date();
	
	var hours = timeObject.getHours();
	var minutes = timeObject.getMinutes();
	var seconds = timeObject.getSeconds();
	var ampm = "AM";
	
	if(minutes < 10)
		minutes = "0" + minutes;

	if(hours >= 12)
		ampm = "PM";
	
	if(hours > 12)
		hours = hours-12;
	
	if(seconds < 10)
		seconds = "0" + seconds;
	
	return hours + ":" + minutes + ":" + seconds + " " + ampm;	
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function writeSideShowData(type)
{
	if(g_sideShowEnabled == true)
	{
		if(isFirstSideShowWrite == true && type == SideShowTypes.Live)
		{
			System.Gadget.SideShow.setFriendlyName("WeatherBug");
			isFirstSideShowWrite = false;
			writeSideShowData( SideShowTypes.Live );
		}
		else if(type == SideShowTypes.Live)
		{
		    SideShowTypes.LiveString = g_liveDataObject.source + " -- " + g_liveDataObject.location + "\n" + g_liveDataObject.temperature + unicodeDegreeSymbol + "\nLast Updated: " + getCurrentTime();
			isSideShowLiveObsWritten = true;
		}
		else if(type == SideShowTypes.Forecast)
		{
			SideShowTypes.ForecastString = "<body><content id='2' title='Forecast'><txt align='c' wrap='0'><em>Mon:</em> 70</txt><br /><txt align='c' wrap='0'><em>Tue:</em> 80</txt><br /><txt align='c' wrap='0'><em>Wed:</em> 90</txt><br /></content></body>";
			isSideShowForecastWritten = true;
		}
		else if(type == SideShowTypes.Radar)
		{
			SideShowTypes.RadarString = "<body><content id='3' title='Radar'><img fit='screen' align='c' id='5' /></content></body>";
			isSideShowRadarWritten = true;
		}
		else if(type == SideShowTypes.Cams)
		{
			SideShowTypes.CamsString = "<body><content id='4' title='Camera'><img fit='screen' align='c' id='6' /></content></body>";
			isSideShowCameraWritten = true;
		}
		updateSideShowData();
	}
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function updateSideShowData()
{
	if(isSideShowLiveObsWritten == true)
	{
		System.Gadget.SideShow.addText(0, SideShowTypes.LiveString);
		System.Gadget.SideShow.addText(1, "<body><menu id='1' title='Main Menu'><item target='2' enable='0'>Additional Data Not Yet Available</item></menu></body>");
	}
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function liveDataLoaded()
{
	temperature_text.innerHTML = small_temperature_text.innerHTML = g_liveDataObject.temperature + unicodeDegreeSymbol;
	
	location_text.innerHTML = getLocationText( g_liveDataObject.source );
	
	wind_magnitude.innerHTML = g_liveDataObject.windmagnitude;
	if(g_preferencesObject.getUnits() == "0")
		wind_units.innerHTML = "mph";
	else
		wind_units.innerHTML = "km/h";
	
	beginVaneAnimation(parseInt(g_liveDataObject.winddirection));
	writeSideShowData( SideShowTypes.Live );
	
    if(g_liveDataObject.pointcount > 0)
    {
        g_liveDataObject.pointcount = 0;
        g_liveDataObject.isAnimatingOtherPoints = true;
        setTimeout("animateAdditionalPoints()",2000);
    }
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function animateAdditionalPoints()
{
    try
    {
        if(g_liveDataObject.pointcount < g_liveDataObject.vanepointarray.length && g_liveDataObject.isAnimatingOtherPoints == true)
        {
            var windmagnitude = g_liveDataObject.vanepointarray[g_liveDataObject.pointcount].lastChild.firstChild.data;
            if(windmagnitude == "N/A" || windmagnitude == "NA"){ wind_magnitude.innerHTML = '--'; }
		    else { wind_magnitude.innerHTML = windmagnitude; }
            beginVaneAnimation(parseInt(g_liveDataObject.vanepointarray[g_liveDataObject.pointcount].firstChild.nextSibling.firstChild.data));
            g_liveDataObject.pointcount++;
            writeSideShowData( SideShowTypes.Live );
            setTimeout("animateAdditionalPoints()",2000);
        }
    }
    catch(e)
    { }
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function forecastDataLoaded()
{
	live_icon.src = small_live_icon.src = g_forecastDataObject.imageArray[0].firstChild.data;
	hi_value.innerHTML = g_forecastDataObject.highArray[0].firstChild.data + unicodeDegreeSymbol;
	lo_value.innerHTML = g_forecastDataObject.lowArray[0].firstChild.data + unicodeDegreeSymbol;
	forecast_title_text.innerHTML = g_forecastDataObject.titleArray[0].firstChild.data + "'s Forecast";

	fc_one_icon.src = g_forecastDataObject.imageArray[0].firstChild.data;
	fc_one_title.innerHTML = g_forecastDataObject.titleArray[0].getAttribute("alttitle");
	fc_one_high.innerHTML = g_forecastDataObject.highArray[0].firstChild.data + unicodeDegreeSymbol;
	fc_one_low.innerHTML = g_forecastDataObject.lowArray[0].firstChild.data + unicodeDegreeSymbol;

	fc_two_icon.src = g_forecastDataObject.imageArray[1].firstChild.data;
	fc_two_title.innerHTML = g_forecastDataObject.titleArray[1].getAttribute("alttitle");
	fc_two_high.innerHTML = g_forecastDataObject.highArray[1].firstChild.data + unicodeDegreeSymbol;
	fc_two_low.innerHTML = g_forecastDataObject.lowArray[1].firstChild.data + unicodeDegreeSymbol;

	if( System.Gadget.Settings.read("type") == "0")
	{
		fc_three_icon.src = g_forecastDataObject.imageArray[2].firstChild.data;
		fc_three_title.innerHTML = g_forecastDataObject.titleArray[2].getAttribute("alttitle");
		fc_three_high.innerHTML = g_forecastDataObject.highArray[2].firstChild.data + unicodeDegreeSymbol;
		fc_three_low.innerHTML = g_forecastDataObject.lowArray[2].firstChild.data + unicodeDegreeSymbol;
	}
	else
	{
		fc_three_icon.src = "../images/other/forecasticons/blank.png";
		fc_three_title.innerHTML = "";
		fc_three_high.innerHTML = "";
		fc_three_low.innerHTML = "";
	}
	
	if(g_forecastDataObject.imageTypeArray[0] == "0")
		g_backgroundImage.dayOrNight = "day";
	else
		g_backgroundImage.dayOrNight = "night";
		
	g_backgroundImage.setImage();
	
	writeSideShowData( SideShowTypes.Forecast );
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function cameraDataLoaded()
{
	cams_image.src = g_cameraDataObject.image;
	writeSideShowData( SideShowTypes.Cams );
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function radarDataLoaded()
{
	radar_image.src = g_radarDataObject.image[0].getAttribute("standard-src");
	writeSideShowData( SideShowTypes.Radar );
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function moreContentLoaded()
{
	//additional_content.innerHTML = g_moreDataObject.tabContent[0].firstChild.nodeValue;
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function dataLoadingError(str,time)
{
	setTimeout(str,time);
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function createStationUrl()
{
	System.Gadget.Settings.write("stationUrl",g_designObject.stationUrl.GetUrl());
	System.Gadget.Settings.write("locationUrl",g_designObject.locationUrl.GetUrl());
	System.Gadget.Settings.write("pid",g_designObject.pid);
	System.Gadget.onSettingsClosed = userChangedLocation;
	previousLocation = g_preferencesObject.getZipCode();
	previousUnits = g_preferencesObject.getUnits();
}

function userChangedLocation()
{
	alerts_tab.style.visibility = "hidden";
	udate.style.visibliity = "hidden";
	
	g_selectedTab = "live";
	g_backgroundImage.section = "current";
	g_backgroundImage.setImage();
	
	tab_title.innerHTML = rf_liveconditions;
	
	// setup new values
	if(g_preferencesObject.getType() == false)
	{
	    g_preferencesObject.SetCurrentZipCode(g_preferencesObject.getZipCode());
	    g_preferencesObject.SetCurrentCityCode("");
	}
	else
	{
	    g_preferencesObject.SetCurrentZipCode("");
	    g_preferencesObject.SetCurrentCityCode(g_preferencesObject.getZipCode());
	}
	
	if(previousLocation != g_preferencesObject.getZipCode())
	{
	    g_preferencesObject.SetCurrentChangeLocation("1");
	    g_preferencesObject.SetCurrentHitType("4");
	    zeroOutInterface();
	}
	else if(previousUnits != g_preferencesObject.getUnits())
	{
	    zeroOutInterface();
	}
	
	g_preferencesObject.SetCurrentStationId(g_preferencesObject.getStationID());
	g_preferencesObject.SetCurrentUnitType(g_preferencesObject.getUnits());
	g_designObject.superReload();
}

function zeroOutInterface()
{
    wind_magnitude.innerHTML = "--";
    wind_units.innerHTML = "--";
    vane_arrow.rotation = 0;
    temperature_text.innerHTML = "--";
    live_icon.src="../images/other/blank.png";
    hi_value.innerHTML = "--" + unicodeDegreeSymbol;
    lo_value.innerHTML = "--" + unicodeDegreeSymbol;
    forecast_title_text.innerHTML = "";
    
	fc_one_title.innerHTML = "";
	fc_one_icon.src="../images/other/blank.png";
	fc_one_high.innerHTML = "--" + unicodeDegreeSymbol;
	fc_one_low.innerHTML = "--" + unicodeDegreeSymbol;
	
    fc_two_title.innerHTML = "";
    fc_two_icon.src="../images/other/blank.png";
    fc_two_high.innerHTML = "--" + unicodeDegreeSymbol;
    fc_two_low.innerHTML = "--" + unicodeDegreeSymbol;

    fc_three_title.innerHTML = "";
    fc_three_icon.src="../images/other/blank.png";
    fc_three_high.innerHTML = "--" + unicodeDegreeSymbol;
    fc_three_low.innerHTML = "--" + unicodeDegreeSymbol;

	radar_image.src="../images/other/blank.png";
	cams_image.src="../images/other/blank.png";
	
    small_live_icon.src="../images/other/forecasticons/blank.png";
    small_temperature_text.innerHTML = "--" + unicodeDegreeSymbol;
    location_text.innerHTML = "";
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function toggleInterface()
{
	if( g_bodySize == "large" )
	{
		g_bodySize = "small";
		document.body.style.height = "105px",
		document.body.style.width = "133px";
		g_backgroundImage.isLarge = false;
		g_backgroundImage.setImage();
		
		collapse_tab.style.visibility = 'hidden';
		live.style.visibility = "hidden";

		additional_content.style.visibility = "hidden";
		cams.style.visibility = "hidden";
		radar.style.visibility = "hidden";
		forecast.style.visibility = "hidden";
		small_interface.style.visibility = "visible";
		expand_tab.style.visibility = 'visible';
	}
	else
	{
		g_bodySize = "large";
		document.body.style.height = "202px";
		document.body.style.width = "133px";
		g_backgroundImage.isLarge = true;
		g_backgroundImage.setImage();


		small_interface.style.visibility = "hidden";
		expand_tab.style.visibility = 'hidden';

		setLiveInterfaceVisible();
		live.style.visibility = "visible";
		g_selectedTab = "live";
		tab_title.innerHTML = rf_liveconditions;
		
		collapse_tab.style.visibility = 'visible';
	}
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function showTab(sender)
{
	if( setup.style.visibility == "hidden" && udate.style.visibility == "hidden")
	{
		g_backgroundImage.tabIsVisible = true;

		if(sender == "body")
		{
			g_backgroundImage.mouseIsOverTab = false;
			if(g_bodySize == "large")
			{
				collapse_tab.style.visibility = "visible";
				collapse_tab.zindex = 7000;
				collapse_image.src = "../images/other/collapse/off.png";
			}
			else
			{
				expand_tab.style.visibility = "visible";
				collapse_tab.zindex = 7001;
				expand_image.src = "../images/other/expand/off.png";
			}
		}
		else if(sender == 'tab')
		{
			g_backgroundImage.mouseIsOverTab = true;
			if(g_bodySize == "large")
			{
				collapse_tab.style.visibility = "visible";
				collapse_image.src = "../images/other/collapse/on.png";
			}
			else
			{
				expand_tab.style.visibility = "visible";
				expand_image.src = "../images/other/expand/on.png";
			}
		}	
		g_backgroundImage.setImage();
	}
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function hideTab(sender)
{
	g_backgroundImage.tabIsVisible = false;
	g_backgroundImage.mouseIsOverTab = false;
	collapse_tab.style.visibility = "hidden";
	expand_tab.style.visibility = "hidden";
	g_backgroundImage.setImage();
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function doTabHide()
{
	if(g_mouseInBody == false && g_mouseInTab == false)
	{
		collapse_tab.style.visibility = 'hidden';
		expand_tab.style.visibility = 'hidden';
	}
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function findChild (element, nodeName)
{
	var child;
	for( child = element.firstChild; child!= null; child = child.nextSibling)
	{
		if(child.nodeName == nodeName)
			return child;	
	}
	return false;
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function openLink ( linkId )
{
	var linkName = "";
	if( g_preferencesObject.getType() == "0" )
	{
		switch(linkId)
		{
			case "001":
			case "002":
			case "004":
			case "005":
			case "006":
				linkName = "forecast_extended";
				break;
			case "003":
				linkName = "alerts_home";
				break;
			case "007":
				linkName = "forecast_weekend";
				break;
			case "008":
				linkName = "forecast_maps_currentregional";
				break;
			case "009":
				linkName = "radar_dopplerlocalanimate";
				break;
			case "010":
				linkName = "radar_dopplerlocalenlarged";
				break;
			case "011":
				linkName = "radar_satelliteradar";
				break;
			case "012":
				linkName = "radar_nationalradar";
				break;
			case "013":
				linkName = "camera_home_animate";
				break;
			case "014":
				linkName = "camera_home";
				break;
			case "015":
				linkName = "camera_featured";
				break;
			case "016":
				linkName = "camera_next";
				break;
			case "017":
				linkName = "camera_home";
				break;
			case "026":
				linkName = "radar_dopplerlocalnormal";
				break;
			case "018":
				linkName = "more_stormcentral";
				break;
			case "019":
				linkName = "more_healthoutdoors";
				break;
			case "020":
				linkName = "more_worldweather";
				break;
			case "021":
				linkName = "more_communityphotos";
				break;
			case "022":
				linkName = "video_videocast";
				break;
			case "023":
				linkName = "more_mobileweather";
				break;
			case "024":
				linkName = "more_weatherbuglabs";
				break;
			case "025":
				linkName = "more_downloadupdate";
				break;
			default:
				linkName = "default";
				break;
		}
		g_preferencesObject.SetCurrentLinkName("us_" + linkName);
		System.Shell.execute( replaceParams(g_designObject.redirectDataUrl.GetUrl()) );
	}
	else
	{
		switch(linkId)
		{
			case "001":
			case "002":
			case "004":
			case "005":
			case "006":
				linkName = "forecastextended";
				break;
			case "003":
			case "009":
			case "010":
				linkName = "";
				break;
			case "007":
				linkName = "forecast_currenttemps";
				break;
			case "008":
				linkName = "forecast_highstomorrow";
				break;
			case "011":
				linkName = "satellite_home";
				break;
			case "012":
				linkName = "precipitationmap";
				break;
			case "013":
				linkName = "camera_animate";
				break;
			case "014":
				linkName = "camera_home";
				break;
			case "015":
				linkName = "camera_home";
				break;
			case "016":
				linkName = "camera_nextclosest";
				break;
			case "017":
				linkName = "camera_home";
				break;
			case "026":
				linkName = "satellite_home";
				break;
			case "018":
				linkName = "more_stormcentral";
				break;
			case "019":
				linkName = "more_healthoutdoors";
				break;
			case "020":
				linkName = "more_worldweather";
				break;
			case "021":
				linkName = "more_communityphotos";
				break;
			case "022":
				linkName = "video_featuredvideo";
				break;
			case "023":
				linkName = "more_mobileweather";
				break;
			case "024":
				linkName = "more_weatherbuglabs";
				break;
			case "025":
				linkName = "more_downloadupdate";
				break;
			default:
				linkName = "default";
				break;
		}
		g_preferencesObject.SetCurrentLinkName("intl_" + linkName);
		System.Shell.execute( replaceParams(g_designObject.redirectDataUrl.GetUrl()) );
	}
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function getLocationText( locationStr )
{
	if(locationStr.length > g_designObject.allowedLocationLength)
		return locationStr.substr(0,g_designObject.allowedLocationLength) + "...";
	else
		return locationStr;
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function replaceParams(url)
{
    // Build the URL, replacing everything possible parameter with values set in the preferences object
	url = url.replace("^^^UserId^^^",g_preferencesObject.GetGuid());
	url = url.replace("^^^LinkName^^^",g_preferencesObject.GetStringValue(g_preferencesObject.GetCurrentLinkName()));
	url = url.replace("^^^AppVersion^^^",g_preferencesObject.GetCurrentAppVersion());
	url = url.replace("^^^AppName^^^",g_preferencesObject.GetCurrentAppName());
	url = url.replace("^^^ZCode^^^",g_preferencesObject.GetCurrentZCode());
	url = url.replace("^^^HitType^^^",g_preferencesObject.GetCurrentHitType());
	url = url.replace("^^^PartnerId^^^",g_preferencesObject.GetCurrentPartnerId());
	url = url.replace("^^^PageType^^^",g_preferencesObject.GetCurrentPageType());
	url = url.replace("^^^ChangeLocation^^^",g_preferencesObject.GetCurrentChangeLocation());
	url = url.replace("^^^UpdateAction^^^",g_preferencesObject.GetCurrentUpdateAction());
	url = url.replace("^^^SearchString^^^",g_preferencesObject.GetCurrentSearchString());
	url = url.replace("^^^UnitType^^^",g_preferencesObject.GetCurrentUnitType());
	url = url.replace("^^^LanguageType^^^",g_preferencesObject.GetCurrentLanguageType());
	url = url.replace("^^^CityCode^^^",g_preferencesObject.GetCurrentCityCode());
	url = url.replace("^^^ZipCode^^^",g_preferencesObject.GetCurrentZipCode());
	url = url.replace("^^^StationId^^^",g_preferencesObject.GetCurrentStationId());
	url = url.replace("^^^ShowPoints^^^",g_preferencesObject.GetCurrentShowPoints());
	url = url.replace("^^^IconSet^^^",g_preferencesObject.GetCurrentIconSet());
	url = url.replace("^^^LinkName^^^",g_preferencesObject.GetCurrentLinkName());
	url = url.replace("^^^NextCameraId^^^",g_preferencesObject.GetCurrentNextCameraId());
    url = url.replace("^^^rnd^^^",Math.floor(Math.random()*10000000000));
    url = url.replace("^^^SkippedVersion^^^",System.Gadget.Settings.read("skippedVersionNumber"));
    
	// after constructing the URL, zero out the HitType and PageType for the next uses
	g_preferencesObject.SetCurrentHitType("");
	g_preferencesObject.SetCurrentPageType("");
	g_preferencesObject.SetCurrentChangeLocation("");
	g_preferencesObject.SetCurrentUpdateAction("");
	return url;
}