﻿/*
//Common Javascript Costa Crociere 
//In this JavaScript is checked:
//- the height of Hot Deals
//- Tab in ExpressBooking HomePage
//- Submenu
//- Faqs Carousel HP
//- Toggle Footer
//- Tooltip
//- New Search
//- Show Hide Description Detail Cruice
//- Tab Description Detail Cruice
//- Utility Layer (resize-center-scroll)
//- Layer
//- Clear Input
//- Show Cabin Details
//- Tab Layer
//- Change Country
*/

var CostaUi = {};

//Hot Deal Height//
CostaUi.setMaxHotDealHeight = function ()
{
    var maxHeight = 0;
    var maxImgHeight = 0;
    var $hotDealBoxes = $("div.hot-deal");
    for (var i = 0; i < $hotDealBoxes.length; i++)
    {
        var boxHeight = parseInt($($hotDealBoxes[i]).outerHeight(), 10);
        maxHeight = Math.max(maxHeight, boxHeight);
        var imgHeight = parseInt($($hotDealBoxes[i]).find("img.promo").height(), 10);
        maxImgHeight = Math.max(maxImgHeight, imgHeight);
    }

    $("div.hot-deal").css("height", (maxHeight + maxImgHeight) + "px");
    $("div.hot-deal img.promo").css({ bottom: "0px", left: "0px" });
}

CostaUi.setMaxElentHeight = function ()
{
    var maxHeight;
    var elementList = $(".row");
    $(elementList).each(function ()
    {
        maxHeight = 0;
        $this = $(this).children("div");

        for (var i = 0; i < $this.length; i++)
        {
            if ($(this).is(".noSetMaxElentHeight"))
            {
                return;
            }
            var boxHeight = parseInt($($this[i]).outerHeight(), 10);
            maxHeight = Math.max(maxHeight, boxHeight);
        }
        for (var i = 0; i < $this.length; i++)
        {
            var boxPadding = parseInt($($this[i]).css("padding-top"), 10) + parseInt($($this[i]).css("padding-bottom"), 10);
            $($this[i]).css("height", (maxHeight - boxPadding) + "px");
        }
    });
    
}


//Tab Reservation Home//
CostaUi.controllTabExpressBooking = function ()
{
    $("div.express-booking div").first().show();
    $("div.express-booking ul li:first").addClass("on");
    $("div.express-booking ul li a").click(function ()
    {

        var nameSplit = $(this).parent().attr("class").split(" on");
        var nameLink = nameSplit[0];
        if ($("div.express-booking div." + nameLink + "").css("display") == "block")
            return;
        $("div.express-booking div").hide();
        $("div.express-booking div." + nameLink).show();
        $("div.express-booking ul li").removeClass("on");
        $(this).addClass("on");
        $(this).parent().addClass("on");
        $(".advanced-search").hide();
        $(".findPrenotationNumber").hide();
        $(".prenotationNumber").show();
        $("a.findPrenotationNumber").show();
        $("a.advanced-search").show();
		
		// verifica espansione campi hidden di ricerca
		if ($(".showAllFields").attr("class") == "showAllFields on")
		{
		    $("#advanced-search-ul-port").show();
		    $("#advanced-search-ul-ship").show();
		}
		
        return;
    });

    $("a.advanced-search").click(function ()
    {
        $(".advanced-search").show();
        $(this).hide();
    });

    $("a.findPrenotationNumber").click(function ()
    {
        $(this).parent().parent().find(".prenotationNumber").hide();
        $("a.prenotationNumber").show();
        $(".ctn-input-data").show();
        $(".findPrenotationNumber").show();

    });

    $("a.prenotationNumber").click(function ()
    {
        $(this).parent().parent().find(".findPrenotationNumber").hide();
        $("a.findPrenotationNumber").show();
        $(".prenotationNumber").show();

    })
}

//SubMenu Position//
CostaUi.subMenuPosition = function ()
{
    var leftPosition = 5;
    var $subMenu = $(".main-menu .ctn-subMenu");
    for (var i = 0; i < $subMenu.length; i++)
    {
        $subMenu.eq(i).find("ul").css({ left: leftPosition + "px" });
        leftPosition += parseInt($subMenu.eq(i).width(), 10) + parseInt($subMenu.eq(i).css("padding-left"), 10) + parseInt($subMenu.eq(i).css("padding-right"), 10);
    }
}

