﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Tui.Web");

Tui.Web.DreamVacation = function() 
{
    Tui.Web.DreamVacation.initializeBase(this);

    this._$dialog = null;
    this._$items = null;
    this._$itemLast = null;
    this._$itemNext = null;
    this._isAnimating = false;
    this._innerDialog = null;
    this._overlayElement = null;
}

Tui.Web.DreamVacation.prototype =
{
    initialize: function() {
        Tui.Web.DreamVacation.callBaseMethod(this, 'initialize');

        this._$dialog = $(this.get_innerDialog().get_element());
        this._$dialog.click(Type.createDelegate(this, this.rootClick));
        this._$items = this._$dialog.find('.item');

        if (this.getQueryStringParameter("thailandcampaign") != "") {
            $("a.link-item, .show-details").click(function() {
                if ($(this).hasClass("show-details")) {
                    var $link = $(this).parent().children().filter("a");
                    if ($link.hasClass("site-link")) {
                        if ($link.attr("target") == "_blank") {
                            window.open($link.attr("href"));
                        }
                        else {
                            window.location.href = $link.attr("href");
                        }
                        return;
                    }
                }

                openSubPage(this, "", "");

            });
        }

        if (this.getQueryStringParameter("view") != "") {
            $("#campaign-start-page, #celebrity-banner, #tips-banner, #top-orkide, #bottom-orkide").hide();
            this.showOverlay();
            this.showDialog();            
            this.attachCloseHandlers();
            openSubPage(this, this.getQueryStringParameter("thailandcampaign"), this.getQueryStringParameter("view"));
            return;
        }


        var hasQuery = (this.getQueryStringParameter("showCampaignPage") != "" || this.getQueryStringParameter("thailandcampaign") != "") ? true : false;

        if (hasQuery || $.cookie("CampaignHasBeenShown") == null) {
            //$("#search-form").hide();
            this.showOverlay();
            this.showDialog();
            this.attachCloseHandlers();
            $.cookie("CampaignHasBeenShown", "true");
        }
    },

    getQueryStringParameter: function(name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.href);
        if (results == null) {
            return "";
        }
        else {
            return results[1];
        }
    },
    rootClick: function(e) {
        var $target = $(e.target);

        if (this._isAnimating) {
            return;
        }

        if ($target.hasClass('show-details')) {
            this.showNextDetails($target.closest('.item'));
        }
        else if ($target.hasClass('hide-details')) {
            this.fadeOutLast();
        }
        else if ($target.hasClass('close-details')) {
            this.fadeOutLast();
        }
    },
    showOverlay: function() {
        $(this.get_overlayElement()).css('height', $(document.body).height() + 300).show();
    },
    showDialog: function() {
        this.get_innerDialog().show();
    },
    attachCloseHandlers: function() {
        this._$dialog.find('.dialog-top-close-link, .close-message a').click(Function.createDelegate(this, function(e) {
            Tui.Web.DreamVacation.showProblematicBookingPanelElements();
            $(this.get_overlayElement()).remove();
        }));

        /*  $(this.get_overlayElement()).click(Type.createDelegate(this, function(e) {
        if (e.target == this.get_overlayElement()) {
        $("#search-form").show();
        Tui.Web.DreamVacation.showProblematicBookingPanelElements();
        this.get_innerDialog().hide();
        $(this.get_overlayElement()).remove();
        }
        }));
        */
    },
    showNextDetails: function($itemNext) {
        this._$itemNext = $itemNext;

        if (this._$itemLast != null) {
            this.fadeOutLast();
        }
        else {
            this.fadeInNext();
        }
    },
    fadeOutLast: function() {
        this._isAnimating = true;
        this._$itemLast.find('a.show-details, a.hide-details').toggle();
        this._$itemLast.find('img.hide-details').removeClass('hide-details').addClass('show-details');
        this._$itemLast.nextAll('.details-container:first').children('.details').fadeOut('normal', Type.createDelegate(this, this.fadeOutLastCallback));
    },
    fadeOutLastCallback: function() {
        this._$itemLast.nextAll('.details-container:first').empty();
        this._$itemLast = null;

        if (this._$itemNext != null) {
            this.fadeInNext();
        }
        else {
            this._isAnimating = false;
        }
    },
    fadeInNext: function() {
        this._isAnimating = true;

        var 
            $itemDetails = this._$itemNext.children('.details'),
            $itemDetailsClone = $itemDetails.clone(),
            $itemDetailsContainer = this._$itemNext.nextAll('.details-container:first');

        var cssClass = ['details-left', 'details-center', 'details-right'][this._$items.index(this._$itemNext) % 3];

        this._$itemNext.find('a.show-details, a.hide-details').toggle();
        this._$itemNext.find('img.show-details').removeClass('show-details').addClass('hide-details');
        $itemDetailsClone.addClass(cssClass);
        $itemDetailsClone.appendTo($itemDetailsContainer);
        $itemDetailsClone.fadeIn('normal', Type.createDelegate(this, this.fadeInNextCallback));
    },
    fadeInNextCallback: function() {
        this._$itemLast = this._$itemNext;
        this._$itemNext = null;

        this._isAnimating = false;
    },
    get_innerDialog: function() { return this._innerDialog; },
    set_innerDialog: function(value) { this._innerDialog = value; },
    get_overlayElement: function() { return this._overlayElement; },
    set_overlayElement: function(value) { this._overlayElement = value; }
}
Tui.Web.DreamVacation.registerClass('Tui.Web.DreamVacation', Sys.Component);
Tui.Web.DreamVacation.hideProblematicBookingPanelElements = function()
{
    if ($.browser.mozilla)
    {
        $('.bp-content:first').find('.list-box span, .list-box-large span').hide();
    }
}
Tui.Web.DreamVacation.showProblematicBookingPanelElements = function()
{            
    if ($.browser.mozilla)
    {
        $('.bp-content:first').find('.list-box span, .list-box-large span').show();
    }
}

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

