var BF = {

    init: function () {

        // Setup console logging
        if (typeof console === 'undefined') {
            console = {
                log: function (message) { },
                warn: function (message) { },
                time: function (message) { },
                timeEnd: function (message) { }
            };
        }

        BF.initCufon();
        BF.initSearch();
        BF.initPrintLinks();
        BF.initMegaNav();
        BF.initTabSwitchers();
        BF.initAccountLoginPopover();
        var gateCookie = 'bf-investor-choice';

        /**
        * Download Gate
        */
        var downloadGates = new BF.modal({
            triggers: 'a.download-gate',
            content: 'confirmation', // this has to be confirmation
            contentTarget: "The information provided herein is derived from a portion of the Baron Fund's prospectus (the &quot;Prospectus&quot;). The Prospectus contains additional information about the Baron Funds. You should carefully read the Prospectus before investing in any of the Baron Funds."
        });

        /**
        * Baron Funds USA Gate
        */
        if ($('.baron-funds-usa').length > 0 && BF.fundGate && !BF.cookie.get('bf-usa-fund')) {
            BF.fundGate.init();
        }

        /**
         * instantiate this code only on pages that need it
         */
        if ($('#homeHeadlines').length > 0) {
            var carousel = new BF.carousel('#homeHeadlines', { nextPrev: false, restTime: 7000 });
        }

        /**
        * instantiate this code only on pages that need it
        */
        if (!BF.cookie.get(gateCookie) && $(document.body).hasClass('gate')) {
            var modal = new BF.modal({
                content: 'inline',
                contentTarget: 'div.gate',
                hideOnEsc: false, // hide modal on ESC key event
                hideOnOutsideClick: false // hide modal on outside click? 
            });
            modal.open();
            $('div.gate a').live('click', function (e) {
                e.preventDefault();
                // for now just set cookie value to class name
                BF.cookie.set(gateCookie, $(this).attr('class'), 365);
                modal.close();
                if ($(this).attr('href') !== '#') {
                    window.location.href = $(this).attr('href');
                }
            });
        }

    },

    initCufon: function () {
        Cufon.replace('h1:not(.offscreen)');
        Cufon.replace('span.title');
        Cufon.replace('h2');
        Cufon.replace('h3');
    },

    initSearch: function () {
        // site search field clear on focus
        var searchField = $('div#siteSearch input.textfield'),
            searchForm = searchField.parent('form'),
            placeholderTxt = 'Search by symbol or keyword';

        if (searchField.length) {
            var searchLabel = searchField.val();
            BF.placeholder.init('#siteSearch input.textfield', { force: true });

            searchForm.submit(function (e) {
                var val = $.trim(searchField.val());
                if (val === '' || val === placeholderTxt) {
                    // do nothing
                    return false;
                } else {
                    return true;
                }
            });

        }
        delete BF.initSearch;
    },

    initPrintLinks: function () {
        if ($('div.printView').length && window.print) {
            var printLink = $('div.printView');
            printLink.show();
            printLink.find('a').click(function (e) {
                e.preventDefault();
                window.print();
            });
        }
    },

    initMegaNav: function () {
        if ($('ul#megaNav').length) {
            BF.megaNav.init();
        }
        delete BF.initMegaNav;
    },

    initTabSwitchers: function () {
        /**
        * instantiate this code only on pages that need it
        */
        if ($('div#homeDailyDataWrap').length) {
            var dailyPriceTabs = new BF.tabSwitcher($('div#homeDailyDataWrap'), $('a.homeDailyTab'), $('table.homeDailyData'), { activeClass: 'active', index: $('a.homeDailyTab.active').index(), fadeSpeed: 0, fixedHeight: true });
            // set cookies depending on what tab is clicked
            $('a.homeDailyTab.Institutional').bind('click', function (e) {
                BF.cookie.set('DefaultShareClass', '2', 365);
            });
            $('a.homeDailyTab.Retail').bind('click', function (e) {
                BF.cookie.set('DefaultShareClass', '1', 365);
            });
        }
        if ($('div#topContributorsWrap').length) {
            var topContributorsTablesTabs = new BF.tabSwitcher($('div#contributorsTables'), $('a.topContributorsTab'), $('div.topContributors'), { activeClass: 'active', index: 0, fadeSpeed: 0, fixedHeight: true });
        }
        if ($('#performanceAsOfWrap').length) {
            var performanceAsOfTabs = new BF.tabSwitcher($('#performanceAsOfWrap'), $('a.performanceAsOfTab'), $('table.performanceTable'), { activeClass: 'active', index: 0, fadeSpeed: 0, fixedHeight: false });
        }
        delete BF.initTabSwitchers;
    },

    initAccountLoginPopover: function () {
        if ($('div#accountLoginPopover').length) {
            BF.accountLoginPopover.init();
        }
        delete BF.initAccountLoginPopover;
    }
};


/**
 * Get / set / destroy cookie
 */
BF.cookie = {
    set: function (name, value, days, domain) {
        var expires;
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = "; expires=" + date.toGMTString();
        } else {
            expires = "";
        }
        if (domain) {
            domain = "; domain=" + domain + ";";
        } else {
            domain = "";
        }
        document.cookie = name + "=" + value + expires + "; path=/" + domain;
    },

    get: function (name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    },

    destroy: function (name) {
        this.set(name, "", -1);
    }

};

$(document).ready(function () {
	$('body').removeClass('no-js');
	BF.init();
});
