﻿// Cross Browser Support for Namespaces
if ("undefined" === typeof registerNamespace) {
    registerNamespace = function(ns) {
        var rootObject = window;
        var namespaceParts = ns.split(".");
        for (var index = 0; index < namespaceParts.length; index++) {
            var currentPart = namespaceParts[index];
            if (!rootObject[currentPart]) rootObject[currentPart] = new Object();
            rootObject = rootObject[currentPart];
        }
    }
}

registerNamespace('GoPromotions.Detect');
registerNamespace('GoPromotions.Detect.Player');
registerNamespace('GoPromotions.Detect.Fonts');

GoPromotions.Detect.installedFonts = null;
GoPromotions.Detect.installedPlayers = null;
GoPromotions.Detect.loopCount = null; 

GoPromotions.Detect.Initialize = function(requestURL) {
    var fontenumerator = document.getElementById('fontenumerator');
    if (fontenumerator == null) setTimeout('GoPromotions.Detect.Initialize(\'' + requestURL + '\')', 250);
    else GoPromotions.Detect.Capabilities(requestURL);
}
GoPromotions.Detect.Capabilities = function (requestURL) {
    GoPromotions.Detect.loopCount = 25;
    GoPromotions.Detect.installedPlayers = GoPromotions.G2.ReadCookie('videoSupport');
    GoPromotions.Detect.installedFonts = GoPromotions.G2.ReadCookie('fontSupport0');
    if ((GoPromotions.Detect.installedFonts == '|unknown|') || (GoPromotions.Detect.installedFonts == '|unsupported|')) GoPromotions.Detect.installedPlayers = null;
    if (GoPromotions.Detect.installedPlayers == null) {
        GoPromotions.Detect.installedPlayers = GoPromotions.Detect.Players();
        GoPromotions.G2.CreateCookie('videoSupport', GoPromotions.Detect.installedPlayers, 30);
    }
    if (GoPromotions.Detect.installedFonts == null) {
        if (GoPromotions.Detect.installedPlayers.indexOf('|flash|') > -1) {
            setTimeout('GoPromotions.Detect.Fonts(\'' + requestURL + '\')', 10);
            return;
        }
        else GoPromotions.G2.CreateCookie('fontSupport0', '|flashfailed|', 30);
    }
    setTimeout('GoPromotions.Detect.Redirect(\'' + requestURL + '\')', 100);
}



GoPromotions.Detect.Players = function() {
    installedPlayers = '|';
    if (GoPromotions.Detect.CanDetectPlugins()) {
        if (GoPromotions.Detect.DetectMediaPlayer()) installedPlayers = installedPlayers + 'mediaplayer|';
        if (GoPromotions.Detect.DetectRealPlayer()) installedPlayers = installedPlayers + 'realplayer|';
        if (GoPromotions.Detect.DetectQuickTime()) installedPlayers = installedPlayers + 'quicktime|';
        if (GoPromotions.Detect.DetectFlash()) installedPlayers = installedPlayers + 'flash|';
        if (GoPromotions.Detect.DetectDirector()) installedPlayers = installedPlayers + 'director|';
        if (installedPlayers == '') installedPlayers = 'none|';
    }
    else installedPlayers = installedPlayers + 'unknown|';
    GoPromotions.G2.CreateCookie('videoSupport', installedPlayers, 30);
    return installedPlayers
}

GoPromotions.Detect.CanDetectPlugins = function() {
    if (("undefined" != typeof ActiveXObject) || (navigator.plugins && navigator.plugins.length > 0)) {
        return true;
    } else {
        return false;
    }
}

GoPromotions.Detect.DetectActiveXControl = function(activeXControlName) {
    try {
        objObject = new ActiveXObject(activeXControlName);
        if (objObject == null) {
            return false;
        }
        else {
            objObject = null;
            return true;
        }
    }
    catch (ex) {
        return false;
    }
}

GoPromotions.Detect.DetectFlash = function() {
    var pluginFound = GoPromotions.Detect.DetectPlugin('Shockwave', 'Flash');
    if (!pluginFound) {
        pluginFound = GoPromotions.Detect.DetectActiveXControl('ShockwaveFlash.ShockwaveFlash.1');
    }
    return pluginFound;
}

GoPromotions.Detect.DetectDirector = function() {
    var pluginFound = GoPromotions.Detect.DetectPlugin('Shockwave', 'Director');
    if (!pluginFound) {
        pluginFound = GoPromotions.Detect.DetectActiveXControl('SWCtl.SWCtl.1');
    }
    return pluginFound;
}

