if (!window.Weather)
	window.Weather = {};

Weather.Scene = function() 
{ 
}

Weather.Scene.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		this.host = $get("SilverlightControl"); 
		this.zipCodeEdit = $get("txtZipCode");
		this.panelIsOpen = false;
		this.listBox = null;
    
	  // attach handlers for main panel to show/hide Current Conditions FloatOver panel
	  rootElement.findName("MainPanel").addEventListener("MouseEnter", Silverlight.createDelegate(this, this.onMouseEnterMainPanel));
    rootElement.findName("MainPanel").addEventListener("MouseLeave", Silverlight.createDelegate(this, this.onMouseLeaveMainPanel));
	
	  // hide the Current Conditions 
		rootElement.findName("CurrentConditionsPanel").Visibility = "Collapsed";
		
		// add handlers for the zip code HTML edit box keyup/keypress 
		//   (note that these are the DOM events and not the Silverlight Events)
		$addHandler(this.zipCodeEdit, 'keypress', Function.createDelegate(this, txtZipCode_onkeypress));
		$addHandler(this.zipCodeEdit, 'keyup', Function.createDelegate(this, txtZipCode_onkeyup));

   	this.zipCodeEdit.style.display = "inline";
	  this.zipCodeEdit.focus();
	},
	
	
  emitHtmlPromoLinks: function (links)
  {
    var list = $get("ulTWCPromoLinks");
    if (list != null)
    {
      // blow away any LI that exist
      list.innerHTML = "";
      
      // loop through and add the links      
      for (var i in links)
      {
        var link = links[i];
        var li = document.createElement("li");
        var a = document.createElement("a");
        a.href = link.URL;
        a.innerText = link.Text;
        a.target = "_blank";
        li.appendChild(a);
        list.appendChild(li);
      }
      
      $get("divTWCPromoLinks").style.visibility = (list.childNodes.length > 0) ? "visible" : "hidden";
    }
  },

  getForecast: function(zipCode)
  {
    $get("imgLoading").style.display = "inline";

    // pass zip code and number of days and a bool for IsMetric
    WeatherWidget.WeatherChannelService.GetWeatherByZipcode(zipCode, 6, false,
      Function.createDelegate(this, this.onGetWeatherComplete), 
      Function.createDelegate(this, this.onGetWeatherTimeOut));    
  },
	
	onGetWeatherComplete: function(forecast)
  {
	  // if no ErrorDesc (or its blank), load the forecast
	  if ((forecast.ErrorDesc == null) || (forecast.ErrorDesc === ""))
	  { 
	    // set items that are for current day
	    this.host.content.findName("Barometer").Text = forecast.Barometer; 	  
		  this.host.content.findName("CurrentDesc").Text = forecast.CurrentDesc;
		  this.host.content.findName("CurrentIcon").Source = forecast.CurrentIcon;
		  this.host.content.findName("CurrentTemp").Text = forecast.CurrentTemp;
      this.host.content.findName("DewPoint").Text = forecast.DewPoint;		  
		  this.host.content.findName("LastUpdated").Text = forecast.LastUpdated;
      this.host.content.findName("FeelsLike").Text = forecast.FeelsLike;
      this.host.content.findName("Humidity").Text = forecast.Humidity; 	  
      this.host.content.findName("Location").Text = forecast.Location; 	  
      this.host.content.findName("Moon").Text = forecast.Moon;
		  this.host.content.findName("Precip").Text = forecast.Precip;
		  this.host.content.findName("Visibility").Text = forecast.Visibility;
  	  this.host.content.findName("UV").Text = forecast.UV;
  	  this.host.content.findName("Wind").Text = forecast.Wind;
  	  
  	  this.Links = forecast.Links;
  	  this.emitHtmlPromoLinks(forecast.Links);
	
	    // build listbox for extended forecast
	    this.listBox = new Weather.ListBox(this.host, "listBox", forecast);
    
      if (!this.panelIsOpen)
      {
        this.toggleDisplay();
    
        // show mainPanel once images are loaded 
		    $get("imgLoading").style.display = "";
		    this.host.content.root.Visibility = "Visible";
		    
		    // throb the current icon as a trigger for user to mouseover
		    this.host.content.findName("CurrentIconStoryboard").begin();
		  }  
    }
    else
    {
      this.host.content.findName("ErrorDesc").Text = forecast.ErrorDesc; 	  
      this.host.content.findName("ErrorPanel").Visibility = "Visible";
      $get("imgLoading").style.display = "none";
    }
  },  
			    

	onGetWeatherTimeOut: function(result)
	{
	  alert("onGetWeatherTimeOut timeout");
	},
	
	onMouseEnterMainPanel: function(sender, eventArgs)
  {
    if (this.panelIsOpen)
    { 
      this.host.content.findName("CurrentIconStoryboard").pause();
      
      this.host.content.findName("MainPanel").Cursor = "Hand";
      this.host.content.findName("MainPanelAnimation").To = 0;
      this.host.content.findName("MainPanelFloatoverAnimation").To = 1;
      this.host.content.findName("MainPanelStoryboard").begin(); 
    }  
  },
  
  onMouseLeaveMainPanel: function(sender, eventArgs)
  {
    if (this.panelIsOpen)
    {
      this.host.content.findName("CurrentIconStoryboard").begin();
    
      this.host.content.findName("MainPanelAnimation").To = 1;
      this.host.content.findName("MainPanelFloatoverAnimation").To = 0;
      this.host.content.findName("MainPanelStoryboard").begin(); 
    }  
  },
	
	toggleDisplay: function()
	{
	  if (this.panelIsOpen)
	  {
	    this.host.content.findName("CurrentConditionsPanel").Visibility = "Collapsed";
	    this.host.content.findName("SplashPanel").Visibility = "Visible";

      // close the sliding panel
      this.host.content.findName("SlidingPanelOpacityAnimation").To = 0;
      this.host.content.findName("SlidingPanelUpDownAnimation").To = 0;
      this.host.content.findName("SlidingPanelStoryboard").begin(); 
      
      // attach handlers for main panel to show/hide Current Conditions Front and Back panels
      var mainPanel = this.host.content.findName("MainPanel");
	    if (mainPanel != null)
	    {
	      mainPanel.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.onMouseEnterMainPanel));
        mainPanel.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.onMouseLeaveMainPanel));
        
        // close the mainPanelBackPanel if closing ListBox
        this.host.content.findName("MainPanelAnimation").To = 1;
        this.host.content.findName("MainPanelFloatoverAnimation").To = 0;
        this.host.content.findName("MainPanelStoryboard").begin(); 
      }  
      
      $get("divTWCPromoLinks").style.visibility = "hidden";
      
      this.panelIsOpen = false;
    }
    else
    {
	    this.host.content.findName("CurrentConditionsPanel").Visibility = "Visible";
	    this.host.content.findName("SplashPanel").Visibility = "Collapsed";

      // close the sliding panel
      this.host.content.findName("SlidingPanelOpacityAnimation").To = 1;
      this.host.content.findName("SlidingPanelUpDownAnimation").To = 473;
      this.host.content.findName("SlidingPanelStoryboard").begin(); 
      
      // detach handlers for main panel to show/hide Current Conditions Front and Back panels
      var mainPanel = this.host.content.findName("MainPanel");
	    if (mainPanel != null)
	    {
	      mainPanel.removeEventListener("MouseEnter", "onMouseEnterMainPanel");
        mainPanel.removeEventListener("MouseLeave", "onMouseLeaveMainPanel");
      }  
      
      this.panelIsOpen = true;
      
      $get("imgLoading").style.display = "none";
    }  
	}
}


