
// ############ Hotels Images - Also in search.js ########################
function checkExternalImageLoaded() {
	(function($) {
		$(".hotels img").load(function(){
			$(".hotels img").addClass('validImg');
		});
	})(jQuery);
}

//Delay Plugin for jQuery
//- http://www.evanbot.com
//- © 2008 Evan Byrne

jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});

	return this;
};

// ############ Toggle the map ########################

function lp_toggle_map (mapToggle, mapId) {
    if ($(mapToggle).hasClass ('closed')) {
        $('a.toggleMap').removeClass('closed');
        $('a.toggleMap').addClass('open');
        jQuery.cookies.set('lpmaps','open');
        if ($('#' + mapId).hasClass ('mapReady')) {
            $('#' + mapId).slideToggle('normal', function() {
                show_map (mapId) ;
            }) ;
        } else {
        	$('#' + mapId).slideToggle('normal') ;
        }
    } else {
        $('a.toggleMap').removeClass('open');
        $('a.toggleMap').addClass('closed');
        jQuery.cookies.del('lpmaps');
        $('#' + mapId).slideToggle('normal') ;
    }
    

    return false;
}

// ############ Initialize a map toggle element ########################

function lp_init_map_toggle (mapToggle, mapId) {
    $(mapToggle).click (function () {
        return lp_toggle_map (this, mapId) ;
    }) ;
    $(mapToggle).attr('style', 'display:block');
    if (jQuery.cookies.get('lpmaps') == 'open') {
        $(mapToggle).removeClass('closed');
        $(mapToggle).addClass('open');
        $('#' + mapId).attr('style', 'display:block');
    }
}

