﻿

var utils = {
    baseUrl:"",    
    ajaxHelper: function(serviceUrl, successCallback, errorCallback, data) {
        $.ajax({
            type: "POST",
            url: serviceUrl,
            data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(result) {
                var dataResult = null;
                if (result.hasOwnProperty('d'))
                    dataResult = result.d;
                else
                    dataResult = result;
                successCallback(dataResult);
            },
            error: function(result) {
                if (errorCallback)
                    errorCallback(result);
            }
        });
    },
    setupAlternatingItemLists: function() {
        //can't use nth-child due to chrome bug
//        $(".flyoutList ul li:nth-child(even), .flyoutListSideMenu ul li:nth-child(even)").addClass("alt");
        $(".flyoutList ul, .flyoutListSideMenu ul").children("li").each(function(index) {
            if (index % 2 > 0) 
                $(this).addClass("alt");            
        });
        $(".flyoutList a[href]").addClass("link");
        $(".flyoutList ul ul").hide();        
        
        $(".flyoutList ul li, .flyoutListSideMenu ul li").hover(function() {
            $(this).addClass("hover");
            $("ul:first", this).show();
            var hasChildList = $(this).find("ul").length > 0;
            if (!hasChildList)
                $(this).addClass("empty");
        }, function() {
            $(this).removeClass("hover");
            $("ul:first", this).hide();
        });        
    },
    cleanStringForTracking: function(tag) {
        return tag.replace(/ /g, "_").replace(/&/g, "and");
    }
}

var pageTabs = {        
    tabSel: ".tab",
    tabContentSel: ".tabContent",
    topImagesSel: ".tabTopImages img",
    nextLink: ".hlNext",
    previousLink: ".hlPrevious",
    fadeSpeed: "fast",
    inAnimation: false,
    previousNextClicked: false,
    
    initialize: function() {
        $(".jqueryTabs .tabs .tab").eq(0).addClass("selected");
        $(".jqueryTabs .tabContentContainer .tabContent").eq(0).addClass("selected");
        $(".jqueryTabs .tabContentContainer .tabContent").hide();
        $(".jqueryTabs .tabContentContainer .selected").show();         
        
        this.setupTabClicks();
        
        this.setupNextPrevLinks();                                                  
    },
    setupTabClicks: function() {    
        $(".jqueryTabs .tabs .tab a").click(function() {            
            pageTabs.handleTabClick(this);
            return false;         
        });     
    },
    handleTabClick: function(sender) {
        if ($(sender).hasClass("serverside"))
            return;
                                
        //using inAnimation flag to prevent user from breaking tabs by rapid-fire clicking
        if (pageTabs.inAnimation == true)
            return;
            
        pageTabs.inAnimation = true;
        var tabIndex = $(sender).parent().prevAll().length;;
        var currentTabNumber = tabIndex + 1;
        
        //check if we need to fade images with tabs
        var topImages = $(pageTabs.topImagesSel);            
        var fadeTopImages = topImages.length >= currentTabNumber;
        if (fadeTopImages)
            topImages.fadeOut(pageTabs.fadeSpeed);
        
        $(sender).parent().parent().children(".selected").removeClass("selected");
        $(sender).parent().addClass("selected");
        $(".jqueryTabs .tabContentContainer").children(".selected").fadeOut(pageTabs.fadeSpeed, function() {
            if (fadeTopImages)
                topImages.eq(tabIndex).fadeIn(pageTabs.fadeSpeed);
        
            $(this).removeClass("selected");
            $(this).parent().children("div:nth-child(" + currentTabNumber + ")").fadeIn(pageTabs.fadeSpeed, function() {
                $(this).addClass("selected");
                
                pageTabs.inAnimation = false;   
                if (!pageTabs.previousNextClicked)
                    pageTabs.fireAnalyticsForCurrentTab(null);                
                pageTabs.previousNextClicked = false;
            });
        });
    },
    setupNextPrevLinks: function() {
        $(this.previousLink).click(function(event) {
            pageTabs.fireAnalyticsForCurrentTab("_previous_link");
            pageTabs.showPreviousTab(this);                   
            event.preventDefault();
        });                                                                       
        $(this.nextLink).click(function(event) {                   
            pageTabs.fireAnalyticsForCurrentTab("_next_link");
            pageTabs.showNextTab(this);
            event.preventDefault();
        });
        
        $(this.tabContentSel).each(function() {
            var myIndex = $(pageTabs.tabContentSel).index($(this));
            if (myIndex == 0)
                $(this).find(pageTabs.previousLink).addClass("noUnderline");
            if (myIndex == pageTabs.getTabsCount() - 1)
                $(this).find(pageTabs.nextLink).addClass("noUnderline");
        });       
    },
    showNextTab: function() {
        var tabsCount = this.getTabsCount();
        var currentIndex = this.getCurrentTabIndex();
        if (currentIndex < tabsCount - 1) {
            var nextTab = $(this.tabSel).eq(currentIndex + 1);
            pageTabs.previousNextClicked = true;
            $(nextTab).children("a").click();
        }                   
    },
    showPreviousTab: function() {
        var tabsCount = this.getTabsCount();
        var currentIndex = this.getCurrentTabIndex();
        if (currentIndex > 0) {
            var previousTab = $(this.tabSel).eq(currentIndex - 1);
            pageTabs.previousNextClicked = true;
            $(previousTab).children("a").click();
        }  
    },
    getTabsCount: function() {
        return $(this.tabContentSel).length;
    },
    getCurrentTabIndex: function() {
        return $(".tabContent").index($(".tabContent:visible"));            
    },
    loadTabForUrlHashTag: function() {
        var hash = window.location.hash;
        if (hash.length > 0) {
            var tab = $(hash + "Tab").find("a");
            if (tab.length > 0)
                tab.click();
        }
    },
    fireAnalyticsForCurrentTab: function(suffix) {
        //analytics tracking
        var tabName = $(".tab.selected a").text().trim();
        var tag = "/" + document.title + "/Tab_" + tabName;
        tag = utils.cleanStringForTracking(tag)
        
        if (suffix)
            tag += suffix;        
        gaTrackClick(tag);        
    }
}


