/*************
Destinations Landing page
Author: Aaron Curry
Mar 2008
*************/

var destinations = {

	handleCityList : {
		success: function(o) {
        	// The test returned a list of results
        	var cityInsiders = o.responseXML.getElementsByTagName('City');
        	var cityDropDown = document.getElementById("cityId");
        	
        	cityDropDown.options.length = 1;
			for (var i=0;i<cityInsiders.length;i++) {
				newCityText = utils.getNodeValue(cityInsiders[i],'CityName');
				newCityValue = utils.getNodeValue(cityInsiders[i],'CityId');
				cityDropDown.options[i+1] = new Option(newCityText,newCityValue);
			}
        },
    	failure: function(o) { return; }// Some kind of failure
  	},

	init: function() {
		
    	// Set up the expandable content areas
		effects.initializeExpandableContent("div","h2");
		effects.initializeExpandableContent("ul","h3");
		
		// Attach event handler to country dropdown
		if(document.getElementById("destinations-country")) {
			document.getElementById("destinations-country").onchange=destinations.toggleDropDowns;
			if (document.getElementById("destinations-country").value == "US") {
    			document.getElementById("state-container").style.display ="inline";
			}
			else if (document.getElementById("destinations-country").value != "") {
				document.getElementById("city-container").style.display ="inline";
				// Populate the list
				var transactionURL = "/search/SearchCityInsider.mi?country=" + document.getElementById("destinations-country").value + "&state=" + document.getElementById("us-state").value;
				var transaction = YAHOO.util.Connect.asyncRequest('GET', transactionURL, destinations.handleCityList, null);	
			}
		}
		if(document.getElementById("us-state")) {
			document.getElementById("us-state").onchange=destinations.toggleDropDowns;
			if (document.getElementById("us-state").value != "") {
				document.getElementById("city-container").style.display ="inline";
			}
		}
		
		// Attach event handlers to city dropdowns
		if(document.getElementById("cityId")) {
			document.getElementById("cityId").onchange=destinations.submitEventForm;
		}
		
		// Handle expandable content areas
		document.getElementById("stay-header").onclick = destinations.toggleStaySection;
			
		
		// city-insider-search-button
    	document.getElementById("city-insider-search-button").style.display = "none";

	},
	
	toggleStaySection : function() {
					       	
       	if(document.getElementById("stay-header").className == ("expanded")) {
       		effects.hideContent.call(document.getElementById("stay-header"));
       	}
       	else if(document.getElementById("stay-header").className == ("collapsed")) {
       		effects.showContent.call(document.getElementById("stay-header"));
       	}
       	
       	
       	document.getElementById("stay-header").onclick = destinations.toggleStaySection;
		
	},
	
	
	toggleDropDowns : function() {
		
		// Hide the city list
		if(document.getElementById("city-container")) {
    		document.getElementById("city-container").style.display ="none";
    	}
		
		if(this.id == "us-state") {
			if (this.value != "") {
				// Show the city list
				document.getElementById("city-container").style.display ="inline";
			}
		}
		else if(this.id == "destinations-country") {
			if(this.value=="US") {
				// Show the state dropdown
	    		document.getElementById("state-container").style.display ="inline";
			}
			else {
	    		document.getElementById("state-container").style.display ="none";
	    		document.getElementById("us-state").value = ""; 
				if(this.value != "") {
					// Show the city dropdown for non-us countries
		    		document.getElementById("city-container").style.display ="inline";
				}
			}
		}
		
		// Populate the list
		var transactionURL = "/search/SearchCityInsider.mi?country=" + document.getElementById("destinations-country").value + "&state=" + document.getElementById("us-state").value;
		var transaction = YAHOO.util.Connect.asyncRequest('GET', transactionURL, destinations.handleCityList, null);	
		
	},
	
	submitEventForm : function() {
		if(this.value=="") {
			return false;
		}
		var cityInsiderURL = document.getElementById("city-insider-url").value;
		window.location.href = cityInsiderURL + "?cityId=" + document.getElementById("cityId").value;
	}
}

addLoadEvent(destinations.init);