﻿$(document).ready(function() {
    /* encodings for the reverse proxy */
    var slash = String.fromCharCode(47);
    var singleQuote = String.fromCharCode(39);

    $('a.tracking').click(function() {
        try {
            /* get the second class attribute as a campaign (i.e. "tracking caru") */
            var classes = new Array();
            classes = $(this).attr('class').split(' ');
            var campaign = classes[1];

            /* get the ending portion of the URL, including filename (i.e. "filename.ext") */
            var segments = new Array();
            segments = $(this).attr('href').split(slash);
            var filename = segments[segments.length - 1];

            /* get the URL extension (i.e. "ext") */
            var extensions = new Array();
            extensions = filename.split('.');
            var extension = extensions[1];

            /* produce the final GA tracking virtual folder */
            var trimmed = slash + campaign + slash + extension + slash + unescape(filename).split(' ').join('_').split('.' + extension).join('');

            /* send the virtual folder to GA */
            pageTracker._trackPageview(singleQuote + trimmed + singleQuote);
        }
        catch (err) {
            /* if Firebug is available, note the error's cause */
            if (window.console && window.console.firebug) {
                console.error('Tracking_Url_Modifier failed. ' + err);
            }
        }
    });
});

function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }
}
window.onload = externalLinks;

// global.js
function ddlNav(ddl) {
    if (ddl.options[ddl.options.selectedIndex].value !== '') {
        var slash = String.fromCharCode(47);
        var url = ddl.options[ddl.options.selectedIndex].value;
        if (url.substring(0, 7) == "http:" + slash + slash) {
            window.location = url;
        }
        else {
            // encoding the slash is required so reverse proxy does not insert the path incorrectly.
            var path = window.location.pathname;
            var pathArray = path.split(slash);
            var startPath = window.location.protocol + slash + slash + window.location.host;
            var endPath = '';

            if (pathArray[1] == "us" || pathArray[1] == "canada") {
                endPath = slash + pathArray[1] + ddl.options[ddl.options.selectedIndex].value;
            }
            else {
                endPath = ddl.options[ddl.options.selectedIndex].value;
            }

            endPath = endPath.replace(slash + slash, slash);
            window.location = startPath + endPath;
        }
    }
}

function setFontPreferences(fontSize) {
    var date = new Date();
    date.setTime(date.getTime() + (31536000000));
    document.cookie = 'cmsfp=' + fontSize + '; expires=' + date.toGMTString() + "; path=/";
}

function getFontPreferences() {
    var prefs = document.cookie.split(';');
    var name = 'cmsfp';
    for (var i = 0; i < prefs.length; i++) {
        var test = prefs[i].substring(0, 6);
        if (test.indexOf(name) > 0) {
            applyFontPreferences(prefs[i].substring(7, 8));
        }
    }
}

function applyFontPreferences(fontSize) {
    switch (fontSize) {
        case '1':
        case 1:
            if (document.getElementById('news-center') != null) document.getElementById('news-center').className = 'normal';
            if (document.getElementById('contentwell') != null) document.getElementById('contentwell').className = 'normal';
            break;
        case '2':
        case 2:
            if (document.getElementById('news-center') != null) document.getElementById('news-center').className = 'larger';
            if (document.getElementById('contentwell') != null) document.getElementById('contentwell').className = 'larger';
            break;
        case '3':
        case 3:
            if (document.getElementById('news-center') != null) document.getElementById('news-center').className = 'largest';
            if (document.getElementById('contentwell') != null) document.getElementById('contentwell').className = 'largest';
            break;
    }
}
