/*  ------------------------------------------------------------------
    Background Image Fix --------------------------------------------- */
    try { document.execCommand("BackgroundImageCache", false, true)} catch(e) {};


/*  ------------------------------------------------------------------
    Functions -------------------------------------------------------- */


    /*  ------------------------------------------------------------------
        TD Tracking ------------------------------------------------------ */

        var tracking_outrider = null;
        var tracking_dbClick = null;

        function tracking(category, action, label, outriderCode, dbClickCode){
            
            //Google Analytics
            ga(category, action, label);
        
            //Outrider Tracking 
            outrider(outriderCode);
            
            //Double Click Tracking 
            dbClick(dbClickCode);
            
            return;
        };
    
    
    /*  ------------------------------------------------------------------
        Google Tracking -------------------------------------------------- */
        function ga(category, action, label){
            
            if(undefined!==window.pageTracker){ 
                pageTracker._trackEvent(category, action , label);
            }
            return;
        };
        
    
    /*  ------------------------------------------------------------------
        Outrider Tracking HREF ------------------------------------------- */
        function outrider(code) {
            if(code != '' && undefined !== code){
                var src = 'http://na.decdna.net/n/64748/'+code+'/x/e?value=0&trans=&domain=na.decdna.net';
                jQuery('#outrider').attr('src', src);
            }
            return;
        };    
        
        
    /*  ------------------------------------------------------------------
        DoubleClick Tracking --------------------------------------------- */
        function dbClick(code) {
            if(code != '' && undefined !== code){
                var axel = Math.random()+"";
                var a = axel * 10000000000000;
                var src = 'http://fls.doubleclick.net/activityi;src=2467206;type=2010f323;cat='+ code +';ord='+ a + '?';
                jQuery('#dbClickFrame').attr('src', src);
            }
            return;
        };   


    /*  ------------------------------------------------------------------
        switchTextSize --------------------------------------------------- */
        function switchTextSize(size, settings) {
            if(size !== ''){
                $('body').removeClass().addClass(size);
                if (settings.saveCookie === true) {
                    $.cookie('textsize', size, { expires: 365, path: '/'  });
                }
            }
        }


    /*  ------------------------------------------------------------------
        loadPopin -------------------------------------------------------- */
        function loadPopin($id) {
            $('#'+$id).modal({
                closeHTML: '<a class="modalCloseImg simplemodal-close" title="X">X<span><!--image--></span></a>',
                overlayClose: true,
                onClose: closePopIn
            });
        }


/*  ------------------------------------------------------------------
    Extend jQuery Functions ------------------------------------------ */
    (function ($) {

    /*  ------------------------------------------------------------------
        switchSize ------------------------------------------------------- */
        $.fn.switchSize = function(settings) {
            // defaults settings
            settings = $.extend({
                saveCookie: true
            },settings);

            return this.each(function(){
                var container = this;
                var curtextsize = $.cookie('textsize');
                if (curtextsize) { $('body').removeClass().addClass(curtextsize); }

                $(this)
                    .find("a")
                    .bind('click', function(){
                        switchTextSize( $(this).attr("rel"), settings);
                        return false;
                    });
            });
        };


    /*  ------------------------------------------------------------------
        Plugin/Cookie ---------------------------------------------------- */
        $.cookie = function(name, value, options) {
            if (typeof value != 'undefined') { // name and value given, set cookie
                options = options || {};
                if (value === null) {
                    value = '';
                    options.expires = -1;
                }
                var expires = '';
                if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
                    var date;
                    if (typeof options.expires == 'number') {
                        date = new Date();
                        date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
                    } else {
                        date = options.expires;
                    }
                    expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
                }
                // CAUTION: Needed to parenthesize options.path and options.domain
                // in the following expressions, otherwise they evaluate to undefined
                // in the packed version for some reason...
                var path = options.path ? '; path=' + (options.path) : '';
                var domain = options.domain ? '; domain=' + (options.domain) : '';
                var secure = options.secure ? '; secure' : '';
                document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
            } else { // only name given, get cookie
                var cookieValue = null;
                if (document.cookie && document.cookie !== '') {
                    var cookies = document.cookie.split(';');
                    for (var i = 0; i < cookies.length; i++) {
                        var cookie = jQuery.trim(cookies[i]);
                        // Does this cookie string begin with the name we want?
                        if (cookie.substring(0, name.length + 1) == (name + '=')) {
                            cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                            break;
                        }
                    }
                }
                return cookieValue;
            }
        };


    })(jQuery);


/*  ------------------------------------------------------------------
    On Load ---------------------------------------------------------- */
    $(function(){
        
        $(".js_show").removeClass("js_show");
        
    /*  ------------------------------------------------------------------
        External Link ---------------------------------------------------- */
        var allowedDomains = [location.hostname];
        $("a[href^=http]:not(.internal):not([rel^='popup'])")
        .filter(function(el){
            for ( var i=0; i < allowedDomains.length; i++ ) {
                if ( this.href.indexOf(allowedDomains[i])>-1 ) return false;
            }
            return true;
        }).bind("click", function(){ return !window.open(this.href); });


    /*  ------------------------------------------------------------------
        Font Switcher ---------------------------------------------------- */
        $("#fontswitcher, .fontswitcher_popin").switchSize();


    /*  ------------------------------------------------------------------
        Popup ------------------------------------------------------------ */
        $("a[rel^='popup']").bind("click", function(){
            rel = $(this).attr("rel");
            rel = rel.replace("popup", "").split("x");
            w = rel[0];
            h = rel[1];
            var my_popup = window.open($(this).attr("href"), $(this).attr("id"), "location=0, status=0, scrollbars=1, menubar=0, toolbar=0, width="+w+", height="+h);
            if (my_popup) {my_popup.focus();}
            return !my_popup;
        });


    /*  ------------------------------------------------------------------
        Launch FAQ Popin ------------------------------------------------- */
        if (window.location.hash == '#faq') $('a#b_get_the_answers').trigger('click');
    });
