var _speed = 500;
var strapline_positive;
var strapline_negative;

$(function(){
	
	//jQuery.preloadCssImages();
	
	// Search field hint
	$('form.search_form').inputHintOverlay(2, 0);
	
	// Promotion toggle
	promotion_toggle();
	
	// Navigation and Promotion area adjustments | Background image adjustment
	adjust_layout();
	set_background_dimensions();
	//window.onresize = function() {}
	//$(window).resize(function() {});
	$(document.body).resize(function(e){
		//alert([$(window).width(), $(document).width()]);
		adjust_layout();
		adjust_background();
	});
	
	$("a.fancyboxed").fancybox();
	
	// Navigation hover
	navigation_hover();
	
	// Promotion prepare
	promotion_prepare();
	
	// Promotion slide
	promotion_slide_buttons();
	
	// Promotion hover
	promotion_hover();
	
	// Start promotion slider
	start_slide();
	
	// Pause / Resume Promotion Interval
	hold_promotion_interval();
	
	// Holiday Deal => Offers toggle
	offers_toggle();
	
	// Form actions
	form_actions();
	
	// Popup form
	popup_form();
	
	// Twitter hover
	twitter();
	
	// Islands Map Tooltips
	island_map_tooltips();
	
	// Island activities selection
	island_activities();
	
	// Island hopping selection
	island_hopping();
	
	// Gallery image transitions
	gallery_images();
	
	// Travel agent tooltips
	map_tooltips();
	
	$.fn.maphilight.defaults = {
		fill: true,
		fillColor: '000000',
		fillOpacity: 0.4,
		stroke: true,
		strokeColor: 'fdbe00',
		strokeOpacity: 1,
		strokeWidth: 1,
		fade: true,
		alwaysOn: false,
		neverOn: false,
		groupBy: false
	}
	
});//.jQuery();


// Adjust width of Navigation and Promotion areas
function adjust_layout() {
	var viewWidth = $(window).width();
	var viewHeight = $(window).height();
	
	var navigation_outside = $('#navigation').width();
	var navigation_inside = $('#navigation .wrapper .main').width();
	
	if(viewWidth > navigation_inside) {
		$('#navigation .wrapper .left').show();
		$('#navigation .wrapper .right').show();
		side = (navigation_outside - navigation_inside) / 2;
		$('#navigation .wrapper').width('100%');
		$('#navigation .wrapper .left').width(side);
		$('#navigation .wrapper .right').width(side);
	} else {
		$('#navigation .wrapper .left').hide();
		$('#navigation .wrapper .right').hide();
	}
	
	var promotion_outside = $('#promotion').width();
	var promotion_inside = $('#promotion .wrapper .main').width();
	
	if(viewWidth > promotion_inside) {
		$('#promotion .wrapper .left').show();
		$('#promotion .wrapper .right').show();
		side = (promotion_outside - promotion_inside) / 2;
		$('#promotion .wrapper').width('100%');
		$('#promotion .wrapper .left').width(side);
		$('#promotion .wrapper .right').width(side);
	} else {
		$('#promotion .wrapper .left').hide();
		$('#promotion .wrapper .right').hide();
	}
}

// Capture initial dimensions of background image
var _backgroundWidth = 0;
var _backgroundHeight = 0;
var _backgroundFactor = 0;
function set_background_dimensions() {
	if($('#background img').length != 0) {
		$('#background').css({'left': '-9999px'}).show();
		if($('#background img').width() != 0 && $('#background img').height() != 0) {
			_backgroundWidth = $('#background img').width();
			_backgroundHeight = $('#background img').height();
			_backgroundFactor = _backgroundWidth / _backgroundHeight;
			adjust_background();
		} else {
			setTimeout('set_background_dimensions()', 100);
		}
	}
}

// Adjust width and height of background image to fill browser
function adjust_background() {
	
	//$('#background').hide();
	
	var viewWidth = $(window).width();
	var viewHeight = $(window).height();
	viewFactor = viewWidth / viewHeight;
	
	if(viewFactor > _backgroundFactor) {
		$('#background img').width(viewWidth);
		$('#background img').height(viewWidth / _backgroundFactor);
	} else {
		$('#background img').width(viewHeight * _backgroundFactor);
		$('#background img').height(viewHeight);
	}
	$('#background').css({'left': '0'}).fadeIn(_speed * 1.75, function(){
		//alert([viewWidth, viewHeight, $('#background img').width(), $('#background img').height()]);
	});
}

