/********************************************************************************
* Flash Tag Object v2.1 - by Lucas Fererira - www.lucasferreira.com		*
* Info and Usage: www.lucasferreira.com/flashtag				*
* bugs/reports: contato@lucasferreira.com					*
*********************************************************************************/

var FlashTag = function()
{
	
	var FP_VERSION = null;
	
	return {
		
		init: function()
		{
			FP_VERSION = this.getPlayerVersion();
			
			//FlashTag embed functions...
			return (
				function(movie, id, width, height, initParams)
				{
					//variables...
					this.onNotDetect = function(){};
					
					this.variables = new Array();
						
					this.flashVersion = (typeof initParams != "undefined" && typeof initParams.flashversion != "undefined") ? initParams.flashversion : "6,0,21";

					this.attributes = {
						"classid": "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",
						"codebase": "http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab#version=" + this.flashversion,
						"type": "application/x-shockwave-flash"
					};

					this.params = { "pluginurl": "http://www.macromedia.com/go/getflashplayer" };
					
					//methods...				
					this.addAttribute = function(prop, val){ this.attributes[prop] = val; };
					this.addParameter = function(prop, val){ this.params[prop] = val; };
					this.addVariable = function(prop, val){ this.variables.push([prop, val]); };
					this.getFlashVars = function()
					{
						for(var i=0, tempString = new Array(); i<this.variables.length; i++) tempString.push(this.variables[i].join("="));
						return tempString.join("&");
					};
					
					this.getObject = function()
					{
						return FlashTag.getObject(this);
					};					
					
					this.toString = function()
					{
						this.html = "";
						this.params.flashVars = this.getFlashVars();
						
						if(FlashTag.isIE())
						{
							//IE
							this.html = "<ob" + "ject";
							var attr = FlashTag.getObjectByExceptions(this.attributes, ["type", "data"]);
							for(var i in attr) if(i.toString() != "extend") this.html += " " + i.toString() + " = \"" + attr[i] + "\"";
							this.html += "> ";
							var params = FlashTag.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 = FlashTag.getObjectByExceptions(this.attributes, ["classid", "codebase"]);
							for(var i in attr) if(i.toString() != "extend") this.html += " " + i.toString() + " = \"" + attr[i] + "\"";
							this.html += "> ";
							var params = FlashTag.getObjectByExceptions(this.params, ["extend"]);
							for(var i in params) if(i.toString() != "extend") this.html += "<param name=\"" + i.toString() + "\" value=\"" + params[i] + "\" /> ";
							this.html += " </obj" + "ect> <!--> <![endif]-->";
						}
						return this.html;
					};
					
					this.redirectToUrl = function(url)
					{
						window.location.href = url;
					};
					
					this.write = function(o)										
					{
						if(typeof o != "undefined")
						{
							return this.writeIn(o);
						}						
						
						document.write( this.toString() );
						
						var f = FlashTag.getPlayerVersion();
						if(!f || (typeof this.params["version"] != "undefined" && !FlashTag.isMajor(f, this.params["version"])))
						{
							if(typeof this.params["redirectUrl"] != "undefined")
							{
								this.redirectToUrl(this.params["redirectUrl"]);
							}
							this.onNotDetect( f );
						}
						
						return true;
					};
					
					this.writeIn = function(o)
					{
						if(typeof o == "undefined")
						{
							return this.write();
						}
						else if(typeof o == "string")
						{
							o = document.getElementById(o) || null;
						}
						
						if(o == undefined || o == null) return false;
						
						var f = FlashTag.getPlayerVersion();
						if(!f || (typeof this.params["version"] != "undefined" && !FlashTag.isMajor(f, this.params["version"])))
						{
							if(typeof this.params["redirectUrl"] != "undefined")
							{
								this.redirectToUrl(this.params["redirectUrl"]);
							}
							this.onNotDetect( f );
						}
						else
						{
							o.innerHTML = this.toString();
						}
						
						return true;
					}
					
					//initial methods...
					if(movie)
					{
						this.addAttribute("data", movie);
						this.addParameter("movie", movie);
					}

					if(id && id != null && (this.id = id)) 
					{
						this.addAttribute("id", this.id);
						this.addAttribute("name", this.id);
					}
					else
					{
						this.id = null;
					}

					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]);
					}					
				}
			);
		},
		
		isIE: function()
		{
			return (window.ActiveXObject && document.all && navigator.userAgent.toLowerCase().indexOf("msie") > -1  && navigator.userAgent.toLowerCase().indexOf("opera") == -1) ? true : false;
		},
		
		getObjectByExceptions: function(obj, excep)
		{
			var tempObj = {};
			for(var i in obj)
			{
				for(var j=0, EOF=false; j<excep.length; j++)
					if(excep[j] == i.toString()) { EOF = true; break; };
					
				if(!EOF) tempObj[i] = obj[i];
			}
			return tempObj;
		},	
		
		getObject: function(fo)
		{
			if(fo.id == null) return null;
			try
			{
				if(window.document[fo.id])
				{
					return window.document[fo.id];
				}
				else
				{
					return document.getElementById(fo.id);
				}
			}
			catch(e) { return null; }
		},		
		
		correctAll: function()
		{
			if(!/msie/.test(navigator.userAgent.toLowerCase()) || !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++);
		},
		
		automatic: function()
		{
			if (!document.uniqueID && document.expando) return;
			
			var t = document.createElement('document:ready'); 	
			try {
			        t.doScroll('left');
				
				//Correct all SWF's in a page...
				FlashTag.correctAll();
			        
			        t = null;

			} catch (err) { setTimeout(arguments.callee, 1); }
		},
		
		isMajor: function(f1, f2)
		{
			for(var i=0, f1 = f1.replace(/[,]/g, ".").split("."); i<f1.length; (f1[i]=parseInt(f1[i])), i++);
			for(var i=0, f2 = f2.replace(/[,]/g, ".").split("."); i<f2.length; (f2[i]=parseInt(f2[i])), i++);			

			if(f1.length > 0 && f2.length > 0)
			{
				for(var i = -1, l = Math.min(f1.length, f2.length); ++i < l && f2[i] == f1[i];);
				return +(i != l && f2[i] - f1[i]) > 0 ? false : true;
			}
			
			return false;			
		},
		
		getPlayerVersion: function()
		{
			if(FP_VERSION == null)
			{
				if(!this.isIE() && navigator.plugins && navigator.mimeTypes.length
					&& (f=navigator.plugins["Shockwave Flash"]) && f.description)
				{
					return (f.description.replace(/[^0-9r.]/g, "").replace(/[r]/g, "."));
				}
				else
				{
					var acx = null;
					try
					{
						acx = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
					}
					catch(e)
					{
						var v = 8;
						do
						{
							try
							{
								acx = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + v);
								try { acx.AllowScriptAccess = "always"; } catch(eex){}
								
							} catch(ee) { acx = null; }
						}
						while(v-- && v > 2 && acx==null);
					}
					return (acx != null && acx) ? acx.GetVariable("$version").replace(/[^0-9,]/g, "").replace(/[,]/g, ".") : false;
				}
			}
			else
			{
				return FP_VERSION;
			}
		}
		
	};
	
}();

//init the lib...
var Flash = FlashTag.init();
