//-- Start Config Object Properties
var CONFIG = {

    tinyMceScriptLocation: '/scripts/tiny_mce/tiny_mce.js',
    smallAjaxLoaderUrl: '/images/loader.gif',
    largeAjaxLoaderUrl: '/images/loader-large.gif',
    tableRowHighlightColor: '#FFFE99',
    tableRowHighlightTime: 400,

    defaultWindowScrollTime: 700,
    defaultNotificationTime: 5000,
    defaultFadeOutTime: 600,

    notificationSlideUpTime: 600,
    notificationSlideDownTime: 600,
    notificationDisplayTime: 10000,

    navBarOverAnimateTime: 200,
    navBarOverEasing: 'swing',
    navBarOutAnimateTime: 400,
    navBarOutEasing: 'easeOutBack',
    navBarHeightIncrease: 10,

    loginAnimateTime: 300,
    loginProcessingText: 'Logging in...',
    loginInvalidShakeTime: 100,
    loginInvalidShakeDistance: 5,
    loginInvalidShakeRepeat: 3,
    loginInvalidFadeTime: 400,
    loginInvalidEasing: 'easeInOutCubic',
    loginInvalidNotificationText: 'We need your username and password to log you in...',
    loginValidSlideTime: 1000,
    loginValidSlideOutDirection: 'left',
    loginValidSlideInDirection: 'left',
    loginValidForwardUrl: '/',

    signUpShowSmallTextTime: 100,
    signUpHideSmallTextTime: 100,
    signUpElectronicAddressFadeOutTime: 300,

    promoToolsFadeAnimateTime: 300,

    ourSitesSliderDisplayTime: 5000,
    ourSitesSliderFadeTime: 300,
    ourSitesSliderControlHeightAnimateTime: 300,
    ourSitesSliderSlideAnimateTime: 600,

    adminArticlesPublishingNotificationFadeTime: 300,
    adminArticlesBoxFadeTime: 300,
    adminArticlesDeleteConfirmationMsg: 'Are you sure you want to delete the selected article(s)?',
    adminArticlesArchiveConfirmationMsg: 'Are you sure you want to archive the selected article(s)?',
    adminArticlesCancelConfirmationMsg: 'Are you sure you want to cancel editing this article?\n\nYou will lose any unsaved changes.'

};

