﻿// JScript File

WMPObject = function(mov, id, w, h) 
{ 
    this.mov = mov; 
    this.id = id; 
    this.width = w; 
    this.height = h; 
    this.redirect = ""; 

    this.sq = document.location.search.split("?")[1] || ""; 
    this.altTxt = "This content requires the Microsoft Windows Media Plugin. <a href='http://www.microsoft.com/windows/windowsmedia/download/' target='_blank'>Download Windows Media Player</a>."; 

    this.bypassTxt = "<p>Already have Windows Media Player? <a href='?detectwmp=false&"+ this.sq +"' target='_blank'>Click here.</a></p>"; 

    this.params = new Object(); 
    this.doDetect = getQueryParamValue('detectwmp'); 
} 

WMPObject.prototype.addParam = function(name, value) 
{ 
    this.params[name] = value; 
} 

WMPObject.prototype.getParams = function() 
{
    return this.params; 
} 


WMPObject.prototype.getParam = function(name) 
{ 
    return this.params[name]; 
} 


WMPObject.prototype.getParamTags = function() 
{ 
    var paramTags = ""; 
    
    for (var param in this.getParams()) 
    { 
        paramTags += '<param name="' + param + '" value="' + this.getParam(param) + '" />'; 
    } 

    if (paramTags == "") 
    { 
        paramTags = null; 
    } 

    return paramTags; 
} 

var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
var is_ff     = (agt.indexOf("firefox") != -1);
var is_opera  = (agt.indexOf("opera") != -1);

var os=navigator.platform.toLowerCase();
var is_windows = (os.indexOf("win") != -1);
var is_mac     = (os.indexOf("mac") != -1);
var is_linux   = (os.indexOf("linux") != -1);


WMPObject.prototype.getHTML = function() 
{ 
    var wmpHTML = ""; 

    if (is_ie) 
    { // is ie
        if (this.mov.toLowerCase().indexOf('wmv') != -1 && is_ff)
            wmpHTML += '<object type="application/x-ms-wmp" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '">'; 
        else
            wmpHTML += '<object type="application/x-mplayer2" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '">'; 
        
        this.addParam("src", this.mov); 
        
        if (this.getParamTags() != null) 
        { 
            wmpHTML += this.getParamTags(); 
        } 
        wmpHTML += '</object>';
        
        wmpHTML += '<script type="text/javascript" language="JScript" for="' + this.id + '" event="ScriptCommand(scType, scParam)">';
        wmpHTML += 'CallScriptCommand(scType, scParam)</script>';
    } 
    else if (is_ff && is_windows)
    {
        if (this.mov.toLowerCase().indexOf('wmv') != -1 && is_ff)
            wmpHTML += '<object type="application/x-ms-wmp" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '">'; 
        else
            wmpHTML += '<object type="application/x-mplayer2" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '">'; 
        
        this.addParam("src", this.mov); 
        
        if (this.getParamTags() != null) 
        { 
            wmpHTML += this.getParamTags(); 
        } 
        wmpHTML += '</object>';
        
        wmpHTML += '<script type="text/javascript" language="JScript" for="' + this.id + '" event="ScriptCommand(scType, scParam)">';
        wmpHTML += 'CallScriptCommand(scType, scParam)</script>';
    }
    else if (is_ff && is_mac)
    {
        wmpHTML += '<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="' 
            + this.mov + '" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '"'; 

        for (var param in this.getParams()) 
        { 
            wmpHTML += ' ' + param + '="' + this.getParam(param) + '"'; 
        } 

        wmpHTML += '></embed>'; 
    }
     else if (navigator.plugins && navigator.plugins.length) 
    { // not ie 
        wmpHTML += '<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="' 
            + this.mov + '" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '"'; 

        for (var param in this.getParams()) 
        { 
            wmpHTML += ' ' + param + '="' + this.getParam(param) + '"'; 
        } 

        wmpHTML += '></embed>'; 
    } 
   
    return wmpHTML; 
} 

// Deal with attributes
WMPObject.prototype.getVariablePairs = function() 
{ 
    var variablePairs = new Array(); 

    for (var name in this.getVariables()) 
    { 
        variablePairs.push(name + "=" + escape(this.getVariable(name))); 
    } 

    if (variablePairs.length > 0) 
    { 
        return variablePairs.join("&"); 
    } else { 
        return null; 
    } 
} 

// Write ut the HTML code to the page
WMPObject.prototype.write = function(elementId) 
{ 
    if(isWMPInstalled() || this.doDetect=='false') 
    { 
        if (elementId) 
        { 
            document.getElementById(elementId).innerHTML = this.getHTML(); 
        } 
        else 
        { 
            document.write(this.getHTML()); 
        } 
    } 
    else 
    { 
        if (this.redirect != "") 
        { 
            document.location.replace(this.redirect); 
        } 
        else 
        { 
            if (elementId) 
            { 
                document.getElementById(elementId).innerHTML = this.altTxt +""+ this.bypassTxt; 
            } 
            else 
            { 
                document.write(this.altTxt +""+ this.bypassTxt); 
            } 
        } 
    } 
} 

// Plug-in detection
function isWMPInstalled() 
{ 
    var wmpInstalled = false; 
    wmpObj = false; 

    if (is_ff && is_mac)
    {
        wmpInstalled = true;
    }
    else if (navigator.mimeTypes && navigator.mimeTypes.length) 
    {
        var wmpInstalled = navigator.mimeTypes &&
                navigator.mimeTypes["application/x-mplayer2"] &&
                navigator.mimeTypes["application/x-mplayer2"].enabledPlugin;
    }
    else if (navigator.plugins && navigator.plugins.length) 
    { 
        for (var i=0; i < navigator.plugins.length; i++ ) 
        { 
            var plugin = navigator.plugins[i]; 
            if (plugin.name.indexOf("Windows Media Player") != -1 || plugin.name.indexOf("Flip4Mac") != -1 ) 
            { 
                wmpInstalled = true; 
            } 
        }
    } 
    else 
    { 
        execScript('on error resume next: wmpObj = IsObject(CreateObject("MediaPlayer.MediaPlayer.1"))','VBScript'); 
        wmpInstalled = wmpObj; 
    }
    
    return wmpInstalled; 
} 



/* get value of querystring param */ 
function getQueryParamValue(param) 
{ 
    var q = document.location.search; 
    var detectIndex = q.indexOf(param); 
    var endIndex = (q.indexOf("&", detectIndex) != -1) ? q.indexOf("&", detectIndex) : q.length; 

    if(q.length > 1 && detectIndex != -1) 
    { 
        return q.substring(q.indexOf("=", detectIndex)+1, endIndex); 
    } 
    else 
    { 
        return ""; 
    } 
} 


/* Usage of the script:

<div id="MyContainer"> 
Loading product demonstration video... 
</div> 


<script type="text/javascript"> 
var vo = new WMPObject("wmpMovie.wmv", "myMovie", "380", "285"); 
vo.addParam("AutoPlay", "False"); 
vo.addParam("ShowStatusBar", "True"); 
vo.write("MyContainer"); 
</script>

*/
