/**
 * @require swfobject.js
 */

function flashUtility(conf) {
	this.swf               = "";
	this.id                = "";
	this.movieCSS          = "";
	this.enabledClassName  = "";
	this.width             = "100%";
	this.height            = "100%";
	this.minWidth          = "";
	this.minHeight         = "";
	this.version           = "";
	this.expressInstallSwf = "";
	this.flashvars         = null;
	this.params            = null;
	this.attributes        = null;
	this.listeners         = [];
	this.remover           = null;
	this.setConfig(conf);
}
flashUtility.target = "flashUtilityTargeNode";
flashUtility.prototype = {
	"isIE" : ((navigator.userAgent.indexOf("MSIE") != -1 && document.all && !window.opera) ? true : false),
	"setConfig" : function(conf) {
		if (conf.swf              ) this.swf               = conf.swf;
		if (conf.id               ) this.id                = conf.id;
		if (conf.movieCSS         ) this.movieCSS          = conf.movieCSS;
		if (conf.enabledClassName ) this.enabledClassName  = conf.enabledClassName;
		if (conf.width            ) this.width             = conf.width;
		if (conf.height           ) this.height            = conf.height;
		if (conf.minWidth         ) this.minWidth          = conf.minWidth;
		if (conf.minHeight        ) this.minHeight         = conf.minHeight;
		if (conf.version          ) this.version           = conf.version;
		if (conf.expressInstallSwf) this.expressInstallSwf = conf.expressInstallSwf;
		if (conf.flashvars        ) this.flashvars         = conf.flashvars;
		if (conf.params           ) this.params            = conf.params;
		if (conf.attributes       ) this.attributes        = conf.attributes;

		if (this.isEnabled() && this.movieCSS) this.addCss(this.movieCSS);

		if (!this.remover) {
			this.remover = function(){
				arguments.callee.util.removeEventListeners();
			};
			this.remover.util = this;
			this.addEventListener(window, "unload", this.remover);
		}
	},
	"isEnabled" : function() {
		return swfobject.hasFlashPlayerVersion(this.version);
	},
	"addCss" : function(href, media) {
		if (!href || !this.isEnabled) return;
		if (!media) media = "all";
		var head = document.getElementsByTagName("head")[0];
		var link = document.createElement("link");
		link.setAttribute("rel",   "stylesheet");
		link.setAttribute("type",  "text/css");
		link.setAttribute("media", media);
		link.setAttribute("href",  href);
		head.appendChild(link);
	},
	"addEventListener" : function(target, type, listener) {
		type = type.replace(/^on/, "");
		if (target === window || target === document) {
			if (type == "DOMContentLoaded") {
				this.addDomLoadEvent(listener);
				return;
			} else if (type == "load") {
				this.addLoadEvent(listener);
				return;
			}
		}

		this.listeners.push({
			target   : target,
			type     : type,
			listener : listener
		});
		if (target.addEventListener) {
			target.addEventListener(type, listener, false);
		} else if (target.attachEvent) {
			target.attachEvent("on" + type, listener);
		} else {
			var _fn = target["on" + type];
			if (_fn) {
				target["on" + type] = function(e){ listener(e); _fn(e); }
			} else {
				target["on" + type] = listener;
			}
		}
	},
	"addDomLoadEvent" : function(fn) {
		swfobject.addDomLoadEvent(fn);
	},
	"addLoadEvent" : function(fn) {
		swfobject.addLoadEvent(fn);
	},
	"removeEventListeners" : function() {
		while (this.listeners.length) {
			var object   = this.listeners.pop();
			var target   = object.target;
			var type     = object.type;
			var listener = object.listener;
			if (target.removeEventListener) {
				target.removeEventListener(type, listener, false);
			} else if (target.detachEvent) {
				target.detachEvent("on" + type, listener);
			} else {
				delete target["on" + type];
			}
		}
	},
	"embed" : function() {
		var minWidth  = (this.minWidth ) ? parseInt(this.minWidth,  10) : 0;
		var minHeight = (this.minHeight) ? parseInt(this.minHeight, 10) : 0;
		var width     = this.width;
		var height    = this.height;
		var ieVersion = (this.isIE) ? parseFloat(navigator.userAgent.match(/MSIE ([0-9]+(\.[0-9]+)?)/) ? RegExp.$1 : 0) : 0;

		if (this.id && (!this.attributes || !this.attributes.id)) {
			if (!this.attributes) {
				this.attributes = {};
			}
			this.attributes.id = this.id;
		}

		if (minWidth || minHeight) {
			if (!this.isIE || (this.isIE && ieVersion >= 7)) {
				if (!this.attributes) {
					this.attributes = {};
				}
				if (!this.attributes.style) {
					this.attributes.style = "";
				}
				if (minWidth ) this.attributes.style += "min-width:"  + minWidth  + "px;";
				if (minHeight) this.attributes.style += "min-height:" + minHeight + "px;";
			}
		}

		if (this.expressInstallSwf || this.isEnabled()) {
			document.write('<ins id="' + flashUtility.target + '" style="display:none;"></ins>');
			swfobject.embedSWF(
				this.swf,
				flashUtility.target,
				this.width,
				this.height,
				this.version,
				this.expressInstallSwf,
				this.flashvars,
				this.params,
				this.attributes
			);
			flashUtility.target += "X";
		}

		if (this.isEnabled()) {

			var enabledClassName = this.enabledClassName;
			if (enabledClassName) {
				this.addDomLoadEvent(function(){
					var body = document.getElementsByTagName("body")[0];
					var className = body.className;
					if (!className) {
						body.className = enabledClassName;
					} else {
						var classNames = className.split(/\s+/);
						for (var i = 0, n = classNames.length; i < n; i++) {
							if (classNames[i] == enabledClassName) {
								return;
							}
						}
						body.className = className + " " + enabledClassName;
					}
				});
			}

			if (this.isIE) {
				var id = this.id || this.attributes.id;
				var fn = function(){
					var util = arguments.callee.util;

					var object = document.getElementsByTagName("object");
					var target = object[i];
					for (var i = 0, n = object.length; i < n; i++) {
						if (object[i].getAttribute("id") == id) {
							target = object[i];
							if (typeof(target.setActive) == "object") {
								target.setActive();
							}
							break;
						}
					}

					if (target && (minWidth || minHeight)) {
						var wPer = parseInt( width.replace(/%$/, "")) / 100;
						var hPer = parseInt(height.replace(/%$/, "")) / 100;
						function setMinSizeIE() {
							var _width  = target.parentNode.offsetWidth;
							var _height = target.parentNode.offsetHeight;
							target.style.width  = (minWidth  && _width  * wPer <= minWidth ) ? minWidth  + "px" : width;
							target.style.height = (minHeight && _height * hPer <= minHeight) ? minHeight + "px" : height;
						}
						setMinSizeIE();
						util.addEventListener(window, "onresize", setMinSizeIE);
						util.addEventListener(window, "onbeforeprint", function(){
							if (height.indexOf("%") != -1) {
								if (minWidth && minHeight) {
									target.style.width  = minWidth  + "px";
									target.style.height = minHeight + "px";
								}

								var _width  = target.offsetWidth;
								var _height = target.offsetHeight;
								target.style.width  = _width + "px";
								target.style.height = Math.round(_width * (_height / _width)) + "px";
							}
						});
						util.addEventListener(window, "onafterprint", function(){
							if (height.indexOf("%") != -1) {
								setMinSizeIE();
							}
						});
					}
				}
				fn.util = this;
				this.addDomLoadEvent(fn);
			}
		}
	}
}
