﻿var imageSwapperIndex = 1;
var popupWindow = '';


$(document).ready(function () {

    $("form").keypress(function (e) {
        if (e.which == 13) {
            return false;
        }
    });

    $(window).resize(function () {
        var innerWidth = $(".innerGlobalContainer").width();
        var windowWidth = $(window).width();
        var imageOffset = ($(".watermarkContainer").width() / 2) + 30;

        $(".watermarkContainer").css("left", (((windowWidth - innerWidth) / 2) + innerWidth - imageOffset) + "px");
    });

    $(window).resize();

    $(".imageSwapper").each(function () {
        var currentImage = this;
        setInterval(function () { swapImages(currentImage) }, 10000);
    });

    $(".animatedExpandTrigger").live('click', function () {
        var targetContainer = $("#" + $(this).attr("rel"));

        if (targetContainer.is(":hidden")) {
            targetContainer.parents(".jstHiddenTab").show();
            $(this).siblings("span").children("input:checkbox").attr('checked', 'checked');
        }
        else
            $(this).siblings("span").children("input:checkbox").attr('checked', '');

        targetContainer.animate({ height: 'toggle' }, 200, function () { if (targetContainer.is(":hidden")) { targetContainer.parents(".jstHiddenTab").hide(); } });

        return false;
    });
    $(".animatedExpandTrigger").each(function () {
        if (!$(this).siblings("span").children("input:checkbox").attr('checked'))
            $("#" + $(this).attr("rel")).hide();
    });

    $(".jstValidate").click(function () {
        var valid = true;
        $(".jstRequiredField").each(function () { if ($(this).val() == "") valid = false; });

        if (!valid) {
            alert("Please make sure you fill in all mandatory fields before submitting.");
            return false;
        }
    });

    $(".cbExpandTrigger input").click(function () {
        $(this).parent().siblings("a").click();
    });

    $(".jstTimedHide:visible").each(function () {
        $(".jstTimedHide").hide();
        setTimeout(function () { $(".jstTimedHide").animate({ opacity: 'toggle' }, 400); }, 1000);
        setTimeout(function () { $(".jstTimedHide").animate({ opacity: 'toggle' }, 400); }, 7000);
    });

    $(".jstHiddenTab").each(function () {
        $(this).hide();
    });

    $(".jstTabTrigger").live("click", function () {
        var targetRel = $(this).attr("rel") != "" ? $(this).attr("rel") : $(this).attr("id");

        $(".active .jstTabTrigger").parent().removeClass("active");
        $(".jstTabTrigger[rel=" + targetRel + "]").parent().addClass("active");

        var parentListItem = $(this).parent();
        parentListItem.siblings().removeClass("active");
        parentListItem.addClass("active");

        var targetTab = $("#" + $(this).attr("rel"));
        targetTab.siblings(".jstTab").hide();
        targetTab.show();

        return false;
    });

    $(".tourListing ul").each(function () {
        $(this).children("li").not(".active").children("ul").hide();
    });

    $(".jstExpandTrigger").click(function () {
        var parentListItem = $(this).parent();
        parentListItem.siblings().each(function () {
            $(this).removeClass("active");
            $(this).children("ul").animate({ height: 'hide' }, 200);
        });

        parentListItem.addClass("active");
        window.location.hash = $(this).attr("rel");

        var targetList = $("#" + $(this).attr("rel"));
        targetList.animate({ height: 'show' }, 200);

        return false;
    });

    $(".jstExpandTriggerHover").hover(function () {
        var target = $("#" + $(this).attr("rel"));
        target.animate({ opacity: 'show' }, 200);

        return false;
    }, function () {
        var target = $("#" + $(this).attr("rel"));
        target.animate({ opacity: 'hide' }, 200);

        return false;
    });

    $(".tourListing ul li ul li a").hover(function () {
        // in
        var overlay = $(this).parent().find(".tourListingOverlay");
        overlay.css("opacity", "0.9");
        overlay.show();
    }, function () {
        // out
        var overlay = $(this).parent().find(".tourListingOverlay");
        overlay.hide();
    });

    $(".smallOverlay").hide();

    $(".jstCustomPopup01").click(function () {
        if ($(".jstCustomPopupValidate input").length <= 0 || $(".jstCustomPopupValidate input").is(':checked')) {

            window.onbeforeunload = function (evt) {
                return "If you close this page your booking will not be complete and any payment made will not be associated with a tour! Please only close this window once you have completed your payment and viewed the booking confirmation!";
            }

            popupWindow = window.open($(this).attr("href"), "paymentWindow", "height = 600, width = 800, resizable = 1, scrolling = 1, menubar=no, toolbar=no, location=no");
            if (window.focus) { popupWindow.focus() }

            var failedContainerName = $(this).attr("rel");
            var linkContainer = $(this);

            $(this).parents(".paymentArea").css({ opacity: 0.3 });
            $(this).parents(".paymentArea").siblings(".smallOverlay").show();

            var interval = setInterval(function () {

                $.ajax({
                    url: '/uk/PaymentSuccess.aspx',
                    cache: false,
                    success: function (data) {
                        if (data.toString().length > 0 && data.toString() != "Started") {
                            clearInterval(interval);

                            if (data.toString() == "Failed") {
                                linkContainer.parents(".paymentArea").css({ opacity: 1 });
                                linkContainer.parents(".paymentArea").siblings(".smallOverlay").hide();
                                $("#" + failedContainerName).show();
                            }
                            else {
                                window.onbeforeunload = function (evt) { };
                                if (typeof (isBookingPage) !== 'undefined')
                                    WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('ctl00$ctl00$PrimaryContent$PrimaryContent$TourWizard$NextStepButton', '', true, '', '', false, true))
                                else
                                    __doPostBack('__Page', 'RefreshPostback');
                            }
                        }
                        else {
                            if (popupWindow.closed) {
                                window.onbeforeunload = function (evt) { };
                                linkContainer.parents(".paymentArea").css({ opacity: 1 });
                                linkContainer.parents(".paymentArea").siblings(".smallOverlay").hide();
                                $("#" + failedContainerName).show();
                                clearInterval(interval);
                            }
                        }
                    }
                });

            }, 500);
        }
        else
            alert("Please read and accept the booking conditions by clicking on the checkbox.");

        return false;
    });


    $(".expandableTab a").click(function () {
        var targetContainer = $("#" + $(this).attr("rel"));

        if ($(this).hasClass("expanded")) {
            $(this).removeClass("expanded");
            targetContainer.hide();
        }
        else {
            $(this).addClass("expanded");
            targetContainer.show();
        }

        return false;
    });

    $(".datepicker").datepicker({ dateFormat: 'dd-mm-yy' });

    $(".jstResultsClose").live("click", hideTourResults);


    $(".errorMessage").each(function () {
        //$(this).attr("style", "");
    });

    $(".jstTab").hide();
    $(".jstTabTrigger").each(function () {
        var target = $("#" + $(this).attr("rel"));
        if ($(this).parent().is(".active")) {
            target.show();
        }
    });

    if (window && window.location && window.location.hash != '') {
        $(".jstTabTrigger[rel='jst" + window.location.hash.replace("#", "") + "']").click();
    }

    if (querySt("tab") != '') {
        $(".jstTabTrigger[rel='jst" + querySt("tab") + "']").click();
    }
});

