/*  ------------------------------------------------------------------
    Background Image Fix --------------------------------------------- */
    try { document.execCommand("BackgroundImageCache", false, true)} catch(e) {};


/*  ------------------------------------------------------------------
    Functions -------------------------------------------------------- */

    /*  ------------------------------------------------------------------
        TD Tracking ------------------------------------------------------ */
        function tracking(category, action, label){

            //Google Analytics
            ga(category, action, label);

            return;
        };


    /*  ------------------------------------------------------------------
        Google Tracking -------------------------------------------------- */
        function ga(category, action, label){
            
            if(undefined!==window.pageTracker){ 
                pageTracker._trackEvent(category, action , label);
            }
            return;
        };


    /*  ------------------------------------------------------------------
        switchTextSize --------------------------------------------------- */
        function switchTextSize(size, settings) {
            if(size !== ''){
                $('body').removeClass().addClass(size);
                if (settings.saveCookie === true) {
                    $.cookie('textsize', size, { expires: 365, path: '/'  });
                }
            }
        }


    /*  ------------------------------------------------------------------
        Subscribe -------------------------------------------------------- */
        function subscribeHome(){
        	if (!(document.getElementById("email_consent")) || document.getElementById("email_consent").checked) {
        		$('#frm_subscribe_home').load('/home/newsletter/from/home/email/'+escape($('#frm_subscribe_home_email').val()));
        	}
        };

        function subscribeHeader(){
        	if (!(document.getElementById("email_consent_header")) || document.getElementById("email_consent_header").checked) {
        		$('#frm_subscribe_header').load('/home/newsletter/from/header/email/'+escape($('#frm_subscribe_header_email').val()));
        	}
        };

/*  ------------------------------------------------------------------
    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;
            }
        };


    /*  ------------------------------------------------------------------
        Vertical Align --------------------------------------------------- */
        $.fn.verticalAlign = function() {
            return this.each(function(i){
	            var ah = $(this).height();
	            var ph = $(this).parent().height();
	            var mh = (ph - ah) / 2;
	            $(this).css('margin-top', mh);
            });
        };


    })(jQuery);


/*  ------------------------------------------------------------------
    On Load ---------------------------------------------------------- */
    $(function(){

	/*  ------------------------------------------------------------------
		Misc ------------------------------------------------------------- */
		$('.valign').verticalAlign();

		//fix IE6 PNG transparency
        if($.browser.msie && $.browser.version < 7) {
        	DD_belatedPNG.fix('img, .png');
		}

        $(".js_show").removeClass("js_show");
        $(".js_hide").hide();
        
        if(!($.browser.msie && $.browser.version == 6)) {
        	$(".js_show_no_ie6").removeClass("js_show_no_ie6");
        	$(".js_hide_no_ie6").hide();
        }


    /*  ------------------------------------------------------------------
        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").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;
        });


    /*  ------------------------------------------------------------------
	    Remove default value on Focus ------------------------------------ */
		$("input[type='text'][@title]")
		.bind("focus",function(){ if(this.value==this.title){ this.value=""; } })
		.bind("blur",function(){ if(this.value==""){ this.value=this.title; } });


    /*  ------------------------------------------------------------------
        Sub-navigation Menu ---------------------------------------------- */
        $('#m_nav li a.open_sub').click(function (){
            $('#m_nav li.withsub').removeClass('withsub_hover');
            $(this).parent().addClass('withsub_hover');
        });

        $('#m_nav li a.close_sub').click(function (){
            $('#m_nav li.withsub_hover').removeClass('withsub_hover');
        });

        /* bind href */
        $('div.change a').click(function (){
        	$('a#m_location').trigger('click');
		});
		$('.change').click(function (){
        	$('a#m_location').trigger('click');
		});

    });
    
    function emailConsent() {
    	if (document.getElementById("email_consent").checked) {
    		document.getElementById("frm_subscribe_home_email").disabled=false;
        } else {
    		document.getElementById("frm_subscribe_home_email").disabled=true;
    	}
    }
    
    function emailConsentHeader() {
    	if (document.getElementById("email_consent_header").checked) {
    		document.getElementById("frm_subscribe_header_email").disabled=false;
        } else {
    		document.getElementById("frm_subscribe_header_email").disabled=true;
    	}
    }
