/* -------------------------------------------------------------------------- */
/** 
 *    @fileoverview
 *       flashGenerator.
 *
 *    @version rev001.2007-05-29
 *    @requires common.js
 */
/* -------------------------------------------------------------------------- */


/* -------------------- Constructor : BAObjectGenerator -------------------- */

function BAObjectGenerator() {
	this.node       = null;
	this.version    = 0;
	this.required   = 0;
	this.src        = "";
	this.id         = "object";
	this.className  = "object";
	this.alternate  = "object-alternate";
	this.width      = "100%";
	this.height     = "100%";
	this.type       = "";
	this.classid    = "";
	this.params     = {};

	if (typeof(config) == "object") {
		this.setConfig(config);
	}
}
BAObjectGenerator.prototype = {
	setConfig : function(config){
		for (var i in config) {
			var value = config[i];
			switch (i) {
				case "required" :
					this.setRequired(value);
					break;
				case "src" :
					this.setSrc(value);
					break;
				case "name" :
					this.setName(value);
					break;
				case "id" :
					this.setId(value);
					break;
				case "className" :
					this.setClassName(value);
					break;
				case "width" :
					this.setWidth(value);
					break;
				case "height" :
					this.setHeight(value);
					break;
				case "type" :
					this.setType(value);
					break;
				case "classid" :
					this.setClassid(value);
					break;
				case "codebase" :
					this.setCodebase(value);
					break;
				default :
					this.setParam(i, value);
					break;
			}
		}
	},
	setVersion : function(version) {
		this.version = version;
	},
	getVersion : function() {
		return parseInt(this.version);
	},
	setRequired : function(required) {
		this.required = new Number(required);
	},
	getRequired : function() {
		return parseInt(this.required);
	},
	setSrc : function(src) {
		this.src = src;
	},
	getSrc : function() {
		return this.src;
	},
	setName : function(name) {
		this.name = name;
	},
	getName : function() {
		return this.name;
	},
	setId : function(id) {
		this.id = id;
	},
	getId : function() {
		return this.id;
	},
	setClassName : function(className) {
		this.className = className;
	},
	getClassName : function() {
		return this.className;
	},
	setWidth : function(width) {
		this.width = width;
	},
	getWidth : function() {
		return this.width.toString();
	},
	setHeight : function(height) {
		this.height = height;
	},
	getHeight : function() {
		return this.height.toString();
	},
	setClassid : function(classid) {
		this.classid = classid;
	},
	getClassid : function() {
		return this.classid;
	},
	setCodebase : function(codebase) {
		this.codebase = codebase;
	},
	getCodebase : function() {
		return this.codebase;
	},
	setType : function(type) {
		this.type = type;
	},
	getType : function() {
		return this.type;
	},
	setParam : function(name, value) {
		this.params[name] = value;
	},
	getParam : function(name) {
		return this.params[name] || "";
	},
	isEnabled : function() {
		return (this.getRequired() <= this.getVersion());
	},
	createElement : function() {
		if (!this.isEnabled()) {
			return null;
		}

		var element = document.createElementBA("object");
		if (this.getName())      element.setAttributeBA("name",   this.getName());
		if (this.getId())        element.setAttributeBA("id",     this.getId());
		if (this.getClassName()) element.setAttributeBA("class",  this.getClassName());
		if (this.getWidth())     element.setAttributeBA("width",  this.getWidth());
		if (this.getHeight())    element.setAttributeBA("height", this.getHeight());
		if (this.getType())      element.setAttributeBA("type",   this.getType());
		if (this.getSrc())       element.setAttributeBA("data",   this.getSrc());

		var paramWithAttribute = (BA.ua.isMacIE || (BA.ua.isGecko && BA.ua.revision <= 0.9));
		if (paramWithAttribute) {
			for (var i in this.params) {
				var value = this.getParam(i);
				if (value != "") {
					element.setAttributeBA(i, this.getParam(i));
				}
			}
		}

		if (BA.ua.isWinIE) {
			if (this.getClassid())  element.setAttributeBA("classid",   this.getClassid());
			if (this.getCodebase())  element.setAttributeBA("codebase", this.getCodebase());
		}

		if (!paramWithAttribute) {
			var _this = this;
			function _createParam(name) {
				if (!name || _this.getParam(name) == "") return null;
				var param = document.createElementBA("param");
				param.setAttributeBA("name",  name);
				param.setAttributeBA("value", _this.getParam(name));
				return param;
			}

			for (var i in this.params) {
				var param = _createParam(i);
				if (param) {
					element.appendChildBA(param);
				}
			}
		}

		return this.ongenerate(element);
	},
	toString : function() {
		if (!this.isEnabled()) {
			return "";
		}

		var element = "<object";
		if (this.getName())      element += " name=\""   + this.getName()      + "\"";
		if (this.getId())        element += " id=\""     + this.getId()        + "\"";
		if (this.getClassName()) element += " class=\""  + this.getClassName() + "\"";
		if (this.getWidth())     element += " width=\""  + (this.getFullScreen() ? "100%" : this.getWidth())  + "\"";
		if (this.getHeight())    element += " height=\"" + (this.getFullScreen() ? "100%" : this.getHeight()) + "\"";
		if (this.getType())      element += " type=\""   + this.getType()      + "\"";
		if (this.getSrc())       element += " data=\""   + this.getSrc()       + "\"";

		var paramWithAttribute = (BA.ua.isMacIE || (BA.ua.isGecko && BA.ua.revision <= 0.9));
		if (paramWithAttribute) {
			for (var i in this.params) {
				var value = this.getParam(i);
				if (value != "") {
					element += " " + i + "=\"" + value + "\"";
				}
			}
		}

		if (BA.ua.isWinIE) {
			if (this.getClassid())  element += " classid=\""  + this.getClassid()  + "\"";
			if (this.getCodebase()) element += " codebase=\"" + this.getCodebase() + "\"";
		}
		element += ">";

		if (!paramWithAttribute) {
			var _this = this;
			function _createParam(name) {
				if (!name || _this.getParam(name) == "") return "";
				return "<param name=\"" + name + "\" value=\"" + _this.getParam(name) + "\"></param>";
			}

			for (var i in this.params) {
				element += _createParam(i);
			}
		}

		element += "</object>";

		return this.ongenerate(element);
	},
	write : function(config) {

		if (typeof(config) == "object") {
			this.setConfig(config);
		}

		document.write(this.toString());
		if (this.getId()) {
			this.node = document.getElementById(this.getId());
		} else {
			var objects = document.getElementsByTagNameBA("object");
			this.node = objects[objects.length - 1];
		}
		if (BA.ua.isWinIE) {
			if (this.node && (typeof(this.node.setActive) == "object" || typeof(this.node.setActive) == "function")) {
				this.node.setActive();
			}
		}

		this.onwrite();
	},
	ongenerate : function(element) {
		return element;
	},
	onwrite : function() {
	}
}



