
/**
* WidevineMediaTransformer
*
* taken from Widevine Adaptive Streaming - 4.5.0.3913
*
* sogden 8-19-11 widevine FF plugin upgrades
*
*/

(function ($) {

    function WidevineMediaTransformer() {

        this.name = ("WidevineMediaTransformer-" + (new Date()).getTime());


        this.auto_install_upgrade = true;
        this.prompt_upgrade = true;

        // Set installer location

        this.base_installer_path = AppVars.WidevinePluginPath;

        this.windows_chrome_installer_exe = this.base_installer_path + "/WidevineMediaTransformerChrome.exe";
        this.windows_activex_installer_exe = this.base_installer_path + "/WidevineMediaTransformer.exe";
        this.windows_activex_location_cab = this.base_installer_path + "/WidevineMediaTransformer.cab";
        this.windows_activex_location_xp_cab = this.base_installer_path + "/WidevineMediaTransformer_xp.cab";
        this.windows_firefox_location = this.base_installer_path + "/WidevineMediaTransformer_windows.xpi";
        this.macintosh_firefox_location = this.base_installer_path + "/WidevineMediaTransformer_osx.xpi";
        this.safari_location = this.base_installer_path + "/WidevineMediaTransformer.dmg";

        //
        this.pluginMessage_detecting = "Detecting the Widevine Plugin...";

        this.pluginMessage_win_chrome = "";
        this.pluginMessage_win_chrome += "<ul>";
        this.pluginMessage_win_chrome += "  <li>You must install the Widevine plugin <\/li>";
        this.pluginMessage_win_chrome += "  <li>Double click the WidevineMediaTransformer.exe installer in the status bar <\/li>";
        this.pluginMessage_win_chrome += "  <li>Press \"Run\" to begin installing<\/li>";
        this.pluginMessage_win_chrome += "  <li>After the installer completes, you must refresh browser to continue watching this content<\/li>";
        this.pluginMessage_win_chrome += "<\/ul>";

        this.pluginMessage_mac_chrome = "";
        this.pluginMessage_mac_chrome += "<ul>";
        this.pluginMessage_mac_chrome += "  <li>You must install the Widevine plugin <\/li>";
        this.pluginMessage_mac_chrome += "  <li>Double click the WidevineMediaTransformer.dmg installer in the status bar <\/li>";
        this.pluginMessage_mac_chrome += "  <li>Double click the Widevine Media Transformer Plug-In Installer<\/li>";
        this.pluginMessage_mac_chrome += "  <li>Click \"Open\" to open the dmg installer<\/li>";
        this.pluginMessage_mac_chrome += "  <li>Enter your password<\/li>";
        this.pluginMessage_mac_chrome += "  <li>Click \"Restart Now\" to restart your browser<\/li>";
        this.pluginMessage_mac_chrome += "  <li>You may have to navigate back to this page after your browser restarts<\/li>";
        this.pluginMessage_mac_chrome += "<\/ul>";
        this.pluginMessage_mac_chrome += "";



        this.pluginMessage_win_safari = "";
        this.pluginMessage_win_safari += "<ul>";
        this.pluginMessage_win_safari += "  <li>Unfortunately Safari on Windows is not a supported browser for Widevine protected content<\/li>";
        this.pluginMessage_win_safari += "  <li>Please try viewing in Chrome, Firefox, or Internet Explorer<\/li>";
        this.pluginMessage_win_safari += "<\/ul>";


        this.pluginMessage_mac_safari = "";
        this.pluginMessage_mac_safari += "<ul>";
        this.pluginMessage_mac_safari += "  <li>You must to install the Widevine plugin <\/li>";
        this.pluginMessage_mac_safari += "  <li>Double click the WidevineMediaTransformer.dmg installer in the status bar <\/li>";
        this.pluginMessage_mac_safari += "  <li>Double click the Widevine Media Transformer Plug-In Installer<\/li>";
        this.pluginMessage_mac_safari += "  <li>Click \"Open\" to open the dmg installer<\/li>";
        this.pluginMessage_mac_safari += "  <li>Enter your password<\/li>";
        this.pluginMessage_mac_safari += "  <li>Click \"Restart Now\" to restart your browser<\/li>";
        this.pluginMessage_mac_safari += "  <li>You may have to navigate back to this page after your browser restarts<\/li>";
        this.pluginMessage_mac_safari += "<\/ul>";

        this.pluginMessage_mac_firefox = "";
        this.pluginMessage_mac_firefox += "<ul>";
        this.pluginMessage_mac_firefox += "  <li>You must install the Widevine plugin <\/li>";
        this.pluginMessage_mac_firefox += "  <li>Click \"Allow\" in the prompt above to download the necessary plugin<\/li>";
        this.pluginMessage_mac_firefox += "  <li>Click \"Install Now\" in the Firefox add-on prompt<\/li>";
        this.pluginMessage_mac_firefox += "  <li>Restart your browser and enter your password to complete the installation<\/li>";
        this.pluginMessage_mac_firefox += "<\/ul>";


        this.pluginMessage_win_firefox = "";
        this.pluginMessage_win_firefox += "<ul>";
        this.pluginMessage_win_firefox += "  <li>You must install the Widevine plugin <\/li>";
        this.pluginMessage_win_firefox += "  <li>Click \"Allow\" in the prompt above to download the necessary plugin<\/li>";
        this.pluginMessage_win_firefox += "  <li>Click \"Install Now\" in the Firefox add-on prompt<\/li>";
        this.pluginMessage_win_firefox += "  <li>Restart your browser to complete the installation<\/li>";
        this.pluginMessage_win_firefox += "<\/ul>";

        this.pluginMessage_win_ie = "";
        this.pluginMessage_win_ie += "<ul>";
        this.pluginMessage_win_ie += "  <li>You must install the Widevine plugin <\/li>";
        this.pluginMessage_win_ie += "  <li>Click \"Install\" when prompted<\/li>";
        this.pluginMessage_win_ie += "  <li>You may need to restart your browser if the video doesn't begin playing after the installer completes<\/li>";
        this.pluginMessage_win_ie += "<\/ul>";

        this.pluginMessage_mac_safari = "";
        this.pluginMessage_mac_safari += "<ul>";
        this.pluginMessage_mac_safari += "  <li>You must install the Widevine plugin<\/li>";
        this.pluginMessage_mac_safari += "  <li>Double click the Widevine Media Transformer Plug-In Installer<\/li>";
        this.pluginMessage_mac_safari += "  <li>Click \"Open\" to open the dmg installer<\/li>";
        this.pluginMessage_mac_safari += "  <li>Enter your password<\/li>";
        this.pluginMessage_mac_safari += "  <li>Click \"Restart Now\" to restart your browser<\/li>";
        this.pluginMessage_mac_safari += "  <li>You may have to navigate back to this page after your browser restarts<\/li>";
        this.pluginMessage_mac_safari += "<\/ul>";





        // Version of plugin pointed by the installer

        this.version = AppVars.WidevinePluginVersion;
        this.ie_version = AppVars.WidevinePluginIEVersion;

        this.plugin_version = ""; // dynamic

        // Set the head end server

        // Widevine Production
        this.signon_url = "https://fcpstage.shibboleth.tv/widevine/cypherpc/cgi-bin/SignOn.cgi";
        this.log_url = "https://fcpstage.shibboleth.tv/widevine/cypherpc/cgi-bin/LogEncEvent.cgi";
        this.emm_url = "https://fcpstage.shibboleth.tv/widevine/cypherpc/cgi-bin/GetEMMs.cgi";

        this.portal = "starz";

        this.xpi_mac = { 'Widevine Media Transformer Plugin': this.macintosh_firefox_location };
        this.xpi_win = { 'Widevine Media Transformer Plugin': this.windows_firefox_location };

        this.java_installed = false;
        this.current_ver = true;
        this.no_upgrade = false;

        // debug

        this.debug = true;
        this.debug_flags = "";

        this.logging = true;

        // NOTE: ie doesn't have a built-in console logger!
        if ((this.logging == true) && (typeof (console) == "undefined")) { this.logging = false; }
    }

    // ----------------------------------------------------------------------- //
    // Init method
    // ----------------------------------------------------------------------- //

    WidevineMediaTransformer.prototype.init = function (aInitCallback, aErrorCallback) {
        widevineMediaTransformer.log("init");

        try {

            var div = widevineMediaTransformer.AddDiv(widevineMediaTransformer.EmbedText());

            if (!widevineMediaTransformer.detectIE()) {
                var aWidevinePlugin = document.getElementById('WidevinePlugin');
                if (aWidevinePlugin == null) {
                    widevineMediaTransformer.EmbedUpgrade(div, false);
                }
                else if (aWidevinePlugin != null
					&& (!aWidevinePlugin.GetVersion || widevineMediaTransformer.upgradeCheck(aWidevinePlugin.GetVersion()))) {
                    widevineMediaTransformer.EmbedUpgrade(div, true);
                }
            } else {
                if (widevineMediaTransformer.upgradeIEPlugin()) {
                    widevineMediaTransformer.EmbedUpgrade(div, true);
                }
            }

            if (typeof (aInitCallback) != "undefined") {
                aInitCallback();
            }

        }
        catch (e) {
            //alert("widevine.init exception: " + e.message);

            if (typeof (aErrorCallback) != "undefined") {
                aErrorCallback();
            }
        }
    }


    // ----------------------------------------------------------------------- //
    // Detection methods
    // ----------------------------------------------------------------------- //
    WidevineMediaTransformer.prototype.doDetect = function (type, value) {
        return eval('navigator.' + type + '.toLowerCase().indexOf("' + value + '") != -1');
    }

    WidevineMediaTransformer.prototype.detectMac = function () { return widevineMediaTransformer.doDetect("platform", "mac"); }
    WidevineMediaTransformer.prototype.detectWin32 = function () { return widevineMediaTransformer.doDetect("platform", "win32"); }
    WidevineMediaTransformer.prototype.detectIE = function () { return widevineMediaTransformer.doDetect("userAgent", "msie"); }
    WidevineMediaTransformer.prototype.detectFirefox = function () { return widevineMediaTransformer.doDetect("userAgent", "firefox"); }
    WidevineMediaTransformer.prototype.detectSafari = function () { return widevineMediaTransformer.doDetect("userAgent", "safari"); }
    WidevineMediaTransformer.prototype.detectChrome = function () { return widevineMediaTransformer.doDetect("userAgent", "chrome"); }
    WidevineMediaTransformer.prototype.detectVistaOrWindows7 = function () { return widevineMediaTransformer.doDetect("userAgent", "windows nt 6"); }




    WidevineMediaTransformer.prototype.versionInstalled = function (v) {
        return widevineMediaTransformer.upgradeCheck(v);
    }

    WidevineMediaTransformer.prototype.pluginInstalled = function () {
        var plugin;
        if (eval('navigator.userAgent.toLowerCase().indexOf("msie") != -1')) {
            try {
                var o = new ActiveXObject("WidevineMediaTransformerInstall.InstallCheck");
                widevineMediaTransformer.plugin_version = o.installVersion;
                if (widevineMediaTransformer.plugin_version == "0.0.0.0000")
                    return false;
                else
                    return true;
            } catch (e) {
                return false;
            }
        } else {
            plugin = document.getElementById('WidevinePlugin');

            if (plugin) {
                try {
                    widevineMediaTransformer.plugin_version = plugin.GetVersion();
                    return true;
                } catch (e) {
                    return false;
                }
            } else {
                return false;
            }
        }
    }

    // ----------------------------------------------------------------------- //
    // Install methods
    // ----------------------------------------------------------------------- //

    WidevineMediaTransformer.prototype.AddDiv = function (html) {
        var div = document.createElement("div");
        document.body.appendChild(div);
        div.innerHTML = html;
        return div;
    }

    ////////////////////////////////////////////
    // checkVersion
    //
    // Checks if the installed version is lesser than version in the js
    // check_version: version of the plugin installed
    ////////////////////////////////////////////
    WidevineMediaTransformer.prototype.checkVersion = function (check_version) {
        var currentVer = widevineMediaTransformer.version.split(".");
        var installedVer = check_version.split(".");

        if (currentVer[0] > installedVer[0]) {
            return true;
        } else if ((currentVer[1] > installedVer[1])
                        	&& (parseInt(currentVer[0]) == parseInt(installedVer[0]))) {
            return true;
        } else if ((currentVer[2] > installedVer[2])
                        	&& (parseInt(currentVer[1]) == parseInt(installedVer[1]))) {
            return true;
        } else if ((currentVer[3] > installedVer[3])
                        	&& (parseInt(currentVer[2]) == parseInt(installedVer[2]))) {
            return true;
        } else {
            return false;
        }
    }

    ////////////////////////////////////////////
    // upgradeCheck
    //
    // Checks if the WidevineMediaTransformer plugin needs to be upgraded
    // installedVersion: version of the plugin installed
    ////////////////////////////////////////////
    WidevineMediaTransformer.prototype.upgradeCheck = function (installedVersion) {
        if (installedVersion == "") {
            return true;
        } else if (!widevineMediaTransformer.auto_install_upgrade) {
            return false;
        } else if (widevineMediaTransformer.no_upgrade) {
            return false;
        }

        return widevineMediaTransformer.checkVersion(installedVersion);
    }

    // chrome

    ////////////////////////////////////////////
    // chromeInstallWin
    //
    // Opens chrome windows installer
    ////////////////////////////////////////////
    WidevineMediaTransformer.prototype.chromeInstallWin = function () {
        window.open(widevineMediaTransformer.windows_chrome_installer_exe, '_blank');
        widevineMediaTransformer.InsertChromeInstallWinText();
    }

    ////////////////////////////////////////////
    // chromeInstallMac
    //
    // Opens chrome mac installer
    ////////////////////////////////////////////
    WidevineMediaTransformer.prototype.chromeInstallMac = function () {
        window.open(widevineMediaTransformer.safari_location, '_blank');
        widevineMediaTransformer.InsertChromeInstallMacText();
    }

    // ie

    ////////////////////////////////////////////
    // pluginInstalledIE
    //
    // Used the InstallCheck plugin to check if WidevineMediaTransformer plugin is installed
    // Note: InstallCheck plugin was introduced in 4.4.5 release
    ////////////////////////////////////////////
    WidevineMediaTransformer.prototype.pluginInstalledIE = function () {
        try {
            var o = new ActiveXObject("WidevineMediaTransformerInstall.InstallCheck");
            if (o.installVersion != "0.0.0.3567")
                return true;
            else
                return false;
        } catch (e) {
            return false;
        }
    }

    ////////////////////////////////////////////
    // upgradeIEPlugin
    //
    // Used the InstallCheck plugin to check if WidevineMediaTransformer plugin needs to be upgraded
    // Note: InstallCheck plugin was introduced in 4.4.5 release
    ////////////////////////////////////////////
    WidevineMediaTransformer.prototype.upgradeIEPlugin = function () {
        try {
            var o = new ActiveXObject("WidevineMediaTransformerInstall.InstallCheck");
            var ieversion = o.installVersion;
            return widevineMediaTransformer.checkVersion(ieversion);
        } catch (e) {
            return false;
        }
    }

    ////////////////////////////////////////////
    // EmbedText
    //
    // Returns embed or object tag for the initializing WidevineMediaTransformer plugin
    ////////////////////////////////////////////
    WidevineMediaTransformer.prototype.EmbedText = function () {
        if (widevineMediaTransformer.detectIE()) {
            if (widevineMediaTransformer.auto_install_upgrade && !widevineMediaTransformer.pluginInstalledIE()) {
                if (widevineMediaTransformer.detectVistaOrWindows7() ||
					navigator.appVersion.indexOf("MSIE 8.0") == -1) {

                    swfobject.getObjectById("StarzVideoPlayer").WvShowPluginMessage(widevineMediaTransformer.pluginMessage_win_ie);

                    var codebase = widevineMediaTransformer.windows_activex_installer_exe + "#version=" + widevineMediaTransformer.ie_version;

                    return ('<object id="WidevinePlugin" classid=CLSID:f8eb59ec-35a8-4b59-8f67-b3e19147fed6 ' +
							'codebase="' + codebase + '" ' +
							'hidden=true style="display:none" height="0" width="0">' +
							'<param name="default_url" value="' + widevineMediaTransformer.signon_url + '">' +
							'<param name="emm_url" value="' + widevineMediaTransformer.emm_url + '">' +
							'<param name="log_url" value="' + widevineMediaTransformer.log_url + '">' +
							'<param name="portal" value="' + widevineMediaTransformer.portal + '">' +
										'<param name="user_agent" value="' + navigator.userAgent + '">' +
							'</object>');
                }
                else {
                    widevineMediaTransformer.InsertIE8InstallText();
                    return "";
                }
            }
            else if (!widevineMediaTransformer.auto_install_upgrade || (widevineMediaTransformer.pluginInstalledIE() && !widevineMediaTransformer.upgradeIEPlugin())) {

                swfobject.getObjectById("StarzVideoPlayer").WvShowPluginMessage(widevineMediaTransformer.pluginMessage_detecting);

                return ('<object id="WidevinePlugin" classid=CLSID:f8eb59ec-35a8-4b59-8f67-b3e19147fed6 ' +
						'hidden=true style="display:none" height="0" width="0">' +
						'<param name="default_url" value="' + widevineMediaTransformer.signon_url + '">' +
						'<param name="emm_url" value="' + widevineMediaTransformer.emm_url + '">' +
						'<param name="log_url" value="' + widevineMediaTransformer.log_url + '">' +
						'<param name="portal" value="' + widevineMediaTransformer.portal + '">' +
						'<param name="user_agent" value="' + navigator.userAgent + '">' +
						'</object>');
            }
            else {
                return "";
            }
        }
        else {
            if (navigator.mimeTypes['application/x-widevinemediatransformer']) {
                widevineMediaTransformer.setCookie("FirefoxDisabledCheck", "");

                swfobject.getObjectById("StarzVideoPlayer").WvShowPluginMessage(widevineMediaTransformer.pluginMessage_detecting);

                return ('<embed id="WidevinePlugin" type="application/x-widevinemediatransformer" default_url="' + widevineMediaTransformer.signon_url +
						'" emm_url="' + widevineMediaTransformer.emm_url +
						'" log_url="' + widevineMediaTransformer.log_url +
						'" portal="' + widevineMediaTransformer.portal +
						'" height="0" width="0' +
								'" user_agent="' + navigator.userAgent +
						'">');
            }
            else {
                return "";
            }
        }
    }

    ////////////////////////////////////////////
    // EmbedUpgrade
    //
    // Returns embed or object tag for upgrading initializion of WidevineMediaTransformer plugin
    ////////////////////////////////////////////
    WidevineMediaTransformer.prototype.EmbedUpgrade = function (div, upgrading) {
        if (!widevineMediaTransformer.auto_install_upgrade) {
            return "";
        }
        if (upgrading && widevineMediaTransformer.prompt_upgrade) {
            if (!confirm("A new version of the Widevine plugin is available.\nWould you like to upgrade?")) {
                if (widevineMediaTransformer.detectIE()) {
                    div.innerHTML = '<object id="WidevinePlugin" classid=CLSID:f8eb59ec-35a8-4b59-8f67-b3e19147fed6 ' +
											'hidden=true style="display:none" height="0" width="0">' +
											'<param name="default_url" value="' + widevineMediaTransformer.signon_url + '">' +
											'<param name="emm_url" value="' + widevineMediaTransformer.emm_url + '">' +
											'<param name="log_url" value="' + widevineMediaTransformer.log_url + '">' +
											'<param name="portal" value="' + widevineMediaTransformer.portal + '">' +
											'<param name="user_agent" value="' + navigator.userAgent + '">' +
											'</object>';
                }
                widevineMediaTransformer.no_upgrade = true;
                return "";
            }
        }

        if (widevineMediaTransformer.detectIE()) {
            if (widevineMediaTransformer.upgradeIEPlugin()) {
                if (widevineMediaTransformer.detectVistaOrWindows7() || navigator.appVersion.indexOf("MSIE 8.0") == -1) {
                    var codebase = widevineMediaTransformer.windows_activex_installer_exe + "#version=" + widevineMediaTransformer.ie_version;

                    div.innerHTML = '<object id="WidevinePlugin" classid=CLSID:f8eb59ec-35a8-4b59-8f67-b3e19147fed6 ' +
											'codebase="' + codebase + '" ' +
											'hidden=true style="display:none" height="0" width="0">' +
											'<param name="default_url" value="' + widevineMediaTransformer.signon_url + '">' +
											'<param name="emm_url" value="' + widevineMediaTransformer.emm_url + '">' +
											'<param name="log_url" value="' + widevineMediaTransformer.log_url + '">' +
											'<param name="portal" value="' + widevineMediaTransformer.portal + '">' +
											'<param name="user_agent" value="' + navigator.userAgent + '">' +
											'</object>';
                } else {
                    widevineMediaTransformer.InsertIE8InstallText();
                }
            }
        } else if (widevineMediaTransformer.detectChrome()) {
            if (widevineMediaTransformer.detectWin32()) {
                widevineMediaTransformer.chromeInstallWin();
            } else {
                widevineMediaTransformer.chromeInstallMac();
            }
        } else if (widevineMediaTransformer.detectSafari()) {
            if (widevineMediaTransformer.detectWin32()) {
                swfobject.getObjectById("StarzVideoPlayer").WvShowPluginMessage(widevineMediaTransformer.pluginMessage_win_safari);
            }
            else {

                widevineMediaTransformer.InsertSafariInstallText();
                //alert('message shoudl be updated for mac safari now');

                if (upgrading) {
                    setTimeout("window.open('" + widevineMediaTransformer.safari_location + "', '_self');", 1000);
                } else {
                    setTimeout("if (confirm('Would you like to install the Widevine plugin?')){window.open('" + widevineMediaTransformer.safari_location + "', '_self');}", 1000);
                }
            }
        } else if (widevineMediaTransformer.detectFirefox()) {
            var install_loop_check = widevineMediaTransformer.getCookie("FirefoxDisabledCheck")

            if (install_loop_check != "") {
                var i = parseInt(install_loop_check)
                if (i > 3) {

                    swfobject.getObjectById("StarzVideoPlayer").WvShowPluginMessage("Please check if 'Widevine Media Transformer Plugin' is disabled.");
                    //alert("Please check if 'Widevine Media Transformer Plugin' is disabled.");
                }
                i++;
                widevineMediaTransformer.setCookie("FirefoxDisabledCheck", "" + i);
            } else {
                widevineMediaTransformer.setCookie("FirefoxDisabledCheck", "1");
            }



            // SAO - NOTE: I replaced jQuery InstallTrigger with window.open because Firefox4 was not working with InstallTrigger	
            if (widevineMediaTransformer.detectMac()) {

                swfobject.getObjectById("StarzVideoPlayer").WvShowPluginMessage(widevineMediaTransformer.pluginMessage_mac_firefox);
                window.open(widevineMediaTransformer.macintosh_firefox_location, '_self');
            } else {

                swfobject.getObjectById("StarzVideoPlayer").WvShowPluginMessage(widevineMediaTransformer.pluginMessage_win_firefox);
                window.open(widevineMediaTransformer.windows_firefox_location, '_self');
            }
        }
        return "";
    }

    ////////////////////////////////////////////
    // InsertIE8InstallText
    //
    // Displays install instructions for IE8
    ////////////////////////////////////////////
    WidevineMediaTransformer.prototype.InsertIE8InstallText = function () {
        swfobject.getObjectById("StarzVideoPlayer").WvShowPluginMessage(widevineMediaTransformer.pluginMessage_win_ie);
    }

    ////////////////////////////////////////////
    // InsertSafariInstallText
    //
    // Displays install instructions for Safari
    ////////////////////////////////////////////
    WidevineMediaTransformer.prototype.InsertSafariInstallText = function () {
        swfobject.getObjectById("StarzVideoPlayer").WvShowPluginMessage(widevineMediaTransformer.pluginMessage_mac_safari);
        WVPluginCheck();
    }

    ////////////////////////////////////////////
    // InsertChromeInstallWinText
    //
    // Displays install instructions for chrome on the pc
    ////////////////////////////////////////////
    WidevineMediaTransformer.prototype.InsertChromeInstallWinText = function () {
        swfobject.getObjectById("StarzVideoPlayer").WvShowPluginMessage(widevineMediaTransformer.pluginMessage_win_chrome);
        WVPluginCheck();
    }

    ////////////////////////////////////////////
    // InsertChromeInstallMacText
    //
    // Displays install instructions for chrome on the macintosh
    ////////////////////////////////////////////
    WidevineMediaTransformer.prototype.InsertChromeInstallMacText = function () {
        swfobject.getObjectById("StarzVideoPlayer").WvShowPluginMessage(widevineMediaTransformer.pluginMessage_mac_chrome);
        WVPluginCheck();
    }


    // ----------------------------------------------------------------------- //
    // Cookie methods
    // ----------------------------------------------------------------------- //

    WidevineMediaTransformer.prototype.getCookie = function (c_name) {
        if (document.cookie.length > 0) {
            var c_start = document.cookie.indexOf(c_name + "=")
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) c_end = document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end))
            }
        }
        return ""
    }

    WidevineMediaTransformer.prototype.setCookie = function (c_name, value, expireseconds) {
        var exdate = new Date();
        exdate.setSeconds(exdate.getSeconds() + expireseconds);
        document.cookie = c_name + "=" + escape(value) +
            ((expireseconds == null) ? "" : ";expires=" + exdate.toGMTString())
    }

    // ----------------------------------------------------------------------- //
    // Logger
    // ----------------------------------------------------------------------- //

    WidevineMediaTransformer.prototype.log = function (aStr) {
        if (widevineMediaTransformer.logging) {
            var tStr = "WidevineMediaTransformer - " + aStr;
            console.log(tStr);
        }
    }

    // ----------------------------------------------------------------------- //
    // Constructor
    // ----------------------------------------------------------------------- //

    // Create a new instance of the widevineMediaTransformer and store it in the window.
    window.widevineMediaTransformer = new WidevineMediaTransformer();

    // Return a new widevineMediaTransformer instance.
    return (window.widevineMediaTransformer);

})(jQuery);