GoPromotions.Detect.DetectQuickTime = function() {
    var pluginFound = GoPromotions.Detect.DetectPlugin('QuickTime');
    if (!pluginFound) {
        try {
            objObject = new ActiveXObject('QuickTimeCheckObject.QuickTimeCheck.1');
            if (objObject == null) {
                pluginFound = false;
            }
            else {
                pluginFound = objObject.IsQuickTimeAvailable(0);
                objObject = null;
            }
        }
        catch (ex) {
            pluginFound = false;
        }
    }
    return pluginFound;
}

GoPromotions.Detect.DetectRealPlayer = function() {
    var pluginFound = GoPromotions.Detect.DetectPlugin('RealPlayer');
    if (!pluginFound) {
        pluginFound = (GoPromotions.Detect.DetectActiveXControl('rmocx.RealPlayer G2 Control') ||
            GoPromotions.Detect.DetectActiveXControl('RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') ||
            GoPromotions.Detect.DetectActiveXControl('RealVideo.RealVideo(tm) ActiveX Control (32-bit)'));
    }
    return pluginFound;
}

GoPromotions.Detect.DetectMediaPlayer = function() {
    var pluginFound = GoPromotions.Detect.DetectPlugin('Windows Media');
    if (!pluginFound) {
        pluginFound = GoPromotions.Detect.DetectActiveXControl('MediaPlayer.MediaPlayer.1');
    }
    return pluginFound;
}

GoPromotions.Detect.DetectPlugin = function() {
    var daPlugins = GoPromotions.Detect.DetectPlugin.arguments;
    var pluginFound = false;
    if (navigator.plugins && navigator.plugins.length > 0) {
        var pluginsArrayLength = navigator.plugins.length;
        for (pluginsArrayCounter = 0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++) {
            var numFound = 0;
            for (namesCounter = 0; namesCounter < daPlugins.length; namesCounter++) {
                if ((navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) ||
		            (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0)) {
                    numFound++;
                }
            }
            if (numFound == daPlugins.length) {
                pluginFound = true;
                break;
            }
        }
    }
    return pluginFound;
}

GoPromotions.Detect.Fonts = function (requestURL) {
    if (GoPromotions.Detect.installedFonts != null) {
        GoPromotions.Detect.UpdateFontCookie();
        setTimeout('GoPromotions.Detect.Redirect(\'' + requestURL + '\')', 10);
    }
    else {
        var objFonts = document.getElementById('fontenumerator');
        if (objFonts != null) {
            if (typeof (objFonts.GetVariable) != 'undefined') {
                try {
                    if (typeof (objFonts.GetVariable('/:fontList')) != 'undefined') {
                        var fonts = objFonts.GetVariable('/:fontList');
                        if (fonts != null) GoPromotions.Detect.UpdateInstalledFonts(fonts);
                    }
                }
                catch (ex) {
                    if (GoPromotions.Detect.loopCount = 0) GoPromotions.Detect.UpdateInstalledFonts();
                }
            }
        }
        GoPromotions.Detect.loopCount = GoPromotions.Detect.loopCount - 1;
        setTimeout('GoPromotions.Detect.Fonts(\'' + requestURL + '\')', 100);
    }
}
GoPromotions.Detect.UpdateInstalledFonts = function (fontList) {
    if (typeof (fontList) != 'undefined') GoPromotions.Detect.installedFonts = fontList;
    else GoPromotions.Detect.installedFonts = '|flashfailed|'
}
GoPromotions.Detect.UpdateFontCookie = function () {
    var fonts = GoPromotions.Detect.installedFonts;
    var index = 0;
    while (fonts.length > 0) {
        if (fonts.length < 2001) {
            GoPromotions.G2.CreateCookie('fontSupport' + index, fonts, 30);
            fonts = '';
        }
        else {
            var index2 = fonts.indexOf('|', 1950);
            var index3;
            while (index2 < 2000) {
                index3 = index2;
                index2 = fonts.indexOf('|', index2 + 1);
            }
            GoPromotions.G2.CreateCookie('fontSupport' + index, fonts.substring(0, index3), 30);
            fonts = fonts.substring(index3, fonts.length);
        }
        index++;
    }
}

GoPromotions.Detect.Redirect = function (requestURL) {
    window.location.href = requestURL;
}