// global variable for main scene
var globalScene = null;

// creates the main scene and loads it
function sceneLoaded(control, userContext, rootElement)
{
  globalScene = new Weather.Scene();
  globalScene.handleLoad(control, userContext, rootElement);
}

function sceneOnError(sender, args)
{
  // the below is to get around a bug in Silverlight with re-using (consecutively) an asset from a zip file.
  // see this blog post:  http://www.mindfusioncorp.com/weblog/2007/12/01/Silverlight+Bug+Using+A+Packaged+Image+Source+For+Multiple+Image+Elements.aspx
  if (args.errorType != "ImageError")
  {
     globalScene.host.content.findName("ErrorDesc").Text = args.errorMessage; 	  
     globalScene.host.content.findName("ErrorPanel").Visibility = "Visible";
     $get("imgLoading").style.display = "none";
  }   
}

function txtZipCode_onkeyup(evt)
{
  var zip = evt.target.value; 

  // if we don't have 5 digits, hide the panel and current conditions
  if (zip.length < 5)
  {
    // hide the error text if visible
    this.host.content.findName("ErrorPanel").Visibility = "Collapsed";
	  
	  // if panel is open, close it
    if (globalScene.panelIsOpen)
    {
      globalScene.toggleDisplay();
    }  
    
    return;
  } 
 
  // test for valid zip code format before calling web service
  //  really only care about the five digit zip, so firing the 
  //  getForecast() at 5 digits
  if ((zip.length == 5) && (!this.panelIsOpen) && (isValidZipFormat(zip)))
  {
	  globalScene.getForecast(zip);
  }
}

function txtZipCode_onkeypress(evt)
{
  var code = evt.charCode;

  //  8 is the backspace key,  48-57 are the chars 0-9
  if ((code != 8) && (code < 48 || code > 57))
  {
    evt.preventDefault();
  }
}