function WvInitWidevine() {

    //alert('WvInitWidevine');

    // we must have widevine to proceed
    widevineMediaTransformer.init(WVCheckWidevineInstall, WVCheckWidevineInstallError);
}

function WVCheckWidevineInstall() {

    //Checking for Plugin

    if (widevineMediaTransformer.pluginInstalled()) {
        if (!widevineMediaTransformer.versionInstalled(widevineMediaTransformer.plugin_version)) {

            swfobject.getObjectById("StarzVideoPlayer").WvInitWidevineSuccess(); // ExternalInterface call to notify player
			
        } else {
            setTimeout(WVCheckWidevineUpgrade, 1000);
        }
    } else {
        setTimeout(WVCheckWidevineInstall, 5000);
    }
}

function WVCheckWidevineUpgrade() {

    //Upgrade Plugin

    widevineMediaTransformer.pluginInstalled();
    if (!widevineMediaTransformer.versionInstalled(widevineMediaTransformer.plugin_version)) {
		
        swfobject.getObjectById("StarzVideoPlayer").WvInitWidevineSuccess(); // ExternalInterface call to notify player
		
    } else {
        setTimeout(WVCheckWidevineUpgrade, 1000);
    }

}

function WVCheckWidevineInstallError() {
    //alert('ERROR: Widevine not initialized properly...');

    swfobject.getObjectById("StarzVideoPlayer").WvInitWidevineError(); // ExternalInterface call to player
}