var COMMON = function() {

    /**
     * Returns a string for the CSS selector that should be used for the current
     * browser when measuring / animating scrollTop
     *
     * @author mattf
     * @return {String} CSS selector
     */
    function getScrollTopElem() {
        var elem = 'html';
        if ( jQuery('html').scrollTop() == 0 && jQuery('body').scrollTop() == 0 )
            elem = 'html, body';
        else if ( jQuery('body').scrollTop() > 0 )
            elem = 'body';
        return elem;
    }

    /**
     * Returns the windows current scrollTop
     *
     * @author mattf
     * @return {Number} windows current scrollTop
     */
    function getScrollTop() {
        return jQuery(getScrollTopElem()).scrollTop();
    }
    
    return {

        copy: function(inElement) {
            if (inElement.createTextRange) {
                var range = inElement.createTextRange();
                if (range && BodyLoaded==1) {
                    range.execCommand('Copy');
                } else {
                    var flashcopier = 'flashcopier';
                    if ( !document.getElementById(flashcopier) ) {
                        var divholder = document.createElement('div');
                        divholder.id = flashcopier;
                        document.body.appendChild(divholder);
                    }
                    document.getElementById(flashcopier).innerHTML = '';
                    var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+escape(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
                    document.getElementById(flashcopier).innerHTML = divinfo;
                }
            }
        },

        /*
         * Scroll the window to jQuery object $element
         *
         * @author  mattf
         * @param {Object, String} $element jQuery element or CSS selector to scroll the window to
         * @param {Number} [time] number of ms to take to scroll, defaults to CONFIG.defaultWindowScrollTime
         * @param {Function} [callback] function to call after scrolling finishes
         */
        scrollWindowToElem: function( $element, time, callback, force ) {

            //-- Expect a jQuery object, but allow for a selector to be passed too
            if ( "string" == typeof($element) )
                $element = jQuery($element);

            //-- Allow force to be an optional field
            if ( "boolean" == typeof(time) )
                force = time;
            else if ( "boolean" == typeof(callback) )
                force = callback;

            //-- Allow time to be an optional field
            if ( "function" == typeof(time) || "undefined" == typeof(time) ) {
                callback = time;
                time = CONFIG.defaultWindowScrollTime;
            }

            var options = {
              scrolltop: $element.position().top,
              time: time,
              force: force
            };

            if ( $element.position().top + $element.height() > getScrollTop() + jQuery(window).height() ) {
              options.force = true;
            }

            this.scrollWindow( options, callback );

        },

        /**
         * Scroll the window to the given config.scrolltop if:
         *   1) config.scrolltop is higher than the current scroll top
         *   2) config.scrolltop is below the currently visible portion of the screen
         *   3) config.scrolltop is visible on the screen but config.scrolltop + config.height is not
         *   4) config.force = true
         *
         * @author mattf
         * @param {Object} config Object containing configurable variables
         * @param {Number} [config.scrolltop] number of pixles from the document top to scroll to, defaults to 0
         * @param {Number} [config.height] number of pixles below config.scrolltop to ensure as many are possible are visible
         * @param {Boolean} [config.force] scroll to the given config.scrolltop regardless of other conditions
         * @param {Number} [config.time] number of miliseconds to take for the animation, defaults to CONFIG.defaultWindowScrollTime
         * @param {Function} [callback] function to call after scrolling finishes
         */
         scrollWindow: function( config, callback ) {
             var options = {
               scrolltop: 0,
               height: 0,
               force : false,
               time : CONFIG.defaultWindowScrollTime
             };
             jQuery.extend(options, config);

             //-- This block is specific to the affiliate site, because of the fixed header
             if ( jQuery('#dvSubNavBar').length > 0 ) {
                 options.scrolltop = options.scrolltop - 40;
             } else {
                 options.scrolltop = options.scrolltop - 15;
             }

             var win = getScrollTopElem();
             if ( options.force || 
                  options.scrolltop < getScrollTop() || // We need to scroll up to see the top of it
                  options.scrolltop + options.height >= getScrollTop() + jQuery(window).height() // We need to scroll down to see the bottom of it
                ) {
                 jQuery(win).animate({
                     "scrollTop": options.scrolltop
                 }, options.time, callback);
             } else {
                 if ('function' == typeof callback ) {
                     callback();
                 }
             }
         },
         
         debug : function( msg ) {

             if ( window.console && window.console.log ) {
                 window.console.log(msg);
             } else {
                 alert(msg);
             }

         },

         notifications : function() {

             var $container = null, timer = null;

             function getContainer() {

                 if ( !$container ) {
                     $container = $('#notification-container');
                     if ( 0 === $container.length ) {
                         $('body').append(
                             $container = $('<div id="notification-container"></div>')
                         );
                     }
                 }
                 
                 return $container;

             };

             return {

                 push : function( msg, error ) {

                     getContainer().html(msg);

                     if ( true === error ) {
                         getContainer().addClass('error');
                     } else {
                         getContainer().removeClass('error');
                     }
                     
                     this.show();

                 },

                 remove : function() {
                     getContainer().remove();
                 },

                 show : function() {
                     
                     if ( timer ) {
                         clearTimeout(timer);
                     }

                     getContainer().show('slide', {
                         direction: 'down'
                     }, CONFIG.notificationSlideUpTime, function() {
                             timer = setTimeout(
                                 function() {
                                     COMMON.notifications.hide();
                                 }, CONFIG.notificationDisplayTime
                             );
                         }
                     );

                 },

                 hide : function() {
                     getContainer().hide('slide', {
                        direction: 'down' 
                     }, CONFIG.notificationSlideDownTime);
                 }

             };

         } ()

    };
    
} ();

$(document).ready(function(){
    if($.browser.msie)
    {
        $('#dvPageHeader #dvPageHeaderBottomNav ul li:last-child').css('border-right', '1px solid #CCCCCC');
    }
});
