﻿var tabs = {
    activeTabSectionId: "",

    TabEnum: {
        Main: "main",
        Zoom: "zoom",
        Features: "features",
        Sizing: "sizing",
        Care: "care"
    },
    Initialize: function() {
        //default to maintab
        this.activeTabSectionId = productGlobals.Tabs.mainTabContent;

        $(productGlobals.Tabs.mainTab).click(function() { tabs.ChangeTabSection(tabs.TabEnum.Main); });
        $(productGlobals.Tabs.zoomTab).click(function() { tabs.ChangeTabSection(tabs.TabEnum.Zoom); });
        $(productGlobals.Tabs.featuresTab).click(function() { tabs.ChangeTabSection(tabs.TabEnum.Features); });
        $(productGlobals.Tabs.sizingTab).click(function() { tabs.ChangeTabSection(tabs.TabEnum.Sizing); });
        $(productGlobals.Tabs.careTab).click(function() { tabs.ChangeTabSection(tabs.TabEnum.Care); });
        $(productGlobals.Links.closeLink).click(function() { tabs.ChangeTabSection(tabs.TabEnum.Main); });
    },
    ChangeTabSection: function(tabEnum) {
        $(productGlobals.Tabs.TabsLinks).each(function() {
            $("#" + this.id).removeClass("on");
        });

        switch (tabEnum) {
            case this.TabEnum.Main:
                $(productGlobals.Tabs.mainTab).addClass("on");

                //in case a glossary term is being displayed(b/c it hides prod image), restore prod
                if (glossaryHotSpot.IsGlossaryTermVisible)
                    glossaryHotSpot.CloseGlossaryTerm();
                    
                if (purchaseOnline.IsVisible)
                    purchaseOnline.Close();
                    
                if (findRetailerView.IsVisible)
                    findRetailerView.Close();

                //if main tab is alrady displayed do nothing more
                if (this.activeTabSectionId == productGlobals.Tabs.mainTabContent)
                    return;

                $(productGlobals.Tabs.mainTabContent).fadeIn("slow");
                $(this.activeTabSectionId).slideUp("slow", function() {
                    $(productGlobals.Links.closeLink).hide();
                    $(productGlobals.Links.printLink).show();
                });
                this.activeTabSectionId = productGlobals.Tabs.mainTabContent;
                break;
            case this.TabEnum.Zoom:
                $(this.activeTabSectionId).fadeOut("normal");
                $(productGlobals.Tabs.zoomTab).addClass("on");
                this.activeTabSectionId = productGlobals.Tabs.zoomTabContent;
                $(this.activeTabSectionId).addClass("on");
                this.DoProperTabChangeEffect(this.activeTabSectionId);
                break;
            case this.TabEnum.Features:
                $(this.activeTabSectionId).fadeOut("normal");
                $(productGlobals.Tabs.featuresTab).addClass("on");
                this.activeTabSectionId = productGlobals.Tabs.featuresTabContent;
                this.DoProperTabChangeEffect(this.activeTabSectionId);
                if ($.browser.versionX < 7 && $.browser.name == "msie") {
                    $(productGlobals.Product.FeaturedImageId).attr('src', productGlobals.Product.FeaturedImageUrl);
                    $(productGlobals.Product.FeaturedImageId).attr('style', 'height:285px;width:250px;');
                    //once new featured images come in use following line instead of above line
                    //$(productGlobals.Product.FeaturedImageId).attr('style', 'height:390px;width:340px;');
                    supersleight.limitTo(productGlobals.Product.FeaturedImageId);
                }
                break;
            case this.TabEnum.Sizing:
                $(this.activeTabSectionId).fadeOut("normal");
                $(productGlobals.Tabs.sizingTab).addClass("on");
                this.activeTabSectionId = productGlobals.Tabs.sizingTabContent;
                this.DoProperTabChangeEffect(this.activeTabSectionId);
                break;
            case this.TabEnum.Care:
                $(this.activeTabSectionId).fadeOut("normal");
                $(productGlobals.Tabs.careTab).addClass("on");
                this.activeTabSectionId = productGlobals.Tabs.careTabContent;
                this.DoProperTabChangeEffect(this.activeTabSectionId);
                break;
        }
    },
    DoProperTabChangeEffect: function(newTabSection) {
        if (this.CheckFade())
            $(newTabSection).fadeIn("fast");
        else {
            $(productGlobals.Links.printLink).hide();
            $(productGlobals.Links.closeLink).show();
            $(newTabSection).slideDown("normal");
            $(productGlobals.Tabs.mainTabContent).fadeOut("slow");
        }
    },
    CheckFade: function() {
        var fadeOn = false;
        if (!$(productGlobals.Tabs.zoomTabContent).is(":hidden") || !$(productGlobals.Tabs.featuresTabContent).is(":hidden") ||
           !$(productGlobals.Tabs.sizingTabContent).is(":hidden") || !$(productGlobals.Tabs.careTabContent).is(":hidden")) {
            fadeOn = true;
        }
        return fadeOn;
    }
};   