//SubMenu Controll//
var t = null;

CostaUi.subMenu = function ()
{
    $("ul.main-menu li.ctn-subMenu").mouseover(function ()
    {
        if (t != null)
        {
            window.clearTimeout(t);
            t = null;
        }
        CostaUi.openSubMenu($(this));
    });

    $("ul.main-menu li.ctn-subMenu").mouseout(function ()
    {
        if (t != null)
        {
            window.clearTimeout(t);
            t = null;
        }
        var $this = $(this);
        t = window.setTimeout(function () { CostaUi.closeSubMenu($this); }, 300);
    });
}

    //open
    CostaUi.openSubMenu = function ($this)
    {
        if ($this.find("ul").css("display") == "block")
        return;
        $("ul.main-menu li ul").hide();
        $("ul.main-menu li.on").removeClass("on");
        $this.find("ul").show();
        $this.addClass("on");
    }

    //close
    CostaUi.closeSubMenu = function ($this)
    {
        $("ul.main-menu li ul").hide();
        $("ul.main-menu li").removeClass("on");
    }

//Faqs Carousel HP//
    CostaUi.moveLeft = function ()
    {
        var widhtAnimation = $("div.carousel ul li").width();
        $("div.carousel ul").animate({
            left: "-" + widhtAnimation + "px"
        }, 500, CostaUi.removeAddLeft);
    }

CostaUi.moveRight = function ()
{
    var widhtAnimation = $("div.carousel ul li").width();
    $("div.carousel ul").animate({
        left: widhtAnimation + "px"
    }, 500, CostaUi.removeAddRight);
}

CostaUi.removeAddLeft = function ()
{
    var firstElemt = $("div.carousel ul li:eq(0)");
    $(firstElemt).remove();
    $("div.carousel ul").css("left", "0px");
    $("div.carousel ul").append(firstElemt);
}

CostaUi.removeAddRight = function()
{
    var thirdElement = $("div.carousel ul li:last");
    $(thirdElement).remove();
    $("div.carousel ul").css("left", "0px");
    $("div.carousel ul").prepend(thirdElement);
}

CostaUi.carousel = function()
{
    $(".arrow-sx a").click(function ()
    {
        CostaUi.moveLeft();
    });
    $(".arrow-dx a").click(function ()
    {
        CostaUi.moveRight();
    });
}

// Toggle Footer //

CostaUi.toggleFooter = function ()
{
    $(".menu-footer a").attr("rel", "elementToggle").click(function ()
    {
        $(".menu-footer li a").removeClass("on");
        var elementToToggle = $(this).attr("class");
        var element = $("ul." + elementToToggle);
        if (element.css("display") == "block")
        {
            element.slideUp();
            return;
        }
        $("ul.ctn-idden-useful-links").slideUp();
        $(this).addClass("on");
        element.slideDown();

    })

}

// Tooltip //
CostaUi.tooltip = function (target_items, name)
{
    $(target_items).each(function (i)
    {
        $("body").append("<div class='" + name + "' id='" + name + i + "'><p>" + $(this).attr('title') + "</p></div>");
        var my_tooltip = $("#" + name + i);

        $(this).removeAttr("title").mouseenter(function ()
        {
            my_tooltip.css({ opacity: 0.9, display: "none" }).fadeIn(200);
        }).mousemove(function (kmouse)
        {
            my_tooltip.css({ left: kmouse.pageX + 15, top: kmouse.pageY - 50 });
        }).mouseleave(function ()
        {
            my_tooltip.fadeOut(200);
        });
    });
}

// New Search //
CostaUi.newSearch = function ()
{
    $("p.new-search a").click(function ()
    {
        $(this).parent().hide();
        $("span.new-search").fadeIn();
    })
}


// Show Hide Description Detail Cruice //

