// Google Analytics account profile defined in global.js

function gaProcessFileURLS($root) {
    // document extensions to look for
    var fileTypes = ['ai', 'doc', 'docx', 'eps', 'jpg', 'mp3', 'pdf', 'ppt', 'pptx', 'psd', 'xls', 'xlsx', 'xml', 'zip', 'sflb\\.ashx'];
    var gaDocumentVpvPrefix = '/vpv/files';
    var gaOutgoingVpvPrefix = '/vpv/outgoing';
    var extRegExp = new RegExp('\\.(' + fileTypes.join('|') + ')(\\?.*)?$'); // RegExp incorporates the tracked filetypes above
    $root.find('a[href]').each(function () {
        var $a = $(this);

        // Get URL, set to lowercase and remove anchor
        var href = $a.attr('href').toLowerCase().replace(/#.*$/, '');
        if (href != null && href != '' && !href.match(/^javascript:/)) { // Skip empty or JavaScript URLs

            if (href.match(/^https?:\/\//)) { // External link
                $a.click(function () {
                    href = href.replace(/^https?:\/\/(www\.)?/, '/').replace(/\/(default\.aspx)?$/, ''); //cleanup for nice GA reports
                    pageTracker._trackPageview(gaOutgoingVpvPrefix + href);
                });
            } else if (extRegExp.test(href)) { // Check for extension
                $a.click(function () {
                    // If no leading slash, then merge with current URL
                    if (href.charAt(0) != '/') {
                        href = location.pathname.replace(/\/[^\/]*$/, '/') + href;
                    }

                    // Clean any ".." folders.  Replace the first instance of "/dirname/../" with "/". Repeat until no change occurred.
                    var tmpHref;
                    do {
                        tmpHref = href;
                        href = href.replace(/\/[^\/]*[^\/\.]+[^\/]*\/\.\.\//, '/');
                    } while (tmpHref != href);

                    // Remove redundant "/./"
                    href = href.replace(/\/\.\//, '/');

                    pageTracker._trackPageview(gaDocumentVpvPrefix + href);
                });
            }
        }
    });
}

$(function () {
    if (typeof (pageTracker) != 'undefined') {
        gaProcessFileURLS($(document));

        //thickbox links
        $("a.thickbox, area.thickbox, input.thickbox").click(function () {
            pageTracker._trackPageview($(this).attr('href'));
        });

        //topnav links
        $("#TopNav a").click(function () {
            //if topnav link is also external, remove http// part
            var href = $(this).attr('href');
            if (href.match(/^http/)) {
                href = href.replace(/^https?\:\//, '');
            }
            pageTracker._trackPageview('/topnav' + href);
        });

        //homepage drawers links      
        $("a.homeDrawer").click(function () {
            pageTracker._trackPageview('/homedrawer/' + getValueFromClass(this, 'hd'));
        });

        //catchcode links      
        $("a.catchCode").click(function () {
            pageTracker._trackPageview('/catchcode/' + getValueFromClass(this, 'ga'));
        });
    }
});


// Track SilverLight 2.0 
function onLoad() {
    var version = getSilverlightVersion();
    if (version) { __utmSetVar(version); }
}

function getSilverlightVersion() {
    var version = '';
    var container = null;
    try {
        var control = null;
        if (window.ActiveXObject) {
            control = new ActiveXObject('AgControl.AgControl');
        }
        else {
            if (navigator.plugins['Silverlight Plug-In']) {
                container = document.createElement('div');
                document.body.appendChild(container);
                container.innerHTML = '<embed type="application/x-silverlight" src="data:," />';
                control = container.childNodes[0];
            }
        }
        if (control) {
            if (control.isVersionSupported('2.0')) { version = 'Silverlight/2.0'; }
            else if (control.isVersionSupported('1.0')) { version = 'Silverlight/1.0'; }
        }
    }
    catch (e) { }
    if (container) {
        document.body.removeChild(container);
    }
    return version;
}
