﻿// Home Bright Ideas Rotator
var HomeBrightIdeasClass = function() {
    var o = this;
    this.iIdeasCount = 8; // Zero based
    this.iCurrentIdea = 0;
    this.iToggleDelay = 6500;
    this.iTransitionTime = 1500;
    this.sCurrentCssClass = 'current';
    this.oExpandedIdeaContainer; this.oExpandedIdeas; this.oBrightIdeaThumbs; this.oIdleCaller;
    
    this.init = function(date) {
        o.oExpandedIdeaContainer = $('div.recent-idea');
        o.oBrightIdeaThumbs = $('div.idea-thumb a');
        o.GetAdditionalIdeas(date);
    }
    
    this.GetAdditionalIdeas = function(date) {
        var _data = 'getControl=MoreIdeas';
        if (date != '') { _data += '&Date=' + date; }
        
        $.ajax({
            url: '/async/async.ashx',
            type: 'POST',
            data: _data,
            error: function(){}, //Don't do anything
            success: function(response){
                if(isNaN(parseInt(response))) { $('div.recent-idea div.bright-idea').css('z-index', '1'); o.oExpandedIdeaContainer.append($(response).css('opacity', 0).css('z-index', '0')); o.AttachEvents(); } //Html = success 
            }
        });
    }
    
    this.AttachEvents = function() {
        $('div.recent-ideas div.idea-thumb a').click(function(e){ o.ExpandIdea(this); return false; }); //o.getSearchResultsWrapper();
        o.oExpandedIdeas  = $('div.recent-idea div.bright-idea');
        o.oIdleCaller = setInterval(o.IdleCycler, o.iToggleDelay);
    }
    
    this.ExpandIdea = function(link) {
        clearInterval(o.oIdleCaller);
        
        if (!$(link).hasClass(o.sCurrentCssClass)) {
            o.oBrightIdeaThumbs.removeClass(o.sCurrentCssClass);
            $(link).addClass(o.sCurrentCssClass);
            
            var iIndex = link.className.match(/bi(\d+)/)[1];

            $(o.oExpandedIdeas).filter(function() { return $(this).css('opacity') > 0; }).stop().fadeTo(o.iTransitionTime, 0).css('z-index', '0');
            $(o.oExpandedIdeas[iIndex]).fadeTo(o.iTransitionTime, 1).css('z-index', '1');
            
            o.SetSendToLink(link);
        }
    }
    
    this.IdleCycler = function() {
        var iNextIdea = o.iCurrentIdea + 1;
        if (o.iCurrentIdea / o.iIdeasCount == 1) { iNextIdea = 0 };
        
        $(o.oBrightIdeaThumbs[o.iCurrentIdea]).removeClass(o.sCurrentCssClass);
        $(o.oBrightIdeaThumbs[iNextIdea]).addClass(o.sCurrentCssClass);
        $(o.oExpandedIdeas[o.iCurrentIdea]).stop().fadeTo(o.iTransitionTime, 0).css('z-index', '0');
        $(o.oExpandedIdeas[iNextIdea]).stop().fadeTo(o.iTransitionTime, 1).css('z-index', '1');
        
        o.iCurrentIdea = iNextIdea;
        o.SetSendToLink(o.oBrightIdeaThumbs[iNextIdea]);
        o.oBrightIdeaThumbs[iNextIdea]
    }
    
    this.SetSendToLink = function(sLink) {
        var rgSegments = ($(sLink).attr('href')).split('/');
        var sHref = $('#st-staf').attr('href');
        sHref = sHref.substring(0, sHref.lastIndexOf('id=') + 3);
        $('#st-staf').attr('href', sHref + rgSegments[rgSegments.length-2]);
    }
}

//FAQ toggle
var FaqToggleClass = function() {
    var o = this;
    this.sToggleCssClass = 'active';
    
    this.init = function() {
        $('dt a').click(function(){ o.toggleFaq(this); return false;});

        if (document.location.hash != '') {
            o.toggleFaq($('#'+document.location.hash.substring(1))[0]);
            document.location.href = document.location;
        }
    }
    
    this.toggleFaq = function(link) {
        $(link).parent().parent().toggleClass(o.sToggleCssClass);
    }
}