CostaUi.showDetail = function ()
{
    $(".activateShowHide").click(function ()
    {
        $(".description").hide();
        $(this).parent().parent().next(".description").show();
    });

    $(".showDetail").click(function ()
    {
        $(".service .arrow").removeClass("open");
        $(".service .showDetail").show();
        $(this).parent().find("a.showDetail").hide();
        $(".service .description").hide();
        $(this).parent().find("a.arrow").show();
        $(this).parent().find(".description").show();
        $(this).parent().find("a.arrow").addClass("open");
		
		var elementClicked = $(this);
        var destination = $(elementClicked).offset().top;
        $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, 600 );
    });
}

// Tab Description Detail Cruice //

CostaUi.tabDetail = function ()
{
    $(".tab a").click(function ()
    {
        var nameTab = $(this).attr("class");
        $(".service .showDetail").show();
        $(".service .arrow").removeClass("open");
        $(".description").hide();
        $(".ctn-tab").children("div").hide();
        $(".ctn-tab div." + nameTab).show();
        $(".tab li").removeClass();
        $(this).parent().addClass("on");

    })
}

CostaUi.tabDetailSelectFirst = function ()
{
    $(".ctn-tab").children("div").hide();
    $(".ctn-tab").children("div:first").show();
    $(".ctn-tab div.tab1").show();
    $(".tab li:first").addClass("on");
    
}

// Utility Layer //
function center(elem)
{
    var hBody = $(window).height();
    var wBody = $(window).width();
    var hZoom = $(elem).height();
    var wZoom = $(elem).width();
    var h = ((hBody - hZoom) / 2);
    var w = (wBody - wZoom) / 2;
    if (h <= 0) h = 24;
    if (w <= 0) w = 0;
    $(elem).css("top", h).css("left", w);

}

$(window).resize(function ()
{

    var overlay = $("#mask").css("display");
    if (overlay == 'block')
    {
        $("#mask").fadeTo(0, 0.4).height($(window).height() + $(window).scrollTop());

        var activLayer = $("div.layer");
        
        for (i = 0; i < activLayer.length; i++)
        {
            if (activLayer.eq(i).css("display") == 'block')
            {
                var nameLayerCenter = activLayer.eq(i).attr("class").split(" ")[1];
                center("div." + nameLayerCenter);
            }
        }
    }

    return;
});


$(window).scroll(function ()
{
    var overlay = $("#mask").css("display");
    if (overlay == 'block')
    {
        $("#mask").fadeTo(0, 0.4).height($(window).height() + $(window).scrollTop());

        var activLayer = $("div.layer");
        
        for (i = 0; i < activLayer.length; i++)
        {
            if (activLayer.eq(i).css("display") == 'block')
            {
                var nameLayerCenter = activLayer.eq(i).attr("class").split(" ")[1];
                center("div." + nameLayerCenter);
            }
        }
    }

    return;
});

// Layer Controll //

CostaUi.openLayer = function ()
{

    $(".open-layer").click(function ()
    {
        var layerNumber = $(this).attr("rel");

        // gestione IE7 problema attributo position-relative 
        if($.browser.msie && $.browser.version.slice(0,3) == "7.0" ) {
            $("#containerID").removeClass("contenitore");
            $("#containerID").addClass("contenitore-IE7");  

            $("#wrapper").css("position","static");
        }
        
        $("#mask").fadeTo(0, 0.4);
        $("."+layerNumber+"").fadeIn();
        center("."+layerNumber+"");
        $(window).resize();
        return false;
    });
}


CostaUi.openLayerExec = function (layerNumber)
{
        $("#mask").fadeTo(0, 0.4);
        $("."+layerNumber+"").fadeIn();
        center("."+layerNumber+"");
        $(window).resize();
}

CostaUi.openLayerCruiseDetail = function (layerNumber,clientIdPnl)
{
        $("#"+clientIdPnl+"").show();
        $("#"+clientIdPnl+"").parent().find(".submitButton").hide();
        $("#mask").fadeTo(0, 0.4);
        $("."+layerNumber+"").fadeIn();
        center("."+layerNumber+"");
        $(window).resize();
        
        
        
}