//note: shadow effect (using 'shadow' class) now added/removed manually since firefox didn't handle the animations with the shadow well
var purchaseOnline = {
    IsVisible: false,
    IsMaximized: false,
    IsIE6: false,
    MinimizedWidth: "",
    MaximizedWidth: "",
    DivShadowClasses: new Array(),

    Initialize: function() {
        $(productGlobals.OnlinePartners.PurchaseOnlineOpener).click(function() {
            purchaseOnline.ShowPartners();
            pageTracker._trackEvent($('#hiddenName').html(), 'Purchase Online (open)');
        });
        $(productGlobals.OnlinePartners.CloseLink).click(function() {
            purchaseOnline.Close();
            pageTracker._trackEvent($('#hiddenName').html(), 'Purchase Online (close)');
        });

        this.IsIE6 = (jQuery.browser.msie && jQuery.browser.version < 7);
        if (this.IsIE6)
            this.RemoveShadowClasses();

        this.MinimizedWidth = "350px";
        this.MaximizedWidth = $(productGlobals.OnlinePartners.Container).css("width");

        if ($(productGlobals.OnlinePartners.AllPartnerLinks).length > 4)
            $(productGlobals.OnlinePartners.ViewAllOnlinePartnersLink).click(function() { purchaseOnline.ViewAllPartners(); });
        else
            $(productGlobals.OnlinePartners.ViewAllOnlinePartnersLink).hide();
    },
    RemoveShadowClasses: function() {
        if (this.DivShadowClasses.length > 0) {
            for (var i in this.DivShadowClasses) {
                var divClassMapping = this.DivShadowClasses[i];
                $(divClassMapping.Id).removeClass(divClassMapping.ShadowClass);
            }
        }
    },
    ShowPartners: function() {
        this.IsVisible = true;

        $(productGlobals.OnlinePartners.Container).width(purchaseOnline.MinimizedWidth);

        $(productGlobals.OnlinePartners.Container).slideDown("normal");

        //for IE6 shadow issues        
        //        if (purchaseOnline.IsIE6)
        //            $(productGlobals.OnlinePartners.Container).slideDown("normal");
        //        else
        //            $(productGlobals.OnlinePartners.Container).slideDown("normal", function() { $(productGlobals.OnlinePartners.Container).addClass("shadow"); });          
    },
    ViewAllPartners: function() {
        this.IsMaximized = true;
        //        $(productGlobals.OnlinePartners.Container).removeClass("shadow");
        $(productGlobals.OnlinePartners.Container).animate({ width: this.MaximizedWidth }, 1000, function() {
            //            if (!purchaseOnline.IsIE6)
            //                $(productGlobals.OnlinePartners.Container).addClass("shadow");

            $(productGlobals.OnlinePartners.AllPartnersContainer).fadeIn();
        });
    },
    OnlinePartnerOpener: function(url) {
        WindowOpener(url, 700, 700, 0, 250);
        return false;
    },
    Close: function() {
        $(productGlobals.OnlinePartners.Container).slideUp("fast", function() {
            $(productGlobals.OnlinePartners.Container).width(purchaseOnline.MinimizedWidth);
            $(productGlobals.OnlinePartners.AllPartnersContainer).hide();

            //TODO: UNCOMMENT THIS CODE
            //            $(productGlobals.OnlinePartners.Container).removeClass("shadow");
        });
        this.IsMaximized = false;
        this.IsVisible = false;
    }
};   