function WVGetURL( arg ) {
    var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
            transformedUrl = aWidevinePlugin.Translate( arg );
        }
        catch (err) {
            return "Error calling Translate: " + err.description;
        }
        return transformedUrl;
}
     
function WVGetCommURL () {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
                return aWidevinePlugin.GetCommandChannelBaseUrl();
        } catch (err) {
                //alert("Error calling GetCommandChannelBaseUrl: " + err.description);
        }
        return "http://localhost:20001/cgi-bin/";
}

function WVSetPlayScale( arg ) {
    var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
            return aWidevinePlugin.SetPlayScale( arg );
        }
        catch (err) {
            alert ("Error calling SetPlayScale: " + err.description);
        }
        return 0;
}

function WVGetMediaTime( arg ) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
            return aWidevinePlugin.GetMediaTime( arg );
        } catch (err) {
            alert("Error calling GetMediaTime: " + err.description);
        }
        return 0;
}

function WVGetClientId() {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
                return aWidevinePlugin.getClientId();
        }
        catch (err) {
                alert ("Error calling GetClientId: " + err.description);
        }
        return 0;
}


function WVSetDeviceId(arg) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.setDeviceId(arg);
        }
        catch (err) {
                alert ("Error calling SetDeviceId: " + err.description);
        }
        return 0;
}

