/*
 * 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,cityCallback,cityErrorCallback,stationsCallback,stationsErrorCallback,pid,preferences)
{
	this.cityURL = new WBUrl(theCityURL); // Base url for cities
	this.stationURL = new WBUrl(theStationsURL); // Base url for staitons
    
	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
    this.preferences = preferences;
    
	// WBDataRequest objects. Does the work in loading data.
	this.cityData = new WBDataRequestXML(this.cityURL,this.cityDataFetched,this.preferences);
	this.stationData = new WBDataRequestXML(this.stationURL,this.stationsDataFetched,this.preferences);

	// 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); }
WBStations.prototype.loadStations = function (str) { this.stationData.load(str); }

// 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
	{
		delete this.cityList;
		delete this.cityZipCodeList;
		delete this.cityTypeList;
		
		this.cityList = new Array;
		this.cityZipCodeList = new Array;
		this.cityTypeList = new Array;

		var xmlDocNode = this.cityData.request.firstChild.nextSibling;
		var locationNodesArray = xmlDocNode.getElementsByTagName("aws:location");
		
		for( var i = 0; i < locationNodesArray.length; i++)
		{
			if( locationNodesArray[i].getAttribute("citytype") == "0" )
			{
				this.cityZipCodeList[this.cityZipCodeList.length] = locationNodesArray[i].getAttribute("zipcode");
				this.cityList[this.cityList.length] = locationNodesArray[i].getAttribute("cityname") + ", " + locationNodesArray[i].getAttribute("statename") + ' (U.S.)';
				this.cityTypeList[this.cityTypeList.length] = false;
			}
			else if ( locationNodesArray[i].getAttribute("citytype") == "1" )
			{
				this.cityZipCodeList[this.cityZipCodeList.length] = locationNodesArray[i].getAttribute("citycode");
				this.cityList[this.cityList.length] = locationNodesArray[i].getAttribute("cityname") + ", " + locationNodesArray[i].getAttribute("countryname") + ' (International)';
				this.cityTypeList[this.cityTypeList.length] = true;
			}
		}
	}
	catch(e)
	{
		alert(e);
		this.loadCitiesErrorCallback();
		return;
	}
		
	this.loadCitiesCallback();

}

// 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 ()
{
	// 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
	{
		var xmlDocNode = this.stationData.request.firstChild.nextSibling;
		var stationNodesArray = xmlDocNode.getElementsByTagName("aws:station");

		delete this.stationsList;
		delete this.stationCodes;
		this.stationsList = new Array;
		this.stationCodes = new Array;
		
		for( var i = 0; i < stationNodesArray.length; i++)
		{
			if(stationNodesArray[i].getAttribute("distance") != "")
				this.stationsList[this.stationsList.length] = stationNodesArray[i].getAttribute("name") + " (" + stationNodesArray[i].getAttribute("distance") + " miles away)";
			else
				this.stationsList[this.stationsList.length] = stationNodesArray[i].getAttribute("name");

			this.stationCodes[this.stationsList.length] = stationNodesArray[i].getAttribute("id");
		}		
	}
	catch(e)
	{
		alert(e);
		this.loadStationsErrorCallback();
		return;
	}
	this.loadStationsCallback();
	
}
