// Extending jQuery
/*(function($) {
	$.findEvent.init = function() {
	
	}
	$.fn.findEvent = function(options, callback) {
		return this.each(function(){
			$.findEvent.init();
		});
	};
})(jQuery);*/

(function($) {
	$.fn.findEvent = function(options, callback) {
		return this.each(function(){

			// Initialise Variables
			var mapComponent = $('img#f_region');
			var imageComponent = $('area.swapMap');
			var path = "/img/map/";
			var defaultMapImage = "wMap.jpg";
			var state = "";
			
			// Dining Type List
			var events = [
				'Water Sports',
				'Water Photography',
				'Water Resources',
				'Water Destinations',
				'Waterwordwide'
			];
			
			// Useful Link categoryID
			var linkPage = {
				nsw: {
					sports: '834',
					photography: '833',
					resources: '832',
					destinations: '835',
					waterwordwide: '902'
				},
				nt: {
					sports: '941',
					photography: '953',
					resources: '947',
					destinations: '959',
					waterwordwide: '908'
				},
				qld: {
					sports: '942',
					photography: '954',
					resources: '948',
					destinations: '960',
					waterwordwide: '903'
				},
				sa: {
					sports: '943',
					photography: '955',
					resources: '949',
					destinations: '961',
					waterwordwide: '906'
				},
				tas: {
					sports: '944',
					photography: '956',
					resources: '950',
					destinations: '962',
					waterwordwide: '905'
				},
				vic: {
					sports: '945',
					photography: '957',
					resources: '951',
					destinations: '963',
					waterwordwide: '904'
				},
				wa: {
					sports: '946',
					photography: '958',
					resources: '952',
					destinations: '964',
					waterwordwide: '907'
				}
			}
			
			var PopulateEventOption = function(events, state, links) {
				var eventComponent = $('#event');
				eventComponent.empty();
				
				// Add Dining Type List
				for(var i = 0; i < events.length; i++ ) {
					eventComponent.append('<option value="' + events[i].toLowerCase() + '">' + events[i] + '</option>');
				}
				
				// Add click event to Dining Types
				$('select#event').click(function() {
					var event = "";
					switch(this.value) {
						case 'water sports' :
							event = 'sports';
							break;
						case 'water photography' :
							event = 'photography';
							break;
						case 'water resources' :
							event = 'resources';
							break;
						case 'water destinations' :
							event = 'destinations';
							break;
						case 'waterwordwide' :
							event = 'waterwordwide';
							break;
					}
					
					var newPage = links[state][event];
					
					// Enable Search Button
					$('input#submit')
					.removeAttr('disabled')
					.fadeTo('normal',1)
					.click(function() {
						window.location = '/dining/useful-links.asp?categoryID=' + newPage;
						return false;
					});
				});
			};
			
			// Disable Search Button
			$('input#submit').attr('disabled','true').fadeTo(0,0.25);
			
			// Map Mouse Over
			imageComponent.mouseover(function() {
				// Find map image to display
				switch(this.id.toLowerCase()) {
					case "wa" :
						mapImage = "wMap_wa.jpg";
						break;
					case "nt" :
						mapImage = "wMap_nt.jpg";
						break;
					case "qld" :
						mapImage = "wMap_qld.jpg";
						break;
					case "act" :
						mapImage = "wMap_act.jpg";
						break;
					case "nsw" :
						mapImage = "wMap_nsw.jpg";
						break;
					case "vic" :
						mapImage = "wMap_vic.jpg";
						break;
					case "tas" :
						mapImage = "wMap_tas.jpg";
						break;
					case "sa" :
						mapImage = "wMap_sa.jpg";
						break;
					default:
						mapImage = defaultMapImage;
						break;
				}
				
				mapComponent.attr('src', path + mapImage);
			});
			
			// Map Mouse Out
			imageComponent.mouseout(function() {
				mapComponent.attr('src', path + defaultMapImage);
			});
			
			// Map click
			imageComponent.click(function() {
				state = this.id;
				
				// Remove any existing selections
				$('#state option:selected').removeAttr("selected");		
				$('#event option:selected').removeAttr("selected");		
				
				// Add current selection
				$('#state option[value="' + state + '"]').attr("selected","selected");
				
				// Populate Dining Type List
				PopulateEventOption(events,state,linkPage);
			});
			
			// Form State Select
			$('#state').change(function() {
				// Find current State
				state = this.value;
				
				// Populate Dining Type List
				PopulateEventOption(events,state,linkPage);
			});
		
		});
	};
})(jQuery);