/*
 * -- Tewonder code description start --
 * Name: se.tewonder.Global.GlobalObjects.MediaManager.MediaObjects.FlashObject
 * Type: Class
 * Version: 0.5.01
 *
 * Edited by: Mattias Ekendahl (mattias@tewonder.se)
 * Updates:
 *
 * Notes:
 * Todo: 
 * -- Tewonder code description end --
 */

function gTewonderLocalScopeFunction() {
	//alert("gTewonderLocalScopeFunction::FlashObject");

	var FlashObject = (function() {
		//alert("se.tewonder.Global.GlobalObjects.MediaManager.MediaObjects.FlashObject");
		this.versionString = FlashObject.DEFAULT_VERSION_STRING;
		this.checkVersion = FlashObject.DEFAULT_CHECK_VERSION;
		this.skipCache = FlashObject.DEFAULT_SKIP_CACHE;
		this.skipCacheTecnique = FlashObject.DEFAULT_SKIP_CACHE_TECNIQUE;
		this.skipCacheQueryStringName = FlashObject.DEFAULT_SKIP_CACHE_QUERY_STRING_NAME;
		this.addWindowVariable = FlashObject.DEFAULT_ADD_WINDOW_VARIABLE;
		this.checkPlugin = FlashObject.DEFAULT_CHECK_PLUGIN;
		this.sendIdAsFlashVars = FlashObject.SEND_ID_AS_FLASH_VARS;
		this.idFlashVarsName = FlashObject.ID_FLASH_VARS_NAME;
		this.type = "flash";
		
		this.flashVarsObject = new Object();

		var currentFunction = function(aName, aValue) {
			this.filePath = aValue;
		}
		this.addSetParameterFunction(currentFunction, ["src", "movie"]);
		
		var currentFunction = function(aName, aValue) {
			var currentArray = aValue.split("&");
			for(var i = -1; ++i < currentArray.length;) {
				var tempArray = currentArray[i].split("=");
				this.addFlashVars(tempArray[0], tempArray[1]);
			}
		}
		this.addSetParameterFunction(currentFunction, ["flashvars"]);
		
		this.addSetParameterFunction(this.setParameterToObjectAttributes, ["onafterupdate", "onbeforeupdate", "onblur", "oncellchange", "ondrag", "ondragend", "ondragenter", "ondragleave", "ondragover", "ondrop", "onfinish", "onfocus", "onhelp", "onload", "onlosecapture", "onpropertychange", "onreadystatechange", "onrowsdelete", "onrowenter", "onrowexit", "onrowsinserted", "onstart", "onscroll", "onbeforeeditfocus", "onactivate", "onbeforedeactivate", "ondeactivate"]);
		
		this.addSetParameterFunction(this.setParameterToParamsAndEmbedAttributes, ["swliveconnect", "play", "pause", "menu", "quality", "scale", "salign", "wmode", "bgcolor", "base", "allowscriptaccess", "allowfullscreen", "devicefont"]);
		
		this.setParameter("classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
		var codebaseString = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab";
		if(this.versionString != null) {
			codebaseString += "#" + this.versionString.split(".").join(",");
		}
		this.setParameter("codebase", codebaseString);
		this.setParameter("pluginspage", "http://www.macromedia.com/go/getflashplayer");
		this.setParameter("type", "application/x-shockwave-flash");
		
		this.setRequiredPluginVersion = (function(aVersionString) {
			//alert("se.tewonder.Global.GlobalObjects.MediaManager.MediaObjects.FlashObject::setRequiredPluginVersion");
			this.versionString = aVersionString;
			this.checkVersion = true;
			this.setParameter("codebase", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#" + this.versionString.split(".").join(","));
		});
		
		this.setFlashVersion = (function(aVersionString) {
			//alert("se.tewonder.Global.GlobalObjects.MediaManager.MediaObjects.FlashObject::setFlashVersion");
			//METODO: error message
			this.setRequiredPluginVersion(aVersionString);
		});
		
		this.canShowMedia = (function() {
			//alert("se.tewonder.Global.GlobalObjects.MediaManager.MediaObjects.FlashObject::canShowMedia");
			if(!this.checkPlugin) return true;
			if(!this.checkVersion) {
				return gTewonderGlobalObjects["pluginDetector"].detectByMimeType("application/x-shockwave-flash", null);
			}
			else {
				return gTewonderGlobalObjects["pluginDetector"].detectByMimeType("application/x-shockwave-flash", this.versionString);
			}
		});
		
		this.canHandleFileByPath = (function(aFilePath) {
			//Returnvalues: 0 = can't handle file, 1 = is primary handler for file, 2 = can handle file
			var filePathWithoutQueryString;
			var questionMarkPosition = aFilePath.indexOf("?");
			if(questionMarkPosition != -1) {
				filePathWithoutQueryString = aFilePath.substring(0, questionMarkPosition);
			}
			else {
				filePathWithoutQueryString = aFilePath;
			}
			var extension = filePathWithoutQueryString.substring(filePathWithoutQueryString.lastIndexOf(".")+1, filePathWithoutQueryString.length);
			if(extension.toLowerCase() == "swf") return 2;
			return 0;
		});
		
		this.addFlashVars = (function(aName, aValue) {
			this.flashVarsObject[aName] = aValue;
		});
		
		this.initializeHtmlRendering = (function() {
			var theFilePath = this.filePath;
			if(this.skipCache) {
				//METODO: change to a url generator
				theFilePath += "?" + this.skipCacheQueryStringName + "=";
				switch(this.skipCacheTecnique) {
					case "random":
						theFilePath += Math.random();
						break;
					default:
						gTewonderGlobalObjects["errorManager"].report("error", 2, "se.tewonder.Global.GlobalObjects.MediaManager.MediaObjects.FlashObject", "initializeHtmlRendering", "no skip cache tecnique named " + this.skipCacheTecnique + ". Using date.");
					case "date":
						var theDate = new Date();
						var NumberConverter = gTewonderClasses.se.tewonder.Global.Utilities.Number.NumberConverter;
						theFilePath += theDate.getFullYear() + "" + NumberConverter.getPaddedNumber(theDate.getMonth()+1, 2) + "" + NumberConverter.getPaddedNumber(theDate.getDate(), 2) + "T" + NumberConverter.getPaddedNumber(theDate.getHours(), 2) + "" + NumberConverter.getPaddedNumber(theDate.getMinutes(), 2) + "" + NumberConverter.getPaddedNumber(theDate.getSeconds(), 2) + "." + NumberConverter.getPaddedNumber(theDate.getMilliseconds(), 3);
						break;
				}
			}
			this.embedAttributesObject["src"] = theFilePath;
			this.paramsObject["movie"] = theFilePath;
			var flashVarsString = "";
			this.setupFlashVars();
		});
		
		this.setupFlashVars = (function() {
			var flashVarsString = "";
			if((this.sendIdAsFlashVars) && (this.id != null)) {
				flashVarsString += this.idFlashVarsName + "=" + this.id;
			}
			var currentObject = this.flashVarsObject;
			for(var objectName in currentObject) {
				if(flashVarsString.length != 0) {
					flashVarsString += "&";
				}
				flashVarsString += objectName + "=" + currentObject[objectName];
			}
			if(flashVarsString != "") {
				this.embedAttributesObject["FlashVars"] = flashVarsString;
				this.paramsObject["FlashVars"] = flashVarsString;
			}			
		});
		
		//MENOTE: function are the same as in BaseObject except that a variable on the window is set
		
		this.replaceHtml = (function(aElement) {
			//alert("se.tewonder.Global.GlobalObjects.MediaManager.MediaObjects.FlashObject::replaceHtml");
			this.initializeHtmlRendering();
			if(!(this.canShowMedia())) {
				gTewonderGlobalObjects["errorManager"].report("warning", 3, "se.tewonder.Global.GlobalObjects.MediaManager.MediaObjects.FlashObject", "replaceHtml", "can't show media");
				return false;
			}
			this.holderElement = aElement;
			this.replacedHtml = aElement.innerHTML;
			aElement.innerHTML = this.getObjectHtml();
			this.element = aElement.firstChild;
			if(this.addWindowVariable) {
				window[this.id] = this.element;
			}
			return true;
		});
		
		this.appendToElement = (function(aElement) {
			//alert("se.tewonder.Global.GlobalObjects.MediaManager.MediaObjects.FlashObject::appendToElement");
			this.initializeHtmlRendering();
			if(!(this.canShowMedia())) {
				gTewonderGlobalObjects["errorManager"].report("warning", 3, "se.tewonder.Global.GlobalObjects.MediaManager.MediaObjects.FlashObject", "appendToElement", "can't show media");
				return false;
			}
			var newNode = document.createElement("div");
			newNode.innerHTML = this.getObjectHtml();
			aElement.appendChild(newNode.firstChild);
			this.element = newNode.firstChild;
			if(this.addWindowVariable) {
				window[this.id] = this.element;
			}
			return true;
		});
		
		this.write = (function() {
			this.initializeHtmlRendering();
			if(!(this.canShowMedia())) {
				gTewonderGlobalObjects["errorManager"].report("warning", 3, "se.tewonder.Global.GlobalObjects.MediaManager.MediaObjects.FlashObject", "write", "can't show media");
				return false;
			}
			document.write(this.getObjectHtml());
			if(this.id != null) {
				this.element = document.getElementById(this.id);
			}
			if(this.addWindowVariable) {
				window[this.id] = this.element;
			}
			return true;
		});
	});
	
	FlashObject.DEFAULT_VERSION_STRING = null;
	FlashObject.DEFAULT_CHECK_VERSION = false;
	FlashObject.DEFAULT_SKIP_CACHE = false;
	FlashObject.DEFAULT_SKIP_CACHE_TECNIQUE = "date";
	FlashObject.DEFAULT_SKIP_CACHE_QUERY_STRING_NAME = "skipCache";
	FlashObject.DEFAULT_CHECK_PLUGIN = true;
	FlashObject.SEND_ID_AS_FLASH_VARS = true;
	FlashObject.ID_FLASH_VARS_NAME = "flashId";
	
	FlashObject.setDefaultRequiredPluginVersion = (function(aVersionString) {
		FlashObject.DEFAULT_VERSION_STRING = aVersionString;
		FlashObject.DEFAULT_CHECK_VERSION = true;
	});
	
	FlashObject.getGetPlayerHtml = (function(aType) {
		var htmlText = "";
		switch(aType) {
			case "default":
				htmlText = "<a href=\"http://www.adobe.com/go/getflashplayer\" target=\"_blank\" border=\"0\"><img src=\"images/promo/getFlashPlayer.jpg\" width=\"160\" height=\"41\"/></a>";
				break;
			default:
				break;
		}
		return htmlText;
	});
	
	
	switch(gTewonderGlobalFunctions["getUsedProgramIdName"]()) {
		case "ie":
			FlashObject.DEFAULT_ADD_WINDOW_VARIABLE = true;
			break;
		default:
			FlashObject.DEFAULT_ADD_WINDOW_VARIABLE = false;
			break;
	}
	
	gTewonderGlobalFunctions.addClass("se.tewonder.Global.GlobalObjects.MediaManager.MediaObjects.FlashObject", FlashObject);
	gTewonderGlobalFunctions.setClassInheritance(gTewonderClasses.se.tewonder.Global.GlobalObjects.MediaManager.MediaObjects.FlashObject, gTewonderClasses.se.tewonder.Global.GlobalObjects.MediaManager.MediaObjects.ObjectsObject);
}

gTewonderLocalScopeFunction();
