if(!jsFrontend) {var jsFrontend = new Object();}

jsFrontend.search = {
	/**
	 * init function
	 */
	init: function() {
		// load datepicker
		$('#start_date').datepicker({minDate: 0, 
									firstDay: 1,
									dateFormat: 'dd/mm/yy', 
									dayNames: ['Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag'],
									dayNamesMin: ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za'],
									monthNames: ['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December'],
									nextText: 'volgende',
									prevText: 'vorige',
									beforeShowDay: function(date) {
										// saturday
										if(date.getDay() == '6') return [true, 'ui-datepicker-saturday'];
										// other days
										return [true, ''];
									}
		});
		
		// load nice title tags
		$('#m_map > area').tooltip({
			showUrl: false,
			top: 15,
			left: 15
		});
	
		// load map
		jsFrontend.search.initMap();
		
		// scrollTo
		if(jsFrontend.search.getUrlParam('search') !== false) {
			$.scrollTo($('.resultHeader'), {duration:1000});
		}
		
		/*
		 * There was an issue with people not having javascript enabled. Therefor, we need to
		 * manually show the submit button if javascript is enabled.
		 */
		$('.buttonSubmit').show();
	},
	
	/**
	 * init the map
	 */
	initMap: function() {
		
		$('#map').maphilight({
			fill: true,
			fillColor: 'FFDF86',
			fillOpacity: 1,
			stroke: true,
			strokeColor: 'FFCC1D',
			strokeOpacity: 1,
			strokeWidth: 1,
			fade: false
		});
		
		// set click event on the map regions
		$('#m_map > area').bind('click', jsFrontend.search.selectRegion);
		
		// bind multiselect
		$('#regions').bind('change', jsFrontend.search.selectMapRegion);
		
		// select 'optgroup'
		$('#regions').bind('change', function(evt) {
			
			// regions
			$(this).children(":selected").filter('[value^="special"]').each(function(index, value) {
				var value = $(value).attr('value');
				
				var aValue = value.split('-');
				aValue.shift();
				
				// create selector
				var selectors = [];
				$.each(aValue, function(index, value) { 
					  selectors.push('#regions > option[value=' + value + ']');
				});

				// get options
				var options = $(selectors.join(','));
				
				options.attr('selected', 'selected');				
			});
			
			/*$(this).children().not(":selected").not("[value^='special']").each(function(index, value) {
				var val = $(value).attr('value');
				$('#regions > option[value^="special"]').filter('[value*="' + val + '"]').attr('selected', '');
			});*/				
			
			jsFrontend.search.selectMapRegion(evt)
		});
	
	},
	
	/**
	 * highlight the map when clicked on the multiselect
	 */
	selectMapRegion: function(evt) {
		var regions = $('#regions').val();
		
		// remove all selected classes on the map
		$('#m_map > area').removeClass('selected');
		
		for(var index in regions) {
			
			$('#m_map > area[href="#' + regions[index] + '"]').addClass('selected');
		}
		
		$('#map').maphilight({
			fill: true,
			fillColor: 'FFDF86',
			fillOpacity: 1,
			stroke: true,
			strokeColor: 'FFCC1D',
			strokeOpacity: 1,
			strokeWidth: 1,
			fade: false
		});		
	
	},
	
	/**
	 * select a region
	 */
	selectRegion: function(evt) {
		
		// prevent default behaviour
		evt.preventDefault();
		
		// selected?
		if($(this).hasClass('selected')) {
			jsFrontend.search.deleteRegion(this);
			$(this).removeClass('selected');
		}
		else {
			jsFrontend.search.addRegion(this);
			$(this).addClass('selected');
		}
		
		$('#map').maphilight({
			fill: true,
			fillColor: 'FFDF86',
			fillOpacity: 1,
			stroke: true,
			strokeColor: 'FFCC1D',
			strokeOpacity: 1,
			strokeWidth: 1,
			fade: false
		});			
	},
	
	/**
	 * add a new region to the input value
	 */
	addRegion: function(el) {
		// get the selected regions
		var aRegionIds = jsFrontend.search.getRegionIds();
		
		// get the region
		var regionId = parseInt($(el).attr('href').slice(1));
		
		// set the option of the ddm selected
		$('#regions option[value="' + regionId + '"]').attr('selected', 'selected');
	},
	
	/**
	 * remove a region from the input value
	 */
	deleteRegion: function(el) {
		// get the selected regions
		var aRegionIds = jsFrontend.search.getRegionIds();
		
		// get the region
		var regionId = parseInt($(el).attr('href').slice(1));
		
		// set the option of the ddm not selected
		$('#regions option[value="' + regionId + '"]').removeAttr('selected');
	},
	
	/**
	 * get an array with the regionIds
	 */
	getRegionIds: function() {
		return $('#regions').val();
	},
	
	/**
	 * get a parameter from the url
	 */
	getUrlParam: function(strParamName) {
		var strReturn = "";
		var strHref = window.location.href;
		if ( strHref.indexOf("?") > -1 ) {
			var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
			var aQueryString = strQueryString.split("&");
			for(var iParam = 0; iParam < aQueryString.length; iParam++) {
				if(aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ) {
					var aParam = aQueryString[iParam].split("=");
					strReturn = aParam[1];
					break;
				}
				else return false;
			}
		}
		else return false;
		return unescape(strReturn);
	}
}

jQuery(function($){
	jsFrontend.search.init();
	
	// selected period
	var period = $('#period').val();
	
	// set default selection
	$('#start_date').datepicker('option', 'defaultDate', new Date(period.split('-')[0], period.split('-')[1] -1, 1, 12, 0, 0));
	
	$('#start_date').change(function()
	{
		var startDate = $('#start_date').val();
		$("#period option[value='"+ startDate.split('/')[2] +"-"+ startDate.split('/')[1] +"']").attr('selected', 'selected');
	});
	
	$('#period').change(function()
	{
		// selected period
		var period = $('#period').val();
		
		// set default selection
		$('#start_date').datepicker('option', 'defaultDate', new Date(period.split('-')[0], period.split('-')[1] -1, 1, 12, 0, 0));
		
		// clear date
		$('#start_date').val(' ');
	});
	
	$('#quick_search').keypress(function(event){
		if(event.keyCode == '13'){
			$('#quick_search_houses').submit();
			//document.quick_search_houses.submit();
		}
	});
	
	$('#quick_search').focus(function(){
		if($(this).val() == 'Vul je gewenste vakantiewoning hier in') $('#quick_search').val('');
	});
	
	$('#quick_search').blur(function(){
		if($(this).val() == '') $('#quick_search').val('Vul je gewenste vakantiewoning hier in');
	});
});
