/**
 * Baseret på:
 * FlashObject v1.3c: Flash detection and embed - http://blog.deconcept.com/flashobject/
 *
 * FlashObject is (c) 2006 Geoff Stearns and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */

var FlashVersion = function(version) {
	this.major = parseInt(version[0])	|| 0;
	this.minor = parseInt(version[1]) || 0;
	this.rev = parseInt(version[2]) || 0;
}

FlashVersion.prototype = {
	isValid: function(minVersion) {
		if (this.major<minVersion.major) return false;
		if (this.major>minVersion.major) return true;
		if (this.minor<minVersion.minor) return false;
		if (this.minor>minVersion.minor) return true;
		if (this.rev  <minVersion.rev  ) return false;
		return true;
	}
}

var FlashObject = function(swf, id, width, height, minVersion, backColor, skipDetect) {
		this.installedVersion = this.getPlayerVersion();
		this.params = new Object();
		this.variables = new Object();
		
		this.swf = swf;
		this.id = id;
		this.width = width;
		this.height = height;
		this.minVersion = new FlashVersion(minVersion.split("."));
		this.skipDetect = skipDetect;
		if (backColor) this.addParam("bgcolor",backColor);
}
		

FlashObject.prototype = {
		setAttribute: function(name,value) {
			this.attributes[name] = value;
		},
		getAttribute: function(name) {
			return this.attributes[name];
		},
		addParam: function(name, value) {
			this.params[name] = value;
		},
		getParams: function() {
			return this.params;
		},
		addVar: function(name,value) {
			this.variables[name] = value;
		},
		getVar: function(name) {
			return this.variables[name];
		},
		getVars: function() {
			return this.variables;
		},
		createParam: function(name,value) {
			var ele = document.createElement("PARAM");
			ele.setAttribute("name",name);
			ele.setAttribute("value",value);
			return ele;
		},
		getVariables: function() {
			var result = [];
			var vars = this.getVars();
			for (var name in vars) {
				result.push(name+"="+vars[name]);
			}
			return result;
		},
		getHTML:function() {
			var html;
			if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
				html = '<embed type="application/x-shockwave-flash" '
				+ 'src="' + this.swf + '" '
				+ 'width="' + this.width + '" '
				+ 'height="' + this.height + '" '
				+ 'id="' + this.id + '" '
				+ 'name="' + this.id + '"'
				
				var params = this.getParams();
				for (var name in params) {
					html += ' ' + name + '="' + params[name] + '"';
				}
				
				var vars = this.getVariables().join("&");
				if (vars.length > 0) {
					html += ' flashvars="' + vars + '"';
				}
				html += ' />';
			} else {
				html = '<object id="' + this.id + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
				+ 'width="' + this.width + '" '
				+ 'height="' + this.height + '">'

				html += '<param name="movie" value="' + this.swf + '" />';
				
				var params = this.getParams();
				for (var name in params) {
					html += '<param name="' + name + '" value="' + params[name] + '" />';
				}
				
				var vars = this.getVariables().join("&");
				if (vars.length > 0) {
					html += '<param name="flashvars" value="' + vars + '" />';
				}
				html += '</object>';
			}
			return html;
		},
		
		output: function(dest) {
			if (this.skipDetect || this.installedVersion.isValid(this.minVersion)) {
				var destele = document.getElementById(dest);
				destele.innerHTML = this.getHTML();
			}
		},
		
		getPlayerVersion: function(minVersion) {
			var result = new FlashVersion([0,0,0]);
			if (navigator.plugins && navigator.mimeTypes.length) {
				var plugin = navigator.plugins["Shockwave Flash"];
				if (plugin && plugin.description) {
					var version = plugin.description;
					version = version.replace(/([a-z]|[A-Z]|\s)+/,"");
					version = version.replace(/(\s+r|\s+b[0-9]+)/,".");
					result = new FlashVersion(version.split("."));
				}
			} else {
				try {
					var plugin = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
					for (var x = 3; plugin != null; x++) {
						plugin = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + x);
						result = new FlashVersion([x,0,0]);
					}
				}
				catch (e) {}
				if (minVersion && result.major > minVersion.major) return result;
				if (!minVersion || ((minVersion.minor != 0 || minVersion.rev != 0) && result.major == minVersion.major) || result.major < 6) {
					try {
						result = new FlashVersion(plugin.GetVariable("$version").split(" ")[1].split(","));
					} catch (e) {}
				}
			}
			return result;
		},
		
		getMovie: function() {
			if (window[this.id]) {
				return window[this.id];
			} else {
				return document[this.id];
			}
		},
		
		isLoaded: function() {
			var movie = this.getMovie();
			if (typeof(movie) != "undefined") {
				return movie.PercentLoaded() == 100;
			} else {
				return false;
			}
		}
}