CostaUi.closeLayer = function ()
{   
    $("#mask").click(function ()
    {   
        $(this).fadeOut();
        $(".layer").fadeOut();
        
       // gestione IE7 problema attributo position-relative 
        if($.browser.msie && $.browser.version.slice(0,3) == "7.0" ) {
            $("#containerID").addClass("contenitore");
            $("#containerID").removeClass("contenitore-IE7");
            
            $("#wrapper").css("position","relative"); 
        }
        
        return false;
    });

    $(".closeLayer").click(function ()
    {
         // gestione IE7 problema attributo position-relative 
        if($.browser.msie && $.browser.version.slice(0,3) == "7.0" ) {
            $("#containerID").addClass("contenitore");
            $("#containerID").removeClass("contenitore-IE7");
            
            $("#wrapper").css("position","relative"); 
        }
    
        $("#mask").fadeOut();
        $(".layer").fadeOut();
        $(".layer .ctn-gallery").hide();
        $(".layer .ctn-video").hide();
        return false;
    });
}

// metodi per gestione EDITING 
CostaUi.showChangeCountry = function ()
{   
        $(".layer").fadeIn();
        return false;
}


CostaUi.hideChangeCountry = function ()
{   
        $(".layer").fadeOut();
        return false;
}

// FINE metodi per gestione EDITING 

CostaUi.showFirstDetailsEdit = function ()
{   
        //$("#mask").fadeTo(0, 0.4);
        $(".first").fadeIn();
        //center(".first");
        //$(window).resize();
        $(".change-country").hide();
        $(".change-country").css("display") = "none";
        $(".first").css("top") = 0;
        return false;
}

CostaUi.closeLayerOldPage = function ()
{   
    $("#mask").fadeOut();
    $(".layer").fadeOut();
    $(".layer .ctn-gallery").hide();
    $(".layer .ctn-video").hide();
    return false;
}

// Clear Input //
CostaUi.clearInput = function ()
{
    $("form input").click(function ()
    {
        if (!this._haschanged) { this.value = '' }; this._haschanged = true;
    })
}

// Show Cabin Details //

CostaUi.showCabinDetails = function ()
{
    $(".ctn-cabin .submitButton").click(function ()
    {
        $(".ctn-cabin .submitButton").show();
        $(".cabin-details .confirmButton").hide();
        $(".ctn-cabin .cabin-details").hide();

        $(this).hide();
        $(this).parent().parent().parent().find(".cabin-details").show();
    });

    $(".cabin-details input").click(function ()
    {
        $(".cabin-details .confirmButton").show();

    })

}

// funzione creata per visualizzare chiudere il messaggio di errore nella pagina newsletter
CostaUi.closeMsgErrorNewsLetter = function()
{
   $(".close-error").click(function ()
   {
   
        $("#pnlBtnYesNo").addClass("NotVisibileError");
        $("#pnlMsgErr").addClass("NotVisibileError");
        $(".newsletter .form td.error").removeClass("error");
        
        return false;
    });
                                               
}

//Hide Ul CruiseFinder HomePage//
CostaUi.ShowULCruiseFinder = function ()
{
    $("#advanced-search-ul-port").removeClass("advanced-search");
    $("#advanced-search-ul-port").addClass("advanced-search-visible");
    
    $("#advanced-search-ul-ship").removeClass("advanced-search");
    $("#advanced-search-ul-ship").addClass("advanced-search-visible");
    
    $(".showAllFields").addClass("on");   
    
    return false;
}

