/*
 * Copyright (c) 2006 AWS Convergence Technologies, Inc. 
 * All rights reserved.
 * Visit http://www.aws.com
 *
 * WBStations.js
 *
 * This scripts contains the WBStations resuable class. A WBStations object
 * takes care of loading and holding cities and stations returned for the user
 * to choose from.
 */

// Object Definition
function WBStations(theCityURL,theStationsURL,pid,cityCallback,cityErrorCallback,stationsCallback,stationsErrorCallback)
{
    this.cityURL = theCityURL; // Base url for cities
	this.stationURL = theStationsURL; // Base url for staitons
	this.pid = pid;
	
	this.cityList = new Array; // Array of returned cities
	this.cityZipCodeList = new Array; // Array of returned zip codes
	this.cityTypeList = new Array; // Array of returned station types

	this.stationsList = new Array; // Array of returned stations
	this.staitonCodes = new Array; // Array of returned station codes

	// WBDataRequest objects. Does the work in loading data.
	this.cityData = new WBDataRequest(this.cityURL,this.cityDataFetched,cityErrorCallback);
	this.stationData = new WBDataRequest(this.stationURL,this.stationsDataFetched,stationsErrorCallback);

	// Callbacks when making city requests
	this.loadCitiesCallback = cityCallback;
	this.loadCitiesErrorCallback = cityErrorCallback;

	// Callbacks when making station requests
	this.loadStationsCallback = stationsCallback;
	this.loadStationsErrorCallback = stationsErrorCallback;

	this.cityDataFetched.currentScope = this; // Used when the dataFetched function has lost scope.
	this.stationsDataFetched.currentScope = this; // Used when the dataFetched function has lost scope.
}

// Do not call this directly, only from an initialized WBDataLocations object.
// Used to reload information in the WBDataLocations object.
WBStations.prototype.loadCities = function (str) { this.cityData.load(str + "&dt=1&PartnerId=" + this.pid); }
WBStations.prototype.loadStations = function (str) { this.stationData.load(str + "&dt=1&PartnerId=" + this.pid); }

// Do not call this directly, only from an initialized WBLiveData object.
// Used to parse the information from the WBDataRequest object into the object variables.
WBStations.prototype.cityDataFetched = function ()
{
    // make sure 'this' is in scope, if not call it again with the correct scope
    if(this != arguments.callee.currentScope){ return arguments.callee.apply(arguments.callee.currentScope, arguments); };
    try
    {
        if(this.cityData.request.readyState == 4 && this.cityData.request.status == 200)
        {
	        try
	        {
		        delete this.cityList;
		        delete this.cityZipCodeList;
		        delete this.cityTypeList;
        		
		        this.cityList = new Array;
		        this.cityZipCodeList = new Array;
		        this.cityTypeList = new Array;
		        
		        //alert(this.cityData.request.responseText);

		        var xmlDocNode = findChild( this.cityData.request.responseXML,"aws:weather");
		        
		        var locations = findChild(xmlDocNode,"aws:locations");
		        for(var item = locations.firstChild; item != null; item = item.nextSibling)
		        {
			        if(item.nodeName == "aws:location") 
			        {
				        if (item.getAttribute("citytype") == "0")
				        {
				            this.cityZipCodeList[this.cityZipCodeList.length] = item.getAttribute("zipcode");
				            this.cityList[this.cityList.length] = item.getAttribute("cityname") + ", " + item.getAttribute("statename") + ' (U.S.)';
				            this.cityTypeList[this.cityTypeList.length] = false;
				        }
				        else if (item.getAttribute("citytype") == "1" )
				        {
				            this.cityZipCodeList[this.cityZipCodeList.length] = item.getAttribute("citycode");
				            this.cityList[this.cityList.length] = item.getAttribute("cityname") + ", " + item.getAttribute("countryname") + ' (International)';
				            this.cityTypeList[this.cityTypeList.length] = true;
				        }
				    }
				}        
			}
	        catch(e)
	        {
		        this.loadCitiesErrorCallback();
		        return;
	        }
	        this.loadCitiesCallback();
	    }
	}
	catch(e)
	{
	    return;
	}
}

// Do not call this directly, only from an initialized WBLiveData object.
// Used to parse the information from the WBDataRequest object into the object variables.
WBStations.prototype.stationsDataFetched = function ()
{
//	alert("Inside stationsDataFetched");
	// make sure 'this' is in scope, if not call it again with the correct scope
    if(this != arguments.callee.currentScope){ return arguments.callee.apply(arguments.callee.currentScope, arguments); };
    try
    {
        if(this.stationData.request.readyState == 4 && this.stationData.request.status == 200)
        { 
	        try
	        {
		        delete this.stationsList;
		        delete this.stationCodes;
		        this.stationsList = new Array;
		        this.stationCodes = new Array;
		        var stationArrayLength;
		        //alert(this.stationData.request.responseText);
		        var xmlDocNode = findChild( this.stationData.request.responseXML,"aws:weather");
        		
        		var stations = findChild(xmlDocNode,"aws:stations");
		        // Iterate through the forecast children, parsing the data and placing it into the corresponding arrays
		        for(var item = stations.firstChild; item != null; item = item.nextSibling)
		        {
			        if(item.nodeName == "aws:station") 
			        {
				        stationArrayLength = this.stationsList.length;
				        if (item.getAttribute("distance") != "")
				        {
				            this.stationsList[stationArrayLength] = item.getAttribute("name") + " (" + item.getAttribute("distance") + " miles away)";
				        }
				        else
				        {
        				    this.stationsList[stationArrayLength] = item.getAttribute("name");
				        }
    				    this.stationCodes[stationArrayLength] = item.getAttribute("id");
    				    
				    }
				}
			}
	        catch(e)
	        {
		        //alert(e);
		        this.loadStationsErrorCallback();
		        return;
	        }
	        this.loadStationsCallback();
        }
    }
    catch(e)
	{
	    return;
	}
}
