/*
jswf 1.0.0
Copyright (c) 2008 Alexey Yu. Chuprina <656b61@gmail.com>
This software is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
*/

(function(){

var _default = {
	requiredVersion: [8,0,0],
	alternative: 'You need to be updated version of <a href="http://www.macromedia.com/go/getflash/">Flash Player</a>',
	params: {
		menu: 'false',
		wmode: 'transparent'
	}
};

var jswf = window.jswf = function(src, width, height) {
	if (!(this instanceof jswf)) return new jswf(src, width, height);
	this._src = src;
	this._width = parseInt(width);
	this._height = parseInt(height);
	
	this._chechVersion = true;
	//this._requiredVersion = null;
	//this._alternative = null;
	//this._id = null;
	this._params = {};
	this._variables = {};
	
	for (var i in _default.params) this._params[i] = _default.params[i];
};

jswf.prototype = {
	insert: function() {
		document.write(this.html());
	},
	html: function() {
		if (this._chechVersion && !jswf.chechVersion(this._requiredVersion || _default.requiredVersion))
			return this.buildAlternative();
		return this.buildEmbed();
	},	
	id: function(v) {
		this._id = v;
		return this;
	},	
	params: function(v) {
		for (var i in v) this._params[i] = v[i];
		return this;
	},
	param: function(n, v) {
		this._params[n] = v;
		return this;
	},	
	variables: function(v) {
		for (var i in v) this._variables[i] = v[i];
		return this;
	},
	variable: function(n, v) {
		this._variables[n] = v;
		return this;
	},	
	chechVersion: function(v) {
		this._chechVersion = !!v;
		return this;
	},	
	requiredVersion: function(v) {
		if (!v) return this;
		if (typeof v == 'string') v = v.split('.', 3);
		this._requiredVersion = [];
		for (var i = 0; i < 3; i++) this._requiredVersion[i] = parseInt(v[0]) || 0;		
		return this;
	},
	buildAlternative: function() {
		return ['<div style="width:', this._width, ';height:', this._height, ';">',
			(this._alternative || _default.alternative), '</div>'].join('');
	},
	buildEmbed: function() {
		var i, html = []; 
		
		var fv = []		
		for (i in this._variables) fv.push(i + '=' + _urlEncode(this._variables[i]));
		if (fv.length) {
			if (this._params.flashvars) this._params.flashvars += fv.join('&');
			else this._params.flashvars += fv.join('&');
		}
		if (fuckingIE) {
			html.push('<object width="', this._width, '" height="', this._height, '"');
			if (this._id) html.push(' id="', this._id ,'"');
			html.push(' classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">');
			html.push('<param name="src" value="', this._src, '"/>');		
			for (i in this._params)	html.push('<param name="', i, '" value="', _htmlChars(this._params[i]), '"/>');
			html.push('</object>');
		} else {
			html.push('<embed width="', this._width, '" height="', this._height, '" src="', this._src, '"');
			if (this._id) html.push(' id="', this._id, '" name="', this._id, '"');
			for (i in this._params) html.push(' ', i, '="', _htmlChars(this._params[i]), '"');
			html.push(' pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"/>');
		} 

		return html.join('');
	}
};


var fuckingIE = (navigator.appVersion.indexOf("MSIE") != -1) 
	&& (navigator.appVersion.toLowerCase().indexOf("win") != -1)
	&& !(navigator.userAgent.indexOf("Opera") != -1); 

jswf.setAlternative = function(v) {
	_default.alternative = v;
};


jswf.setRequiredVersion = function(v) {
	_default.requiredVersion = [];
	if (typeof v == 'string') v = v.split('.', 3);
	for (var i = 0; i < 3; i++) _default._requiredVersion[i] = parseInt(v[0]) || 0;
};

var _version;
jswf.getVersion = function() {
	if (_version !== undefined) return _version;
	_version = null;	
	var np = navigator.plugins; 
	if (np != null && np.length > 0) {
		var desc = false;
		if (np['Shockwave Flash']) desc = np['Shockwave Flash'].description;
		else if (np['Shockwave Flash 2.0']) desc = np['Shockwave Flash 2.0'].description;
		if (desc) {			
			desc = desc.split(' ');
			var version = desc[2].split('.');					
			var versionRevision = desc[3];
			if (versionRevision == '') versionRevision = desc[4];
			if (versionRevision[0] == 'd') {
				versionRevision = versionRevision.substring(1);
			} else if (versionRevision[0] == 'r') {
				versionRevision = versionRevision.substring(1);
				if (versionRevision.indexOf('d') > 0) versionRevision = versionRevision.substring(0, versionRevision.indexOf('d'));
			}
			_version = [parseInt(version[0]), parseInt(version[1]), parseInt(versionRevision)];
			version = versionRevision = null;
		}
		desc = null;
	}
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) playerVersion = [4,0,0];
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) playerVersion = [3,0,0];
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) playerVersion = [2,0,0];
	else if (fuckingIE) {
		var version = false, axo, e;
		
		try {
			axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.7');
			version = axo.GetVariable('$version');
		} catch (e) { try {
			axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
			version = 'WIN 6,0,21,0';
			axo.AllowScriptAccess = 'always';
			version = axo.GetVariable('$version');	
		} catch (e) { try {
			axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.3');
			version = axo.GetVariable('$version');
		} catch (e) { try {
			axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.3');
			version = 'WIN 3,0,18,0';
		} catch (e) { try {
			axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
			version = 'WIN 2,0,0,11';
		} catch (e) {}
		}}}}
		
		if (version) {		
			version = version.split(' ')[1].split(",");
			_version = [parseInt(version[0]), parseInt(version[1]), parseInt(version[2])];
		}
		version = axo = e = null;
	}
	np = null;
	return _version;
};

jswf.chechVersion = function(version) {
	if (!version) return false;
	var r = [], v = jswf.getVersion();
	if (!v) return false;
	if (typeof version == 'string') version = version.split('.', 3);
	for (var i = 0; i < 3; i++) r[i] = parseInt(version[0]) || 0;
	if (v[0] > r[0]) return true;
	else if (v[0] == r[0]) {
		if (v[1] > r[1]) return true;
		else if (v[1] == r[1] && v[2] >= r[2]) return true;
	}
	return false;
};

var _htmlChars = function(string) {
	return string.replace('"', '&quot;')
		.replace('&', '&amp;')
		.replace('<', '&lt;')
		.replace('>', '&gt;');
};

var _urlEncode = function(string) {
	string = string.replace(/\r\n/g, '\n');
	var utftext = '';
	for (var n = 0; n < string.length; n++) {
		var c = string.charCodeAt(n);
		if (c < 128) {
			utftext += String.fromCharCode(c);
		} else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		} else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}
	}
	return escape(utftext);
};

})();