// Add necessary .hover class to navigation elements for rollover effects
function navigation_hover() {
	
	// Top Level
	$('#navigation .content').children().children().hover(
		function() {
			if(!$(this).hasClass('act')) {
				$('#navigation ul li.act').addClass('force_closed');
				$(this).addClass('hover');
				
				$('#navigation ul li').removeClass('next_act');
				$('#navigation ul li').removeClass('prev_act');
				$(this).prev().addClass('next_act');
				$(this).next().addClass('prev_act');
			}
		},
		function() {
			$('#navigation ul li.act').removeClass('force_closed');
			$(this).removeClass('hover');
			
			$('#navigation ul li').removeClass('next_act');
			$('#navigation ul li').removeClass('prev_act');
			$('#navigation ul li.act').prev().addClass('next_act');
			$('#navigation ul li.act').next().addClass('prev_act');
		}
	);
	
	// Sub Level
	$('#navigation .content ul ul li').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	);
	
	// 3rd Level
	$('#main .topbar ul li').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	);
}

// Add toggle functionality for promotions caroussel
var default_content_padding_bottom = 158;
function promotion_toggle() {
	
	if($.cookie('promotion') == 'closed') {
		$('#promotion').removeClass('open');
		$('#main .scrollpadder').css({'paddingBottom': 44});
	} else if($.cookie('promotion') == 'open') {
		$('#promotion').addClass('open');
		$('#main .scrollpadder').css({'paddingBottom': default_content_padding_bottom});
	} else {
		if($('#promotion').hasClass('open') && $('#promotion').hasClass('delay')) { // Hide promotions after delay
			setTimeout('hide_promotions()', promotions_delay*1000);
			$('#main .scrollpadder').css({'paddingBottom': default_content_padding_bottom});
		} else if(!$('#promotion').hasClass('open') && $('#promotion').hasClass('delay')) { // Show promotions after delay
			setTimeout('show_promotions()', promotions_delay*1000);
			$('#main .scrollpadder').css({'paddingBottom': 44});
		}
	}
	
	if(!$('#promotion').length) {
		$('#main .scrollpadder').css({'paddingBottom': 28});
	}
	
	$('#promotion .toggle').click(function() {
		
		if(!default_content_padding_bottom) default_content_padding_bottom = parseInt($('#main .scrollpadder').css('padding-bottom'));
		
		if($('#promotion').hasClass('open')) {
			$.cookie('promotion', 'closed', { path: '/' });
			hide_promotions();
		} else {
			$.cookie('promotion', 'open', { path: '/' });
			show_promotions();
		}
	
	});
}

function show_promotions() {
	// Promotion
	$('#promotion').animate({
		bottom: 0 + $('#footer').height()
	}, _speed, function(){
		$('#promotion').addClass('open');
	});
	
	// Content padder
	$('#main .scrollpadder').animate({
		paddingBottom: default_content_padding_bottom
	}, _speed);
}

function hide_promotions() {
	// Promotion
	$('#promotion').animate({
		bottom: -$('#promotion').height()+40
	}, _speed, function(){
		$('#promotion').removeClass('open');
	});
	
	// Content padder
	$('#main .scrollpadder').animate({
		paddingBottom: 44
	}, _speed);
}

// Prepare promotions area (by duplicating promotions in case there is a small amount of promotions)
var promotion_width = 0;
var necessary_width = 0
function promotion_prepare() {
	$('#promotion .content .item').each(function(i){
		promotion_width = $(this).width();
		necessary_width += $(this).width();
	});
	$('#promotion .content').width(necessary_width);
	var copies = 2;
	for(var i = 1; i <= copies; i++) {
		var clone = $('#promotion .content.original').clone();
		clone.appendTo('#promotion .slider').removeClass('original');
	}
	$('#promotion .slider').width(necessary_width*(copies+1));
}

// Add sideways sliding functionality to promotion buttons
function promotion_slide_buttons() {
	$('#promotion a.previous').click(function(event){
		promotion_slide('-', true);
	});
	$('#promotion a.next').click(function(event){
		promotion_slide('+', true);
	});
}

