if(!Control) var Control = {};
Control.ViewFLV = Class.create();

Control.ViewFLV.prototype = {
	ua: navigator.userAgent.toLowerCase(),
	pluginType: "",
	fv: [0,0],

	initialize: function(handle, options) {
		this.handle = $(handle);
		this.options = options || {};

		this.base = this.options.base || '';

		this.movie = this.options.movie || "/player.swf";
		this.width = parseInt(this.options.width) || 480;
		this.height = parseInt(this.options.height) || 380;
		this.majorversion = parseInt(this.options.majorversion) || 7;
		this.build = parseInt(this.options.build) || 0;

		this.bgcolor = this.options.bgcolor || '#000000';
		this.play = this.options.play;
		this.loop = this.options.loop;
		this.menu = this.options.menu;
		this.allowfullscreen = this.options.allowfullscreen;
		this.allowscriptaccess = this.options.allowscriptaccess;
		this.swliveconnect = this.options.swliveconnect;

		var flashvars = new Array();
		flashvars.push('overstretch=false');
		if (this.options.file) flashvars.push('file=' + escape(this.base + this.options.file));
		if (this.options.image) flashvars.push('image=' + escape(this.base + this.options.image));
		this.flashvars = flashvars.join('&');

		this.mainCalled = false;

		if (this.uaHas("ieMac")) return;
		this.getFlashVersion();
		this.eventDOMContentLoaded = this.main.bindAsEventListener(this);
		this.domLoad();
//		Event.stopObserving(document, "DOMContentLoaded", this.eventDOMContentLoaded);
	},

	uaHas: function(ft) {
		switch(ft) {
			case "ieMac":
				return /msie/.test(this.ua) && !/opera/.test(this.ua) && /mac/.test(this.ua);
			case "ieWin":
				return /msie/.test(this.ua) && !/opera/.test(this.ua) && /win/.test(this.ua);
			case "gecko":
				return /gecko/.test(this.ua) && !/applewebkit/.test(this.ua);
			case "opera":
				return /opera/.test(this.ua);
			case "safari":
				return /applewebkit/.test(this.ua);
			default:
				return false;
		}
	},
	
	getFlashVersion: function() {
		if (this.fv[0] != 0) return;  
		if (navigator.plugins && typeof navigator.plugins["Shockwave Flash"] == "object") {
			this.pluginType = "npapi";
			var _d = navigator.plugins["Shockwave Flash"].description;
			if (typeof _d != "undefined") {
				_d = _d.replace(/^.*\s+(\S+\s+\S+$)/, "$1");
				var _m = parseInt(_d.replace(/^(.*)\..*$/, "$1"), 10);
				var _r = /r/.test(_d) ? parseInt(_d.replace(/^.*r(.*)$/, "$1"), 10) : 0;
				this.fv = [_m, _r];
			}
		}
		else if (window.ActiveXObject) {
			this.pluginType = "ax";
			try { // avoid fp 6 crashes
				var _a = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
			}
			catch(e) {
				try { 
					var _a = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
					this.fv = [6, 0];
					_a.AllowScriptAccess = "always"; // throws if fp < 6.47 
				}
				catch(e) {
					if (this.fv[0] == 6) return;
				}
				try {
					var _a = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
				}
				catch(e) {}
			}
			if (typeof _a == "object") {
				var _d = _a.GetVariable("$version"); // bugs in fp 6.21/6.23
				if (typeof _d != "undefined") {
					_d = _d.replace(/^\S+\s+(.*)$/, "$1").split(",");
					this.fv = [parseInt(_d[0], 10), parseInt(_d[2], 10)];
				}
			}
		}
	},

	main: function() {
		if (this.mainCalled) return;
		if (this.load_timer) clearInterval(this.load_timer);
		this.load_timer = null;

		this.mainCalled = true;

		Element.hide(this.handle);
		if (this.hasFlashVersion(this.majorversion, this.build)) this.writeSWF();
		Element.show(this.handle);
	},

	domLoad: function() {
		this.load_timer = setInterval(this.main.bind(this), 250);
		Event.observe(document, "DOMContentLoaded", this.eventDOMContentLoaded);
	},

	hasFlashVersion: function(major, release) {
		return (this.fv[0] > major || (this.fv[0] == major && this.fv[1] >= release)) ? true : false;
	},

	writeSWF: function() {
		if (this.pluginType == "npapi") {
			if (this.uaHas("gecko")) {
				while(this.handle.hasChildNodes()) this.handle.removeChild(this.handle.firstChild);
				var _obj = document.createElement("object");
				_obj.setAttribute("type", "application/x-shockwave-flash");
				_obj.setAttribute("data", this.movie);
				_obj.setAttribute("width", this.width);
				_obj.setAttribute("height", this.height);
				_obj.setAttribute("bgcolor", this.bgcolor);

				if (this.play) _obj.setAttribute("play", this.play);
				if (this.loop) _obj.setAttribute("loop", this.loop);
				if (this.menu) _obj.setAttribute("menu", this.menu);
				if (this.allowfullscreen) _obj.setAttribute("allowfullscreen", this.options.allowfullscreen);
				if (this.allowscriptaccess) _obj.setAttribute("allowscriptaccess", this.allowscriptaccess);
				if (this.swliveconnect) _obj.setAttribute("swliveconnect", this.swliveconnect);
				if (this.flashvars) _obj.setAttribute("flashvars", this.flashvars);

				this.handle.appendChild(_obj);
			}
			else {
				var _emb = ' bgcolor="' + this.bgcolor + '"';
				if (this.play) _emb += ' play="' + this.play + '"';
				if (this.loop) _emb += ' loop="' + this.loop + '"';
				if (this.menu) _emb += ' menu="' + this.menu + '"';
				if (this.allowfullscreen) _emb += ' allowfullscreen="' + this.options.allowfullscreen + '"';
				if (this.allowscriptaccess) _emb += ' allowscriptaccess="' + this.allowscriptaccess + '"';
				if (this.swliveconnect) _emb += ' swliveconnect="' + this.swliveconnect + '"';
				if (this.flashvars) _emb += ' flashvars="' + this.flashvars + '"';
				this.handle.innerHTML = '<embed type="application/x-shockwave-flash" src="' + this.movie + '" width="' + this.width + '" height="' + this.height + '" pluginspage="http://www.macromedia.com/go/getflashplayer"' + _emb + '></embed>';
			}
		}
		else if (this.pluginType == "ax") {
			var _par = "";
			if (this.play) _par += '<param name="play" value="' + this.play + '" />';
			if (this.loop) _par += '<param name="loop" value="' + this.loop + '" />';
			if (this.menu) _par += '<param name="menu" value="' + this.menu + '" />';
			if (this.allowfullscreen) _par += '<param name="allowfullscreen" value="' + this.options.allowfullscreen + '" />';
			if (this.allowscriptaccess) _par += '<param name="allowscriptaccess" value="' + this.allowscriptaccess + '" />';
			if (this.swliveconnect) _par += '<param name="swliveconnect" value="' + this.swliveconnect + '" />';
			if (this.flashvars) _par += '<param name="flashvars" value="' + this.flashvars + '" />';

			var _p = window.location.protocol == "https:" ? "https:" : "http:";
			this.handle.innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + this.width + '" height="' + this.height + '" codebase="' + _p + '//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + this.majorversion + ',0,' + this.build + ',0"><param name="movie" value="' + this.movie + '" />' + _par + '</object>';
		}
	}


};