/* -------------------- Constructor : BAFlashGenerator inherits BAObjectGenerator -------------------- */

function BAFlashGenerator(config) {
	this.node       = null;
	this.required   = 0;
	this.src        = "";
	this.id         = "flash";
	this.className  = "flash";
	this.alternate  = "flash-alternate";
	this.width      = "100%";
	this.height     = "100%";
	this.fullScreen = false;
	this.params     = {};

	if (typeof(config) == "object") {
		this.setConfig(config);
	}
}
BAFlashGenerator.prototype = new BAObjectGenerator;
BAFlashGenerator.prototype.type = "application/x-shockwave-flash";
BAFlashGenerator.prototype.version = (function(){
	var _version = 0;
	var _plugin  = null;
	var _type    = BAFlashGenerator.prototype.getType();
	if (navigator.mimeTypes && navigator.mimeTypes[_type]) {
		_plugin = navigator.mimeTypes[_type].enabledPlugin;
		if (_plugin.description.match(/([0-9]+)\./)) {
			_version = parseInt(RegExp.$1);
		}
	} else if (BA.ua.isWinIE) {
		document.writeln("<script language=\"VBScript\">");

		document.writeln("on error resume next");
		document.writeln("Dim version");
		document.writeln("If IsObject(CreateObject(\"ShockwaveFlash.ShockwaveFlash\")) Then");
		document.writeln("version=Left(Hex(Int(CreateObject(\"ShockwaveFlash.ShockwaveFlash\").FlashVersion)), 1)");
		document.writeln("End If");
		document.writeln("</script>");
		_version = parseInt(version, 16) || 0;
	}
	return _version;
}());
BAFlashGenerator.prototype.setConfig = function(config){
	for (var i in config) {
		var value = config[i];
		switch (i) {
			case "required" :
				this.setRequired(value);
				break;
			case "src" :
				this.setSrc(value);
				break;
			case "name" :
				this.setName(value);
				break;
			case "id" :
				this.setId(value);
				break;
			case "className" :
				this.setClassName(value);
				break;
			case "alternate" :
				this.setAlternate(value);
				break;
			case "width" :
				this.setWidth(value);
				break;
			case "height" :
				this.setHeight(value);
				break;
			case "fullScreen" :
				this.setFullScreen(value);
				break;
			case "type" :
				this.setType(value);
				break;
			case "classid" :
				this.setClassid(value);
				break;
			case "codebase" :
				this.setCodebase(value);
				break;
			default :
				this.setParam(i, value);
				break;
		}
	}
}
BAFlashGenerator.prototype.setAlternate = function(alternate) {
	this.alternate = alternate;
}
BAFlashGenerator.prototype.getAlternate = function() {
	return this.alternate;
}
BAFlashGenerator.prototype.setFullScreen = function(fullScreen) {
	if (typeof(fullScreen) == "boolean") {
		this.fullScreen = fullScreen;
	} else if (typeof(fullScreen) == "string") {
		this.fullScreen = (fullScreen == "true");
	} else {
		this.fullScreen = (!!fullScreen);
	}
}
BAFlashGenerator.prototype.getFullScreen = function() {
	return this.fullScreen;
}
BAFlashGenerator.prototype.ongenerate = function(element) {
	var _this = this;
	function _createParamTag(name, value) {
		if (!name || value == "") return "";
		return "<param name=\"" + name + "\" value=\"" + value + "\"></param>";
	}
	function _createParamElement(name, value) {
		if (!name || value == "") return null;
		var param = document.createElementBA("param");
		param.setAttributeBA("name",  name);
		param.setAttributeBA("value", value);
		return param;
	}

	var reviseFlag = false;
	if (BA.ua.isGecko) {
		var ua     = navigator.userAgent;
		var key    = 'rv:';
		var sRv    = ua.substr(ua.indexOf(key) + key.length, 4);
		var fRv    = BA.ua.revision;
		reviseFlag = (fRv > 1.4 && (fRv < 1.7 || sRv == "1.7a"));
	}
	var paramWithAttribute = (BA.ua.isMacIE || (BA.ua.isGecko && BA.ua.revision <= 0.9));

	if (typeof(element) == "string") {
		if (this.getFullScreen()) {
			element = element.replace(/ width="[0-9]+\%?"/, " width=\"100%\"");
			element = element.replace(/ height="[0-9]+\%?"/, " height=\"100%\"");
		}
		if (BA.ua.isWinIE && this.getSrc()) {
			element = element.replace(/ data="[^"]+"/, "");
			element = element.replace("</object>", _createParamTag("movie", this.getSrc()) + "</object>");
		}
		if (BA.ua.isGecko) {
			if (!paramWithAttribute) {
				var align = element.match(/<param name="align" value="[^"]+"><\/param>/);
				var scale = element.match(/<param name="scale" value="[^"]+"><\/param>/);
				if (align && scale) {
					element = element.replace(/<param name="(align|scale)" value="[^"]+"><\/param>/g, "");
					if (reviseFlag) {
						element = element.replace("</object>", align + scale + "</object>");
					} else {
						element = element.replace("</object>", scale + align + "</object>");
					}
				}
			} else {
				var align = element.match(/ align="[^"]+"/);
				var scale = element.match(/ scale="[^"]+"/);
				if (align && scale) {
					element = element.replace(/ (align|scale)="[^"]+"/g, "");
					if (reviseFlag) {
						element = element.replace("<object", "<object" + align + scale);
					} else {
						element = element.replace("<object", "<object" + scale + align);
					}
				}
			}
		}
	} else {
		if (this.getFullScreen() && !BA.ua.isMacIE) {
			element.setAttributeBA("width",  "100%");
			element.setAttributeBA("height", "100%");
		}
		if (BA.ua.isWinIE && this.getSrc()) {
			element.removeAttribute("data");
			element.appendChildBA(_createParamElement("movie", this.getSrc()));
		}
		if (BA.ua.isGecko) {
			if (!paramWithAttribute) {
				var params = element.getElementsByTagName("param");
				var align = null;
				var scale = null;
				for (var i = 0, n = params.length; i < n; i++) {
					if (params[i].getAttributeBA("name") == "align") {
						align = params[i];
					} else if (params[i].getAttributeBA("name") == "scale") {
						scale = params[i];
					}
					if (align && scale) {
						break;
					}
				}
				if (align && scale) {
					element.removeChildBA(align);
					element.removeChildBA(scale);
					if (reviseFlag) {
						element.appendChildBA(align);
						element.appendChildBA(scale);
					} else {
						element.appendChildBA(scale);
						element.appendChildBA(align);
					}
				}
			} else {
				var align = element.getAttributeBA("align");
				var scale = element.getAttributeBA("scale");
				if (align && scale) {
					element.removeAttribute("align");
					element.removeAttribute("scale");
					if (reviseFlag) {
						element.setAttributeBA("align", align);
						element.setAttributeBA("scale", scale);
					} else {
						element.setAttributeBA("scale", scale);
						element.setAttributeBA("align", align);
					}
				}
			}
		}
	}
	return element;
}
BAFlashGenerator.prototype.onwrite = function() {
	if (this.getFullScreen() && this.node) {
		function _initWindowHeight(){
			if (typeof(document.readyState) == "string" && document.readyState != "complete") {
				return;
			} else {
				document.documentElement.style.height = "100%";
				if (document.body) {
					document.body.style.height = "100%";
				}
			}
		}

		BAAddOnload(function (){
			if (typeof(document.readyState) == "string" && document.readyState != "complete") {
				document.addEventListenerBA("readystatechange", _initWindowHeight);
			} else if (!BA.ua.isMacIE) {
				_initWindowHeight();
			}
		});

		this.node.style.width     = "100%";
		this.node.style.height    = "100%";

		var minWidth = this.getWidth();
		if (minWidth.indexOf("%")  == -1) minWidth  = minWidth  + "px";
		this.node.style.minWidth  = minWidth;

		var minHeight = this.getHeight();
		if (minHeight.indexOf("%") == -1) minHeight = minHeight + "px";
		this.node.style.minHeight = minHeight;

		function _setMinSize(){
			if (this.node) {
				BAGetGeometry();
				var width = this.getWidth();
				if (width && BA.geom.windowW < width) {
					if (width.indexOf("%") == -1) width = width + "px";
				} else {
					width = "100%";
				}
				this.node.style.width = width;

				var height = this.getHeight();
				if (height && BA.geom.windowH < height) {
					if (height.indexOf("%") == -1) height = height + "px";
				} else {
					height = (BA.ua.isGecko && BA.ua.revision <= 0.9) ? BA.geom.windowH + "px" : "100%";
				}
				this.node.style.height = height;
			}
		}
		var target    = (BA.ua.isOpera) ? document           : window;
		var loadEvent = (BA.ua.isGecko) ? "DOMContentLoaded" : "load";
		target.addEventListenerBA("resize",  _setMinSize, this);
		target.addEventListenerBA(loadEvent, _setMinSize, this);
	}
}
BAFlashGenerator.prototype.hideAlternate = function() {
	if (this.isEnabled() && this.getAlternate()) {
		if (BA.ua.isGecko) {
			document.write('<style type="text/css" media="screen">.' + this.getAlternate() + ' { display: none  !important }</style>');
			document.write('<style type="text/css" media="screen">.' + this.getClassName() + ' { display: block !important }</style>');
			document.write('<style type="text/css" media="print">.'  + this.getClassName() + ' { display: none  !important }</style>');
		} else {
			document.write('<style type="text/css" media="all">.'    + this.getAlternate() + ' { display: none  !important }</style>');
			document.write('<style type="text/css" media="all">.'    + this.getClassName() + ' { display: block !important }</style>');
		}
	}
}

