$.extend({
	getUrlParams: function() {
		var params = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for (var i = 0; i < hashes.length; i++) {
			hash = hashes[i].split('=');
			params.push(hash[0]);
			params[hash[0]] = hash[1];
		}
		return params;
	},
	getUrlParam: function(name) {
		return $.getUrlParams()[name];
	}
});

$(document).ready(function() {
	
	function init_search() {
		$('#q').val('Search...');
		$('#q').addClass('inactive');
		
		$('#q').focus(function() {
			if ($(this).val() == 'Search...') {
				$(this).val('').removeClass('inactive').addClass('active', 200);
				$(this).next().addClass('extended');
			}
		});
		$('#q').blur(function() {
			if ($(this).val() == '') {
				$(this).addClass('inactive', 200).removeClass('active').val('Search...');
				$(this).next().removeClass('extended');
			}
		});
	}
	
	function init_colorbox() {
		$(".colorbox-image").colorbox({photo:true, transition:"fade",opacity:0.7,current:"item {current} of {total}"});
		$(".colorbox-map").colorbox({iframe:true, transition:"fade",opacity:0.7,width:"800px",height:"400px",current:"item {current} of {total}"});
		$(".colorbox-youtube").colorbox({iframe:true, transition:"fade",opacity:0.7,width:"650px",height:"535px",current:"item {current} of {total}"});
		$(".colorbox-prevu").colorbox({iframe:true, transition:"fade",opacity:0.7,width:"550px",height:"450px",current:"item {current} of {total}"});
	}
	
	$('.nav_content a').hover(function() {
		$(this).next().css('font-weight', 'bold');
	}, function() {
		$(this).next().css('font-weight', 'normal');
	});
	
	function toggle_more_destinations(add, remove) {
		if (!$('#more-destinations').text()) {
			$.ajax('/ajax-more-destinations.html', {
				success: function(data) {
					$('#more-destinations').html(data);
					$('#more-destinations').addClass(add);
					$('#more-destinations').removeClass(remove);
				}
			});
		} else {
			$('#more-destinations').toggleClass(add);
		}
	}
	$('body').click(function() {
		$('#more-destinations').removeClass('top');
		$('#more-destinations').removeClass('left');
	});

	$('#more-destinations-link-left').click(function() {
		toggle_more_destinations('left', 'top');
		return false;
	});
	$('#more-destinations-link-top').click(function() {
		toggle_more_destinations('top', 'left');
		return false;
	});
	
	if ($('#content .media').length) {
		var q = $.getUrlParam('q');
		if (q == '0') {
			$('#content .media .wizard-widget').hide();
			$('#content .media .specials-widget').hide();
		}
	}
	
	$('#wizard-widget-date_from_display').datepicker({
		minDate: 0,
		maxDate: '+2y',
		changeYear: true,
		dateFormat: 'D d M yy',
		altField: '#wizard-widget-date_from',
		altFormat: 'yy-mm-dd',
		beforeShow: function() {
			setTimeout(function() { $(".ui-datepicker").css("z-index", 6); }, 10);
		},
		onSelect: function(dateText, inst) {
			oDate = new Date($('#wizard-widget-date_from_display').datepicker('getDate'));
			oDate.setTime(oDate.getTime()+(1000*60*60*24));
			$('#wizard-widget-date_until_display').datepicker('option', 'minDate', oDate);
			wizard_widget_nights();
		}
	});
	$('#wizard-widget-date_until_display').datepicker({
		minDate: 1,
		maxDate: '+2y',
		changeYear: true,
		dateFormat: 'D d M yy',
		altField: '#wizard-widget-date_until',
		altFormat: 'yy-mm-dd',
		beforeShow: function() {
			setTimeout(function() { $(".ui-datepicker").css("z-index", 6); }, 10);
		},
		onSelect: function(dateText, inst) {
			wizard_widget_nights();
		}
	});
	
	function wizard_widget_nights() {
		date_from_array = $('#wizard-widget-date_from').val().split('-');
		date_from = new Date(date_from_array[0], date_from_array[1], date_from_array[2]);
		date_until_array = $('#wizard-widget-date_until').val().split('-');
		date_until = new Date(date_until_array[0], date_until_array[1], date_until_array[2]);
		nights = (date_until.getTime()-date_from.getTime()) / (1000*60*60*24);
		$('#wizard-widget-nights').val(nights);
		wizard_widget_cookie_write();
	}
	
	$('#wizard-widget-travellers').change(function() {
		wizard_widget_cookie_write();
	});
	
	function wizard_widget_cookie_write() {
		$.cookie('wizard-widget-date_from', $('#wizard-widget-date_from').val(), { path: '/' });
		$.cookie('wizard-widget-date_from_display', $('#wizard-widget-date_from_display').val(), { path: '/' });
		$.cookie('wizard-widget-date_until', $('#wizard-widget-date_until').val(), { path: '/' });
		$.cookie('wizard-widget-date_until_display', $('#wizard-widget-date_until_display').val(), { path: '/' });
		$.cookie('wizard-widget-travellers', $('#wizard-widget-travellers').val(), { path: '/' });
		$.cookie('wizard-widget-nights', $('#wizard-widget-nights').val(), { path: '/' });
	}
	
	function wizard_widget_cookie_read() {
		if ($.cookie('wizard-widget-date_from')) { $('#wizard-widget-date_from').val($.cookie('wizard-widget-date_from')); }
		if ($.cookie('wizard-widget-date_from_display')) { $('#wizard-widget-date_from_display').val($.cookie('wizard-widget-date_from_display')); }
		if ($.cookie('wizard-widget-date_until')) { $('#wizard-widget-date_until').val($.cookie('wizard-widget-date_until')); }
		if ($.cookie('wizard-widget-date_until_display')) { $('#wizard-widget-date_until_display').val($.cookie('wizard-widget-date_until_display')); }
		if ($.cookie('wizard-widget-travellers')) { $('#wizard-widget-travellers').val($.cookie('wizard-widget-travellers')); }
		if ($.cookie('wizard-widget-nights')) { $('#wizard-widget-nights').val($.cookie('wizard-widget-nights')); }
	}
	
	function init_wizard_widget() {
		$('#wizard-widget-date_from_display').datepicker('setDate', '0');
		$('#wizard-widget-date_until_display').datepicker('setDate', '+1');
		wizard_widget_cookie_read();
	}
	
	if (location.hash.substr(0, 3) == '#s-') {
		$('#content .specials .special').removeClass('special_active');
		$(location.hash).addClass('special_active');
		$('#content .specials .special div').css('visibility', 'hidden');
		$('#content .specials .special div').css('visibility', 'visible');
		$('#content .specials .special h3').css('visibility', 'hidden');
		$('#content .specials .special h3').css('visibility', 'visible');
	}
	
	$('#compare .results .compare-thumb').hover(function() {
		$(this).addClass('compare-thumb-hover');
	}, function() {
		$(this).removeClass('compare-thumb-hover');
	});
	
	$('#compare .results .compare-thumb').click(function() {
		$(this).closest('table').find('.compare-thumb').removeClass('compare-thumb-active');
		$(this).closest('table').find('.compare-thumb-details').hide();
		$(this).addClass('compare-thumb-active');
		$(this).next().show();
		if ($.browser.msie && $.browser.version < 9) {
			$(this).next().find('.compare-thumb-details-actions a').each(function(i, el) {
				$(this).next().find('.compare-thumb-details-actions').append(el);
			});
		} else {
			$(this).next().find('.compare-thumb-details-actions a').css('visibility', 'hidden');
			$(this).next().find('.compare-thumb-details-actions a').css('visibility', 'visible');
		}
	});
	$('#compare .results .compare-thumb a').click(function(e) {
		e.stopPropagation();
	});
	
	function compare_filter() {
		$('#compare .results .compare-thumb-filter').each(function() {

			var name_match = true;
			var min_match = true;
			var max_match = true;
			var region_match = true;
			var type_match = true;

			var thumb_details = $(this).val().split('~');

			if (!thumb_details[1].toLowerCase().match($('#compare_property').val().toLowerCase())) {
				name_match = false;
			}
			if (thumb_details[2] < $('#compare_min').val()) {
				min_match = false;
			}
			if (thumb_details[2] > $('#compare_max').val()) {
				max_match = false;
			}
			if ($('#compare_region').length > 0) {
				if ($('#compare_region').val() != '') {
					if ((thumb_details[4] != $('#compare_region').val())) {
						region_match = false;
					}
				}
			}
			if ($('#compare_type').length > 0) {
				if ($('#compare_type').val() != '') {
					type_match = false;
					for (var j = 5; j < thumb_details.length; j++) {
						if ($('#compare_type').val() == thumb_details[j]) {
							type_match = true;
						}
					}
				}
			}
			
			if (name_match == false || min_match == false || max_match == false || region_match == false || type_match == false) {
				$(this).closest('.compare-thumb').hide();
				$(this).closest('.compare-thumb').removeClass('compare-thumb-active');
				$(this).closest('.compare-thumb').next().hide();
			} else {
				$(this).closest('.compare-thumb').show();
			}
		});
		$('#compare .results').show();
		if ($('#compare .results .compare-thumb:visible').length == 0) {
			$('#compare .no-results').show();
			$('#compare .results').hide();
		} else {
			$('#compare .no-results').hide();
			$('#compare .results').show();
		}
	}
	$('#compare_property').keyup(function() {
		compare_filter();
	});
	$('#compare_min, #compare_max, #compare_region, #compare_type').change(function() {
		compare_filter();
	});
	
	$('#compare .results .compare-thumb-heading a').click(function() {
		var sort_by = $(this).text();
		var tmp_array = new Array();
		$('#compare .results .compare-thumb-filter').each(function(i) {
			var tmp_object = new Object();
			var thumb_details = $(this).val().split('~');
			if (sort_by == 'Property') {
				tmp_object.name = thumb_details[1];
			}
			if (sort_by == 'Rating') {
				tmp_object.name = (6-thumb_details[2])+'-'+thumb_details[1];
			}
			if (sort_by == 'Price') {
				tmp_object.name = str_pad(thumb_details[3].substr(1), 5)+'-'+thumb_details[1];
			}
			tmp_object.thumb = $(this).closest('tr');
			tmp_object.thumb_details = $(this).closest('tr').next();
			tmp_array[i] = tmp_object;
		});
		tmp_array.sort(function(a, b) { if (a.name < b.name) return -1; if (a.name > b.name) return 1; return 0; });
		$(tmp_array).each(function(i, el) {
			$('#compare .results').append(el.thumb);
			$('#compare .results').append(el.thumb_details);
		});
		return false;
	});
	
	function str_pad(number, length) {
		var str = '' + number;
		while (str.length < length) {
			str = '0' + str;
		}
		return str;
	}
	
	$('#compare .no-results a.no-results-reset').click(function() {
		$('#compare_property').val('');
		$('#compare_min').val('2');
		$('#compare_max').val('5');
		$('#compare_region').val('');
		$('#compare_type').val('');
		compare_filter();
	});

	
	init_search();
	init_colorbox();
	init_wizard_widget();


});