function WVSetStreamId(arg) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.setStreamId(arg);
        }
        catch (err) {
                alert ("Error calling SetStreamId: " + err.description);
        }
        return 0;
}

function WVSetClientIp(arg) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.setClientIp(arg);
        }
        catch (err) {
                alert ("Error calling SetClientIp: " + err.description);
        }
        return 0;
}

function WVSetEmmURL(arg) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.setEmmUrl(arg);
        }
        catch (err) {
                alert ("Error calling SetEmmURL: " + err.description);
        }
        return 0;
}


function WVSetEmmAckURL(arg) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.setEmmAckUrl(arg);
        }
        catch (err) {
                alert ("Error calling SetEmmAckUrl: " + err.description);
        }
        return 0;
}

function WVSetHeartbeatUrl(arg) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.setHeartbeatUrl(arg);
        }
        catch (err) {
                alert ("Error calling SetHeartbeatUrl: " + err.description);
        }
        return 0;
}


function WVSetHeartbeatPeriod(arg) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.setHeartbeatPeriod(arg);
        }
        catch (err) {
                alert ("Error calling SetHeartbeatPeriod: " + err.description);
        }
        return 0;
}



function WVSetOptData(arg) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.setOptData(arg);
        }
        catch (err) {
                alert ("Error calling SetOptData: " + err.description);
        }
        return 0;
}