var glossarizer = {
    ElementIdsToGlossarize : new Array(),
    ReplacementTextFormatString : "",
    
    Initialize: function(replacementTextFormatString) {
        this.ReplacementTextFormatString = replacementTextFormatString;        
    },        
    AddElementIdToGlossarize: function(elementId) {
        this.ElementIdsToGlossarize.push(elementId);
    },    
    Glossarize: function() {
        for(var i = 0; i < this.ElementIdsToGlossarize.length; i++)
        {
            var textContainer = $('#' + this.ElementIdsToGlossarize[i]);
            if (textContainer.length > 0) //makes jquery resolved the object
            {
                Wigwam.UI.Web.FrontEnd.Services.Glossary.set_defaultSucceededCallback(glossarizer.OnGlossarizeTextSuccessCallback);
                Wigwam.UI.Web.FrontEnd.Services.Glossary.set_defaultFailedCallback(glossarizer.OnGlossarizeTextFailedCallBack);
                Wigwam.UI.Web.FrontEnd.Services.Glossary.set_defaultUserContext(this.ElementIdsToGlossarize[i]);
                Wigwam.UI.Web.FrontEnd.Services.Glossary.CheckTextForGlossaryItems(textContainer.html(), this.ReplacementTextFormatString);
//                Wigwam.UI.Web.FrontEnd.Services.Glossary.CheckTextForGlossaryItems(textContainer.html(), this.ReplacementTextFormatString, glossarizer.OnGlossarizeTextSuccessCallback, glossarizer.OnGlossarizeTextFailedCallBack, glossarizer.ElementIdsToGlossarize[i]);   
            }
        }
    },    
    OnGlossarizeTextSuccessCallback: function(result, elementContext) {
        $("#" + elementContext).html(result);
    },    
    OnGlossarizeTextFailedCallBack: function(result) {
        alert(result.get_message());
    }
};

var glossaryHotSpot = {
    IsGlossaryTermVisible : false,

    Initialize: function() {
        $(productGlobals.Glossary.Term.CloseLink).click( function () { glossaryHotSpot.CloseGlossaryTerm(); });
        $(productGlobals.Glossary.Illustration.CloseLink).click( function () { glossaryHotSpot.CloseGlossaryTerm(); });
    },    
    ShowGlossaryTerm: function(glossaryTermId) {
        Wigwam.UI.Web.FrontEnd.Services.Glossary.GetGlossaryDefinition(glossaryTermId, glossaryHotSpot.OnGotGlossaryTermSuccess);
    },    
    OnGotGlossaryTermSuccess: function(result) {
        glossaryHotSpot.DisplayGlossaryTerm(result.Term, result.Definition, result.ImageFileName);
    },    
    DisplayGlossaryTerm: function(term, definition, imageUrl) {
        this.IsGlossaryTermVisible = true;        
        
        $(productGlobals.Glossary.Illustration.Container).hide();        
        $(productGlobals.Glossary.Container).fadeIn("slow");
        $(productGlobals.Glossary.Term.TermContainer).fadeIn("slow");
        
        $(productGlobals.Tabs.mainTabProductContainer).hide();
        
        $(productGlobals.Glossary.Term.Title).text(term);     
        $(productGlobals.Glossary.Term.Text).text(definition);
        
        if (imageUrl == null)
            $(productGlobals.Glossary.Term.LogoImage).hide();
        else                                
        {
            $(productGlobals.Glossary.Term.LogoImage).attr("src", imageUrl);
            $(productGlobals.Glossary.Term.LogoImage).show();
        }                
    },    
    DisplayIllustration: function(imageUrl) {
        this.IsGlossaryTermVisible = true;        
    
        $(productGlobals.Glossary.Term.TermContainer).hide();        
        $(productGlobals.Glossary.Container).fadeIn("slow");
        $(productGlobals.Glossary.Illustration.Container).fadeIn("slow");
        
        $(productGlobals.Tabs.mainTabProductContainer).hide();
        
        $(productGlobals.Glossary.Illustration.Image).attr("src", imageUrl);
    },    
    CloseGlossaryTerm: function () {
        this.IsGlossaryTermVisible = false; 
        $(productGlobals.Tabs.mainTabProductContainer).fadeIn("slow");
        $(productGlobals.Glossary.Container).hide();                    
    }             
};

