function addLoadEvent(func)
{
    var oldonload = window.onload; 
    if (typeof window.onload != 'function')
        window.onload = func; 
    else
    {
        window.onload = function()
        { 
            oldonload(); 
            func(); 
        }
    }
} 
 
function toggleSearchField(e)
{
    if(!e)
        var e = window.event;
    
    if(e.type == "focus")
    {
        if(this.value == "search")
            this.value = "";
    }
    
    else if(e.type == "blur")
    {
        if(this.id.match("search") && this.value == "")
            this.value = "search";
    }
}

addLoadEvent(function()
{
    var searchQuery = document.getElementById("main-search-query");
    if($('#main-search-query').length > 0) {
       searchQuery.onfocus = toggleSearchField;
       searchQuery.onblur = toggleSearchField;
    }
    // preload images, register rollovers
});




(function($) {


    /**
     * promocode from cookie 
     * or attached to url on the page then added to cookie 
     */
    var CePromo = {};
    
    //promo code	
    CePromo.code = "";
    //promocode length in get variable
    CePromo.GET_VARIABLE_LENGTH = 10; 
    //cookie value based on jquery.cookie.js    
    CePromo.cookie = $.cookie('promocode', { path: '/' });
    //id in the nav to look for session link
    CePromo.signUpLinkId = "#signUpLink"; 

    /**
     * get promo from the signup link
     * this is set by php session
     */
    CePromo.getSignUpPromo = function () {
        "use strict";
        if ($(CePromo.signUpLinkId).length > 0) {
            CePromo.getLinkPromo(CePromo.signUpLinkId);
        }
        
    };
    
    /**
     * get promo from a hrefs on page
     * solves problem with affiliates page not setting correctly
     * will only work if there isn't already a promo in the session
     */
    CePromo.getAhrefPromo = function () {
        "use strict";
        //if signup link is empty
        //loop through all a tags and look for promocode=
        if (CePromo.code === "") {
            //assuming there aren't conflicting codes on the page
            //last one on the page will win
            $("a").each(function (index, domEle) {
                // domEle == this
                if ($(domEle).attr('href')) {
                    CePromo.getLinkPromo(domEle);
                }
            });
        }
    };
    
    /**
     * Get promocode from a specific element on the page
     * by id or passed reference
     * @domEle is id or specific element of dom passed 
     */
    CePromo.getLinkPromo = function (domEle) {
        "use strict";
        var curHref = $(domEle).attr('href'),
            codeStart = curHref.lastIndexOf("promocode=");
	    if (codeStart > 0) {
	        CePromo.code = curHref.substring(codeStart + CePromo.GET_VARIABLE_LENGTH);
	    }
    };
    

    /**
     * Get promocode from a Get Variable 
     */
    CePromo.getURLPromo = function () {
        "use strict";
        if (CePromo.code === "") {

            var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
            
            for(var i = 0; i < hashes.length; i++) {
                var hash = hashes[i].split('=');
                if (hash[0] === "promocode") {
                    CePromo.code = hash[1];
                }
            }

            
        }
    }


    /**
     * get promo code from cookie if it exists
     */
    CePromo.getCookie = function () {
        "use strict";
        //if no promocode in a tag look in cookie
        if (CePromo.code === "") {
            if (CePromo.cookie !== null) {
                CePromo.code = CePromo.cookie;
            }
        }
    };
    
    /**
     * set promo code cookie if promo exists
     */
    CePromo.setCookie = function () {
        "use strict";
        //if promocode in a tag replace cookie 
        if (CePromo.code !== "") {
            $.cookie('promocode', CePromo.code, { path: '/', expires: 365 });
        }
    };
    
    /** 
     * set promocode get variables into a href going to signup
     */	
    CePromo.setPromoCodes = function () {	
        "use strict";
        //loop through all a tags for href="https://secure.cvnt.net/signup/" 
        //and add promocode or update it 
        if (CePromo.code !== "") {
            //for each a tag add the promocode if not already attached  
            $("a").each(function (index, domEle) {
                // domEle == this
                if ($(domEle).attr('href')) {
                    CePromo.setCodeForSignUpLink(domEle);
                }
            });
        }
    };
    
    /**
     * Add promo code to all sign up links on page
     * @domEle is id or specific element of dom passed 
     */
    CePromo.setCodeForSignUpLink = function (domEle) {
        "use strict";
        var curHref = $(domEle).attr('href'),
            codeStart = curHref.lastIndexOf("promocode="),
            isSignupPage = curHref.lastIndexOf('https://secure.cvnt.net/signup/'),
            curGetVarStart = curHref.lastIndexOf('?');
	    
        //check if this is a signup page
        if ((isSignupPage > -1) && (codeStart === -1)) {
             //check if there are already get variables and add correctly
            if (curGetVarStart === -1) {
                curHref += "?promocode=" + CePromo.code;
                $(domEle).attr('href', curHref);
            } else {
                //check to see if there is a ?nocode set by the session
                //if so ignore it and overwrite to correct with new
                //promo
                codeStart = curHref.lastIndexOf("?nocode");
                if (codeStart === -1) {
                    curHref += "&amp;promocode=" + CePromo.code;
                } else {
                    curHref = curHref.substring(0, codeStart) + "?promocode=" + CePromo.code;
                }
                $(domEle).attr('href', curHref);
            }
        }
    };
        
    /**
     * update promo code on page 
     */
    CePromo.updatePromoCode = function () {
        "use strict";
        //get code from signup link
        CePromo.getSignUpPromo();    
        
        //get code from another a href
        CePromo.getAhrefPromo();
        
	//get code from URL
        CePromo.getURLPromo();

        //get code from cookie 
        CePromo.getCookie();
        
        //get code from cookie 
        CePromo.setCookie();
    
        //set a hrefs that go to signup with promocode if necessary
        CePromo.setPromoCodes();
    };
    
    /** 
     * end promo code
     */

    var ce = {

        selectStory: function($link) {
            var key = $link.attr('id').replace('story-link-','');
            
            $link.closest('ul').find('.current').removeClass('current');
            $link.addClass('current');

            $('#audience-section-2').find('.current').removeClass('current').fadeOut(function() {
                $('#story-'+key).fadeIn().addClass('current');
            });
        }
    };


    $(document).ready(function() {
        
        // Handle click on a person's name
        $('#story-list a.person-link').click(function(e) {
            e.preventDefault();
            ce.selectStory($(this));
 
        });
        
        // Handle clicks on the arrows
        $('#story-list .next a').click(function(e) {
            e.preventDefault();
            $this = $(this);
            $selected = $this.parent().siblings().find('.current').parent();
                if (!$selected.next().hasClass('next')) {
                $this.closest('ul').find('a').removeClass('current');
                ce.selectStory($selected.next().find('a').addClass('current'));
            }
        });
        $('#story-list .previous a').click(function(e) {
            e.preventDefault();
            $this = $(this);
            $selected = $this.parent().siblings().find('.current').parent();
                if (!$selected.prev().hasClass('previous')) {
                $this.closest('ul').find('a').removeClass('current');
                ce.selectStory($selected.prev().find('a').addClass('current'));
            }
        });
        
        $('#support-search-query').change(function() {
           $('#query').val($(this).val() + ' more:support_articles_only');
        });
        
        $('#main-search-query').removeAttr('style');
   
	//start slideshow on homepage
	if($('#slider').length > 0) {
            $('#slider').nivoSlider({
                effect: 'fade',
                pauseTime: 7000
            });
 	}
        //set promocode
        CePromo.updatePromoCode();
    })
	
})(jQuery);