//Parte relativa ai link statici dell'Header
CostaUi.openLinkHeader = function()
{

   $("#link_header_registrati").click(function ()
   {
        var linkPage =  $("#HdnWhyRegisterLink").attr("value");
        var hostName =  $("#HdnHostNameLink").attr("value");
        
        if ((hostName != "#") && (linkPage != "#"))
        {
            var address = "https://" + hostName + "/" + linkPage;
            //alert(address);
            document.location.href = linkPage;
        }
        
      
    });
    
   $("#link_header_forgotpassword").click(function ()
   {
        var linkPage =  $("#HdnForgotPasswordLink").attr("value");
        var hostName =  $("#HdnHostNameLink").attr("value");
        
        if ((hostName != "#") && (linkPage != "#"))
        {
            var address = "https://" + hostName + "/" + linkPage;
            //alert(address);
            document.location.href = linkPage;
        }
        
      
    });
    
   $("#link_header_myCosta").click(function ()
   {
        var linkPage =  $("#HdnMyCostaLink").attr("value");
        var hostName =  $("#HdnHostNameLink").attr("value");
        
        if ((hostName != "#") && (linkPage != "#"))
        {
            var address = "https://" + hostName + "/" + linkPage;
            //alert(address);
            document.location.href = linkPage;
        }
        
      
    });
    
   $("#link_header_becomeACostaClub").click(function ()
   {
        var linkPage =  $("#HdnBecomeACostaClubLink").attr("value");
        var hostName =  $("#HdnHostNameLink").attr("value");
        
        if ((hostName != "#") && (linkPage != "#"))
        {
            var address = "https://" + hostName + "/" + linkPage;
            //alert(address);
            document.location.href = linkPage;
        }
        
      
    });
    
   $("#link_header_editProfile").click(function ()
   {
        var linkPage =  $("#HdnEditProfileLink").attr("value");
        var hostName =  $("#HdnHostNameLink").attr("value");
        
        if ((hostName != "#") && (linkPage != "#"))
        {
            var address = "https://" + hostName + "/" + linkPage;
            //alert(address);
            document.location.href = linkPage;
        }
        
      
    });
   
    $("#link_header_changePassword").click(function ()
   {
        var linkPage =  $("#HdnChangePasswordLink").attr("value");
        var hostName =  $("#HdnHostNameLink").attr("value");
        
        if ((hostName != "#") && (linkPage != "#"))
        {
            var address = "https://" + hostName + "/" + linkPage;
            //alert(address);
            document.location.href = linkPage;
        }
        
      
    });                                                
}

// Clear Input //

CostaUi.clearInput = function () {
    $("form input").click(function () {
        if (!this._haschanged) { this.value = '' }; this._haschanged = true;
    });
}

// Show Cabin Details //

CostaUi.showCabinDetails = function ()
{
    $(".ctn-cabin .submitButton").click(function ()
    {
        $(".ctn-cabin .submitButton").show();
        $(".cabin-details .confirmButton").hide();
        $(".ctn-cabin .cabin-details").hide();

        $(this).hide();
        $(this).parent().parent().parent().find(".cabin-details").show();
    });

    $(".cabin-details input").click(function ()
    {
        $(".cabin-details .confirmButton").show();

    })

}

// Tab Layer //

CostaUi.tabLayer = function () {
    $(".layer .ctn-tab a").click(function () {
        var nameTab = $(this).attr("rel");

        $(".layer .ctn-tab a").removeClass("on");
        $(".layer .ctn-gallery").hide();
        $(".layer .ctn-video").hide();
        $(this).addClass("on");
        $(".layer div." + nameTab + "").show();

        var classLayer = $(this).parent().parent().parent().attr("class").split(" ");
        var nameLayerCenter = classLayer[1];
        center("div." + nameLayerCenter);
    });
}



// Change Country //

CostaUi.changeCountry = function () 
{
    $("a.changeCountry").click(function () 
    {
        $("ul.continents li").removeClass("on");
        $(this).parent().addClass("on");
        var whatToShow = $(this).attr("rel");
        $("ul.state").hide();
        $("ul.state." + whatToShow).show();
    })
}



// Inizialize //
CostaUi.init = function () 
{
    CostaUi.setMaxHotDealHeight();
    CostaUi.controllTabExpressBooking();
    CostaUi.subMenuPosition();
    CostaUi.subMenu();
    CostaUi.carousel();
    CostaUi.toggleFooter();
    CostaUi.setMaxElentHeight();
    CostaUi.tooltip(".tooltip", "layerTooltip");
    CostaUi.changeCountry();
    CostaUi.clearInput();
}

// Inizialize //
CostaUi.initHeaderFooterV4 = function ()
{
    CostaUi.closeLayerOldPage();
    
   
    CostaUi.openLayer();
    CostaUi.closeLayer();
    CostaUi.subMenuPosition();
    CostaUi.subMenu();
 //   CostaUi.toggleFooter();
	CostaUi.changeCountry();
}

function goto(url) {
    location.href = url;
}