var productColors = {
    ShowRolloverColorName: function(color, sizes) {
        $(productGlobals.ProductColors.CurrentColorName).hide();
        $(productGlobals.ProductColors.RolloverColorName).text(color);
        $(productGlobals.ProductColors.RolloverColorName).show();
        productSizes.ShowRolloverSizes(sizes);
    },
    HideRolloverColorName: function() {
        $(productGlobals.ProductColors.RolloverColorName).hide();
        $(productGlobals.ProductColors.CurrentColorName).show();
        productSizes.HideRolloverSizes();
    },
    ColorNameClick: function(color, imageUrl, sizes) {
        $(productGlobals.ProductColors.CurrentColorName).text(color);
        $(productGlobals.ProductSizes.CurrentSizesLink).text(sizes);
        $(productGlobals.Product.ImageId).attr("src", imageUrl);
        if ($.browser.versionX < 7 && $.browser.name == "msie") {
            supersleight.limitTo(productGlobals.Product.ImageId);
        }
    }
};

var productSizes = {
    Initialize: function() {
        if ($(productGlobals.Tabs.sizingTab).length) {
            var clickFn = function() { tabs.ChangeTabSection(tabs.TabEnum.Sizing); }
            $(productGlobals.ProductSizes.CurrentSizesLink).click(clickFn);
            $(productGlobals.ProductSizes.RolloverSizesLink).click(clickFn);
        }
    },
    ShowRolloverSizes: function(sizes) {
        $(productGlobals.ProductSizes.CurrentSizes).hide();
        $(productGlobals.ProductSizes.RolloverSizes).text(sizes);
        $(productGlobals.ProductSizes.RolloverSizes).show();
    },
    HideRolloverSizes: function() {
        $(productGlobals.ProductSizes.RolloverSizes).hide();
        $(productGlobals.ProductSizes.CurrentSizes).show();
    }
};


