﻿
var PartnerSearch = (function(options) {
	var defaults = {
		keyword: '#keyword',
		categories: '#cats',
		distances: '#dist',
		cities: '#cities',
		regions: '#regions',
		price: '#price',
		groupfriendly: '#groupfriendly',
		petfriendly: '#petfriendly',
		handicap: '#handicap',
		book: '#book',
		submit: '#submit',
		hidBHBL: '#hidBHBL',
		hidGL: '#hidGL',
		hidGLAP: '#hidGLAP',
		hidSE: '#hidSE',
		mapDiv: '#mapDiv',
		width: 100,
		height: 70,
		path: null,
		replaceFirstOption: false,
		doKeywordReplace: true
	};

	var _options = $.extend({}, defaults, options);
	var _citiesClone;
	var _miniMap;
	var _keywordLabel;

	var onSubmit = function() {
		var price = "";

		if ($(_options.price).val() != undefined) {
			price = $(_options.price).val();
		}

		gf = ($(_options.groupfriendly).attr('checked')) ? 1 : false;
		pf = ($(_options.petfriendly).attr('checked')) ? 1 : false;
		h = ($(_options.handicap).attr('checked')) ? 1 : false;
		b = ($(_options.book).attr('checked')) ? 1 : false;

		var search = "?"
			+ "q=" + escape($(_options.keyword).val())
			+ "&c=" + $(_options.categories).val()
			+ "&d=" + $(_options.distances).val()
			+ "&cid=" + $(_options.cities).val()
			+ "&rid=" + $(_options.regions).val()
			+ "&gf=" + (gf ? "1" : "")
			+ "&pf=" + (pf ? "1" : "")
			+ "&h=" + (h ? "1" : "")
			+ "&b=" + (b ? "1" : "")
			+ "&p=" + price;

		var url = new String(window.location);
		if (_options.path) {
			url = _options.path;
		} else {
			var index = url.lastIndexOf("?");
			if (index > 0)
				url = url.substring(0, index);
		}
		window.location = url + search;

		return false;
	}

	var onRegionChange = function() {
		var val = $(_options.regions).val();
		setRegion(val);
	}

	var setRegion = function(val) {
		var city = _citiesClone.clone(true);
		var oldcity = $(_options.cities).replaceWith(city);
		var selectedCity = oldcity.val();
		var hide = true;

		if (val) {
			var hid;
			switch (val) {
				case 'BHBL': hid = $(_options.hidBHBL); break;
				case 'GL': hid = $(_options.hidGL); break;
				case 'GLAP': hid = $(_options.hidGLAP); break;
				case 'SE': hid = $(_options.hidSE); break;
			}

			if (_miniMap)
				_miniMap.setRegion(val);

			var array = hid.val().split(',');

			var options = $('option', city).get();
			for (var x = options.length - 1; x >= 0; x--) {
				var optval = $(options[x]).val();
				if (optval && ($.inArray(optval, array) < 0)) {
					options[x].parentNode.removeChild(options[x]);
				}
			}
		} else {
			if (_miniMap)
				_miniMap.hideAll();
			hide = false;
		}

		if (_options.replaceFirstOption) {
			if (hide)
				$('label[for="' + _options.regions.substring(1) + '"]').hide();
			else
				$('label[for="' + _options.regions.substring(1) + '"]').show();
		}

		city.val(selectedCity);
		onCityChange();
	}

	var onCityChange = function() {
		var city = $(_options.cities);
		var val = city.val();
		if (val && !city.attr('disabled')) {
			$(_options.distances).removeAttr('disabled');
		} else {
			$(_options.distances).attr('disabled', 'disabled');
		}

		if (_options.replaceFirstOption) {
			if (val)
				$('label[for="' + _options.cities.substring(1) + '"]').hide();
			else
				$('label[for="' + _options.cities.substring(1) + '"]').show();
		}
	}

	var onCategoryChange = function() {
		var cateogry = $(_options.categories);
		var val = cateogry.val();

		if (_options.replaceFirstOption) {
			if (val)
				$('label[for="' + _options.categories.substring(1) + '"]').hide();
			else
				$('label[for="' + _options.categories.substring(1) + '"]').show();
		}
	}

	var onPriceChange = function() {
		var price = $(_options.price);
		var val = price.val();

		if (_options.replaceFirstOption) {
			if (val)
				$('label[for="' + _options.price.substring(1) + '"]').hide();
			else
				$('label[for="' + _options.price.substring(1) + '"]').show();
		}
	}

	$(_options.submit).bind('click', onSubmit);
	$(_options.regions).bind('change', onRegionChange);
	$(_options.cities).bind('change', onCityChange);
	$(_options.categories).bind('change', onCategoryChange);
	$(_options.price).bind('change', onPriceChange);
	$(_options.keyword).keypress(function(e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			onSubmit();
			return false;
		}
		return true;
	});

	if ($(_options.keyword).val() == 'Keyword' || $(_options.keyword).val() == 'Keyword Search')
		$(_options.keyword).val('');

	_miniMap = new MiniMap({
		id: _options.mapDiv,
		width: _options.width,
		height: _options.height,
		onClick: function(region) {
			region = region.toUpperCase();
			$(_options.regions).val(region);
			setRegion(region);
		},
		onLoad: function() {
			var val = $(_options.regions).val();
			if (val)
				_miniMap.setRegion(val);
		}
	});

	if (_options.doKeywordReplace) {
		_keywordLabel = $('label[for="' + _options.keyword.substr(1) + '"]').text();
		var kwd = $(_options.keyword);
		var current = kwd.val();
		if (!current || current == _keywordLabel)
			kwd.addClass('invalid').val(_keywordLabel);
		kwd.bind('focus', function() {
			var t = $(this);
			if (t.val() == _keywordLabel)
				t.removeClass('invalid').val('');
		}).bind('blur', function() {
			var t = $(this);
			if (t.val() == '')
				t.addClass('invalid').val(_keywordLabel);
		});
	}

	_citiesClone = $(_options.cities).clone(true);
	onRegionChange();
	onCategoryChange();
});


