/* 
*  Location selector for YoWeather
*  v0.1
*  26 Mar 2010
*  Oxinet Creative
*
* This requires jQuery, currently tested with v1.3.1
* Also requires swfobject, currently tested with v2.1
*/

// Optional parameters specific to FMI page
var replaceFMIWeatherHeaderId = "weatherHead";
var replaceFMIWeatherHeader = true;
var replaceFMIWeatherHeaderText = "YOUR LOCAL WEATHER";

// the ID of the div in which the weather swf will be displayed
var outerPlaceholderName = "weatherOuterPlaceholder";
var outerPlaceholderSelector = "#"+outerPlaceholderName;

// helper content - used to create the inner placeholder for the weather swf
var innerPlaceholderContent = "<div id=\"weatherInnerPlaceholder\"></div>";
var innerPlaceholderName = "weatherInnerPlaceholder";

//css class that will be applied to all of the select drop downs
var weatherSelectCssClassName = "weatherselect";
//Ids for continent div and list
var weatherSelectContinentListId = "weatherContinentList";
var weatherSelectContinentListDivId = "weatherContinentListDiv";
//Ids for country div and list
var weatherSelectCountryListId = "weatherCountryList";
var weatherSelectCountryListDivId = "weatherCountryListDiv";
//Ids for city div and list
var weatherSelectCityListId = "weatherCityList";
var weatherSelectCityListDivId = "weatherCityListDiv";

// content within the continent div
var weatherContinentDivContent = "<p>Select your continent:<br />\n";
var weatherContinentDivContentPost = "</p>/n";
// content within the country div
var weatherCountryDivContent = "<p>Select your country:<br />\n";
var weatherCountryDivContentPost = "</p>/n";
//content within the city div
var weatherCityDivContent = "<p>Select your city:<br />\n";
var weatherCityDivContentPost = "</p>/n";

// settings for the weather swf
var weatherLang = "en";
var weatherSwfUrl = "http://swf.yowindow.com/wimo/hpPal/hpPal.swf"; //"http://192.168.1.18/~tomh/fmi/swf/hpPal.swf";

/* Dev URLs
//Urls for lists
var weatherAllContinentListUrl = "http://192.168.1.18/~tomh/fmi/continent.xml"; //"http://server2.yowindow.com/cgi-bin/wimo/server/index.pl?request=world&id=6295630&depth=1&detail=brief";
var weatherAllCountryListUrl = "http://server2.yowindow.com/cgi-bin/wimo/server/index.pl?request=world&id=6295630&depth=2&detail=brief";
//Url stems for lists
var weatherDepth1ListUrlStem = "http://192.168.1.18/~tomh/fmi/europe.xml?test="; //"http://server2.yowindow.com/cgi-bin/wimo/server/index.pl?request=world&depth=1&detail=brief&id="
var weatherDepth2ListUrlStem = "http://192.168.1.18/~tomh/fmi/uk.xml?test=";//"http://server2.yowindow.com/cgi-bin/wimo/server/index.pl?request=world&depth=2&minPopulation=50000&detail=brief&id="
*/


/* Urls for server */
var weatherAllContinentListUrl = "/weather-proxy.php?yws_path=cgi-bin%2Fwimo%2Fserver%2Findex.pl%3Frequest%3Dworld%26id%3D6295630%26depth%3D2%26detail%3Dbrief"; 

//Url stems for lists
var weatherDepth1ListUrlStem = "/weather-proxy.php?yws_path=cgi-bin%2Fwimo%2Fserver%2Findex.pl%3Frequest%3Dworld%26depth%3D1%26detail%3Dbrief%26id%3D"; 
var weatherDepth2ListUrlStem = "/weather-proxy.php?yws_path=cgi-bin%2Fwimo%2Fserver%2Findex.pl%3Frequest%3Dworld%26depth%3D2%26minPopulation%3D50000%26detail%3Dbrief%26id%3D";
/* */



//Some Ids for specific locations
var weatherLocationIdVolgograd = "472757";
var weatherLocationIdAylesbury = "2656719";

var weatherCurrentLocationId = "";

//clears the outer placeholder and fill with inner content
function clearOuterPlaceholder () {
	$('#'+outerPlaceholderName).html = innerPlaceholderContent;
}

//Load the swf into inner placeholder with a particular location id
function updateSpecificWeatherSwf(locationid) 
 {
 	var lang = weatherLang;

        var flashvars = {
    		location_auto_ip: 0,
    		location_id: "gn:"+locationid,
    		background: "#FFFFFF",
    		role: "setup",
    //d		mini_change_button: true,
    		time_format: "12",
    		lang: lang
    //d		trace_text_area: "1",
        };

        myHpPalFlashVars = flashvars;

        var params = {
        	quality: "high",
        	bgcolor: "#FFFFFF",
    		allowscriptaccess: "always",
    		allowfullscreen: "true"
        };

        var attributes = {
        	id:innerPlaceholderName,
        	name:innerPlaceholderName
        };

        var url = weatherSwfUrl;
        //if (myIsLocal) {
    	//    url = "http://localhost/wimo/hpPal/hpPal.swf";
        //}

        swfobject.embedSWF(
        	url, 
        	innerPlaceholderName, 
        	"280", 
        	"194",
        	"9.0.0",
        	"expressInstall.swf",
        	flashvars,
        	params,
        	attributes
        );
 }