(function($) {
$(function() {
// ############ Shop Carousel ########################
	var theCarousel = $(".shopCarousel"); 
	
	if (theCarousel.length > 0){
		theCarousel.carousel({
			speed: 500,
			visible: 1, 
			auto: null,
			start: 1
		});
		
		var interval = null;

		$('.next', theCarousel).click(function () {
			theCarousel.carousel('next');
		});
		$('.prev', theCarousel).click(function () {
			theCarousel.carousel('prev');
		});
	}

// ############ Destinations List ########################
	var listId = $('#destList');
	if (listId.length > 0){
		
		listId.find('div:first').css('left', '0');
		
	 $('#destList > li > a').click(function(event){
		 event.preventDefault();
		 listId.find('div').css('left', '-9999px');
		 listId.find('.current').removeClass('current');
		 
		 $(this).next().css('left', '0');
		 $(this).parent().addClass('current');

	 });
	}
	
//############ ACCORDION ########################
	if ($("#list1a").length > 0){
	jQuery('#list1a').accordion({header: ".head",animated: "bounceslide", autoheight: false/*,active: ".current"*/});
	}

// ############ BUTTON HOVER ########################
	$('button').hover(function() {
	  $(this).addClass('ieHover');
	}, function() {
	  $(this).removeClass('ieHover');
	});
	$('a.submitBtnShort').hover(function() {
	  $(this).addClass('ieHover');
	}, function() {
	  $(this).removeClass('ieHover');
	});

// ############ New Window ########################
	$('a').filter(function() {
		var rel = $(this).attr('rel');
		if(!rel || rel.indexOf('external') == -1) {
			return false;
		}
		return true;
	}).click(function(){
        window.open(this.href);
        return false;
    });
    
// ############ About Us Video ########################
    $('.about a[rel="popUp"]').click(function(){
    		var features = "height=520,width=760,scrollTo,resizable=1,scrollbars=1,location=0";
		    newwindow=window.open(this.href, 'PopUp', features);
		    return false;
    });

// ############ MAP SLIDER ########################
    $('#map').addClass ('mapReady') ;

// ############ REVIEW TOGGLE ########################
	$(".toggleReview").show();
	$(".toggleReview").next().addClass('hidden');
    $(".toggleReview").click(function(){
			$(this).next().removeClass('hidden');
            var $this = $(this);
            if( $this.is('.closed') ) {
                    $(this).next().slideDown("fast");
                    $this.removeClass('closed');
                    $this.addClass('open');
					$('.toggleReview span').html('Hide');
            }
            else {
                    $(this).next().hide("fast");
                    $this.removeClass('open');
                    $this.addClass('closed');
					$('.toggleReview span').html('Show');
            }
            return false;
    });

// ############ SHOW MORE ########################
	$(".readMore").show();
	$(".contentToReveal").addClass('hidden');
    $(".readMore").click(function(){
            var $this = $(this);
            if( $this.is('.closed') ) {
					$(".contentToReveal").removeClass('hidden');
                    $(".contentToReveal").slideDown("fast");
                    $this.removeClass('closed');
                    $this.addClass('reveal');
					$('.readMore').html('<a href="">Hide full overview</a>');
            }
            else {
                    $(".contentToReveal").slideUp("fast");
                    $this.removeClass('reveal');
                    $this.addClass('closed');
					$('.readMore').html('<a href="">Show full overview</a>');
            }
            return false;
	});

// ############ Warnings ########################
	$(".travelWarning").show();
	$(".travelWarning").next().addClass('hidden');
    $(".travelWarning").click(function(){
			$(this).next().removeClass('hidden');
            var $this = $(this);
            if( $this.is('.closed') ) {
                    $(this).next().slideDown("fast");
                    $this.removeClass('closed');
                    $this.addClass('reveal');

            }
            else {
                    $(this).next().hide("fast");
                    $this.removeClass('reveal');
                    $this.addClass('closed');
            }
            return false;
    });
 
//############ Generic Hide Show ########################
 	$(".showMore").show();
 	
    $(".showMore").click(function(){
            var $this = $(this);
            if( $this.is('.closed') ) {
            		$(".theHiddenContent").slideDown('500');
                    $this.removeClass('closed');
                    $this.addClass('reveal');
                    $('.showMore a strong').html('Hide');

            }
            else {
            		$(".theHiddenContent").slideUp('500');
                    $this.removeClass('reveal');
                    $this.addClass('closed');
                    $('.showMore a strong').html('Show');
            }
            return false;
    });

//############ PROFILES - HOBBIES AUTOSUGGEST ########################
    if ($("#prHobbies div.autocomplete").length>0) {
        $("#prHobbies div.autocomplete li").hover(
            function() {
                $(this).addClass("hover");
            },
            function() {
                $(this).removeClass("hover");
            }
        );
    }

// ############ AUTO FOCUS ON  USERNAME FIELD ON SIGNIN PAGE ########################
    if ($("#j_username").length>0) {
        $("#j_username").focus();
    }
});

// ############ DATE OF BIRTH - CLIENT SIDE VALIDATION ########################
$(function(){
    // Only initiate calendar object if the required DOB fieldset exists in the current page
    if ($("#prDOB").length>0) {
        monthSelect = $("#prDOBMonth");
        dateSelect = $("#prDOBDay");
        yearSelect = $("#prDOBYear");
        calendar.init(monthSelect, dateSelect, yearSelect);
    }
});

var calendar = {

    months: ['31','29','31','30','31','30','31','31','30','31','30','31'],
    daysInSelectedMonth:31,

    init:function(monthSelect,dateSelect,yearSelect) {
        $(monthSelect).change(function() {
            selectedMonth = calendar.getSelectedMonth(this);
            selectedDate = calendar.getSelectedDate($(dateSelect));
            selectedYear = calendar.getSelectedYear($(yearSelect));
            if (selectedMonth==2 && !calendar.isLeapYear(selectedYear) && selectedYear!==0) {
                daysInSelectedMonth=28;
            }
            else if (selectedMonth==2 && calendar.isLeapYear(selectedYear) && (selectedYear!==0)) {
                daysInSelectedMonth=29;
            }
            else if (selectedMonth!==0) {
                daysInSelectedMonth=calendar.months[selectedMonth-1];
            }
            else {
                daysInSelectedMonth=31;
            }
            calendar.populateDate(daysInSelectedMonth, selectedDate);
            $(dateSelect).val(selectedDate);
        });

        $(yearSelect).change(function() {
            selectedMonth = calendar.getSelectedMonth($(monthSelect));
            selectedDate = calendar.getSelectedDate($(dateSelect));
            selectedYear = calendar.getSelectedYear(this);
            if (selectedMonth==2 && !calendar.isLeapYear(selectedYear) && selectedYear!==0) {
                daysInSelectedMonth=28;
            }
            else if (selectedMonth==2 && calendar.isLeapYear(selectedYear) && selectedYear!==0) {
                daysInSelectedMonth=29;
            }
            else if (selectedMonth!==0) {
                daysInSelectedMonth=calendar.months[selectedMonth-1];
            }
            else {
                daysInSelectedMonth=31;
            }
            calendar.populateDate(daysInSelectedMonth,selectedDate);
            $(dateSelect).val(selectedDate);
        });
    },

    isLeapYear:function(theYear) {
        if (theYear%4 === 0 && theYear!==0) {
            return true;
        }
        else {
            return false;
        }
    },

    getSelectedMonth:function(monthSelect) {
        return $(monthSelect).val();
    },

    getSelectedDate:function(dateSelect) {
        return $(dateSelect).val();
    },

    getSelectedYear:function(yearSelect) {
        return $(yearSelect).val();
    },

    populateDate:function(daysInMonth, daySelected) {
        $("select#prDOBDay option").remove();
        $("select#prDOBDay").append("<option value=\"\">Day:</option>");
        
        for (i=1; i<=daysInMonth; i++) {
            var iFormatted = i;
            // TG TODO Perhaps try and integrate proper number formatting i.e. http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/
            if (i < 10) {
                iFormatted = "0" + i;
            }
            $("select#prDOBDay").append("<option value=\""+iFormatted+"\">"+iFormatted+"</option>");
        }
    },
    
    disablejQueryDOBBehaviour:function() {
        $(monthSelect).unbind();
        $(yearSelect).unbind();
    }

} ;


jQuery.fn.extend({
// ############ IE6 PNGFIX ########################
    ie6PNGFix: function()
    {
        if (!(jQuery.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent))) {
            return this;
        }
        return this.each(function() {
        	var image = $(this);
	        var image_url = image.css('background-image').replace(/url|\(|\)|"/g, '');
	        if (image_url.match(/\.png|\.PNG/)) {
	        	
		        var alphaImageLoader =
		          "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" +
		          image_url +
		          "', sizingMethod='scale');" ;
	            image.attr('style', alphaImageLoader);
		        image.css('background-image', 'none');
	        }
        });
    },
    
// ############ IE6 HOVER FIX ########################
    ie6HoverFix: function()
    {
        if (!(jQuery.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent))) {
            return this;
        }
        return this.hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
    },

// ############ IMAGE PRELOADER ########################
	backgroundLoadImage: function(src, callback) {
		var self = this;
		var image = $(new Image()).load(function () {
			self.filter('img').each(function() {
				$(this).attr('src', src);
				if (callback) {
					callback.call(this);
				}
			});
		}).attr('src', src);
		return this;
	},

// ############ IMAGE CENTERING ########################
	centerImageToBox: function(maxWidth, maxHeight) {
		return this.filter('img').each(function() {
			var image = $(this);
			// copy src to temporary image so that CSS styles don't influence sizing (eg. in webkit)
			var img = new Image();
			$(img).attr('src', image.attr('src'));
			
			var scaledHeight = img.height * (maxWidth/img.width);
			if (scaledHeight < maxHeight) {
				image.css({width: maxWidth, height: scaledHeight});
				image.css('padding-top', Math.floor(((maxHeight - scaledHeight) / 2)));
			} else {
				var scaledWidth = img.width * (maxHeight/img.height);
				image.css({width: scaledWidth, height: maxHeight});
				image.css({display: 'block', margin: 'auto'});
			}
		});
	},

//############ ANIMATION HELPERS ########################
	animDelay: function(delay) {
		return this.queue(function() {
			var e = $(this);
			window.setTimeout(function() { e.dequeue() ; }, delay);
		});
	},
	
	afterAnim: function(callback) {
		return this.queue(function() { callback(); $(this).dequeue(); });
	}
});
        
//############ PNG fix for Gradient in Header bar ########################        
jQuery(function () {
		jQuery("#gradient").ie6PNGFix();
        });

//############ CUSTOM JQUERY UTILITIES ########################
jQuery.extend({
	first: function(array, callback) {
		var elem = null;
		$.each(array, function(i,e) {
			if (callback(i,e) === true) {
				elem = e;
				return false;
			}
		});
		return elem;
	}
});

//############ STRING PADDING ########################
String.prototype.leftPad = function(length, pad) {
	if (typeof(pad) == 'undefined') {
		pad = ' ';
	}
	var padding = '' ;
	var n = length - this.length ;
	for (var i = 0 ; i < n ; i++) {
		padding += pad ;
	}
	return padding + this ;
};

/** 
#  * Copyright (c) 2008 Pasyuk Sergey (www.codeasily.com) 
#  * Licensed under the MIT License: 
#  * http://www.opensource.org/licenses/mit-license.php 
#  *  
#  * Splits a <ul>/<ol>-list into equal-sized columns. 
#  *  
#  * Requirements:  
#  * <ul> 
#  * <li>"ul" or "ol" element must be styled with margin</li> 
#  * </ul> 
#  *  
#  * @see http://www.codeasily.com/jquery/multi-column-list-with-jquery 
#  */  
jQuery.fn.makeacolumnlists = function(settings){
	settings = jQuery.extend({
		cols: 2,		// set number of columns
		colWidth: 0,		// set width for each column or leave 0 for auto width
		equalHeight: false, 	// can be false, 'ul', 'ol', 'li'
		startN: 1		// first number on your ordered list
	}, settings);
 
	if(jQuery('> li', this)) {
		this.each(function() {
			var y=jQuery('.li_container').size(),
		    	height = 0, 
		        maxHeight = 0,
				t = jQuery(this),
				classN = t.attr('class'),
				listsize = jQuery('> li', this).size(),
				percol = Math.ceil(listsize/settings.cols),
				contW = t.width(),
				bl = ( isNaN(parseInt(t.css('borderLeftWidth'),10)) ? 0 : parseInt(t.css('borderLeftWidth'),10) ),
				br = ( isNaN(parseInt(t.css('borderRightWidth'),10)) ? 0 : parseInt(t.css('borderRightWidth'),10) ),
				pl = parseInt(t.css('paddingLeft'),10),
				pr = parseInt(t.css('paddingRight'),10),
				ml = parseInt(t.css('marginLeft'),10),
				mr = parseInt(t.css('marginRight'),10),
				col_Width = Math.floor((contW - (settings.cols-1)*(bl+br+pl+pr+ml+mr))/settings.cols),
				eh,
				border_top,
				border_bottom;
			if (settings.colWidth) {
				col_Width = settings.colWidth; 
			}
			var colnum=1,
				percol2=percol;
			jQuery(this).addClass('li_cont1').wrap('<div id="li_container' + (++y) + '" class="li_container"></div>');
			if (settings.equalHeight=='li') {
			    jQuery('> li', this).each(function() {
			        var e = jQuery(this);
			        var border_top = ( isNaN(parseInt(e.css('borderTopWidth'),10)) ? 0 : parseInt(e.css('borderTopWidth'),10) );
			        var border_bottom = ( isNaN(parseInt(e.css('borderBottomWidth'),10)) ? 0 : parseInt(e.css('borderBottomWidth'),10) );
			        height = e.height() + parseInt(e.css('paddingTop'), 10) + parseInt(e.css('paddingBottom'), 10) + border_top + border_bottom;
			        maxHeight = (height > maxHeight) ? height : maxHeight;
			    });
			}
			for (var i=0; i<=listsize; i++) {
				if(i>=percol2) { percol2+=percol; colnum++; }
				eh = jQuery('> li:eq('+i+')',this);
				eh.addClass('li_col'+ colnum);
				if(jQuery(this).is('ol')){eh.attr('value', ''+(i+settings.startN)) ;}
				if (settings.equalHeight=='li') {
			        border_top = ( isNaN(parseInt(eh.css('borderTopWidth'),10)) ? 0 : parseInt(eh.css('borderTopWidth'),10) );
			        border_bottom = ( isNaN(parseInt(eh.css('borderBottomWidth'),10)) ? 0 : parseInt(eh.css('borderBottomWidth'),10) );
					mh = maxHeight - (parseInt(eh.css('paddingTop'), 10) + parseInt(eh.css('paddingBottom'), 10) + border_top + border_bottom );
			        eh.height(mh);
				}
			}
			jQuery(this).css({cssFloat:'left', width:''+col_Width+'px'});
			for (colnum=2; colnum<=settings.cols; colnum++) {
				if(jQuery(this).is('ol')) {
					jQuery('li.li_col'+ colnum, this).appendTo('#li_container' + y).wrapAll('<ol class="li_cont'+colnum +' ' + classN + '" style="float:left; width: '+col_Width+'px;"></ol>');
				} else {
					jQuery('li.li_col'+ colnum, this).appendTo('#li_container' + y).wrapAll('<ul class="li_cont'+colnum +' ' + classN + '" style="float:left; width: '+col_Width+'px;"></ul>');
				}
			}
			if (settings.equalHeight=='ul' || settings.equalHeight=='ol') {
				var fn = function() {
                    var e = jQuery(this);
                    var border_top = ( isNaN(parseInt(e.css('borderTopWidth'),10)) ? 0 : parseInt(e.css('borderTopWidth'),10) );
                    var border_bottom = ( isNaN(parseInt(e.css('borderBottomWidth'),10)) ? 0 : parseInt(e.css('borderBottomWidth'),10) );
                    height = e.height() + parseInt(e.css('paddingTop'), 10) + parseInt(e.css('paddingBottom'), 10) + border_top + border_bottom;
                    maxHeight = (height > maxHeight) ? height : maxHeight;
                } ;
				for (colnum=1; colnum<=settings.cols; colnum++) {
				    jQuery('#li_container'+ y +' .li_cont'+colnum).each(fn);
				}
				for (colnum=1; colnum<=settings.cols; colnum++) {
					eh = jQuery('#li_container'+ y +' .li_cont'+colnum);
			        border_top = ( isNaN(parseInt(eh.css('borderTopWidth'),10)) ? 0 : parseInt(eh.css('borderTopWidth'),10) );
			        border_bottom = ( isNaN(parseInt(eh.css('borderBottomWidth'),10)) ? 0 : parseInt(eh.css('borderBottomWidth'),10) );
					mh = maxHeight - (parseInt(eh.css('paddingTop'), 10) + parseInt(eh.css('paddingBottom'), 10) + border_top + border_bottom );
			        eh.height(mh);
				}
			}
		    jQuery('#li_container' + y).append('<div style="clear:both; overflow:hidden; height:0px;"></div>');
		});
	}
} ;
 
jQuery.fn.uncolumnlists = function(){
	jQuery('.li_cont1').each(function(i) {
		var j ;
		var onecolSize = jQuery('#li_container' + (++i) + ' .li_cont1 > li').size();
		if(jQuery('#li_container' + i + ' .li_cont1').is('ul')) {
			jQuery('#li_container' + i + ' > ul > li').appendTo('#li_container' + i + ' ul:first');
			for (j=1; j<=onecolSize; j++) {
				jQuery('#li_container' + i + ' ul:first li').removeAttr('class').removeAttr('style');
			}
			jQuery('#li_container' + i + ' ul:first').removeAttr('style').removeClass('li_cont1').insertBefore('#li_container' + i);
		} else {
			jQuery('#li_container' + i + ' > ol > li').appendTo('#li_container' + i + ' ol:first');
			for (j=1; j<=onecolSize; j++) {
				jQuery('#li_container' + i + ' ol:first li').removeAttr('class').removeAttr('style');
			}
			jQuery('#li_container' + i + ' ol:first').removeAttr('style').removeClass('li_cont1').insertBefore('#li_container' + i);
		}
		jQuery('#li_container' + i).remove();
	});
} ;

jQuery(document).ready(function($){
    
    $('ul#twoColList').makeacolumnlists({cols:2,colWidth:0,equalHeight:false,startN:1});

    });

})(jQuery);

function HotDealsTab(country, selector){
	this.show = function(){
		if (jQuery.cookies.get("country")==country){
			jQuery(selector).show();
		}
	};
}

function DestinationsGlobalNav(navHtmlSnippet, selector){
    jQuery(selector + ' > ul.menu').append(navHtmlSnippet);
    jQuery(selector + ' > a > img.arrow').removeClass('invisible');                
    jQuery(selector).removeClass('tabNotReady').each(createMainNavTab);	
}