function setPageNumber(a) {

    var 
        selected = "/includes/Images/BlueSites/ThailandCampaign/page-selected.png",
        notSelected = "/includes/Images/BlueSites/ThailandCampaign/page-not-selected.png",
        currentItemClass = $(a).attr("class"),
        parentId = "#" + $(a).parents().filter("div.sub-pages").attr("id"),
        layoutId = "#" + $(a).parents().filter("div.sub-pages").parent().parent().attr("id"),
        firstPage = parentId + " .first-page",
        secondPage = parentId + " .second-page",
        thirdPage = parentId + " .third-page",
        fourthPage = parentId + " .fourth-page",
        fifthPage = parentId + " .fifth-page";
    switch (currentItemClass) {
        case "list-item-0":
        case "list-item-10":
            $(firstPage).attr("src", selected);
            $(secondPage).attr("src", notSelected);
            $(thirdPage).attr("src", notSelected);
            $(fourthPage).attr("src", notSelected);
            $(fifthPage).attr("src", notSelected);
            break;
        case "list-item-2":
        case "list-item-3":
        case "list-item-16":
            $(firstPage).attr("src", notSelected);
            $(secondPage).attr("src", selected);
            $(thirdPage).attr("src", notSelected);
            $(fourthPage).attr("src", notSelected);
            $(fifthPage).attr("src", notSelected);
            break;        
        case "list-item-4":
        case "list-item-6":
        case "list-item-22":
            if (currentItemClass == "list-item-6" && layoutId == "#layout-2") {
                $(firstPage).attr("src", notSelected);
                $(secondPage).attr("src", notSelected);
                $(thirdPage).attr("src", notSelected);
                $(fourthPage).attr("src", selected);
                $(fifthPage).attr("src", notSelected);
                break;
            }
            else {
                $(firstPage).attr("src", notSelected);
                $(secondPage).attr("src", notSelected);
                $(thirdPage).attr("src", selected);
                $(fourthPage).attr("src", notSelected);
                $(fifthPage).attr("src", notSelected);
            }
            break;    
        case "list-item-8":
            $(firstPage).attr("src", notSelected);
            $(secondPage).attr("src", notSelected);
            $(thirdPage).attr("src", notSelected);
            $(fourthPage).attr("src", notSelected);
            $(fifthPage).attr("src", selected);
            break;    
    }
}
function fadeOutOrHide(item) {
    if (jQuery.support.opacity) {
        $(item).fadeOut("slow");
    }
    else {
        $(item).hide();
    }
}
function fadeInOrShow(item) {
    if (jQuery.support.opacity) {
        $(item).fadeIn("slow");
    }
    else {
        $(item).show();
    }
}
function updateOverlayHeight() {
    var newHeight = $("#sub-pages-container").height() + 300;
    if (newHeight < 1700) {
        newHeight = 1700;
    }
    $(".dream-vacation-overlay").css('height', newHeight).show();
}


