// ############ Shop Carousel ########################
$(function() {
	var theCarousel = $(".shopCarousel"); 
	
	if (theCarousel.length > 0){
		theCarousel.jCarouselLite({
			speed: 500,
			visible: 1,
			initCallback: function(carousel) {
				var interval = null;
				function stopAuto() {
					if (interval) {
						clearInterval(interval);
						interval = null;
					}
				};
				function startAuto() {
					stopAuto();
					interval = setInterval(function() { carousel.next() }, 6500);
				};
				theCarousel.hover(function() {
			        //stopAuto();
			    }, function() {
			    	//startAuto();
			    });
				$('.next', theCarousel).click(function () {
					//stopAuto();
					carousel.next();
					//startAuto();
				});
				$('.prev', theCarousel).click(function () {
					//stopAuto();
					carousel.prev();
					//startAuto();
				});
				//startAuto();
			}
		});
		
	}
});

//############ Breadcrumb Resizer ########################
$(function(){
	
	var theBreadcrumbHeight = $('#breadcrumbWrap').height();
	var theBreadcrumbLink = $('#breadcrumb li a');
	var subBreadcrumbLinks = $('#breadcrumb li ul li a');

	if (theBreadcrumbHeight > 40 && theBreadcrumbHeight < 75){
		theBreadcrumbLink.css("font-size", ".7em").css( "margin-top", "3px");
		subBreadcrumbLinks.css("font-size", "1em");
		return true;
	}
	
	if (theBreadcrumbHeight > 75){
		theBreadcrumbLink.css("font-size", ".55em").css( "padding-right", "2px").css( "padding-left", "2px").css( "margin-top", "3px");
		subBreadcrumbLinks.css("font-size", "1em");
		return true;
	}
});

// ############ Destinations List ########################
$(function(){
	
	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 ########################
$(function(){
	if ($("#list1a").length > 0){
	jQuery('#list1a').accordion({header: ".head",animated: "bounceslide", autoheight: false/*,active: ".current"*/});
	}
});

// ############ BUTTON HOVER ########################
$(function(){
	$('button').hover(function() {
	  $(this).addClass('ieHover');
	}, function() {
	  $(this).removeClass('ieHover');
	});
	$('a.submitBtnShort').hover(function() {
	  $(this).addClass('ieHover');
	}, function() {
	  $(this).removeClass('ieHover');
	});
});

// ############ New Window ########################
$(function(){
    $('a[rel="external"]').click(function(){
        window.open(this.href);
        return false;
    });
});

// ############ MAP SLIDER ########################
$(function() {
	$('a.toggleMap').click(function(){	 
		var theClass = $(this).attr('class');		  
			if( theClass.indexOf('closed') != -1) {
			   $('a.toggleMap').removeClass('closed');
				 $('a.toggleMap').addClass('open');
				 $('a.pin').html("Close map");
			}
			else {
			   $('a.toggleMap').removeClass('open');
				 $('a.toggleMap').addClass('closed');
				 $('a.pin').html("Show on map");
			}
  		$('#map').slideToggle();
      
    	return false;
	 });
 });

 $(function(){
		$(".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 ########################
$(function(){
		$(".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 ########################
 $(function(){
	$(".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;
        });
		});



// ############ GOOGLE ADSENSE ########################
function google_ad_request_done(google_ads) {
var s = '';
var i;
if (google_ads.length == 0) {
return;
}

if (google_ads.length == 1) {
	s += '<h2><a rel="external" href=\"' + google_info.feedback_url + '\">Ads by Google</a></h2>'
	s += '<div><p class="title"><a rel="external" href="' +
	google_ads[0].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' +
	google_ads[0].visible_url + '\';return true"> ' +
	google_ads[0].line1 + '</a></p> <p>' +
	google_ads[0].line2 + '' +
	google_ads[0].line3 + '</p> <p><a rel="external"  href="' +
	google_ads[0].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' +
	google_ads[0].visible_url + '\';return true">' +
	google_ads[0].visible_url + '</a></p></div>';

} else if (google_ads.length > 1) {
	s += '<h2><a rel="external" href=\"' + google_info.feedback_url + '\">Ads by Google</a></h2>'
	for(i = 0; i < google_ads.length; ++i) {
		
		if (i == 2){
			 s += '<div class="clearAd"></div>'
			}
		s += '<div><p class="title"><a rel="external" href="' +
		google_ads[i].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' +
		google_ads[i].visible_url + '\';return true"> ' +
		google_ads[i].line1 + '</a></p> <p>' +
		google_ads[i].line2 + '' +
		google_ads[i].line3 + '</p> <p><a rel="external" href="' +
		google_ads[i].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' +
		google_ads[i].visible_url + '\';return true">' +
		google_ads[i].visible_url + '</a></p></div>';
	}
}
    
    document.write(s);
    return;
  }



// ############ 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:new Array('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();
    }

}

// ############ PROFILES - HOBBIES AUTOSUGGEST ########################
$(function(){
    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 ########################
$(function(){
    if ($("#j_username").length>0) {
        $("#j_username").focus();
    }
});


// ############ BROWSER DETECTION ########################
function isMacOSXFireFox() {
	  var userAgent = navigator.userAgent.toLowerCase();
	  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
	    return true;
	  }
	  return false;
}
function isIE6() {
	if (typeof document.body.style.maxHeight === "undefined") {
		return true;
	}
	return false;
}

