var data = {
	
	'-1' : {
		name : "Anywhere"
	},
	'127910' : {
		name : "Florida",
		children : {
	        '-1' : { name : "Anywhere" },    
			'128028' : {
				name : "Orlando",
				children : {
					"-1" : { name : "Anywhere" },
					"128222" : { name : "Kissimmee" },		
					"128221" : { name : "International Drive" },	
					"128223" : { name : "Lake Buena Vista" },
					"128224" : { name : "Universal Orlando Resort" },
					"128225" : { name : "Walt Disney World Resort" }
				}
			},
	        '131830' : {
				name : "Gulf Coast",
				children : {
					"-1" : { name : "Anywhere" },
					"131835" : { name : "Clearwater" },
					"131839" : { name : "St Pete Beach" },
					"131847" : { name : "Indian Rocks" },
					"131850" : { name : "Treasure Island" },
					"131852" : { name : "Sarasota" },
					"131854" : { name : "Bradenton" },
					"131841" : { name : "Fort Myers" },
					"136997" : { name : "New Port Richey" }
				}
			}
		}
	},
	'127879' : {
		name : "Caribbean",
		children : {
			"-1" : { name : "Anywhere" },
			"127913" : { name : "Barbados" },
			"127911" : { name : "Antigua" },
			"127918" : { name : "St Lucia" },
			"127916" : { name : "Jamaica" }
		}
	}
};

//overwrite ajax call function
function performLocationSearch() { return }

//Hijack on change events of selectedLocations0, selectedLocations1, selectedLocations2
jQuery( document ).ready( function() {
	
	var locations = [
	 	jQuery('#selectedLocations0'),
		jQuery('#selectedLocations1'),
		jQuery('#selectedLocations2')
	]

	var i = 0, l = locations.length;
	for(; i < l; i++ ) { //hijack change event
		
		if( !locations[ i ].length && i > 0 ) { //sometimes the session won't print the last dropdown
			var missingSelect = jQuery("<select style='width:215px;' id='selectedLocations"+ i + "' name='selectedLocations[" + i + "]' class='wideSelect'></select>");
			locations[ i ] = missingSelect.insertAfter( locations[ i - 1 ] );
			locations[ i ].hide();
		}
		locations[ i ].bind( 'change', populateMenu );
	}
	
	function populateMenu( ) {
		
		var id = jQuery( this ).val()// get this from args
		var level = parseInt( jQuery( this ).attr('id').replace('selectedLocations', '') );
			
		//loop through affected levels
		var i = level, l = locations.length, dropdownData = {};
		for(; i < l; i++ ) {
			
			if( typeof locations[ i + 1 ] != 'undefined' ) { //don't go to far

				//this could be done better
				switch ( level ) {
					case 2 :
						dropdownData = data[ locations[0].val() ].children[ locations[1].val() ].children[ id ];
						break;
					case 1 :
						if( typeof data[ locations[0].val() ].children != 'undefined' ) {
							dropdownData = data[ locations[0].val() ].children[ id ];
						} else {
							dropdownData = {};
						}
						break;
					default: // 0
						dropdownData = data[ id ];
				}
				
				//remove all options
				locations[ i + 1 ].find( 'option' ).each( function() {
					jQuery( this ).remove();
				})
				
				//add new options by looping through data level, if there are children
				if( typeof dropdownData != 'undefined' && dropdownData.children ) {
					
					var childId;
					for( childId in dropdownData.children ) {
						locations[ i + 1 ].append( jQuery( "<option value='" + childId + "'>" + dropdownData.children[ childId ].name + "</option>" ) );
					}
					locations[ i + 1 ].show();					
				} else {
					locations[ i + 1 ].hide();
				}
			}
			
			level++; //next level
		}
	}
} );