function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

function swapImages(currentImage)
{
    var currentUrl = $(currentImage).attr("src");
    var newImage;
    
    if ($(".imageSwapper[src=" + currentUrl.replace("01", pad(imageSwapperIndex+1, 2)) + "]").size() > 0)
    {
        newImage = $(".imageSwapper[src=" + currentUrl.replace("01", pad(imageSwapperIndex, 2)) + "]");
        // if the image already exists then show it.
        newImage.siblings().not(":hidden").animate({ opacity: 0 }, 1000);
        newImage.animate({ opacity: 1 }, 1000);
    }
    else
    {
        newImage = $(currentImage).clone();
        newImage.attr("src", newImage.attr("src").replace("01", pad(imageSwapperIndex+1, 2)));
        newImage.css({ position: "absolute", left: "0px", top: "0px", opacity: 0 });
        
        $(currentImage).parent().append(newImage);

        newImage.error(function(e) {
            if (imageSwapperIndex > 1) {
                imageSwapperIndex = 0;
                return swapImages(currentImage);
            }
            else return;
        });
        
        newImage.load(function() {
            newImage.siblings().not(":hidden").animate({ opacity: 0 }, 1000);
            newImage.animate({ opacity: 1 }, 1000);
        });
    }
    
    imageSwapperIndex++;
}

function showTourResults()
{
    $(".tourResultsOverlay .contents").animate({ height: 'show' }, 200);
    $(".tourResultsOverlay").parent().parent().siblings().each(function() {
        $(this).animate({opacity: 0.3}, 200);
    });
    $(".mainContentContainer").animate({opacity: 0.3}, 400);
}

function hideTourResults()
{
    $(".tourResultsOverlay .contents").animate({ height: 'hide' }, 200);
    $(".tourResultsOverlay").parent().parent().siblings().each(function() {
        $(this).animate({opacity: 1}, 200);
    });
    $(".mainContentContainer").animate({opacity: 1}, 400);
    
    return false;
}

function pad(number, length) {
   
    var str = '' + number;
    
    while (str.length < length) {
        str = '0' + str;
    }
   
    return str;
}