var carousel = {   
    runCarousel: false,
    fadeSpeed: "fast",
    showPlayControls: true,
    autoPlay: true,
    cycleOutsideContent: false,
    outsideContentClass: "",                
    currentSlideIndex: 0,
    slidesCount: 0,
    playPauseContainer: ".playPauseControls",
    btnPlay: ".carouselPlay",
    btnPause: ".carouselPause",        
    timeoutId: null,
    inAnimation: false,        
         
    initialize: function(options) {
        this.runCarousel = options.runCarousel;
        this.fadeSpeed = options.fadeSpeed;
        this.showPlayControls = options.showPlayControls;
        this.autoPlay = options.autoPlay;
        this.cycleOutsideContent = options.cycleOutsideContent;
        this.outsideContentClass = options.outsideContentClass;
    
        if (!this.runCarousel)
            return;                 
        if (this.fadeSpeed.length == 0)
            this.fadeSpeed = "fast";    
        if (this.cycleOutsideContent && this.outsideContentClass.length == 0)
            this.cycleOutsideContent = false;
            
        $(".carouselImages .imageLink").eq(0).addClass("selected");
        $(".carouselCaptions .caption").eq(0).addClass("selected");
        $(".carouselImages .imageLink").hide();
        $(".selected").show();            
        this.slidesCount = $(".carouselImages .imageLink").length;

        $(".carouselCaptions .caption span").click(function() {     
            carousel.handleSlideClick(this);
        });
        
        if (this.showPlayControls) 
            $(this.playPauseContainer).show();            
        
        if (this.autoPlay)  
            this.play();
        else
            this.pause();             
    },
    handleSlideClick: function(sender) {
        this.pause();
        this.changeSlideForCaption(sender);
    },
    changeSlideForCaption: function(sender) {        
        if (this.inAnimation)
            return;
        
        this.inAnimation = true;                   
        this.currentSlideIndex = $(sender).parent(".caption").prevAll().length;
        
        $(sender).parents(".carouselCaptions").children(".selected").removeClass("selected");
        $(sender).parent(".caption").addClass("selected");
        
        if (this.cycleOutsideContent) {                                
            var contentToShow = $(this.outsideContentClass).eq(this.currentSlideIndex);
            if (contentToShow.length > 0) {
                $(this.outsideContentClass + ":visible").fadeOut(this.fadeSpeed, function() {
                    contentToShow.fadeIn(carousel.fadeSpeed);
                });
            }
        }
                    
        $(".carouselImages").children(".selected").fadeOut(this.fadeSpeed, function() {
            $(this).removeClass("selected");
            $(this).parents(".carouselImages").children("div.imageLink").eq(carousel.currentSlideIndex).fadeIn(carousel.fadeSpeed, function() {
                $(this).addClass("selected");
                carousel.inAnimation = false;
            });
        });            
    },
    changeSlide: function(slideIndex) {
        this.currentSlideIndex = slideIndex;
        var slideCaption = $(".carouselCaptions .caption span").eq(slideIndex);
        this.changeSlideForCaption(slideCaption);
        
        if (this.autoPlay)
            this.setupNextSlideTimeout();
    },
    setupNextSlideTimeout: function() {
        var delay = $(".carouselCaptions .caption").eq(this.currentSlideIndex).children("input[type=hidden]").val();                                    
        var nextSlide = (this.currentSlideIndex < this.slidesCount - 1) ? this.currentSlideIndex + 1 : 0;            
        this.timeoutId = window.setTimeout(function() {carousel.changeSlide(nextSlide);}, parseInt(delay));
    },
    play: function() {
        this.autoPlay = true;
        $(this.btnPlay).hide();            
        if (this.showPlayControls)
            $(this.btnPause).show();                
        this.setupNextSlideTimeout();
        return false;
    },
    pause: function() {
        window.clearTimeout(this.timeoutId);
        this.timeoutId = null;            
        this.autoPlay = false;
        $(this.btnPause).hide();
        if (this.showPlayControls)
            $(this.btnPlay).show();                
        return false;
    }        
}