//Get the URL of a depth 1 list
function getWeatherDepth1ListUrlFor(id) {
	return weatherDepth1ListUrlStem+id;
}
//Get the URL of the depth 2 list
function getWeatherDepth2ListUrlFor(id) {
	return weatherDepth2ListUrlStem+id;
}
//Parses xml for cities, sorts and populates the CityListDiv
function parseWeatherCityXml(xml)
{
	//alert("parseWeatherCityXml");
	var sRes = weatherCityDivContent;
	sRes += "<select id=\""+weatherSelectCityListId+"\" class=\""+weatherSelectCssClassName+" weathercity\">\n";
	sRes += "<option>-- Select a City</option>\n";
	$(xml).find("l").each(function() {
		var thisName = $(this).attr("name");
		var thisId = $(this).attr("id");
		var thisItem = "<option value=\""+thisId+"\">"+thisName+"</option>\n";
		sRes = sRes+thisItem;
	});
	sRes += weatherCityDivContentPost;
	$("#"+weatherSelectCityListDivId).html(sRes);
	$("#"+weatherSelectCityListId+" > option").tsort();
	$("#"+weatherSelectCityListId).change(function (e) {
		var selected = $("#"+weatherSelectCityListId+" option:selected");
		var selText = selected.val();
		//alert("You selected "+selText);
		updateSpecificWeatherSwf(selText);
		if (replaceFMIWeatherHeader) {
			$("#"+replaceFMIWeatherHeaderId).text(replaceFMIWeatherHeaderText);
		}
		weatherCurrentLocationId = selText;
	});
}
//Load city data for a particular country using jquery
function loadCityWeatherData(id)
{
	//alert("loadCityWeatherData");
	var cityUrl = getWeatherDepth2ListUrlFor(id);
	$.ajax({
		type: "GET",
		url: cityUrl,
		dataType: "xml",
		success: parseWeatherCityXml
	});	
}


//Parses xml for countries, sorts and populates the CityListDiv
function parseWeatherCountryXml(xml)
{
	//alert("parseWeatherCountryXml");
	var sRes = weatherCountryDivContent;
	sRes += "<select id=\""+weatherSelectCountryListId+"\" class=\""+weatherSelectCssClassName+" weathercountry\">\n";
	sRes += "<option>-- Select a country</option>\n";
	$(xml).find("country").each(function() {
		var thisName = $(this).attr("name");
				var thisId = $(this).attr("id");
				var thisItem = "<option value=\""+thisId+"\">"+thisName+"</option>\n";
			sRes = sRes+thisItem;
	});
	sRes += weatherCountryDivContentPost;
	$("#"+weatherSelectCountryListDivId).html(sRes);
	$("#"+weatherSelectCountryListId+" > option").tsort();
	$("#"+weatherSelectCountryListId).change(function (e) {
		var selected = $("#"+weatherSelectCountryListId+" option:selected");
		var selId = selected.val();
		//alert("You selected "+selId);
		loadCityWeatherData(selId);
	});
}

function loadCountryWeatherData(id) {
	//alert("loadWeatherCountryData");
	var countryUrl = getWeatherDepth1ListUrlFor(id);
	$.ajax({
		type: "GET",
		url: countryUrl,
		dataType: "xml",
		success: parseWeatherCountryXml
	});	
}

//Parses xml for continents, sorts and populates the CityListDiv
function parseWeatherContinentXml(xml)
{
	//alert("parseWeatherContinentXml");
	var sRes = weatherContinentDivContent;
	sRes += "<select id=\""+weatherSelectContinentListId+"\" class=\""+weatherSelectCssClassName+" weathercontinent\">\n";
	//add a select
	sRes += "<option>-- Select a continent</option>\n";
	$(xml).find("continent").each(function() {
				var thisName = $(this).attr("name");
				var thisId = $(this).attr("id");
				var thisItem = "<option value=\""+thisId+"\">"+thisName+"</option>\n";
				if (thisId != "6255152") {
					sRes = sRes+thisItem;
					}
			});
	sRes += weatherContinentDivContentPost;
	$("#"+weatherSelectContinentListDivId).html(sRes);
	$("#"+weatherSelectContinentListId).change(function (e) {
			var selected = $("#"+weatherSelectContinentListId+" option:selected");
			var selId = selected.val();
			loadCountryWeatherData(selId);
		});
}

function loadContinentWeatherData(feedUrl) {
	$.ajax({
		type: "GET",
		url: feedUrl,
		dataType: "xml",
		success: parseWeatherContinentXml
	});
}



$(document).ready(function() {
 					//alert('hello');
 			 		//clearOuterPlaceholder();
 			 		var myContent = $(outerPlaceholderSelector).html();
 			 		$(outerPlaceholderSelector).html(innerPlaceholderContent);
 			 		updateSpecificWeatherSwf(weatherLocationIdAylesbury);
 			 		//alert('ready');
 					loadContinentWeatherData(weatherAllContinentListUrl);
 					
 		});
 		
 		
 		