//Bird's Nest Tooltip
var ToolTipClass = function() {
    var o = this;
    this.toolTipText;
    this.toolTipId = 'tooltip';
    this.div = function() { return document.createElement('DIV'); }
    
    this.init = function(link) {
        var lnk = $(link);
        this.toolTipText = lnk.attr('title');
        lnk.removeAttr('title');
        lnk.alt = ''; // also remove alt attribute to prevent default tooltip in IE
        lnk.append($(new this.div()).attr('id', this.toolTipId).append(this.toolTipText));
        
        lnk.mouseover(function(){ o.toggle(this); });
        lnk.mouseout(function(){ o.toggle(this); });
        lnk.click(function(){ return false; });
    }

    this.toggle = function(link) {
        $(link).find('#'+this.toolTipId).toggle();
    }
}

var ProductFinderClass = function() {
    var o = this;

    this.init = function() {
        $('.product-finder .product-button a, .product-finder .product-list .btn-close').click(function() { o.toggleProductFinder(); return false; });
    }
    
    this.toggleProductFinder = function() {
        scj.omniture.trackLink({channel: 'product', linkName: 'product_quick_finder', linkType: 'o'});
        $('.product-list').toggle();
        $('.touts-wrapper .tout').toggleClass('vis-hidden');
    }
}

var ProductsToggleClass = function() {
    var o = this;
    this.sToggleCssClass = 'collapse';
    
    this.init = function() {
        $('div.toggler').click(function(){ o.toggleProducts(this); return false; });
    }
    
    this.toggleProducts = function(toggler) {
        $(toggler).parent().toggleClass(o.sToggleCssClass);
    }
}

var ProductsTabsClass = function() {
    var o = this;
    this.sActiveClass = 'active'
    this.sTabSelector = '.product-page .tabs-wrapper div a';
    this.sContentSelector = '.product-page .product-info .tab-content .panel'
    this.sTabContentSelector = '.product-page .product-info .tab-content .';
    this.sDemoFlashPlayerSelector = '.product-page .product-info .tab-content #DemoSwf';
    this.sPauseVideo = 'stop video';
    
    this.init = function() {
        $(o.sTabSelector).click(function(){ o.toggleTab(this); return false; });
    }
    
    this.toggleTab = function(link) {
        var lnk = $(link);
        
        if (!lnk.hasClass(o.sActiveClass)) {
            $(o.sTabSelector).parent().removeClass(o.sActiveClass);
            lnk.parent().addClass(o.sActiveClass);
            
            $(o.sContentSelector).removeClass(o.sActiveClass);
            $(o.sTabContentSelector + lnk.parent().attr('id')).addClass(o.sActiveClass);
            
            if ($(o.sDemoFlashPlayerSelector)[0] && typeof $(o.sDemoFlashPlayerSelector)[0].sendCallToFlash != 'undefined') {
                $(o.sDemoFlashPlayerSelector)[0].sendCallToFlash(o.sPauseVideo);
            }
        }
    }
}

var GetMarthaModalClass = function() {
    var o = this;
    this.sModalId = '#martha-modal';
    this.sModalTrigger = '.tout.martha-windows-video';
    this.sDemoFlashPlayerSelector = '.product-page .product-info .tab-content #DemoSwf';
    
    this.init = function() {
        $(o.sModalTrigger).click(function(){ o.getModal(); });
    }
    
    this.getModal = function() {
        $.ajax({
            url: '/async/async.ashx',
            type: 'POST',
            data: 'getControl=MarthaWindowsDemoOverlay',
            error: function(){}, //Don't do anything
            success: function(response){
                if(isNaN(parseInt(response))) { $('body').append(response); o.attachEvents(); } //Html = success
            }
        });
    }
    
    this.attachEvents = function() {
        $(o.sModalId).jqm({ overlay:90, onHide:o.closeModal }).jqmShow();
        if ($(o.sDemoFlashPlayerSelector)[0] && typeof $(o.sDemoFlashPlayerSelector)[0].sendCallToFlash != 'undefined') {
            $(o.sDemoFlashPlayerSelector)[0].sendCallToFlash('stop video');
        }
    }
    
    this.closeModal = function(hash) {
        hash.o.remove();
        hash.w.empty();
        hash.w.remove();
    }
}