$(document).ready(function() {
	function loadStates() {
		$.post(baseUrl + "/ajax/loadStates/",
			{country: $("#ajax_country").val()}, 
			function(xml) {
				refreshStates(xml);
				resetAll(xml);
			}
		);
	}
	
	function refreshStates(xml) {
		var options = '<option value="-1">-- Select --</option>';
		$("states",xml).each(function(id) {
			state = $("states",xml).get(id);
			options += '<option value="' + $(state).attr('id') + '">' + $(state).text() + '</option>';
		});
		
		$("#ajax_state").html(options);
		$('#ajax_state option:first').attr('selected', 'selected');
	}
	
	function loadSuburbs() {
		if ($("#ajax_state").val() == '-1') {
			var options = '<option value="-1">-- Select State --</option>';
			$("#ajax_suburb").html(options);
			$('#ajax_suburb option:first').attr('selected', 'selected');
		} else {
			$.post(baseUrl + "/ajax/loadSuburbs/",
				{country: $("#ajax_country").val(), state: $("#ajax_state").val()}, 
				function(xml) {
					refreshSuburbs(xml);
				}
			);
		}
	}
	
	function refreshSuburbs(xml) {
		var options = '<option value="-1">-- Select --</option>';
		$("suburbs",xml).each(function(id) {
			sub = $("suburbs",xml).get(id);
			options += '<option value="' + $(sub).attr('id') + '">' + $(sub).text() + '</option>';
		});
		
		$("#ajax_suburb").html(options);
		$('#ajax_suburb option:first').attr('selected', 'selected');
	}
	
	function resetAll(xml) {
		if ($("#ajax_country").val() == '-1') {
			$("#searchresultsdiv").html('');
			var options = '<option value="-1">-- Select Country --</option>';
			$("#ajax_state").html(options);
			$('#ajax_state option:first').attr('selected', 'selected');
		} else {
			var options = '<option value="-1">-- Select State --</option>';
		}
		$("#ajax_suburb").html(options);
		$('#ajax_suburb option:first').attr('selected', 'selected');
	}
	
	function loadResults(scountry, sstate, ssuburb) {
		if ($("#ajax_country").val() != '-1') {
			$.post(baseUrl + "/ajax/loadResults/",
				{country: scountry, state: sstate, suburb: ssuburb}, 
				function(xml) {
					refreshResults(xml);
				}
			);
		}
	}
	
	function refreshResults(xml) {
		var resultshtml = '<table cellspacing="0" cellpadding="5" border="0">';
		$("results",xml).each(function(id) {
			stock = $("results",xml).get(id);
			resultshtml += '<tr><td width="200"><strong>' + $(stock).text() + '</strong><br />' + $(stock).attr('address') + '<br />' + $(stock).attr('suburb') + '<br />';
			resultshtml += $(stock).attr('state') + ' ' + $(stock).attr('postcode') + '<br /><strong>Ph:</strong> ' + $(stock).attr('phone');
			
			if ($(stock).attr('fax')) { resultshtml += '<br /><strong>Fax:</strong> ' + $(stock).attr('fax'); }
			if ($(stock).attr('email')) { resultshtml += '<br /><strong>Email:</strong> ' + $(stock).attr('email'); }
			if ($(stock).attr('website')) { resultshtml += '<br /><strong>Web:</strong> <a href="http://' + $(stock).attr('website') + '" target="_blank" style="color: #ffffff;">' + $(stock).attr('website') + '</a>'; }
			
			resultshtml += '</td><td>';
			if ($(stock).attr('has_riderz') == 1) { resultshtml += '<img src="' + baseUrl + '/public/images/where-to-buy/riderz.gif" width="60" height="30" alt="Riderz" />'; }
			else { resultshtml += '&nbsp;'; }
			resultshtml += '</td><td>';
			if ($(stock).attr('has_polarized') == 1) { resultshtml += '<img src="' + baseUrl + '/public/images/where-to-buy/polarized.gif" width="60" height="30" alt="Polarized" />'; }
			else { resultshtml += '&nbsp;'; }
			resultshtml += '</td><td>';
			if ($(stock).attr('has_prescription') == 1) { resultshtml += '<img src="' + baseUrl + '/public/images/where-to-buy/prescription.gif" width="60" height="30" alt="Prescription" />'; }
			else { resultshtml += '&nbsp;'; }
			resultshtml += '</td><td>';
			if ($(stock).attr('has_safety') == 1) { resultshtml += '<img src="' + baseUrl + '/public/images/where-to-buy/safety.gif" width="60" height="30" alt="Safety" />'; }
			else { resultshtml += '&nbsp;'; }
			resultshtml += '</td>';
			
			resultshtml += '</tr>';
		});
		resultshtml += '</table>';
		
		$("#searchresultsdiv").html(resultshtml);
	}
	
	$("#ajax_country").change(function(){
		loadStates();
		loadResults($("#ajax_country").val(), '-1', '-1');
	});
	
	$("#ajax_state").change(function(){
		loadSuburbs();
		loadResults($("#ajax_country").val(), $("#ajax_state").val(), '-1');
	});
	
	$("#ajax_suburb").change(function(){
		loadResults($("#ajax_country").val(), $("#ajax_state").val(), $("#ajax_suburb").val());
	});
});