function WVGetDeviceId() {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.getDeviceId();
        }
        catch (err) {
                alert ("Error calling GetDeviceId: " + err.description);
        }
        return 0;
}

function WVGetStreamId() {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.getStreamId();
        }
        catch (err) {
                alert ("Error calling GetStreamId: " + err.description);
        }
        return 0;
}

function WVGetClientIp() {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.getClientIp();
        }
        catch (err) {
                alert ("Error calling GetClientIp: " + err.description);
        }
        return 0;
}


function WVGetEmmURL() {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.getEmmUrl();
        }
        catch (err) {
                alert ("Error calling GetEmmURL: " + err.description);
        }
        return "";
}


function WVGetEmmAckURL() {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.getEmmAckUrl();
        }
        catch (err) {
                alert ("Error calling GetEmmAckUrl: " + err.description);
        }
        return "";
}

function WVGetHeartbeatUrl() {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.getHeartbeatUrl();
        }
        catch (err) {
                alert ("Error calling GetHeartbeatUrl: " + err.description);
        }
        return "";
}



function WVGetHeartbeatPeriod() {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.getHeartbeatPeriod();
        }
        catch (err) {
                alert ("Error calling GetHeartbeatPeriod: " + err.description);
        }
        return "";
}


function WVGetOptData() {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.getOptData();
        }
        catch (err) {
                alert ("Error calling GetOptData: " + err.description);
        }
        return "";
}


