document.observe('dom:loaded', init, false);
var states = {AL:"Alabama", AK: 'Alaska',  AZ: 'Arizona',  AR: 'Arkansas',  CA: 'California',  CO: 'Colorado',  CT: 'Connecticut',  DE: 'Delaware',  DC: 'District Of Columbia',  FL: 'Florida',  GA: 'Georgia',  HI: 'Hawaii',  ID: 'Idaho',  IL: 'Illinois',  IN: 'Indiana',  IA: 'Iowa',  KS: 'Kansas',  KY: 'Kentucky',  LA: 'Louisiana',  ME: 'Maine',  MD: 'Maryland',  MA: 'Massachusetts',  MI: 'Michigan',  MN: 'Minnesota',  MS: 'Mississippi',  MO: 'Missouri',  MT: 'Montana', NE: 'Nebraska', NV: 'Nevada', NH: 'New Hampshire', NJ: 'New Jersey', NM: 'New Mexico', NY: 'New York', NC: 'North Carolina', ND: 'North Dakota', OH: 'Ohio',  OK: 'Oklahoma',  OR: 'Oregon',  PA: 'Pennsylvania',  RI: 'Rhode Island',  SC: 'South Carolina',  SD: 'South Dakota', TN: 'Tennessee',  TX: 'Texas',  UT: 'Utah',  VT: 'Vermont',  VA: 'Virginia',  WA: 'Washington',  WV: 'West Virginia',  WI: 'Wisconsin',  WY: 'Wyoming'};
var fdc_links = null;
var linkTimer = null;

function handleResponse(req, targetElem){
	var xml = req.responseXML;
	var listingCounts = xml.getElementsByTagName('listingCount');
	var counties = xml.getElementsByTagName('county');
	var lastUpdate = xml.getElementsByTagName('lastUpdate').item(0).firstChild.nodeValue;
	
	var updateTarget = null;
	var labelTarget = null;
	
	if(targetElem == null){
		$('fdc_last_updated').update(lastUpdate);
		updateTarget = $('fdc_national_foreclosure_count');
	}
	if(targetElem == $('fdc_state_selector')){
		updateTarget = $('fdc_state_foreclosure_count');
		labelTarget = $('fdc_st_label');
	}
	if(targetElem == $('fdc_county_selector')){
		updateTarget = $('fdc_county_foreclosure_count');
		labelTarget = $('fdc_co_label');
	}
	
	$A(listingCounts).each(function(node){
		var nodeType = node.getAttribute('type');
		if(nodeType == 'foreclosure'){
			var nodeVal = node.firstChild.nodeValue;
			updateTarget.update(nodeVal);
			labelTarget.show();
			return;
		}		
	});
	
	if(counties.length){
		var countySelector = $('fdc_county_selector');		
		// empty out the current options
		reset_county_selector();
		$A(counties).each(function(node){
			var cno = node.getAttribute('id');
			var countyName = node.firstChild.nodeValue;
			var opt = new Option(countyName, cno);
			countySelector.options[countySelector.options.length] = opt;
			countySelector.enable();
		});
	}
}

function reset_county_selector(){
	var countySelector = $('fdc_county_selector');
	$A(countySelector.options).each(function(opt){countySelector.removeChild(opt); opt=null; });
	countySelector.options[countySelector.options.length] = new Option("Select County","");
}

function init(){		
	//initialize the states dropdown
	var stateSelector = $('fdc_state_selector');
	for(var state in states){
		var opt = new Option(states[state], state);
		stateSelector.options[stateSelector.options.length] = opt;
	}
	// get links
	new Ajax.Request('/lib/xstats-0_0_1/linksJSON.js', {
		method: 'get',
		onSuccess: function(transport){
			var links = transport.responseText.evalJSON();
			if(links){ fdc_links = links;	}
			// set up links cycling
			$('fdc_link').update("<div>"+fdc_links[0]+"</div>");
			setTimeout("cycle_link()", 5000);
		}
	});
	
	$('fdc_links_holder').observe('mouseover', function(){ cancel_link_cycle(); });
	$('fdc_links_holder').observe('mouseout', function(){ linkTimer = setTimeout("cycle_link()", 2000); });
	
	// set up state and county dropdown events
	$('fdc_state_selector').observe('change', onStateSelectorChange );
	$('fdc_county_selector').observe('change', onStateSelectorChange );
	$('fdc_stats_form').request({method:'get', onComplete: function(transport){ handleResponse(transport, null); }, evalJSON: false, evalJS: false });
}

function onStateSelectorChange(e){
	if(e.target == $('fdc_state_selector')){
		$('fdc_county_foreclosure_count').update("&nbsp;");
		$('fdc_co_label').hide();
		reset_county_selector();
	}
	if($('fdc_state_selector').value == ""){
		$('fdc_state_foreclosure_count').update("&nbsp;");
		$('fdc_st_label').hide();
		$('fdc_county_selector').disable();
		return;
	}
	if(e.target == $('fdc_county_selector') && $('fdc_county_selector').value == ""){
		$('fdc_county_foreclosure_count').update("&nbsp;");
		$('fdc_co_label').hide();
		return;
	}
	var statsForm = $('fdc_stats_form');
	statsForm.request({method:'get', onComplete: function(transport){ handleResponse(transport, e.target);}, evalJSON: false, evalJS: false });
}

function cycle_link(){
	cancel_link_cycle();
	// get random link from links array
	link_no = Math.floor ( Math.random() * fdc_links.length );
	var fdc_link = $('fdc_link');
	if(fdc_link){
		fdc_link.dropOut({
			afterFinish: function(){
				fdc_link.update("<div>"+fdc_links[link_no]+"</div>");
				fdc_link.appear();
			}});
		linkTimer = setTimeout("cycle_link()", 6000);
	}
}

function cancel_link_cycle(){
	if(linkTimer){
		clearTimeout(linkTimer);
		linkTimer = null;
	}
}

