/*
Copyright (c) Copyright (c) 2007, Carl S. Yestrau All rights reserved.
Code licensed under the BSD License: http://www.featureblend.com/license.txt
Version: 1.0.1
*/
function FlashTML(minVersion, source, width, height){
	var self = this; 
	var baseElement = document.createElement("div");
	var options = arguments[4] || {};
	var winIE = ((navigator.appVersion.toLowerCase().indexOf("win")!=-1) && (navigator.appName=="Microsoft Internet Explorer"));
	var idCount = FlashTML.idCount++;
	var namespaceAdded = false;
	var namespaceName = "flashtml";
	var namespaceURN = "http://www.featureblend.com/2007/flashtml/";
	self.domTemplate = "";
	self.innerHTML = "";
	self.xhtml = "";
	self.inDocumentElement = "";
	var getCabVersion = function(minVersion){
		return minVersion + ",0,0,0";//B+
	};
	var getNameValueAttrFromOptions = function(name){
		return (typeof options[name] != "undefined")?getNameValueAttributes(name, options[name].toString()):"";
	};
	var getNameValueAttributes = function(name, value){
		return {
			"@name":name,
			"@value":value
		};
	};
	var createElementFromRule = function(name, target){
		var newElement = safeCreateElement(name.replace("#",""));
		return target.appendChild(newElement);
	};
	var getID = function(){
		return options.id || FlashTML.idPattern + idCount;
	};
	var setAttributeFromRule = function(name, value, target){
		target.setAttribute(name.replace("@",""), value);
	};
	var safeCreateElement = function(name){
		if((name=="object" || name=="param") && document.namespaces){
			if(!namespaceAdded){
				document.namespaces.add(namespaceName, namespaceURN);
				namespaceAdded = true;
			}
			return document.createElement(namespaceName + ":" + name);
		}else{
			return document.createElement(name);
		}
	};
	var htmlTidy = function(str){
		str = str.replace(/<\?xml([^>]*)>/, "");
		str = str.replace(eval("/"+namespaceName+":/g"),"");
		str = str.replace(/><\/param>/g,">");
		str = str.replace(/(<param )(.*?)(>)/g, "<param $2 \/>");
		return str;
	};
	var parseRules = function(position, target){
		for(var i in position){
			if(i.charAt(0)=="#"){
				var appendedElement;
				if(typeof position[i] == "object" && position[i].length){
					for(var j=0; j<position[i].length; j++){
						if(position[i][j]()!==""){
							appendedElement = createElementFromRule(i, target);
							parseRules(position[i][j](), appendedElement);
						}
					}
				}else if(typeof position[i]=="function" && position[i]()!==""){
					appendedElement = createElementFromRule(i, target);
					parseRules(position[i](), appendedElement);
				}
			}else if(i.charAt(0)=="@"){
				if(typeof position[i]=="function" && position[i]()!==""){
					setAttributeFromRule(i, position[i](), target);
				}else if(typeof position[i] == "string"){
					setAttributeFromRule(i, position[i], target);
				}
			}
		}
	};
	var structuralRules = {
	    "#object":function(){
	    	return {
	    		"@type":function(){
	    			return (!winIE)?"application/x-shockwave-flash":"";
	    		},
		    	"@codebase":function(){
		    		return (winIE)?"http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab#version="+getCabVersion(minVersion):"";
		    	},
		    	"@classid":function(){
		    		return (winIE)?"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000":"";
		    	},
		    	"@data":function(){
		    		return (!winIE)?source:"";
		    	},
		    	"@width":function(){
		    		return width.toString();
		    	},
		    	"@height":function(){
		    		return height.toString();
		    	},
		    	"@id":function(){
		    		return getID();
		    	},
				"@tabindex":function(){
					return options.tabindex || "";
				},
		    	"#param":[
		    		function(){
		    			return (winIE)?getNameValueAttributes("movie", source):"";
		    		},
		    		function(){
		    			return getNameValueAttrFromOptions("allowscriptaccess");
		    		},
		    		function(){
		    			return getNameValueAttrFromOptions("swliveconnect");
		    		},
		    		function(){
		    			return getNameValueAttrFromOptions("play");
		    		},
		    		function(){
		    			return getNameValueAttrFromOptions("loop"); 
		    		},
		    		function(){
		    			return getNameValueAttrFromOptions("menu"); 
		    		},
		    		function(){
		    			return getNameValueAttrFromOptions("quality"); 
		    		},
		    		function(){
		    			return getNameValueAttrFromOptions("scale"); 
		    		},
		    		function(){
		    			return getNameValueAttrFromOptions("align"); 
		    		},
		    		function(){
		    			return getNameValueAttrFromOptions("salign"); 
		    		},
		    		function(){
		    			return getNameValueAttrFromOptions("wmode"); 
						
		    		},
		    		function(){
		    			return getNameValueAttrFromOptions("bgcolor");
		    		},
		    		function(){
		    			return getNameValueAttrFromOptions("base");
		    		},
		    		function(){
		    			var result = "";
		    			if(options.flashvars){
		    				var flashVarsStr = (typeof options.flashvars == "object")?FlashTML.getQueryStrFromObj(options.flashvars):options.flashvars;
		    				result = getNameValueAttributes("flashvars", flashVarsStr);
		    			}
		    			return result;
		    		}
		    	]
	    	};
	    }
	};
	self.append = function(target){
		return addToElement(target, false);
	};
	self.replace = function(target){
		return addToElement(target, true);
	};
	var addToElement = function(target, replace){
		var result = false;
		var targetObj = (typeof target=="object")?target:document.getElementById(target);
		if(!self.inDocumentElement){
			if(winIE){
				if(replace){
					targetObj.innerHTML = self.xhtml;
				}else{
					targetObj.innerHTML += self.xhtml;
				}
				self.inDocumentElement = document.getElementById(getID());
				window.attachEvent("onunload", self.garbageCollection);
			}else{
				if(replace){
					while (targetObj.firstChild) {
					  targetObj.removeChild(targetObj.firstChild);
					}					
				}
				self.inDocumentElement = targetObj.appendChild(baseElement.childNodes[0]);
			}
			result = self.inDocumentElement;
		}
		return result;
	};
	self.garbageCollection = function(){
		if(winIE && self.inDocumentElement){
			self.inDocumentElement = null;
		}
	};
	self.FlashTML = function(){
		parseRules(structuralRules, baseElement);
		self.domTemplate = baseElement.childNodes[0];
		self.innerHTML = baseElement.innerHTML;
		self.xhtml = htmlTidy(self.innerHTML);
	}();
}
FlashTML.getQueryStrFromObj = function(nameValue){
	var str = "";
	for(var i in nameValue){
		if(nameValue.hasOwnProperty(i)){
			str += (encodeURI(i) + "=" + encodeURI(nameValue[i]) + "&");
		}
	}
	return str.substring(0, str.length-1);
};
FlashTML.idCount = 0;
FlashTML.idPattern = "__FlashTML_";
FlashTML.release = "1.0.1";