function WVAlert( arg ) {
    alert(arg);
        return 0;
}

////////////////////////////////////////////////////////
// WVPluginCheck
//
// Helper function for chrome installer.  This function continues until the installed plugin version
// is equal to or greater than the version in the js.
///////////////////////////////////////////////////////

function WVPluginCheck(){
    try{
        navigator.plugins.refresh(false);
        if ( navigator.mimeTypes['application/x-widevinemediatransformer'] ) {
            var cur_ver = WVGetPluginVersionFromEmbed();
            //alert(cur_ver);
            if(!widevine.versionInstalled(cur_ver)){
                window.location.reload();
            }else{
                setTimeout("WVPluginCheck()", 15000);
            }
        }else{
            setTimeout("WVPluginCheck()", 2000);
        }
    }catch(e){
        //alert("WVPluginCheck: " + e.name + " "+ e.message);
    }
}

///////////////////////////////////////////////////////
// WVGetPluginVersionFromEmbed
//
// Gets the plugin version using the embed tag
///////////////////////////////////////////////////////
function WVGetPluginVersionFromEmbed(){
    var tmp_version = "";
    try{
        var tmp_wv = document.createElement('div'); 
        tmp_wv.id = 'tmpWv';
        tmp_wv.innerHTML = '<embed id="tmpWidevinePlugin" type="application/x-widevinemediatransformer" >'
        document.body.appendChild(tmp_wv);
        if(document.getElementById("tmpWidevinePlugin")){
            try{
                tmp_version = document.getElementById("tmpWidevinePlugin").GetVersion();
            }catch(e){
            
            }
            document.body.removeChild(tmp_wv);
        }
    }catch(e){
        //alert("WVGetPluginVersionFromEmbed: " + e.name + "  " + e.message);
    }
    return tmp_version;
    
}

