﻿
if(Browser == undefined){
	var Browser = {
		isIE: function(){ return (window.ActiveXObject && document.all && navigator.userAgent.toLowerCase().indexOf("msie") > -1  && navigator.userAgent.toLowerCase().indexOf("opera") == -1) ? true : false; }
	}
}

var WMP = function(movie, id, width, height, initParams){

	this.html = "";
	this.attributes = this.params = this.variables = null;
	
	this.variables = new Array();
	this.attributes = {
	
		"classid": "CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6",
		"type": "video/x-ms-wmv"
	}
	this.params = {};
	
	if(movie) {
		this.addAttribute("data", movie);
		this.addParameter("url", movie);
	}
	
	if(id && id != null) this.addAttribute("id", id);
	if(width) this.addAttribute("width", width);
	if(height) this.addAttribute("height", height);
	
	if(initParams != undefined){
		for(var i in initParams){
			this.addParameter(i.toString(), initParams[i]);
		}
	}
	
}
WMP.version = "1.0b";
WMP.getObjectByExceptions = function(obj, excep){
	var tempObj = {};
	for(var i in obj){
		var inclui = true;
		for(var j=0; j<excep.length; j++)
			if(excep[j] == i.toString()) { inclui = false; break; };
		if(inclui) tempObj[i] = obj[i];
	}
	return tempObj;
}
WMP.prototype.addAttribute = function(prop, val){ this.attributes[prop] = val; }
WMP.prototype.addParameter = function(prop, val){ this.params[prop] = val; }
WMP.prototype.addVariable = function(prop, val){ this.variables.push([prop, val]); }
WMP.prototype.getWMPVars = function(){
	var tempString = new Array();
	
	for(var i=0; i<this.variables.length; i++)
		tempString.push(this.variables[i].join("="));
		
	return tempString.join("&");
}
WMP.prototype.toString = function(){
	
	this.params.WMPVars = this.getWMPVars();
	if(Browser.isIE()){
		//IE
		this.html = "<ob" + "ject";
		var attr = WMP.getObjectByExceptions(this.attributes, ["type", "data"]);
		for(var i in attr) if(i.toString() != "extend") this.html += " " + i.toString() + " = \"" + attr[i] + "\"";
		this.html += "> ";
		var params = WMP.getObjectByExceptions(this.params, ["pluginurl", "extend"]);
		for(var i in params) if(i.toString() != "extend") this.html += "<param name=\"" + i.toString() + "\" value=\"" + params[i] + "\" /> ";
		this.html += " </obj" + "ect>";
	} else {
		//non-IE
		this.html = "<!--[if !IE]> <--> <obj" + "ect";
		var attr = WMP.getObjectByExceptions(this.attributes, ["classid", "codebase"]);
		for(var i in attr) 
		    if(i.toString() != "extend") {
		        if(i.toString()=="data")
		            this.html += " " + i.toString() + " = \"" + attr[i].replace("rtsp://","http://") + "\"";
		        else
		            this.html += " " + i.toString() + " = \"" + attr[i] + "\"";
		    }
		this.html += "> ";
		var params = WMP.getObjectByExceptions(this.params, ["extend"]);
		for(var i in params) 
		    if(i.toString() != "extend") {
		        if(i.toString()=="src")
		            this.html += "<param name=\"" + i.toString() + "\" value=\"" + params[i].replace("rtsp://","http://") + "\" /> ";
		        else
		            this.html += "<param name=\"" + i.toString() + "\" value=\"" + params[i] + "\" /> ";
		    }
		this.html += " </obj" + "ect> <!--> <![endif]-->";
	}

	return this.html;
	
}
WMP.prototype.write = WMP.prototype.outIn = WMP.prototype.writeIn = function(w){
	if(typeof w == "string" && document.getElementById) var w = document.getElementById(w);
	if( w != undefined && w ) w.innerHTML = this.toString();
	else document.write( this.toString() );
}

//funções de automatização...
WMP.correctAll = function(){
	
	if(!/MSIE (5|6)/.test(navigator.userAgent) || !document.getElementsByTagName) return false;

	for (var i = 0, objects = document.getElementsByTagName("OBJECT");
			i < objects.length; (objects[i].outerHTML ? (objects[i].outerHTML = objects[i].outerHTML, objects[i].style.visibility = "visible") : null), i++);

}
WMP.automatic = function(r){
	
	if(r && window.attachEvent){
	
		for (var i = 0, objects = document.getElementsByTagName("OBJECT");
				i < objects.length; (objects[i].style.visibility = "hidden"), i++);
		
		window.attachEvent("onload", WMP.correctAll);
		window.attachEvent("onunload", function(){	window.detachEvent("onload", WMP.correctAll);	});
		
	} else {
		WMP.correctAll();
	}

}
