(function(){
    var yuiDom = YAHOO.util.Dom;

    var yuiEvent = YAHOO.util.Event;

    var yuiConnect = YAHOO.util.Connect;

    var TripTypeSelector = (function(){
        var defaultTripTypeId = "";
        var trigger = null;
        var instructionBar = null;
        var tripTypesContainer = null;
        var tripTypeContentContainer = null;
        var selectedTripTypeTextContainer = null;
        var promoTextContainer = null;
        var captionTextContainer = null;
        var startOverTrigger = null;
        var tripTypesToggler = null;
        var atlasAdsToggler = null;
        var promoTextToggler = null;
        var loadingMsgToggler = null;

        var selectTripTypeCycler = null;
        var instructionCyler = null;
        var instructionInterval = null;
        
        var closeAdsTimeout = null;
        var openAdsTimeout = null; 
        var hideDelayTimeout = null;
        var tripTypesContainerHeight = 0;
        var currentTripTypeElement = null;
        var brandCode = "WI";
        var tripType = {};
        var isHovering = false;
        var loadingTripTypeId;
        var startOver = false;
        var defaultPromoTextColor = "#ffffff";
        var defaultPromoTextLinkColor = "#ffffff";
        var defaultCaptionTextColor = "#ffffff";
        var promoTextColor = null;
        var promoTextLinkColor = null;
        var captionTextColor = null;
        var initialLoad = true;
        var clicked = false;
        var locale = "";
        var interstateExtraLight = { src: '/westin/sIFR/interstateExtraLight.swf' };
        var self = {
            initialize: function (defaultId) {
                defaultTripTypeId = defaultId;
                trigger = yuiDom.get("tripTypeTrigger");
                instructionBar = yuiDom.get("instructionBar");
                tripTypesContainer = yuiDom.get("tripTypes");
                tripTypeContentContainer = yuiDom.get("tripTypeContent");
                selectedTripTypeTextContainer = yuiDom.get("selectedTripTypeText");
                locale = SW.tools.Cookie.get("LOCALECOOKIE");

                var tripTypes = yuiDom.getElementsByClassName("tripType","li",tripTypesContainer);

                tripTypes.forEach(function(tripType) {
                    yuiEvent.addListener(tripType, "click", self.selectTripType, tripType.id);
                    yuiEvent.addListener(tripType, "mouseover", self.hover.mouseover);
                    yuiEvent.addListener(tripType, "mouseout", self.hover.mouseout);
                    yuiEvent.addListener(tripType, "mouseover", self.hover.clearHideDelay);
                    yuiEvent.addListener(tripType, "mouseout", self.hover.hideTripTypes);
                });

                captionTextContainer = document.createElement("div");
                yuiDom.addClass(captionTextContainer,"tripTypeCaptionText");

                promoTextContainer = document.createElement("div");
                yuiDom.addClass(promoTextContainer,"tripTypePromoText");

                yuiDom.get("tripTypesContainer").appendChild(promoTextContainer);
                yuiDom.get("contentContainer").appendChild(captionTextContainer);

                selectTripTypeCycler = SW.widget.displayToggler({
                    element: yuiDom.get("selectedTripTypeText"),
                    showOnComplete: function(config) {
                        yuiDom.setStyle(config.element, "zIndex", "2");
                    },
                    hideOnComplete: function(config) {
                        yuiDom.setStyle(config.element, "zIndex", "1");
                    },
                    showDuration:.800,
                    hideDuration:.500
                });

                instructionCyler = SW.widget.displayToggler({
                    element: yuiDom.get("tripTypeTriggerText"),
                    showOnComplete: function(config) {
                        yuiDom.setStyle(config.element, "zIndex", "2");
                    },
                    hideOnComplete: function(config) {
                        yuiDom.setStyle(config.element, "zIndex", "1");
                    },
                    showDuration:.800,
                    hideDuration:.500
                });

                instructionInterval = setInterval(SW.local.TripTypeSelector.cycleInstructions, 2000);
                
                promoTextToggler = SW.widget.displayToggler({
                    element: promoTextContainer,
                    showDuration:.3,
                    hideDuration:.5,
                    isShowing: true,
                    showAttributes: {
                        opacity: {to: 1}
                    },
                    hideAttributes: {
                        opacity: {to: 0}
                    },
                    showOnStart: function (config) {
                        yuiDom.setStyle(config.element, "opacity", 0);
                    },
                    showOnComplete: function(config) {
                        if (typeof sIFR == "object") {
                            if (locale != "ja_JP" && locale != "zh_CN" && YAHOO.env.ua.ie <= 0) {
                                sIFR.activate(interstateExtraLight);
                                sIFR.replace(interstateExtraLight, {
                                    selector: '.tripTypePromoText h1',
                                    wmode: 'transparent',
                                    forceSingleLine: false,
                                    fitExactly :false,
                                    transparent: true,
                                    css: ['.sIFR-root {color: '+ promoTextColor +'; background-color: transparent; leading: -5px;}']
                                });
                            }
                        }
                    },
                    hideOnComplete: function(config) {
                        // shady business going on here
                        setTimeout(promoTextToggler.show, 500);
                    }
                });

                tripTypesContainerHeight = 270;
                tripTypesToggler = SW.widget.displayToggler({
                    element: tripTypesContainer,
                    showDuration:.3,
                    hideDuration:.5,
                    showAttributes: {
                        height: {to: tripTypesContainerHeight}
                    },
                    hideAttributes: {
                        height: {to: 0}
                    },
                    showOnStart: function (config) {
                        clearTimeout(hideDelayTimeout);
                        yuiDom.setStyle(config.element, "height", 0);
                        yuiDom.setStyle(promoTextContainer, "zIndex", "-1");
                        yuiDom.addClass(trigger, "show");
                    },
                    showOnComplete: function(config) {
                        yuiDom.setStyle(config.element, "height", tripTypesContainerHeight);
                    },
                    hideOnStart: function () {
                        yuiDom.removeClass(trigger, "show");
                    },
                    hideOnComplete: function () {
                        yuiDom.setStyle(promoTextContainer, "zIndex", "0");
                        clicked = false;
                    }
                });

/*
                setTimeout(function() {
                    self.hover.hideTripTypes(null, true);
                    }, 1000
                );
*/
                yuiEvent.addListener(trigger, "mouseover", self.hover.showTripTypes);
                yuiEvent.addListener(trigger, "mouseover", self.hover.mouseover);
                yuiEvent.addListener(trigger, "mouseout", self.hover.mouseout);
                yuiEvent.addListener(trigger, "mouseover", self.hover.clearHideDelay);
                yuiEvent.addListener(trigger, "mouseout", self.hover.hideTripTypes);
                self.hover.showTripTypes();

/*
                yuiEvent.addListener(trigger, "mouseover", self.hover.clearHideDelay);
                yuiEvent.addListener(trigger, "mouseout", self.hover.hideTripTypes);
*/

                loadingMsgToggler = SW.widget.displayToggler({
                    element: yuiDom.get("tripTypeLoadingMessageContainer"),
                    showDuration:.3,
                    hideDuration:.3,
                    showAttributes: {
                        opacity: {to: 1}
                    },
                    hideAttributes: {
                        opacity: {to: 0}
                    },
                    showOnStart: function (config) {
                        yuiDom.setStyle("tripTypeLoadingMessageContainer", "display", "block");
                        yuiDom.setStyle("tripTypeLoadingMessageContainer", "zIndex", "10");
                        yuiDom.setStyle("atlasAdsContainer","zIndex","0");

                        yuiDom.setStyle(config.element, "opacity", 0);
                    },
                    hideOnStart: function() {
                        yuiDom.setStyle("atlasAdsContainer","display","block");
                    },
                    hideOnComplete: function(config) {
                        yuiDom.setStyle("tripTypeLoadingMessageContainer", "display", "none");
                        yuiDom.setStyle("tripTypeLoadingMessageContainer", "zIndex", "0");
                        yuiDom.setStyle("atlasAdsContainer","zIndex","10");
                    }
                });

                atlasAdsToggler = SW.widget.displayToggler({
                    element: yuiDom.get("atlasAds"),
                    showDuration: .3,
                    hideDuration: .5,
                    showAttributes: {
                        height: {to: 161}
                    },
                    hideAttributes: {
                        height: {to: 53}
                    },
                    showOnComplete: function () {
                        yuiDom.addClass("tripTypeContent", "open");
                        if(currentTripTypeElement && currentTripTypeElement.AtlasIframe) {
                            yuiDom.addClass(window.frames[currentTripTypeElement.AtlasIframe.name].document.getElementById("tripTypeOffersContainer"), "open");
                        }
                    },
                    hideOnComplete: function(config) {
                        yuiDom.removeClass("tripTypeContent", "open");
                        if(currentTripTypeElement && currentTripTypeElement.AtlasIframe) {
                            yuiDom.removeClass(window.frames[currentTripTypeElement.AtlasIframe.name].document.getElementById("tripTypeOffersContainer"), "open");
                        }
                    }
                });

                yuiEvent.addListener("atlasAdsContainer","mouseover", function(e){
                    clearTimeout(closeAdsTimeout);
                    closeAdsTimeout = null;
                    openAdsTimeout = setTimeout(self.openOffer, 500);
                    e = e || window.event;
                    yuiEvent.preventDefault(e);

                });

                yuiEvent.addListener("atlasAdsContainer","mouseout", function(e){
                    clearTimeout(openAdsTimeout);
                    openAdsTimeout = null;
                    closeAdsTimeout = setTimeout(self.closeOffers, 500);
                    e = e || window.event;
                    yuiEvent.preventDefault(e);

                });

                loadingTripTypeId = SW.tools.Cookie.get("WI_TripType") || defaultTripTypeId;
                self.selectTripType(null,loadingTripTypeId);
            },
            cycleInstructions: function(e) {
                if(!instructionCyler.isShowing()) {
                    instructionCyler.show();
                    selectTripTypeCycler.hide();
                }
                else {
                    instructionCyler.hide();
                    selectTripTypeCycler.show();
                }
            },
            selectTripType: function(e, tripTypeId) {
                if(e) {
                    clicked = true;
                    tripTypesToggler.hide();
                }
                atlasAdsToggler.hide();
                if(currentTripTypeElement) {
                    yuiEvent.addListener(currentTripTypeElement, "mouseover", self.hover.mouseover);
                    yuiEvent.addListener(currentTripTypeElement, "mouseout",  self.hover.mouseout);
                    yuiDom.setStyle(currentTripTypeElement,"display","block");
                    yuiDom.setStyle(currentTripTypeElement.AtlasIframe,"display","none");
                }

                currentTripTypeElement = yuiDom.get(tripTypeId);

                yuiDom.setStyle(currentTripTypeElement,"display","none");
                yuiDom.setStyle("atlasAdsContainer","display","none");
                selectedTripTypeTextContainer.innerHTML = yuiDom.get(tripTypeId+"_Text").innerHTML;
                yuiDom.addClass(trigger,"selectedTripType");
                self.getTripTypeContent(tripTypeId);
            },
            startOver: function() {
                startOver = true;
                yuiDom.removeClass(trigger,"selectedTripType");
                selectedTripTypeTextContainer.innerHTML = "";
                if(currentTripTypeElement) {
                    yuiDom.removeClass(currentTripTypeElement, "selected");
                    yuiEvent.addListener(currentTripTypeElement, "mouseover", self.hover.mouseover);
                    yuiEvent.addListener(currentTripTypeElement, "mouseout",  self.hover.mouseout);
                    yuiDom.setStyle(currentTripTypeElement.AtlasIframe,"display","none");
                    yuiDom.setStyle(currentTripTypeElement,"display","block");
                }
                currentTripTypeElement = startOverTrigger;
                yuiDom.setStyle(startOverTrigger,"display", "none");
                self.getTripTypeContent(defaultTripTypeId,self.resetTripTypes);
                atlasAdsToggler.hide();
                yuiDom.setStyle("atlasAdsContainer","display","none");
            },
            getTripTypeContent: function(tripTypeId, successFunc) {
                var successFunction = successFunc || self.setTripTypeContent;
                if(!initialLoad) {
                    loadingMsgToggler.show();
                }

                SW.tools.Cookie.set("WI_TripType", tripTypeId, "NEVER");
                var userCountryCode = SW.tools.Url.getParameter(document.location.href,"userCountryCode");
                var countryCodeParam = "";
                if (userCountryCode != null && userCountryCode != "") countryCodeParam = "&userCountryCode="+userCountryCode;
                setTimeout(function() {
                    yuiConnect.asyncRequest("GET", "/ajax/triptypelookup?tripTypeId=" + tripTypeId + "&brandCode=" + brandCode + countryCodeParam + "&unique=" + new Date().getTime(),
                        {
                            success: successFunction
                        }
                    );
                }, 1500);
            },
            resetTripTypes: function(xhr) {
                tripType = eval("("+xhr.responseText+")");
                if(tripType.backgroundImage) {
                    SW.local.TripTypeBackgroundSwitcher.changeBackground(tripType.Id, tripType.backgroundImage);
                    promoTextContainer.innerHTML = unescape(tripType.backgroundImage.text);
                    yuiDom.setStyle(promoTextContainer,"top", tripType.backgroundImage.textCoords.y);
                    yuiDom.setStyle(promoTextContainer,"left", tripType.backgroundImage.textCoords.x);
                    if(tripType.backgroundImage.textContainerWidth) {
                        yuiDom.setStyle(promoTextContainer,"width", tripType.backgroundImage.textContainerWidth);
                    }
                    if(tripType.backgroundImage.textColor && tripType.backgroundImage.textColor != "") {
                        promoTextColor = tripType.backgroundImage.textColor;
                    }
                    else {
                        promoTextColor = defaultPromoTextColor;
                    }
                    if(tripType.backgroundImage.textLinkColor && tripType.backgroundImage.textLinkColor != "") {
                        promoTextLinkColor = tripType.backgroundImage.textLinkColor;
                    }
                    else {
                        promoTextLinkColor = defaultPromoTextLinkColor;
                    }
                    yuiDom.setStyle(promoTextContainer.getElementsByTagName("h1")[0], "color", promoTextColor);
                    yuiDom.setStyle(promoTextContainer.getElementsByTagName("a")[0], "color", promoTextLinkColor);

                    promoTextToggler.hide();
                }

                loadingMsgToggler.hide();

            },
            setTripTypeContent: function(xhr) {
                tripType = eval("("+xhr.responseText+")");
                if(tripType.backgroundImage) {
                    SW.local.TripTypeBackgroundSwitcher.changeBackground(tripType.Id, tripType.backgroundImage);
                    promoTextContainer.innerHTML = unescape(tripType.backgroundImage.text);
                    captionTextContainer.innerHTML = unescape(tripType.backgroundImage.captionText);
                    yuiDom.setStyle(promoTextContainer,"top", tripType.backgroundImage.textCoords.y);
                    yuiDom.setStyle(promoTextContainer,"left", tripType.backgroundImage.textCoords.x);
                    if(tripType.backgroundImage.textContainerWidth) {
                        yuiDom.setStyle(promoTextContainer,"width", tripType.backgroundImage.textContainerWidth);
                    }
                    if(tripType.backgroundImage.textColor && tripType.backgroundImage.textColor != "") {
                        promoTextColor = tripType.backgroundImage.textColor;
                    }
                    else {
                        promoTextColor = defaultPromoTextColor;
                    }
                    if(tripType.backgroundImage.textLinkColor && tripType.backgroundImage.textLinkColor != "") {
                        promoTextLinkColor = tripType.backgroundImage.textLinkColor;
                    }
                    else {
                        promoTextLinkColor = defaultPromoTextLinkColor;
                    }
                    if(tripType.backgroundImage.captionColor && tripType.backgroundImage.captionColor != "") {
                        captionTextColor = tripType.backgroundImage.captionColor;
                    }
                    else {
                        captionTextColor = defaultCaptionTextColor;
                    }
                    yuiDom.setStyle(promoTextContainer.getElementsByTagName("h1")[0], "color", promoTextColor);
                    yuiDom.setStyle(promoTextContainer.getElementsByTagName("a")[0], "color", promoTextLinkColor);
                    yuiDom.setStyle(captionTextContainer, "color", captionTextColor);

                    promoTextToggler.hide();

                    if(initialLoad) {
                        self.hover.hideTripTypes();
                    }
                }

                if(!currentTripTypeElement.AtlasIframe) {
                    currentTripTypeElement.AtlasIframe = SW.tools.AtlasAds.outputIframe({
                        decodedTagId: tripType.atlasAdId,
                        urlPrefix: tripType.urlPrefix,
                        frameIdName: tripType.atlasAdId+"_iframe",
                        containerElement: yuiDom.get("atlasAds"),
                        allowTransparency: "true",
                        height: "165",
                        width: "700"
                    });
                }
                else {
                    yuiDom.setStyle(currentTripTypeElement.AtlasIframe,"display","block");
                }
                if(!initialLoad) {
                    atlasAdsToggler.show();
                    setTimeout(atlasAdsToggler.hide,3000);
                }
                setTimeout(loadingMsgToggler.hide, 1000);
                initialLoad = false;
            },
            openOffer:function() {
                if(!atlasAdsToggler.isShowing()) {
                    atlasAdsToggler.show();
                }
            },
            closeOffers: function() {
                if(atlasAdsToggler.isShowing()) {
                    atlasAdsToggler.hide();
                }
            },
            hover: {
                mouseover: function (e) {
                    yuiDom.addClass(this, "selected");
                },
                mouseout: function (e) {
                    yuiDom.removeClass(this, "selected");
                },
                hideTripTypesTimeout: function () {
                    if(!isHovering) {
                        hideDelayTimeout = setTimeout(tripTypesToggler.hide, 1000);
                    }

                },
                hideTripTypes: function(e) {
                    isHovering = false;
                    self.hover.hideTripTypesTimeout(null, true);
                },
                showTripTypes: function (e) {
                    if(!clicked) {
                        self.hover.clearHideDelay(e);
                        isHovering = true;

                        if(e && instructionInterval != null) {
                            clearInterval(instructionInterval);
                            instructionInterval = null;
                            instructionCyler.hide();
                            selectTripTypeCycler.show();
                        }
                        if(!tripTypesToggler.isShowing()) {
                            tripTypesToggler.show();
                        }
                    }
                },
                clearHideDelay: function (e) {
                    isHovering = true;
                    clearTimeout(hideDelayTimeout);
                }
            }
        }
        return self;
    })();
    YAHOO.namespace("SW.local.TripTypeSelector");
    SW.local.TripTypeSelector = TripTypeSelector;

    yuiEvent.onDOMReady(function() {
        var tripTypes = yuiDom.getElementsByClassName("tripType","li", "tripTypes");

        tripTypes.forEach(function(tripType) {
            yuiEvent.addListener(tripType, "click", omnitureTrack, tripType.id);
        });
    });

})();