function WVPDLNew(mediaPath, pdlPath) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
                pdl_new =  aWidevinePlugin.PDL_New(mediaPath, pdlPath);
                return pdl_new;
        }
        catch (err) {
               //alert ("Error calling PDL_New: " + err.description);
        }
        return "";
}

function WVPDLStart(pdlPath, trackNumber, trickPlay) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.PDL_Start(pdlPath, trackNumber, trickPlay);
        }
        catch (err) {
               //alert ("Error calling PDL_Start: " + err.description);
        }
        return "";
}

function WVPDLResume(pdlPath) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.PDL_Resume(pdlPath);
        }
        catch (err) {
               //alert ("Error calling PDL_Resume: " + err.description);
        }
        return "";
}


function WVPDLStop(pdlPath) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.PDL_Stop(pdlPath);
        }
        catch (err) {
               //alert ("Error calling PDL_Stop: " + err.description);
        }
        return "";
}

function WVPDLCancel(pdlPath) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.PDL_Cancel(pdlPath);
        }
        catch (err) {
               //alert ("Error calling PDL_Stop: " + err.description);
        }
        return "";
}

function WVPDLGetProgress(pdlPath) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.PDL_GetProgress(pdlPath);
        }
        catch (err) {
               //alert ("Error calling PDL_GetProgress: " + err.description);
        }
        return "";
}


