/*
	jQuery dSearch v1.1
	
	Wouter Beeftink
	wouter@footsteps.nl
*/
(function($) {

	$.fn.dSearch = function(settings) {
		
		var settings = $.extend({

			source : '.dSearch-source',
			locations : '.dSearch-locations',
			options : '.dSearch-options',
			firstOption : 'Maak uw keuze',
			target : '/stores',
			requireProvince : false,
			country : 'Land',
			province : 'Provincie',
			city : 'Stad'
			
		}, settings);
		
		$.fn.enable = function() {
			return $(this).removeAttr('disabled');
		};
		
		$.fn.disable = function() {
			return $(this).attr('disabled', 'disabled');
		};
		
		$.fn.addOption = function(text, value) {
			return $(this).append('<option value="' + value + '">' + text + '</option>');
		};
		
		function createRule(label, id) {
			
			var rule = $(
				'<div class="regel">' +
					'<label for="' + id + '">' + label + '</label>' +
				'</div>'
			);
			
			var dropdown = $('<select id="' + id + '" />')
				.addOption(settings.firstOption, 0)
				.appendTo(rule);
			
			rule
				.append('<div class="clear" />')
				.appendTo(locations);
			
			return dropdown;
				
		};
		
		function optionPrefix() {

			if(!firstOption)
				return '&';
				
			firstOption = false;
			return '?';

		};
		
		function updateSelectProvince() {
			
			var country = selectCountry.children(':selected').text();
			
			selectProvince.disable();
			
			selectProvince
				.empty()
				.addOption(settings.firstOption, 0);
			
			if(selectCountry.val() != 0) {
				
				source.children().each(function() {
					
					var list = $(this);
					
					if(list.children('a').text() == country) {
						
						list.children('ul').children().children('a').each(function() {
						
							var node = $(this),
								href = node.attr('href');
							
							selectProvince.addOption(node.text(), href.substring(href.lastIndexOf('/')));
						
						});
						
						return false;
						
					};
					
				});

				selectProvince.enable();
				options.enable();
				
				if(!settings.requireProvince)
					button.enable();
				
				updateSelectCity();
			
			} else {
							
				selectProvince.disable();
				selectCity.disable();
				options.disable();
				
				button.disable();
				
			};
			
		};
		
		function updateSelectCity() {
			
			var country = selectCountry.children(':selected').text(),
				province = selectProvince.children(':selected').text();
				
			selectCity.disable();
			
			selectCity
				.empty()
				.addOption(settings.firstOption, 0);
				
			if(selectProvince.val() != 0) {
				
				source.children().each(function() {
					
					var node = $(this);
					
					if(node.children('a').text() == country) {
						
						node.children('ul').children().each(function() {
							
							var node = $(this);
							
							if(node.children('a').text() == province) {
								
								node.children('ul').children().children().each(function() {
								
									var node = $(this)
										href = node.attr('href');
									
									selectCity.addOption(node.text(), href.substring(href.lastIndexOf('/')));
								
								});
								
								return false;
								
							};
						
						});
						
						return false;
						
					};	
					
				});
				
				if(settings.requireProvince)
					button.enable();
				
				selectCity.enable();
				
			} else {
				
				if(settings.requireProvince)
					button.disable();
				
				selectCity.disable();
				
			};
			
		};
		
		function fire() {
			
			var country = selectCountry.val(),
				province = selectProvince.val(),
				city = selectCity.val(),
				fBool = true;
				
			if(country != 0) {
				
				var address = settings.target + country;
				
				if(province != 0) {
					address += province;
					
					if(city != 0)
						address += city;
						
				};
				
				options.each(function() {
					
					if(this.nodeName == 'SELECT') {
						
						if(this.value != 0)
							address += optionPrefix() + this.name + '=' + this.value;
						
					} else {
						
						if(this.checked)
							address += optionPrefix() + this.name + '=' + this.value;
						
					};
					
				});
				
				button.disable();
			
				window.location.href = address;

			};

		};
		
		var container = this,
			source = $(settings.source, container),
			locations = $(settings.locations, container),
			options = $('input[type=checkbox], select', container),
			button = $('[type=submit]', container),
			selectCountry = createRule(settings.country, 'store-country'),
			selectProvince = createRule(settings.province, 'store-province'),
			selectCity = createRule(settings.city, 'store-city'),
			firstOption = true;

		// Hide and sanitize data
		source
			.hide()
			.find('.dummy').remove();
		
		source.children().children('a').each(function() {
			var href = $(this).attr('href');
			selectCountry.addOption($(this).text(), href.substring(href.lastIndexOf('/')));
		});

		button.show();

		selectCountry
			.change(function() {
				updateSelectProvince();
			})
			.keyup(function() {
				updateSelectProvince();
			});
		
		selectProvince
			.change(function() {
				updateSelectCity();
			})
			.keyup(function() {
				updateSelectCity();
			});
			
		button.click(function() {
			fire();
		});
		
		$(function() {
			updateSelectProvince();
		});
		
		return this;
		
	};

})(jQuery);
