DEPARTURE_DATE_FORMAT = "ddd, d MMM yyyy";
DATEPICKER_OPTIONS = {
	showOn: 'both',
	buttonImageOnly: true
}
SLIDER_MARKERS = true

$(document).ready(function(){

	/*
		Polyfill elements for faux-CSS3 dropshadows. CSS rules are defined in screen.css
	*/
	$("div.subnavigation").append($("<div>").addClass("shadow bottom"));
	$("div.subnavigation").append($("<div>").addClass("shadow left"));
	$("div.subnavigation").append($("<div>").addClass("shadow right"));
	$("div.subnavigation").append($("<div>").addClass("shadow bottom-left"));
	$("div.subnavigation").append($("<div>").addClass("shadow bottom-right"));

	/*
		Position subnavigation elements so that they are centered relative to
		their parent list itme. Also includes checkes to make sure content doesn't
		spill beyond bounds of the document structure.
	*/
	$("div#navigation > ul > li").each(function(){
		var subnav = $(this).find("div.subnavigation");
		offset_width = $(this).position().left - (subnav.width() / 2) + ($(this).width() / 2)
		subnav.css('left', offset_width + "px");

		// Hanging too far to the left...?
		if (parseInt(subnav.css('left')) < 0) {
			subnav.css('left', 0);
		}
		
		// ...or the right?
		if (parseInt(subnav.css('left')) + subnav.width() > $(this).parent().width()) {
			tweaked_offset = $(this).position().left + $(this).width() - subnav.outerWidth();
			subnav.css('left', tweaked_offset + "px");
		}
		
		// Make sure IE6 recognises :hover 
		$(this).hover(function() { $(this).addClass('hover')}, function() { $(this).removeClass('hover')});
	})
	
	/*
		Logic for tabbed subnavigation
	*/
	show_tabbed_navigation_item = function(el) {
		$(el).closest("div.subnavigation").find(".nested").hide();
		$(el).closest("div.subnavigation").find(".nested#" + $(el).attr('rel')).show();
	}

	make_tabbed_navigation_item_active = function(el) {
		$(el).closest("ul.highlights").find("li").removeClass('active');
		$(el).parent().addClass('active');
	}

	show_defatul_tabbed_navigation_items = function() {
		$("div#navigation li.tabbed ul.highlights li.active a").each(function () {
			show_tabbed_navigation_item(this);
			make_tabbed_navigation_item_active(this);
		});
	}

	$("div#navigation li.tabbed ul.highlights a").click(function() {
		// If 'highlights' navigation link has a 'rel' attribute, then we assume there is a nested
		// element with the same 'id'.
		if ($(this).attr('rel')) {
			show_tabbed_navigation_item(this);
			make_tabbed_navigation_item_active(this);
			return false;
		// ...else, just navigate to the link as normal
		} else {
			return true;
		}

	})
	
	show_defatul_tabbed_navigation_items();
	
	/* Work in progress */
	$("#email_signup_box").append($("<a>").html("Close").addClass('close'));
	$("#email_signup_box a.close").click(function() {
		$(this).parent().hide();
	})
	
	$(".launch-signup-modal").live('click', function() {
		$("#email_signup_box").css('left', ($(this).offset().left + $(this).outerWidth() - $("#email_signup_box").outerWidth()) + "px");
		$("#email_signup_box").css('top', $(this).offset().top + "px");
		$("#email_signup_box").show();
		return false;
	})

	/*
		Hero panel logic
		1. Create a JS slideshow using assets passed form the template
	*/
	
	if (typeof hero_slideshow != "undefined" && typeof hero_panel_height != "undefined") {
		if (!hero_slideshow) {
			console.log("You need to define properties for hero_slideshow variable");
		} else if (!hero_panel_height) {
			console.log("You need to define a height for the hero panel using hero_panel_height");
		} else {
			$(".hero .imagery").slideshow({ assets: hero_slideshow, height: hero_panel_height });
		}
	}
	
	/*
		2. If a Flash movie has been defined, replace HTML content with it
	*/
	
	if (typeof hero_flash_url != "undefined") {
		if (!hero_flash_url) {
			console.log("hero_flash_url is empty or undefined")
		} else {
			$(".hero .imagery").flashembed({src: hero_flash_url, wmode: 'opaque'});
			$(".hero .imagery").height(hero_panel_height + "px");
		}
	}
	
	/*
		Initiate homepage carousel
	*/
	
	$(".carousel").jcarousel({ scroll: 1 });
	
	/*
		Initiate homepage accordion for activites section
	*/
	
	$("ul.brands").accordion({});


	/*  Cheeky little hack to make sure Google Map displays correclty.
		By sending it offscreen, rather than hiding it, proper dimensions are retained for initialization,,
		preventing any odd map behaviour */
	$("div.map_view").css({
		'position': 'absolute',
		'left': "-9999px"
	});
	
	$("a.open_map_view").toggle(function() {
		$("div.map_view").hide()
		$("div.map_view").css({
			'position': 'relative',
			'left': '0'
		})
		$("div.map_view").fadeIn();
		$(this).text("[-] Close Map View");
		return false;
	}, function() {
		$("div.map_view").hide();
		$(this).text("[+] Open Map View");
		return false;		
	})


	if(typeof(location_lat_lng) != 'undefined' && typeof(location_marker_img) != 'undefined') {
		var location_map;
		var location_map_options = {
		  zoom: 15,
		  center: location_lat_lng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		$(".detail_map").each(function() {
			location_map = new google.maps.Map(this, location_map_options);
		});
		marker = new google.maps.Marker({
			position: location_lat_lng, 
			map: location_map,
			icon: location_marker_img
		});
	}
	
	if (typeof(slideshow) != 'undefined') {
		$(".detail #slideshow-container").slideshow({ assets: slideshow, height: 370, width: 664, left: 'center' });
	}
	
	/*  Some IE6 specific tweaks.
		Fixing PNGs for IE6, using a plugin which utilisies ImageFilter rather than any snazzy SVG, to prevent too
		much of a performance hit.
		Also adding hover functionality for elements which aren't anchors. */
	
	$('.pngfix').pngfix();
	$(".ie6 .compare-button").hover(function() { $(this).addClass('hover')}, function() { $(this).removeClass('hover')});
	
	/* 	Additional booking panel functionality.
		This is in addition to the standard panel functionality found in js/devere/components/bookingpanel.js
		In this instance, we're using the qTip pluign for some advanced popup positioning, and also a very
		bespoke solution for the homepage overlay. */
	
	$("#booking-panel-overlay .booking-overlay-toggle").toggle(function() {
		$("#booking-panel-overlay #booking-panel-forms").slideDown();
		$(this).removeClass('active');
	}, function() {
		$("#booking-panel-overlay #booking-panel-forms").slideUp();
		$(this).addClass('active');
	}).css('cursor', 'pointer');
	$("#booking-panel-overlay #booking-panel-forms").hide();
	$("#booking-panel-overlay .booking-overlay-toggle").addClass('active');
	
	$( '#booking-panel-forms FORM' ).hide();
	var activated = false;
	
	$( "#booking-panel-forms UL.book-type-tabs A" ).each(function(){
		var target = $(this).attr('href');
		$(this).attr('formTarget',target.substring(1,$(this).attr('href').length) );
		
		if ( location.href.indexOf( $(this).attr('formTarget') ) > -1 ) {
			activate($(this));
		} else if ( !activated && $(this).parent().hasClass('active') ) {
			$( '#' + $(this).attr('formTarget') ).show();
		}
		
		$(this).bind('click', function() {
			activate($(this));
			return false;
		});
		
		function activate(el) {
			el.parent().siblings().removeClass('active');
			el.parent().addClass('active');
			$( '#booking-panel-forms FORM' ).hide();
			$( '#' + el.attr('formTarget') ).show();
			activated = true;
		}
	})
	
	
	
	
	refreshQtipLinks = function () {
		elems = $('.booking-panel-qtip').click(function() { return false; });
		if(elems.length) {
			$('.booking-panel-qtip:first').qtip({
				show: {
					event: 'click',
					target: elems
				},
				hide: {
					event: 'nothing',
					fixed: true,
					target: elems
				},
				content: {
					text: $("#booking-panel-popup")
				},
				style: {
					classes: 'booking-panel-qtip'
				},
				position: {
			      	my: 'bottom right',
			      	at: 'bottom right',
			      	adjust: {
				       	x: 30,
						y: 20
				    },
			      	target: 'event',
					viewport: $(window)
			   	},
				events: {
					render: function(event, api) {
						$('a.close-qtip', api.elements.content).click(function() {return false;});
						$('a.close-qtip', api.elements.content).click(api.hide);
					},
					show: function(event, api) {
						original_trigger = event.originalEvent.currentTarget;
						property_uuid = $(original_trigger).attr('rel');
						property_obj = $.grep(propertyData, function(a) { return a.uuid == property_uuid})[0];
						api.elements.content.find('span.property-name').text(property_obj.name);
						api.elements.content.find('span.property-type').text(property_obj.place);
						api.elements.content.find('span.brand-logo').html($("<img>").attr('src', property_obj.logoImage));
						api.elements.content.find("input[name='PropertyCode']").val(property_obj.propertyCode);
					}
				}
			});
		}
	};
	
	refreshQtipLinks();
	
	/*
		Add toggle button for offers T&Cs
	*/
	$(".offer-terms").before("<a href='#' class='offer-terms-toggle'></a>");
	$(".offer-terms-toggle").toggle(
		function() {
			$(this).text("[+] Show full offer terms & conditions");
			$(this).next().hide();
		},
		function() {
			$(this).text("[-] Hide full offer terms & conditions");
			$(this).next().show();
		}
	).click();
	
});



