function WVPDLGetTotalSize(pdlPath) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.PDL_GetTotalSize(pdlPath);
        }
        catch (err) {
               //alert ("Error calling PDL_GetTotalSize: " + err.description);
        }
        return "";
}

function WVPDLFinialize(pdlPath) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.PDL_Finialize(pdlPath);
        }
        catch (err) {
               //alert ("Error calling PDL_Finialize: " + err.description);
        }
        return "";
}

function WVPDLCheckHasTrickPlay(pdlPath) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.PDL_CheckHasTrickPlay(pdlPath);
        }
        catch (err) {
               //alert ("Error calling PDL_CheckHasTrickPlay: " + err.description);
        }
        return "";
}

function WVPDLGetTrackBitrate(pdlPath, trackNumber) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.PDL_GetTrackBitrate(pdlPath, trackNumber);
        }
        catch (err) {
               //alert ("Error calling PDL_GetTrackBitrate: " + err.description);
        }
        return "";
}

function WVPDLGetTrackCount(pdlPath) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.PDL_GetTrackCount(pdlPath);
        }
        catch (err) {
                //alert ("Error calling PDL_GetTrackCount: " + err.description);
        }
        return "";
}

function WVPDLGetDownloadMap(pdlPath) {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.PDL_GetDownloadMap(pdlPath);
        }
        catch (err) {
                //alert ("Error calling PDL_GetDownloadMap: " + err.description);
        }
        return "";
}

function WVPDLGetLastError() {
        var aWidevinePlugin = document.getElementById('WidevinePlugin');
        try {
               return aWidevinePlugin.PDL_GetLastError();
        }
        catch (err) {
               //alert ("Error calling PDL_GetLastError: " + err.description);
        }
        return "";
}
