// Global Var
var currentCityId = "currentcity";


// Updates drop down menu with selected province's cities
//addLoadEvent(createQuickLink);

//function createQuickLink() {

	var quickLink = document.getElementById('quickLinkForm');
	var c1 = 'hidden';
   var rep=quickLink.className.match(' '+c1)?' '+c1:c1;
   quickLink.className=quickLink.className.replace(rep,'');
	//return;
//}
setProvince();

// Sets province selection list to province of city page
function setProvince() {
	var provinceCode = ""; 
	var provinceCodeObj;
	var provinceSelectObj = document.getElementById('provinces');	

	if (document.getElementById('provincecode')) {
		provinceCodeObj = document.getElementById('provincecode');
		provinceCode = provinceCodeObj.title;

		for (var i=0; i<provinceSelectObj.options.length; i++) {
			if (provinceSelectObj.options[i].value.toLowerCase() == provinceCode) {
				provinceSelectObj.selectedIndex = i;
				break;
			}
		}
	} 
	updatecities(provinceSelectObj, document.body.className); // class name is 'en' or 'fr'
}

function updatecities(provinceFieldObj, lang) {
	var cityFieldObj = document.getElementById('cities');
	var cityList = null;
	var provincialSummary;
	var olympicSummary;
	var cityCode = ""; 
	var foundDefaultCity = 0;
	var defaultCityIndex = 0;
	var newCitySelection = document.createElement('select');

	cityFieldObj.options.length=0; // clear options list

	// Remove forecast quick link error label (if present)
	if (document.getElementById('errorMessage')) {
		var quickLink = document.getElementById("quickLink");
		quickLink.removeChild(quickLink.lastChild);
	}

	if (document.getElementById('citycode')) {
		cityCode = document.getElementById('citycode').title;
	}

	if (lang == 'en') {
		cityList = citiesEn[provinceFieldObj[provinceFieldObj.selectedIndex].value]; // grab selected English city list
	} else {
		cityList = citiesFr[provinceFieldObj[provinceFieldObj.selectedIndex].value]; // grab selected French city list
	}

	if (provinceFieldObj.selectedIndex != 0) {
		if (lang == 'en') {
			provincialSummary = "Provincial Summary";
		} else {
			provincialSummary = "Sommaire provincial";
		}
		// Add Provincial Summary Link
		cityFieldObj.options[cityFieldObj.options.length] = new Option(provincialSummary, provinceFieldObj[provinceFieldObj.selectedIndex].value);
	}

	// Create city list for selected province
	for (cityOption in cityList) {
		var optionVals = cityList[cityOption].split('|');

		cityFieldObj.options[cityFieldObj.options.length] = new Option(optionVals[0], optionVals[1]);
		if (optionVals[1].toLowerCase() == cityCode) {
			cityFieldObj.options[cityFieldObj.options.length-1].id = currentCityId; // set element ID so that can be accessed by DOM for the setTimeout
			foundDefaultCity = 1;
		}
	}

	if (foundDefaultCity == 0) {
		if (provinceFieldObj[provinceFieldObj.selectedIndex].value == '--') {
			setCity(0);
		} else {
			setCity(1);
		}
	} else {
		setCity(currentCityId);
	}
}

// Set default city to first in city list or current city
function setCity(index) {
//	var isOpera = navigator.userAgent.toLowerCase().indexOf('opera');
	// NOTE: must use setTimeout for Opera when setting the selectedIndex. 
	// Otherwise, "ghost" options are generated for the selection box
	// Reference: http://www.greywyvern.com/code/opera/Bug-GhostOptions
	//				  http://my.opera.com/community/forums/topic.dml?id=167366

	if (index == currentCityId) {// set to current city 
		setTimeout("document.getElementById('" + currentCityId + "').selected = \"selected\";", 0);
	} else { // set to first city in list
		setTimeout("document.getElementById('cities').selectedIndex = " + index + ";", 0);
	}
}

function jumpToCity(cityObj, provinceObj, units, lang) {
	var citycode = cityObj.options[cityObj.selectedIndex].value;
	var provincecode = provinceObj.options[provinceObj.selectedIndex].value;

	if (provincecode == "--") {
		// This means that no province has been selected, so display error message
		if (document.getElementById('errorMessage')==null) { // only need to display it once
			var quicklinkObj = document.getElementById('quickLink');
			var errorLabel = document.createElement('div');
			errorLabel.setAttribute('id', 'errorMessage');
			errorLabel.className = 'error';
			if (lang == "e") { // display message based on language
				errorLabel.appendChild(document.createTextNode("Please select a region"));
			} else {
				errorLabel.appendChild(document.createTextNode("S.V.P. Choisissez une région"));
			}
			quicklinkObj.appendChild(errorLabel);
		}
	} else if (cityObj.selectedIndex == 0) {
		location.href = "/forecast/canada/summary_" + lang + ".html?" + citycode; // display summary
	} else {
		if (units == 'metric') {
			location.href = "/city/pages/" + citycode.toLowerCase() + "_" + units + "_" + lang + ".html"; // metric city page
		} else {
			location.href = "/forecast/city_" + lang + ".html?" + citycode.toLowerCase() + "&unit=i"; // imperial city page
		}
	}
}

