
var TimezoneElement = jscore.def ('TimezoneElement', {

	TimezoneElement: function () {
	
		this.country = null;
		this.region = null;
		this.locality = null;
	},
	go: function (countryId, regionId, localityId) {

		this.country = countryId;
		this.region = regionId;
		this.locality = localityId;
		// query the available timezones
		this.getTimezones();
		
	},
	init: function (htmlName) {
		this.elementName = htmlName;
	},
	close: function () {
	 $("select#"+this.elementName).parent().parent().fadeOut("slow");
	},
	open: function () {
	
	 $("select#"+this.elementName).parent().parent().fadeIn("slow");
//	 $("select#"+this.elementName).parent().parent().parent().fadeIn("slow");
	},
	getTimezones: function () {
		var thisObj = this;
		
		// setup the following selects
		this.clearSelect($("#"+this.elementName));
		
		$("select#"+thisObj.elementName).attr("disabled", "disabled");
		$("#"+this.elementName+"_timezoneLoading").show();
		
		// query the regions...
		$.getJSON("/ajax/geo/timezone_query__"+this.country+"_"+this.region+"_"+this.locality+"/",
		        function(data){
		        	if (data.items.length > 1) {	
			        	 thisObj.open();		        	
			        } else {
				       thisObj.close();
			        }
			        /*
			        $.each(data.items, function(i,item){
			        	$("select#"+thisObj.elementName).append(new Option(item.text, item.value));		   
			        });  
			        */
			        
			        
					var options = '<option value="">Please Select</option>';	
			        $.each(data.items, function(i,item){
						options += '<option value="' + item.value + '">' + item.text + '</option>';	   
			        });  
					$("select#"+thisObj.elementName).html(options);
					
			        
			        
			        // hide loading message
					$("#"+thisObj.elementName+"_timezoneLoading").hide();
					$("select#"+thisObj.elementName).removeAttr("disabled");
		        });
	},
	clearSelect: function (domObj) {
		domObj.get(0).options.length = 0;
		
	}
});