//to be used only on Product Details Page
var findRetailerView = {
    FindRetailerCore: null,
    IsVisible: false,
    IsIE6: false,
    DivShadowClasses: new Array(),

    Initialize: function() {
        $(productGlobals.FindRetailers.FindRetailerOpener).click(function() {
            findRetailerView.ShowRetailerFinder();
            pageTracker._trackEvent($('#hiddenName').html(), 'Find Retailer (open)');
        });
        $(productGlobals.FindRetailers.CloseLink).click(function() {
            findRetailerView.Close();
            pageTracker._trackEvent($('#hiddenName').html(), 'Find Retailer (close)');
        });

        //will need to make an extra call to google when searching for IE6
        this.IsIE6 = (jQuery.browser.msie && jQuery.browser.version < 7);
        if (this.IsIE6)
            this.RemoveShadowClasses();

        $(productGlobals.FindRetailers.Container).keydown(findRetailerView.HandleEnterPressed);
        $(productGlobals.FindRetailers.SearchButton).click(function() { findRetailerView.PerformSearch(); });

        this.FindRetailerCore = new FindRetailerCore();
        this.FindRetailerCore.MapContainer = productGlobals.FindRetailers.MapContainer;
        this.FindRetailerCore.ResultsContainer = productGlobals.FindRetailers.ResultsContainer;
        this.FindRetailerCore.NoResultsContainer = productGlobals.FindRetailers.NoResultsContainer;
        this.FindRetailerCore.ResultTemplateURL = productGlobals.FindRetailers.ResultTemplateURL;
        this.FindRetailerCore.ThemedRetailerResultIconUrl = productGlobals.FindRetailers.ThemedRetailerResultIconUrl;
        this.FindRetailerCore.ThemedRetailerMapIconUrl = productGlobals.FindRetailers.ThemedRetailerMapIconUrl;
        this.FindRetailerCore.IsUsedForProdDetailsFinder = true;
        this.FindRetailerCore.Initialize();
    },
    RemoveShadowClasses: function() {
        if (this.DivShadowClasses.length > 0) {
            for (var i in this.DivShadowClasses) {
                var divClassMapping = this.DivShadowClasses[i];
                $(divClassMapping.Id).removeClass(divClassMapping.ShadowClass);
            }
        }
    },
    ShowRetailerFinder: function() {
        this.IsVisible = true;

        $(productGlobals.FindRetailers.Container).slideDown("slow");
        //        if(findRetailerView.IsIE6)
        //            $(productGlobals.FindRetailers.Container).slideDown("slow");
        //        else
        //            $(productGlobals.FindRetailers.Container).slideDown("slow", function() { $(productGlobals.FindRetailers.Container).addClass("shadow"); });
    },
    HandleEnterPressed: function(sender) {
        if (sender.keyCode == 13) {
            sender.preventDefault();
            findRetailerView.PerformSearch();
        }
    },
    PerformSearch: function() {
        var zip = $(productGlobals.FindRetailers.ZipCodeTextBox).attr("value");
        var distance = $(productGlobals.FindRetailers.DistanceDropDown).val();
        pageTracker._trackEvent($('#hiddenName').html(), 'Find Retailer Search', zip + " | " + distance);

        if (this.AreFieldsValid(zip, distance)) {
            $(productGlobals.FindRetailers.DisclaimerText).fadeIn();
            this.FindRetailerCore.GeocodeAddressAndDoSearch(null, zip, distance, null, this.ZipNotFoundCallback); //3rd param is categoryFilter
        }
    },
    ZipNotFoundCallback: function() {
        $(productGlobals.FindRetailers.ResultsContainer).hide();

        $(productGlobals.FindRetailers.NoResultsContainer).text(productGlobals.FindRetailers.ZipNotFoundMessage);
        $(productGlobals.FindRetailers.NoResultsContainer).fadeIn("normal");
    },
    AreFieldsValid: function(zip, distance) {
        var defaultZipText = $find(productGlobals.AspNetExtenders.ZipCodeWaterMark).get_WatermarkText();

        var zipValidator = $(productGlobals.FindRetailers.ZipCodeValidator);
        var distanceValidator = $(productGlobals.FindRetailers.DistanceValidator);

        var zipNotValid = (zip == defaultZipText);
        var distanceNotValid = (distance == "0");

        this.ShowHideFields(zipValidator, zipNotValid);
        this.ShowHideFields(distanceValidator, distanceNotValid);

        return (!zipNotValid && !distanceNotValid)
    },
    ShowHideFields: function(jqueryObj, show) {
        if (show)
            jqueryObj.show("fast");
        else
            jqueryObj.hide("fast");
    },
    Close: function() {
        $(productGlobals.FindRetailers.Container).slideUp("fast", function() { $(productGlobals.FindRetailers.Container).removeClass("shadow"); });
        this.IsVisible = false;
    }
}; 

var PageUtilities = {
    PrintPageOpener: function(url) {
        WindowOpener(url, 700, 700, 0, 250);
        return false;
    },
    SwapImage: function(elementId, imageUrl) {
        $("#" + elementId).attr("src", imageUrl);
    },
    ParseCSSProperty: function(selector, cssProperty)            
    {
        return parseInt(($(selector).css(cssProperty).replace(/px/, "")));
    }     
}