/*
 * WMPObject embed
 * Based on Geoff Stearns QTObject embed
 * http://blog.deconcept.com/2005/01/26/web-standards-compliant-javascript-quicktime-detect-and-embed/
 *
 * by Ben Powell (http://www.benpowell.co.uk/)
 *
 * v1.0.1 - 26-09-2007
 *
 * Embeds a Windows Media movie to the page, includes plugin detection
 *
 * Usage:
 *
 *	myWMPObject = new WMPObject("path/to/movie.wmv", "movid", "width", "height");
 *	myWMPObject.altTxt = "Upgrade your Windows Media Player!";    // optional
 
 *  myWMPObject.addParam("AutoPlay", "False");              // optional
 *	myWMPObject.write();
 *	myWMPObject.write("myContainerDivTag");
 *
 */

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/'>Download Windows Media Player</a>.";
	this.bypassTxt = "<p>Already have Windows Media Player? <a href='?detectwmp=false&"+ this.sq +"'>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;
}

WMPObject.prototype.getHTML = function() {
	var wmpHTML = "";
	var wmpHTMLe = "";
	// type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/"
	wmpHTMLe += '<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="' 
	+ this.mov + '" width="' + this.width + '" '+(this.height > 0?'height="' + this.height + '"':'')+' id="' + this.id + '_e"';
	for (var param in this.getParams()) {
		wmpHTMLe += ' ' + param + '="' + this.getParam(param) + '"';
	}
	wmpHTMLe += '></embed>';
	if (navigator.plugins && navigator.plugins.length) { // not ie
		wmpHTML = wmpHTMLe;
		var mv = document.getElementById(this.id+'_pre');
		if (mv) {
			mv.style.display = "none";
		}
    }
    else { // pc ie
        wmpHTML += '<object classid="clsid:6bf52a52-394a-11d3-b153-00c04f79faa6" type="application/x-oleobject" width="' + this.width + '"' + (this.height > 0?' height="' + this.height + '"':'')+' id="' + this.id + '_ie">';
        this.addParam("url", this.mov);
        if (this.getParamTags() != null) {
            wmpHTML += this.getParamTags();
        }
		//wmpHTML += wmpHTMLe;
        wmpHTML += '</object>';
    }
	
	/*
	wmpHTML = '<OBJECT width="190" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,0902" standby="Loading Microsoft(r) Windows Media(tm) Player components..." type="application/x-oleobject">';
	wmpHTML += '<PARAM NAME="AutoStart" VALUE="True"><PARAM NAME="ShowControls" VALUE="True"><PARAM NAME="ShowStatusBar" VALUE="False"><PARAM NAME="ShowDisplay" VALUE="False"><PARAM NAME="FileName" VALUE="' + this.mov + '">';
	wmpHTML += '<EMBED type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" SRC="' + this.mov + '" width="190" autostart=1 showControls=1 volume=0></EMBED>';
	wmpHTML += '</OBJECT>';
	*/
	
    return wmpHTML;
}

function ustawStan() {
	var mv = document.getElementById(globalObject.id+'_pre');
	if (mv) {
		mv.style.display = "none";
	}
}


WMPObject.prototype.startListening = function() {
	var mv = document.getElementById(globalObject.id+'_ie');
	//if (!mv) mv = document.getElementById(globalObject.id+'_e');
	if (mv) {
		mv.attachEvent("playstatechange", ustawStan);
		globalObjectUstawione = true;
	}
    if (globalObjectUstawione != true) setTimeout('globalObject.startListening()', 10)
}


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;
    }
}

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);
			}
		}
	}
	globalObjectUstawione = false;
	globalObject = this;
	this.startListening();
}

function isWMPInstalled() {
	var wmpInstalled = false;
	wmpObj = 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 Player") > -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 "";
	}
}


