//
// File : forecast.js
//
// Description : Javascript functions for Weather
//
// Contents    : 
//               localityChanged
//               initWeather
//               setLocality
//               loadStations
//               getCookie
//               setCookie




var forecast = null;

  //
  // returns the max # of forecasts that can be shown on 
  // current interface
  function getMaxNumForecasts() {
  	return 99;
  }

  //
  // Loads the 'weatherforecast' html element with forecast
  // information presented with imgs for each condition
  //
  var loadForecast = function(forecastdata) {
    //alert("load forecast : " + data);
    var htmlElement = document.getElementById("weatherforecast");
    if(!htmlElement)
      return;

	var source = "";
	var html = '<div align="center">';     
    for(var i=0;i<forecastdata.length && i < getMaxNumForecasts(); i++) {	   	
   		html += forecastdata[i].html;    
   		source = forecastdata[i].source;
    }
    
    html += '</div><div style="clear:both;"></div>';

    htmlElement.innerHTML = html;
  }
  
  //
  // Loads the 'weathercurrent' html element with the current weather
  // observation information 
  //
  var loadCurrent = function(data) {
     //alert("load loadCurrent : " + data);
	 var htmlElement = document.getElementById("weathercurrent");
     if(!htmlElement || !data || data.length == 0)
       return;
    
     var html = data[0].html;
     
   	 if(data[0].source) {
	    var sourceHtml = '<span style="font-size: 0.79em;"><b>Source:</b> <a href="' + data[0].source + '">Environment Canada</a></span>';
	    html += sourceHtml;
     }
 
     htmlElement.innerHTML = html;
  }    
  
  //
  // overrides the localityChanged() function in weatherpage.js
  //
  var localityChanged = function () {
    var options = document.getElementById("localityselect").options;
  	var index = document.getElementById("localityselect").selectedIndex;
  	if(index == 0) return; //first option is just the msg string
  	
	var locality = options[index].text;
	
	if(locality == "Other Locality...")  {
	  chooseOtherLocality();
	  return;
	}
	
	savedLocality = locality;
	
    setCookie("locality", locality);
    
    	var locdisplay = document.getElementById("localityname");
	  	if(locdisplay)
	  	   locdisplay.innerHTML = locality;
	  	   
    loadWeather(locality);
  }

	
  //
  // Loads the weather for a specified locality id
  //
  function loadWeather(locality) {
    if(locality) {
    	WeatherDataProvider.getCurrentForLocality(locality, loadCurrent);	
    	WeatherDataProvider.getForecastForLocality(locality, loadForecast);	
    }
  }
  
  //
  // Initialize this page's widgets
  //
  var initWeather = function() {
     //alert('locality : ' + savedLocality);   
     setLocality( document.getElementById("localityselect") );  
     loadWeather( getLocality() );  
  }
  
  
  