function openSubPage(thisRef, episerverId, layoutView) {
    var epiId = episerverId == "" ? $(thisRef).parents().filter("div.item").attr("id").replace("epiid-", "") : episerverId;
    fadeOutOrHide("#top-orkide, #bottom-orkide");
    fadeOutOrHide(".campaign-start-page");                
    var layout = layoutView == "" ? $(thisRef).parents().children().filter(".hidden-layout").val() : layoutView;
    $("#sub-pages-container").load(String.format("/BlueSites/Templates/Units/DreamVacationDialog/ThailandSubPage.aspx?thailandcampaign={0}&view={1}", epiId, layout), function() {                    
        fadeInOrShow("#sub-pages-container");
        $.getScript("/includes/Js/jQuery/imageViewerJqueryPlugins.js", function() {
            $(function() {
                $("a.show-prev").addClass("disabled");
                $(".image-gallery").jCarouselLite({
                    btnNext: ".next-img",
                    btnPrev: ".prev-img",
                    visible: 1,
                    afterEnd: function(a) {
                        var alt = $(a[0]).children().filter("img").attr("alt").split(" | ");
                        $("#image-description h3").text(alt[0]);
                        $("#image-description p").text(alt[1]);
                    },
                    speed: 800
                });
                $("#subpage-0 .list").jCarouselLite({
                    btnNext: "#subpage-0 .show-next",
                    btnPrev: "#subpage-0 .show-prev",
                    btnGo: ["#subpage-0 .first-page", "#subpage-0 .second-page", "#subpage-0 .third-page", "#subpage-0 .fourth-page", "#subpage-0 .fifth-page"],
                    afterEnd: function(a) {
                        setPageNumber(a);
                    },
                    visible: 1,
                    circular: false,
                    speed: 800
                });
                $("#subpage-1 .list").jCarouselLite({
                    btnNext: "#subpage-1 .show-next",
                    btnPrev: "#subpage-1 .show-prev",
                    btnGo: ["#subpage-1 .first-page", "#subpage-1 .second-page", "#subpage-1 .third-page", "#subpage-1 .fourth-page", "#subpage-1 .fifth-page"],
                    afterEnd: function(a) {
                        setPageNumber(a);
                    },
                    visible: 1,
                    circular: false,
                    speed: 800
                });
                $("#subpage-2 .list").jCarouselLite({
                    btnNext: "#subpage-2 .show-next",
                    btnPrev: "#subpage-2 .show-prev",
                    btnGo: ["#subpage-2 .first-page", "#subpage-2 .second-page", "#subpage-2 .third-page", "#subpage-2 .fourth-page", "#subpage-2 .fifth-page"],
                    afterEnd: function(a) {
                        setPageNumber(a);
                    },
                    visible: 1,
                    circular: false,
                    speed: 800
                });
                $("#subpage-3 .list").jCarouselLite({
                    btnNext: "#subpage-3 .show-next",
                    btnPrev: "#subpage-3 .show-prev",
                    btnGo: ["#subpage-3 .first-page", "#subpage-3 .second-page", "#subpage-3 .third-page", "#subpage-3 .fourth-page", "#subpage-3 .fifth-page"],
                    afterEnd: function(a) {
                        setPageNumber(a);
                    },
                    visible: 1,
                    circular: false,
                    speed: 800
                });
                updateOverlayHeight();
            });
        });
    });

}
