﻿function docWrite(str) {
    // Useful if you need to write a swf file, that needs to work in IE.
    document.write(str);
}

function PopUp(url, features, name) {
    // Attempt to pop up a new window.  If blocked, redirect to that page.
    try {
        if (/^https?:\/\/.*/.test(url) 
                && (!mcmsSiteRoot || (url.indexOf(mcmsSiteRoot) != 0))
                && (!mcmsSecureSiteRoot || (url.indexOf(mcmsSecureSiteRoot) != 0)) ){
            _gaq.push(['_trackEvent', 'outbound', 'click', url]);
        }
    } catch (err) { }
    
    var newwin = window.open(url, name, features);
    if (!newwin) { document.location = url; }
}

function closePopUp() {
    try {
        var winopener = window.opener;
        if (winopener) {
            // Make sure popup blocker is not enabled, before attempting window.close();
            if (winopener.location != window.location) {
                window.close();
                return 
            }
        }
        if (history.length > 0) {
            // PopUp blocker detected, and history exists, try to just go back.
            // Note that history.length starts at 1 in Firefox, but in Firefox, 
            // the previous step has probably already closed this window.
            history.go(-1);
        } else {
            // PopUp blocker detected, but no history exists, try to close current window.
            var winself = window.open('', '_self', '');
            window.close();
        }
    } catch (e) {
    }
}

function mcmsEml() {
    $(function() {
        $('.mcmsEml').each(function() {
            if (this.href)
                this.href = this.href.replace(/\.at\./, '@').replace(/.*#/, 'mailto:');
            this.innerHTML = this.innerHTML.replace(/\.at\./, '@');
        }); 
    }); 
}

var mcmsSiteRoot, mcmsSecureSiteRoot, mcmsFileRoot;
function mcmsInitGALinks(sSiteRoot,sSecureSiteRoot,sFileRoot) {
    // Google Analytics helper.
    if (sSiteRoot) mcmsSiteRoot = sSiteRoot;
    if (sSecureSiteRoot) mcmsSecureSiteRoot = sSecureSiteRoot;
    if (sFileRoot) mcmsFileRoot = sFileRoot;
    $(function() {
        $("a").click(function() {
            try {
                if (this.href && _gaq && !$(this).attr('gatracked')) {
                    var sHref = this.href.toLowerCase();
                    $(this).attr('gatracked', true);
                    if (/^https?:\/\/.*/.test(sHref) && (mcmsSiteRoot || mcmsSecureSiteRoot)) {
                        if ((mcmsSiteRoot && (sHref.indexOf(mcmsSiteRoot) == 0))
                                || (mcmsSecureSiteRoot && (sHref.indexOf(mcmsSecureSiteRoot) == 0))) {
                            // internal link...
                            if (sHref.indexOf(mcmsFileRoot) >= 0) {
                                _gaq.push(['_trackEvent', 'download', 'click', sHref]);
                            }
                        } else {
                            _gaq.push(['_trackEvent', 'outbound', 'click', sHref]);
                        }
                    } else if (sHref.indexOf(mcmsFileRoot) >= 0) {
                        _gaq.push(['_trackEvent', 'download', 'click', sHref]);
                    } else if (/mailto:/.test(sHref)) {
                        _gaq.push(['_trackEvent', 'mailto', 'click', sHref.substr(7, sHref.length)]);
                    }
                }
            } catch (err) { }
        }).disposable();
    }); 
}

function mcmsOnEndRequest(sender, args) {
    // Display a pop-up message if there are any unhandled server-side errors processing an AJAX Request.
    var err = args.get_error();
    if (err != null) {
        args.set_errorHandled(true);
        var s = err.message;
        s = s.substr(s.indexOf(':') + 2, s.length);
        
        alert(s);
    }
}

function mcmsEditItem(sItemSelector, sLinkSelector) {
    // Find each item that matches the sItemSelector.  
    // If that item contains exactly one match for the sLinkSelector, clicking anywhere in the item will trigger that link click.
    if (!sLinkSelector) sLinkSelector = "a";
    $(sItemSelector).each(function() {
        var elItem = $(this)
        var elLink = $(sLinkSelector, this);
        // item must also not contain any enabled inputs, because users would naturally be tempted to click on those as well.
        var elInput = $("input:enabled", this);
        var sHref = elLink.attr("href");
        if ((elInput.length == 0) && (elLink.length == 1) && (typeof sHref == "string")) {
            elItem.disposable();
            elItem.hover(
                function() { elItem.addClass('mcmsEditItemHover'); },
                function() { elItem.removeClass('mcmsEditItemHover'); }
            );
            elItem.click(function() {
                document.location = sHref;
            });
            if (!elItem.attr("title")) {
                var sTitle = elLink.attr("title");
                if (sTitle)
                    elItem.attr("title", sTitle);
                else
                    elItem.attr("title", elLink.text());
            }
        }
    });
}
(function($) {
    // Garbage Collection Plugin for jQuery/ie
    // via: http://www.codeproject.com/KB/ajax/jqmemleak.aspx
    $.fn.disposable = function(cln) {
        return this.each(function() {
            var el = this;
            if (!el.dispose) {
                el.dispose = cleanup;
                $(window).bind("unload", cleanup);
            }
            function cleanup() {
                if (!el) return;
                $(el).unbind();
                $(window).unbind("unload", cleanup);
                el.dispose = null;
                el = null;
            };
        });
    };
})(jQuery);

