//-------------------------------------
//	Code: ryan@ryanmills.net
//	Requires: MooTools 1.2.3
//-------------------------------------

var Stations = new Class({
	
	//-------------------------------------
	//	Init the class
	//-------------------------------------
	initialize: function(){
		//alert('initialize');
		window.addEvent('domready', this.onload);
	},
	
	//-------------------------------------
	//	Load the staions list when onload
	//	is fired.
	//-------------------------------------
	onload: function(){
		//alert('onload');
		var jsonRequest = new Request.JSON({ url: "http://skifreedeals.com/stations/load", onSuccess: Stations.initFinder }).get();
	},
	
	//-------------------------------------
	//	Doc state is ready, lets get this
	//	party started.
	//-------------------------------------
	initFinder: function( data ){
		//alert('initFinder');
		
		
		//-------------------------------------
		//	This is the primary array of
		//	station data.
		//-------------------------------------
		Stations.data = data;
		
		
		//-------------------------------------
		//	Add event handlers
		//-------------------------------------
		$('finder_state').addEvent('click', function(element){
			Stations.setState($('finder_state').value);
		});
		
		$('finder_city').addEvent('click', function(element){
			Stations.setCity($('finder_city').value);
		});
		
		$('finder_zip').addEvent('click', function(element){
			Stations.setZip($('finder_zip').value);
		});
		
		
		//-------------------------------------
		//	Stack all the states
		//-------------------------------------
		states = new Array();
		i=0;
		while (i<Stations.data.length){
			if( !states.contains(Stations.data[i].state) ){
				states.push(Stations.data[i].state);
			}
			i++;
		} 
		
		
		states.sort();
		
		
		//-------------------------------------
		//	Create and attach options to select box
		//-------------------------------------
		i=0;
		while (i<states.length){
			var temp_elem  = new Element('option', {value: states[i], html: states[i]});
			$('finder_state').adopt(temp_elem);
			i++;
		}
		
		//-------------------------------------
		//	Enable the select boxs
		//-------------------------------------
		Stations.enableInput();

	},
	
	
	//-------------------------------------
	//	Fired on selecting a state.
	//
	//	This will set both the state and
	//	zip select boxes. Results are not
	//	cleared for easier use and less
	//	stress on the host.
	//-------------------------------------
	setState: function( state ){
		//alert('setState: '+state);
		
		//-------------------------------------
		//	Disable select boxes so the
		//	host does not overload.
		//-------------------------------------
		Stations.disableInput();
		
		
		//-------------------------------------
		//	clear city and zip select boxes
		//-------------------------------------
		var removeElements = $('finder_city').getChildren();
		i=0;
		while (i<removeElements.length){
			removeElements[i].destroy();
			i++;
		}
		removeElements = $('finder_zip').getChildren();
		i=0;
		while (i<removeElements.length){
			removeElements[i].destroy();
			i++;
		}
		
		
		//-------------------------------------
		//	Find and stack citys and zips
		//-------------------------------------
		var citys = new Array();
		var zips = new Array();
		i=0;
		while (i<Stations.data.length){
			if(Stations.data[i].state == state ){
				
				if( !citys.contains(Stations.data[i].city) ){
					citys.push(Stations.data[i].city);
				}
				
				if( !zips.contains(Stations.data[i].zip) ){
					zips.push(Stations.data[i].zip);
				}
			}
			i++;
		}
		
		
		//-------------------------------------
		//	Sort them nice and pretty
		//-------------------------------------
		citys.sort();
		zips.sort();
		
		
		//-------------------------------------
		//	Create and attach to select boxes
		//-------------------------------------
		i=0;
		while (i<citys.length){
			var temp_elem  = new Element('option', {html: citys[i], value: citys[i]});
			$('finder_city').adopt(temp_elem);
			i++;
		}
		i=0;
		while (i<zips.length){
			temp_elem = new Element('option', {html: zips[i], value: zips[i]});
			$('finder_zip').adopt(temp_elem);
			i++;
		}
		
		
		//-------------------------------------
		//	Enable select boxes
		//-------------------------------------
		Stations.enableInput();
		
	},
	
	
	//-------------------------------------
	//	Fired on selecting a city.
	//
	//	This will set zips and display results
	//-------------------------------------
	setCity: function( city ){
		//alert('setCity: '+city);
		
		
		//-------------------------------------
		//	Disable select boxes so the
		//	host does not overload.
		//-------------------------------------
		Stations.disableInput();
		
		
		//-------------------------------------
		//	clear zip select options and results
		//-------------------------------------
		var removeElements = $('finder_zip').getChildren();
		i=0;
		while (i<removeElements.length){
			removeElements[i].destroy();
			i++;
		}
		removeElements = $('station-finder-results').getChildren();
		i=0;
		while (i<removeElements.length){
			removeElements[i].destroy();
			i++;
		}
		
		
		//-------------------------------------
		//	Find and stack zips and display state results
		//-------------------------------------
		var zips = new Array();
		i=0;
		while (i<Stations.data.length){
			if(Stations.data[i].city == city){
				if( !zips.contains(Stations.data[i].zip) ){
					zips.push(Stations.data[i].zip);
				}
				
				if(Stations.data[i].name == ''){
					station_name = "SHELL STATION";
				}else{
					station_name = Stations.data[i].name;
				}
				
				//-------------------------------------
				//	format station output
				//-------------------------------------
				theStation = "<b>"+station_name+"</b><br />";
				//theStation += Stations.data[i].phone+"<br />"; //disable phone numbers
				theStation += Stations.data[i].address+"<br />";
				theStation += Stations.data[i].city+" "+Stations.data[i].state+" "+Stations.data[i].zip;
				
				temp_elem  = new Element('div', {"class": "station-result", html: theStation});
				$('station-finder-results').adopt(temp_elem);
				
			}
			i++;
		}
		
		//-------------------------------------
		//	Sort nice and pretty
		//-------------------------------------
		zips.sort();
		
		
		//-------------------------------------
		//	Sort nice and pretty
		//-------------------------------------
		i=0;
		while (i<zips.length){
			temp_elem  = new Element('option', {html: zips[i], value: zips[i]});
			$('finder_zip').adopt(temp_elem);
			i++;
		}
		Stations.enableInput();
		
	},
	
	
	//-------------------------------------
	//	Fired on selecting a zip
	//
	//	Sets results
	//-------------------------------------
	setZip: function( zip ){
		//alert('setZip: '+zip);
		
		//-------------------------------------
		//	Disable select boxes so the
		//	host does not overload.
		//-------------------------------------
		Stations.disableInput();

		
		//-------------------------------------
		//	clear results
		//-------------------------------------
		var removeElements = $('station-finder-results').getChildren();
		i=0;
		while (i<removeElements.length){
			removeElements[i].destroy();
			i++;
		}
		
		
		//-------------------------------------
		//	find and display results
		//-------------------------------------
		i=0;
		while (i<Stations.data.length){
			if(Stations.data[i].zip == zip){
				
				//-------------------------------------
				//	fix for missing station names
				//-------------------------------------
				if(Stations.data[i].name == ''){
					station_name = "SHELL STATION";
				}else{
					station_name = Stations.data[i].name;
				}
				
				//-------------------------------------
				//	format station output
				//-------------------------------------
				theStation = "<b>"+station_name+"</b><br />";
				//theStation += Stations.data[i].phone+"<br />"; disable phone
				theStation += Stations.data[i].address+"<br />";
				theStation += Stations.data[i].city+" "+Stations.data[i].state+" "+Stations.data[i].zip;
				
				var temp_elem  = new Element('div', {"class": "station-result", html: theStation});
				$('station-finder-results').adopt(temp_elem);
				
			}
			i++;
		}

		Stations.enableInput();
		
	},
	
	
	
	//-------------------------------------
	//	Used to disable select boxes
	//-------------------------------------
	disableInput: function(){
		//alert('disableInput');
		$('finder_state').setProperty('disabled', 'disabled');
		$('finder_city').setProperty('disabled', 'disabled');
		$('finder_zip').setProperty('disabled', 'disabled');
	},
	
	
	//-------------------------------------
	//	Used to enable select boxes
	//-------------------------------------
	enableInput: function(){
		//alert('enableInput');
		$('finder_state').removeProperty('disabled' );
		$('finder_city').removeProperty('disabled');
		$('finder_zip').removeProperty('disabled');
	}
	
	
});
var Stations = new Stations();
