/*
 * WMObject embed
 * http://blog.deconcept.com/2005/01/26/web-standards-compliant-javascript-quicktime-detect-and-embed/
 *
 * by Geoff Stearns (geoff@deconcept.com, http://www.deconcept.com/)
 *
 * v1.0.2 - 02-16-2005
 *
 * Embeds a quicktime wmvie to the page, includes plugin detection
 *
 * Usage:
 *
 *	myWMObject = new WMObject("path/to/wmv.wmv", "wmvid", "width", "height");
 *	myWMObject.altTxt = "Upgrade your Quicktime Player!";    // optional
 
 *  myWMObject.addParam("controller", "false");              // optional
 *	myWMObject.write();
 *
 */

WMObject = function(wmv, id, w, h) {
	this.wmv = wmv;
	this.id = id;
	this.width = w;
	this.height = h;
	this.redirect = "";
	this.sq = document.location.search.split("?")[1] || "";
	this.altTxt = "This content requires the QuickTime Plugin. <a href='http://www.apple.com/quicktime/download/'>Download QuickTime Player</a>.";
	this.bypassTxt = "<p>Already have QuickTime Player? <a href='?detectwm=false&"+ this.sq +"'>Click here.</a></p>";
	this.params = new Object();
	this.doDetect = getQueryParamValue('detectwm');
}

WMObject.prototype.addParam = function(name, value) {
	this.params[name] = value;
}

WMObject.prototype.getParams = function() {
    return this.params;
}

WMObject.prototype.getParam = function(name) {
    return this.params[name];
}

WMObject.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;
}

WMObject.prototype.getHTML = function() {
    var wmHTML = "";
	if (navigator.plugins && navigator.plugins.length) { // not ie
        wmHTML += '<embed type="application/x-mplayer2" src="' + this.wmv + '" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '"';
        for (var param in this.getParams()) {
            wmHTML += ' ' + param + '="' + this.getParam(param) + '"';
        }
        wmHTML += '></embed>';
    }
    else { // pc ie
        wmHTML += '<object classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '">';
        this.addParam("url", this.wmv);
        if (this.getParamTags() != null) {
            wmHTML += this.getParamTags();
        }
        wmHTML += '</object>';
    }
    return wmHTML;
}


WMObject.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;
    }
}

WMObject.prototype.write = function(elementId) {
	if(isWMInstalled() || 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);
			}
		}
	}		
}

function isWMInstalled() {
	var wmInstalled = false;
	wmObj = false;
	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") > -1) {
			wmInstalled = true;
         }
      }
	} else {
		execScript('on error resume next: wmObj = IsObject(CreateObject("MediaPlayer.MediaPlayer.1"))','VBScript');
		wmInstalled = wmObj;
	}
	return wmInstalled;
}

/* 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 "";
	}
}