// Add sideways sliding action for promotions
function promotion_slide(direction, manual) {
	if(manual) stop_slide();
	if(direction == undefined) direction = '+';
	if(direction == '-') {
		if(parseInt($('#promotion .slider').css('left')) >= 0) {
			$('#promotion .slider').css('left', parseInt($('#promotion .slider').css('left')) - necessary_width);
		}
		$('#promotion .slider').animate({
			'left': parseInt($('#promotion .slider').css('left')) + promotion_width
		}, _speed);
	} else  if(direction == '+') {
		if(parseInt($('#promotion .slider').css('left')) <= -necessary_width) {
			$('#promotion .slider').css('left', parseInt($('#promotion .slider').css('left')) + necessary_width);
		}
		$('#promotion .slider').animate({
			'left': parseInt($('#promotion .slider').css('left')) - promotion_width
		}, _speed);
	}
}

// Add hover functionality for all promotions
function promotion_hover() {
	$('#promotion .content .item .container').each(function(i){
		//var disposition = $(this).height() - $(this).find('.overlay h4').innerHeight();
		//$(this).find('.overlay').css({'top':disposition}, _speed);
		
		$(this).hover(
			function() {
				$(this).find('.overlay').animate({'top':0}, _speed);
			},
			function() {
				var disposition = $(this).height() - $(this).find('.overlay h4').innerHeight();
				$(this).find('.overlay').animate({'top':disposition}, _speed);
			}
		);
		
		$(this).click(function(event){
			var link = $(this).parent().find('a').attr('href');
			var target = $(this).parent().find('a').attr('target');
			if(target == '_blank') {
				window.open(link, target);
			} else {
				window.location = link;
			}
		});
	});
}

// Add auto slide functions
var slide_interval = null;
function start_slide() {
	slide_interval = setInterval(function() { promotion_slide(); }, promotions_delay*1000);
}
function stop_slide() {
	clearInterval(slide_interval);
}

// Add pause / resume functions for promotional interval scroll
function hold_promotion_interval() {
	$('#promotion .frame').hover(
		function () {
			stop_slide()
		},
		function () {
			start_slide();
		}
	);
}

function offers_toggle() {
	$('.offers_toggle').each(function(i){
		$(this).click(function(event){
			event.preventDefault();
			$(this).closest('.deals').find('.deal_toggle').slideToggle();
			$(this).closest('.deal').slideToggle();
		});
	});
}

function form_actions() {
	$('.form_proceed').click(function(event){
		event.preventDefault();
		$('.proceed_content').show();//slideDown(_speed);
		$(this).hide();//slideUp(_speed);
		adjust_layout();
	});
	
	$(".bahamas_before").click(function() {
		if($(this).val() == 1) {
			$(".form_extra").slideDown(_speed, function(){
				scrollTo('#form_extra');
			});
		} else {
			$(".form_extra").slideUp(_speed);
		}
	});
	
	$(".requires_address").click(function() {
		simulate_requires_address_click();
	});
}

function simulate_requires_address_click() {
	var address_required = false;
	$(".requires_address:checked").each(function(){
		if($(this).val() == 1) {
			address_required = true;
		}
	});
	
	if(address_required) {
		$('.address').show();
	} else {
		$('.address').hide();
	}
}

function scrollTo(caller) {	
	var locationHref = window.location.href
	
	var destination = $(caller).offset().top;
	$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, _speed*5, function() {
		//window.location.hash = caller
	});
  	return false;
}

function popup_form() {
	$('a.login').click(function(event){
		event.preventDefault();
		$('form#register_form').hide();
		$('form#login_form').show();
	});
	$('a.register').click(function(event){
		event.preventDefault();
		$('form#login_form').hide();
		$('form#register_form').show();
	});
}

function twitter() {
	$("#twitter_element").bind("mouseenter",function(){
      //$('#twitter_element .tweet').fadeIn(_speed/2);
      $('#twitter_element .tweet').slideDown(_speed);
    }).bind("mouseleave",function(){
      //$('#twitter_element .tweet').fadeOut(_speed/2);
      $('#twitter_element .tweet').slideUp(_speed);
    });
}

