//------------------------------------
//	GLOBAL.JS
//	Author: 	Engage Interactive
//	Requires:	jquery 1.4.3
//	Version:	0.1
//------------------------------------

Cufon.replace('h2:not(.nocufon), #phone, .main .left h3');


////////////////////////////
// BEGIN JQUERY

$(function(){

	//////////////////////////
	// TARGET BLANK
	
	$('a[href*="http://"]:not([href*="'+location.hostname+'"]):not([class*="internal"]), .external').live('click',function() {
	    window.open($(this).attr('href'));
	    return false;
	});
	
	
	//////////////////////////
	// STAF
	
	$('#staf').attr('href','mailto:?subject=' + document.title + '&body=%0A%0A' + document.title + '%0A' + document.location );
	
	
	
	
	
	//////////////////////////
	// LOADING GOOGLE MAPS
	
	$('a#open_map').click(function(){
		$('#google_map').slideToggle(400,'easeInOutExpo');
		load();
		return false;
	});

	//////////////////////////
	// DROP DOWN

	$('#navigation .drop_down > a').each(function(){

		var linkHover = false;
		var dropHover = false;
		var $e = $(this);

		$e.mouseenter(function(){

			linkHover = true;

			if( $('#drop_down').length == 0 ){

				$e.addClass('hover').siblings('div').clone().appendTo('#header').attr('id', 'drop_down').css({
					top:	$e.position().top + $e.outerHeight(),
					left:	$e.position().left
				});
				
				
				if($e.hasClass('auto_width')) {
					
					$dd_width = $e.outerWidth() - parseInt($('#drop_down ul').css('margin-right')) - parseInt($('#drop_down ul').css('margin-left'))

					$('#header').find('#drop_down').css({
						width: $dd_width
					})
				}


				$('#drop_down').mouseenter(function(){

					dropHover = true;

				}).mouseleave(function(){

					dropHover = false;

					cleanUp();

				});
				
				if( $.browser.msie && $.browser.version == '6.0' ) $('select').css({marginLeft:-10000});

			}

		});

		$e.mouseleave(function(){

			linkHover = false;

			cleanUp();

		});

		function cleanUp(){

			setTimeout(function(){

				if( dropHover == false && linkHover == false ){

					$('#drop_down').remove();

					$e.removeClass('hover');
					
					if( $.browser.msie && $.browser.version == '6.0' ) $('select').css({marginLeft:0});

				}

			},50);

		}

	});
	

	//////////////////////////
	// BOOK NOW PANEL
	
	// LOG IN FORM
	function logIn(){
	
		var $b = $('#book_now');
		var mooseBan = false;
	
		$('#log_in').click(function(e){
			
			if( mooseBan == false ){
			
				mooseBan = true;
			
				if( $(this).children('a').text() == 'Stayed before? Login here' ){
					$(this).children('a').text('Book now');
					$b.find('h3').text('Log in');
				}else{
					$(this).children('a').text('Stayed before? Login here');
					$b.find('h3').text('Book now');
				}
				
				// Hide overflow on container
				$b.css({
					'overflow':'hidden'
				});
				
				// Get width of container
				var w = $b.width() + 20;
				
				// Slide out the booking form
				$b.find('form:visible').animate({
					'margin-left': w
				},300,'easeInExpo',function(){
					$(this).hide().siblings('form').css({
						'margin-left': '-' + w + 'px'
					}).show().animate({
						'margin-left': 0
					},300,'easeOutExpo',function(){
						$b.css({
							'overflow':'visible'
						});
						mooseBan = false;
					});
				});
			
			}
			
			e.preventDefault();
		});
		
	}
	
	logIn();
	
	// Input placeholder
	if( 'placeholder' in document.createElement('input') === false ){
	
		$('input').each(function(){

			var $el = $(this);
			
			// skip if we do not have the placeholder attribute
			if( !$el.is('[placeholder]') )
				return;

			// we cannot do password fields, but supported browsers can
			if( $el.is(':password') )
				return;

			$el.bind('focus.placeholder', function(){
				if( this.value == $el.attr('placeholder' ) )
					$el.val('');
			});
			
			$el.bind('blur.placeholder', function(){
				if(this.value == '')
					$el.val( $el.attr('placeholder') )
            });

            $el.triggerHandler('blur');

			// Prevent incorrect form values being posted
			$el.parents('form').submit(function(){
				$el.triggerHandler('focus.placeholder');
			});
			
		});

	}
	
	//////////////////////////
	// DATE INPUT
	
//	$('input.date').dateTimePicker();
	$('input.date').datepicker({
		minDate: 0,
//		maxDate: '1 year',
		dateFormat: 'dd M yy',
		beforeShow: function(input, inst)
		{
			inst.dpDiv.css({marginTop: -input.offsetHeight + 'px', marginLeft: 4+input.offsetWidth + 'px'});
			
			// Make sure the object is initialized
			setTimeout(function(){
				// If the arrow doesn't exist yet
				if( $('#ui-datepicker-div .arrow').length == 0 ){

					$('<span/>',{
						'class':	'arrow'
					}).appendTo('#ui-datepicker-div');
				}
			}, 100)
		}
	});

	// use the same callback for two different events. possible with bind
	$('.date').bind('onShow onHide', function()  {
		$(this).parent().toggleClass('active');
	});
	
	banChange = false;

	// when first date input is changed
	$('#arrival').change(function() {
		
		$arrival	= $(this).datepicker('getDate');
		$departure = $arrival;
		
		$departure.setDate($arrival.getDate() + 1);
		
//		$('#departure').datepicker('setDate', $departure);
		
		// If they've not manually changed the Departure date, auto-update the departure date
		if( banChange == false ){
			
			$('#departure').datepicker('setDate', $departure).datepicker('option', 'minDate', $arrival);
			
//			$departure.data('dateinput').setMin($('#arrival').data('dateinput').getValue(), true)
//			$departure.data('dateinput').setValue($('#arrival').data('dateinput').getValue()).addDay(1);

		}else{

			$('#departure').datepicker('option', 'minDate', $arrival);
		
		}
		
	});
//
		
//	
//	});
//	
	$('#departure').blur(function() {
		banChange = true;
	});
	
	
	//////////////////////////
	// PEOPLE SLIDER
	
	function people(){
	
		// Initial variables
		var $p = $('#people');
		var name = $p.attr('name');
		var value = $p.val();
		var t = $p.children('option').length;
		var hover = false;
		var drag = false;
		var segHtml = '';
		
		// Replace the drop down with the slider using all the metrics from the drop down
		$('<div/>',{
			'id':	'people',
			'html':	'<span class="track"></span><div class="grip"></div><input type="text" name="' + name + '" value="' + value + '">'
		}).insertAfter('#people').siblings('#people').remove();
		
		// Now that we have removed the select box set up the people slider
		$p = $('#people');
		
		// And set up the gripper
		$g = $('#people .grip');
		
		// Now that we have everything on the page, calculate the min and max
		var min = 0;
		var max = $p.width() - $g.width();
		
		// Put the gripper in its default position based on select value
		$g.css({
			'left':	Math.round( ( max / ( t - 1 ) ) * ( value - 1 ) )
		}).hover(function(){
			hover = true;
			$tt.show();
		},function(){
			hover = false;
			if( drag == false ){
				$tt.hide();
			}
		});
		
		// Set up counter in label
		$('<em/>',{
			'html':	' (' + value + ')'
		}).appendTo('label[for=people]');
		
		// Create tooltip
		var $tt = $('<div/>',{
			'html':		'<span></span>' + value,
			'class':	'tooltip'
		}).appendTo('#people .grip');
		
		$tt.css({
			'margin-left': '-' + Math.round( $tt.outerWidth() / 2 ) + 'px'
		});
		
		for( i = 0; i <= ( t - 1 ); i++ ){
		
			segHtml += '<span style="left:' + Math.round( ( i * ( max / ( t - 1 ) ) ) + 8 ) + 'px"></span>';
			
			if( i == ( t - 1 ) ){
				$('#people .track').append(segHtml);
			}
		}
		
		// MOUSE MOVE
		$(document).mousedown(function(e){
		
			if( hover == true ){
			
				e.preventDefault();
			
				drag = true;
			
				$(this).bind('mousemove', function(e){
						
					var offset = $('#people').offset();
					var x = ( e.pageX - offset.left ) - 11;
					
					if( x > max ){
						x = max;
					}else if( x < min ){
						x = min;
					}
					
					// Snap the grip
					$g.css({
						'left': Math.round( ( max / ( t - 1 ) ) * Math.round( x / ( max / ( t - 1 ) ) ) )
					});
					
					//NewValue = (((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin
					var num = Math.round( ( x * ( t - 1 ) ) / max ) + 1 ;
					
					$('#people input').val( num );
					$('#people .grip .tooltip').html('<span></span>' + num);
					$('label[for=people] em').text(' (' + num + ')');
					
					$tt.css({
						'margin-left': '-' + Math.round( $tt.outerWidth() / 2 ) + 'px'
					});
	
				});

			}
			
		});
		
		// MOUSE UP
		$(document).mouseup(function(){
			$(this).unbind('mousemove');
			drag = false;
			if( hover == false ){
				$tt.hide();
			}
		});
		
	}
	
	people();
	
	
	$('dl#extras dt a').click(function(e){
		
		$(this).parent().next().next().slideToggle();
		
		e.preventDefault();
	})

});


jQuery.extend( jQuery.easing,{
	easeInOutExpo: function (x, t, b, c, d){
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	}
});
