/*
 * 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.divid = 'divPlayer';
	this.sq = document.location.search.split("?")[1] || "";
	this.altTxt = "Para escutar a radio ZonaDez você precisa instalar o plugin do Windows Media Player.";
	this.bypassTxt = "<p>Já tem Media Player Instalado? <a href='?detectwmp=false&"+ this.sq +"'>Clique aqui</a></p>";
	this.params = new Object();
	this.doDetect = getQueryParamValue('detectwmp');
}

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

WMPObject.prototype.setDivMsg = function(value) {
	this.divid = 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 = "";
	if (navigator.plugins && navigator.plugins.length) { // not ie
        wmpHTML += '<embed type="application/asx" 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 { // pc ie
        wmpHTML += '<object classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '">';
        this.addParam("src", this.mov);
        if (this.getParamTags() != null) {
            wmpHTML += this.getParamTags();
        }
        wmpHTML += '</object>';
    }
    return wmpHTML;
}


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) {
				
					var tagBody = document.getElementsByTagName('body').item(0);
					var div = document.createElement('div');
					div.setAttribute('id', this.divid);
					div.style.width = "600px";
					div.style.height = "50px";
					div.style.position = "absolute";
					div.style.marginLeft = "-300px";
					div.style.left = "50%";
					div.style.top = "3px";
					div.style.padding = "5px";
					div.style.backgroundColor = "#000000";
					div.style.border = "1px solid #FFFFFF";
					div.style.color = "#FFFFFF";
					var texto = unescape(this.altTxt);
					if (navigator.plugins && navigator.plugins.length)
						texto = texto + "<br/><br/>Para baixar o plugin <a href=\"http://port25.technet.com/videos/downloads/wmpfirefoxplugin.exe\" target=\"_blank\"><strong>clique aqui</strong></a>.";
					else
						texto = texto + "<br/><br/>Para baixar o plugin <a href=\"http://www.microsoft.com/windows/windowsmedia/br/\" target=\"_blank\"><strong>clique aqui</strong></a>.";
					div.innerHTML = texto;
					tagBody.appendChild(div);					
				
			} else {
				document.write(this.altTxt +""+ this.bypassTxt);
			}
		}
	}		
}

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