function island_map_tooltips() {
	var id = null;
	var horizontal_disposition = 15;
	var vertical_disposition = -15;
	$('#main.home .island_area .left a').mouseover(function(){
		id = $(this).attr('rel');
		$('.tooltip_'+id).show();
	});
	$('#main.home .island_area .left a').mouseout(function(){
		id = $(this).attr('rel');
		$('.tooltip_'+id).hide();
	});
	$('#main.home .island_area .left').mousemove(function(e){
		if(id) {
			$('.tooltip_'+id).css({'top': e.pageY + vertical_disposition, 'left': e.pageX + horizontal_disposition});
		}
	});
}

function island_activities() {
	if($('.activity_selector').length > 0) {
		$('.activity_selector').change(function(){
			var category = $(this).val();
			$('.category').hide();
			$('.category_'+category).show();
		});
		
		$('.activity_selector:first').click();
		$('.activity_selector:first').change();
	}
}

function island_hopping() {
	if($('.hopping_selector').length > 0) {
		$('.hopping_selector').change(function(){
			var route = $(this).val();
			var location = window.location.pathname;
			window.location.replace(location+'?route='+route);
		});
	}
}

var current_image = 0;
var total_images = 0;		
		
function gallery_images() {
	if($('.gallery_images').length > 0) {
		var interval = null;
		current_image = 0;
		total_images = $('.gallery_images img').size();
		
		$('.gallery_images img').hide();
		$('.gallery_images img').first().show();
		
		$('.gallery_images .labels div').hide();
		$('.gallery_images .labels div').first().show();
		
		$('.gallery_images a.previous').click(function(event){
			event.preventDefault();
			gallery_fade_out();
			if(current_image) {
				current_image = current_image - 1;
			} else {
				current_image = total_images - 1;
			}
			gallery_fade_in();
			gallery_pause();
		});
		$('.gallery_images a.next').click(function(event){
			event.preventDefault();
			gallery_fade_out();
			if(current_image == total_images - 1) {
				current_image = 0;
			} else {
				current_image = current_image + 1;
			}
			gallery_fade_in();
			gallery_pause();
		});
		$('.gallery_images a.play').click(function(event){
			event.preventDefault();
			gallery_play(true);
		});
		$('.gallery_images a.pause').click(function(event){
			event.preventDefault();
			gallery_pause();
		});
		
		gallery_play(false);
		
	}
}

function gallery_play(immediate) {
	if(immediate)
		gallery_proceed();
	interval = setInterval('gallery_proceed()', 5000);
	$('.gallery_images a.play').hide();
	$('.gallery_images a.pause').show();
}

function gallery_pause() {
	clearInterval(interval);
	$('.gallery_images a.pause').hide();
	$('.gallery_images a.play').show();
}

function gallery_fade_out() {
	$('.gallery_images img').eq(current_image).fadeOut(_speed);
	$('.gallery_images .labels div').eq(current_image).fadeOut(_speed);
}

function gallery_fade_in() {
	$('.gallery_images img').eq(current_image).fadeIn(_speed);
	$('.gallery_images .labels div').eq(current_image).fadeIn(_speed);
}

function gallery_proceed() {
	gallery_fade_out();
	if(current_image) {
		current_image = current_image - 1;
	} else {
		current_image = total_images - 1;
	}
	gallery_fade_in();
}

function map_tooltips() {

	var horizontal_disposition = 15;
	var vertical_disposition = -15;
	
	$('.tooltip').mouseover(function(event){
		$('.map-tooltip').show();
		$('.map-tooltip .wrapper strong').html($(this).attr('title'));
		if(parseInt($(this).attr('ref')) > 0) {
			//alert(strapline_positive);
			$('.map-tooltip .wrapper span.strapline').html(strapline_positive);
			$('.map-tooltip .wrapper span.count').html($(this).attr('ref'));
		} else {
			//alert(strapline_negative);
			$('.map-tooltip .wrapper span.strapline').html(strapline_negative);
			$('.map-tooltip .wrapper span.count').html('');
		}
		$(this).attr('title', null);
	});
	
	$('.tooltip').mouseout(function(event){
		$('.map-tooltip').hide();
		$(this).attr('title', $(this).attr('alt'));
	});
	
	$('.tooltip').mousemove(function(e){
		$('.map-tooltip').css({'top': e.pageY + vertical_disposition, 'left': e.pageX + horizontal_disposition});
	});
	
}
