/* 
 * Environment Object
 */
	
Environment = {
	getWindowHeight : function(){
		var h = 0;
		if (document.documentElement && document.documentElement.clientHeight) {
			h = document.documentElement.clientHeight;
		} else if (document.body && document.body.clientHeight) {
			h = document.body.clientHeight;
		} else {
			h = window.innerHeight;
	    }
	    return h;
	},
	getWindowWidth : function(){
		var w = 0;
		if (document.documentElement && document.documentElement.clientWidth) {
			w = document.documentElement.clientWidth;
		} else if (document.body && document.body.clientWidth) {
			w = document.body.clientWidth;
		} else {
			w = window.innerWidth;
	    }
	    return w;
	}
};
(function(){
	var ua = navigator.userAgent.toLowerCase();
	
	// platform
	Environment.win = ua.indexOf('win') != -1;
	Environment.mac = ua.indexOf('mac') != -1;
	
	// explorer
	Environment.ie = false;
	/*@cc_on
		Environment.ie = true;
	@*/
	
	// netscape, firefox, safari, ect... basically, not explorer.
	Environment.gecko = ua.indexOf('gecko') != -1;
	
	// safari
	Environment.safari = ua.indexOf('safari') != -1;
	Environment.safari2 = false;
	if (Environment.safari) {
		var ver = parseInt(ua.substring(ua.lastIndexOf('/')+1));
		if (ver > 400) Environment.safari2 = true;
	}
	
	// mobile
	Environment.ipod = ua.indexOf('ipod') != -1;
	Environment.iphone = ua.indexOf('iphone') != -1;
	Environment.pre = ua.indexOf('webos') != -1 && ua.indexOf('pre') != -1;
	Environment.android = ua.indexOf('android') != -1;
	
})();

/* 
 * ElementHelper Object
 */

ElementHelper = {
	createElement : function (type, content) {
		var elem = document.createElement(type);
		if (content) elem.innerHTML = content;
		return elem;
	},
	createTextElement : function (content) {
		var div = document.createElement('div');
		div.innerHTML = content;
		return div.firstChild;
	}
};

/* 
 * Element Object
 *
 * Requires: ElementHelper
 */

Element = {
	get : function (elem) {
		if (typeof(elem) == 'string') {
			elem = document.getElementById(elem);
		}
		return elem || null;
	},
	remove : function (elem) {
		elem = this.get(elem);
		if (elem) elem.parentNode.removeChild(elem);
	},
	css : function (elem, name, value) {
		elem = this.get(elem);
		if (elem) elem.style[name] = value;
	},
	insertInto : function (elem, content) {
		elem = this.get(elem);
		if (elem && content) {
			if (typeof(content) == 'string') {
				elem.innerHTML = content;
			} else {
				elem.innerHTML = '';
				elem.appendChild(content);
			}
		}
	},
	appendTo : function (elem, content) {
		elem = this.get(elem);
		if (elem && content) {
			if (typeof(content) == 'string') {
				content = ElementHelper.createTextElement(content);
			}
			elem.appendChild(content);
		}
	}
};

/* 
 * Flash Object
 *
 * Requires: Environment
 */

Flash = {
	hasVersion : function (versionRequired) {
		versionRequired = parseInt(versionRequired);
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes['application/x-shockwave-flash'] && navigator.mimeTypes['application/x-shockwave-flash'].enabledPlugin) {
			var description = navigator.plugins['Shockwave Flash'].description;
			var version = parseInt(description.split(' ')[2].split('.')[0]);
			return version >= versionRequired;
		} else if (Environment.win && Environment.ie) {// && window.execScript
			this.hasVersionResult = null;
			execScript('on error resume next: Flash.hasVersionResult=IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.' + versionRequired + '"))','VBScript');
			return this.hasVersionResult;
		}
		return false;
	}
};

/* 
 * FlashElement Class
 *
 * Requires: Element, Environment, Flash
 */

FlashElement = function (movie, options) {
	this.movie = movie;
	this.version = options.version || '10';
	this.vars = options.vars || new Object();
	this.alt = options.alt || null;
	this.options = new Object();
	this.options.id                = options.id                || null;
	this.options.width             = options.width             || '550';
	this.options.height            = options.height            || '400';
	this.options.align             = options.align             || null;
	this.options.allowFullScreen   = options.allowFullScreen   || null;
	this.options.allowNetworking   = options.allowNetworking   || null;
	this.options.allowScriptAccess = options.allowScriptAccess || null;
	this.options.base              = options.base              || null;
	this.options.bgcolor           = options.bgcolor           || null;
	this.options.menu              = options.menu              || null;
	this.options.quality           = options.quality           || null;
	this.options.salign            = options.salign            || null;
	this.options.scale             = options.scale             || null;
	this.options.wmode             = options.wmode             || null;
	this.attributeNames = ['id', 'width', 'height', 'align'];
	var flashvars = '';
	for (var n in this.vars) {
		var v = this.vars[n];
		if (v) flashvars += n + '=' + escape(v) + '&';
	}
	this.options.FlashVars = flashvars;
	this.movie += '?' + flashvars;
	var embed = (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length);
	if (embed) {
		this.options.src = this.movie;
		this.options.type = 'application/x-shockwave-flash';
		this.options.pluginspage = 'http://www.macromedia.com/go/getflashplayer';
		if (this.options.id != null) {
			this.options.name = this.options.id;
			//delete this.options.id;
		}
		var html = '<embed';
		for (var n in this.options) {
			var v = this.options[n];
			if (v) html += ' ' + n + '="' + v + '"';
		}
		html += '></embed>';
	} else {
		this.options.movie = this.movie;
		var attrs = new Object();
		attrs.classid = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
		attrs.codebase = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + this.version + ',0,0,0';
		for (var i = 0; i < this.attributeNames.length; i++) {
			var a = this.attributeNames[i];
			attrs[a] = this.options[a];
			delete this.options[a];
		}
		var html = '<object';
		for (var n in attrs) {
			var v = attrs[n];
			if (v) html += ' ' + n + '="' + v + '"';
		}
		html += '>';
		for (var n in this.options) {
			var v = this.options[n];
			if (v) html += '<param name="' + n + '" value="' + v + '" />';
		}
		html += '</object>';
		if (attrs.id && Environment.win && Environment.ie && Flash.hasVersion(8)) {
			window.attachEvent('onunload', function(){
				var obj = Element.get(attrs.id);
				if (obj) {
					for (var i in obj) {
						if (typeof obj[i] == 'function') {
							obj[i] = function(){};
						}
					}
					Element.remove(obj);
				}
			});
		}
	}
	this.html = html;
};
FlashElement.prototype.getHtml = function(){
	if (this.alt != null && ! Flash.hasVersion(this.version)) {
		return this.alt;
	} else {
		return this.html;
	}
};
FlashElement.prototype.write = function(){
	document.write(this.getHtml());
};
FlashElement.prototype.insertInto = function (elem) {
	Element.insertInto(elem, this.getHtml());
};
FlashElement.prototype.appendTo = function (elem) {
	Element.appendTo(elem, this.getHtml());
};


