/*  jquery.swfo.js -- jQuery plugin for swf content creation, emdedding
**  Licensed under GPL <http://www.gnu.org/licenses/gpl.txt>
 */
// User-Agent	Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_2; en-us) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.1 Safari/525.18

(function($) {

  var UNDEF = 'undefined',
		OBJECT = 'object',
		SHOCKWAVE_FLASH = 'Shockwave Flash',
		SHOCKWAVE_FLASH_AX = 'ShockwaveFlash.ShockwaveFlash',
		FLASH_MIME_TYPE = 'application/x-shockwave-flash';

   /*  object constructor  */
    $.swfo = function () {
		( typeof console !== 'undefined' ) ? console.log ( '$> $.swfo.constructor ' + $.swfo.constructor ):0;
//         this.bucket = {};

//		var defaults = { foreground: 'red', background: 'yellow' };
// build options
// 		var opts = $.extend( {}, defaults, options );
//?
        return ;
	};

    /*  object methods  */
    $.swfo.prototype = {

		ua: ( function() {
		
// 			var w3cdom = typeof doc.getElementById != UNDEF 
// 			&& typeof doc.getElementsByTagName != UNDEF 
// 			&& typeof doc.createElement != UNDEF 
// 			&& typeof doc.appendChild != UNDEF 
// 			&& typeof doc.replaceChild != UNDEF,

			var w3cdom = typeof document.createElement != UNDEF,
				playerVersion = [0,0,0],
				d = null;

//		FLASH_MIME_TYPE = "application/x-shockwave-flash",
	var fla = navigator.mimeTypes[ FLASH_MIME_TYPE ];
//	.description ;
		( typeof console !== 'undefined' ) ? console.log ( ' FLASH_MIME_TYPE description: ' + fla.description + ' enabledPlugin: ' + fla.enabledPlugin ):0;
		( typeof console !== 'undefined' ) ? console.log ( '  navigator.mimeTypes[ FLASH_MIME_TYPE ].enabledPlugin.description: ' + fla.enabledPlugin.description ):0;
				
			if (typeof navigator.plugins != UNDEF && typeof navigator.plugins[SHOCKWAVE_FLASH] == OBJECT) {
				d = navigator.plugins[SHOCKWAVE_FLASH].description;

		( typeof console !== 'undefined' ) ? console.log ( '  navigator.plugins[SHOCKWAVE_FLASH].description : ' + d ):0;
				
				if ( d ) {
					d = d.replace(/^.*\s+(\S+\s+\S+$)/, "$1");
					playerVersion[0] = parseInt(d.replace(/^(.*)\..*$/, "$1"), 10);
					playerVersion[1] = parseInt(d.replace(/^.*\.(.*)\s.*$/, "$1"), 10);
					playerVersion[2] = /r/.test(d) ? parseInt(d.replace(/^.*r(.*)$/, "$1"), 10) : 0;
				}
			}
			else if ( typeof window.ActiveXObject != UNDEF ) {
				var a = null, fp6Crash = false;
				try {
					a = new ActiveXObject(SHOCKWAVE_FLASH_AX + ".7");
				}
				catch(e) {
					try { 
						a = new ActiveXObject(SHOCKWAVE_FLASH_AX + ".6");
						playerVersion = [6,0,21];
						a.AllowScriptAccess = "always";  // Introduced in fp6.0.47
					}
					catch(e) {
						if (playerVersion[0] == 6) {
							fp6Crash = true;
						}
					}
					if (!fp6Crash) {
						try {
							a = new ActiveXObject(SHOCKWAVE_FLASH_AX);
						}
						catch(e) {}
					}
				}
				if (!fp6Crash && a) { // a will return null when ActiveX is disabled
					try {
						d = a.GetVariable("$version");  // Will crash fp6.0.21/23/29
						if (d) {
							d = d.split(" ")[1].split(",");
							playerVersion = [parseInt(d[0], 10), parseInt(d[1], 10), parseInt(d[2], 10)];
						}
					}
					catch(e) {}
				}
			};
			
			var u = navigator.userAgent.toLowerCase(),
				p = navigator.platform.toLowerCase(),

// jQuery.browser.version
				// returns either the webkit version or false if not webkit
				webkit = /webkit/.test( u ) ? parseFloat( u.replace( /^.*webkit\/(\d+(\.\d+)?).*$/, "$1" ) ) : false, 

// 				ie = false,
				ie = $.browser.msie,
				
				windows = p ? /win/.test(p) : /win/.test(u),
				mac = p ? /mac/.test(p) : /mac/.test(u);
			
			( typeof console !== 'undefined' ) ? console.log ( '$> navigator.platform: ' + p   ):0;
			( typeof console !== 'undefined' ) ? console.log ( '$> ua webkit: ' + webkit +' ie: ' + ie ):0;
	
			return { w3cdom:w3cdom, pv:playerVersion, webkit:webkit, ie:ie, win:windows, mac:mac };
		} )(),

		hasPlayerVersion: function( rv ) {
		
	// 		var pv = ua.pv, v = rv.split(".");
			var pv = this.ua.pv, v = rv.split(".");
			
			v[0] = parseInt(v[0], 10);
			v[1] = parseInt(v[1], 10);
			v[2] = parseInt(v[2], 10);
			return (pv[0] > v[0] || (pv[0] == v[0] && pv[1] > v[1]) || (pv[0] == v[0] && pv[1] == v[1] && pv[2] >= v[2])) ? true : false;
		},

		getFlashPlayerVersion: function() {
			return { major:ua.pv[0], minor:ua.pv[1], release:ua.pv[2] };
		},
		
		appendSWF: function ( op ) {
	//		( typeof console !== 'undefined' ) ? console.log ( '@appendSWF op.toString: ' + op.toString() ):0;
	//	op.swfUrlStr, op.replaceElemIdStr, op.widthStr,  op.heightStr, op.swfVersionStr, op.xiSwfUrlStr, op.flashvarsObj, op.parObj, op.attObj
	
	// 		if ( !ua.w3cdom || !swfUrlStr || !replaceElemIdStr || !widthStr || !heightStr || !swfVersionStr) { return; }
	
	// 		if ( ! this.ua.w3cdom ||
			if ( ! this.ua.w3cdom ||
					! op.swfUrlStr || ! op.replaceElemIdStr || ! op.widthStr || ! op.heightStr || ! op.swfVersionStr ) { return }
	
	// 		op = $.extend( {}, defaults, op );
	
	// 		var flaObj = {};
			var flaObj = false;
			
			if ( this.hasPlayerVersion( op.swfVersionStr ) )
			{
				$( op.replaceElemIdStr ).hide();
				
				var att = (typeof op.attObj == OBJECT) ? op.attObj : {};
				
				att.data = op.swfUrlStr;
				att.width = op.widthStr +''; // typecast to string to make it idiot proof
				att.height = op.heightStr +'';
				
				var par = (typeof op.parObj == OBJECT) ? op.parObj : {};
	
	// return swf object ref?
				flaObj = this._createSWF( { attObj:att, parObj:par, id: op.replaceElemIdStr } );
					
				if ( att.id == op.replaceElemIdStr) {
	// 					setVisibility(replaceElemIdStr, true);
					$( op.replaceElemIdStr ).hide();
				}
			}
			return flaObj;
		},

        /*  private SWF creation  */
// 	function _createSWF( op ) {
		_createSWF: function ( op ) {
	
			var attObj=op.attObj, parObj=op.parObj, id=op.id;
			
			var r, el =  document.getElementById( id );
			
			if (typeof attObj.id == UNDEF) { // if no 'id' is defined for the object element, it will inherit the 'id' from the alternative content
				attObj.id = id;
			}
			
			if ( this.ua.ie && this.ua.win ) { // IE, the object element and W3C DOM methods do not combine: fall back to outerHTML
				var att = "";
				for (var i in attObj) {
					if (attObj[i] != Object.prototype[i]) { // Filter out prototype additions from other potential libraries, like Object.prototype.toJSONString = function() {}
						if (i == "data") {
							parObj.movie = attObj[i];
						}
						else if (i.toLowerCase() == "styleclass") { // 'class' is an ECMA4 reserved keyword
							att += ' class="' + attObj[i] + '"';
						}
						else if (i != "classid") {
							att += ' ' + i + '="' + attObj[i] + '"';
						}
					}
				}
				var par = "";
				for (var j in parObj) {
					if (parObj[j] != Object.prototype[j]) { // Filter out prototype additions from other potential libraries
						par += '<param name="' + j + '" value="' + parObj[j] + '" />';
					}
				}
				el.outerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' + att + '>' + par + '</object>';
	//?
	//			fixObjectLeaks(attObj.id); // This bug affects dynamic publishing only
	
				r =  document.getElementById( attObj.id );
			}
			else if ( this.ua.webkit && this.ua.webkit < 312) { // Older webkit engines ignore the object element's nested param elements: fall back to the proprietary embed element
				var e = createElement("embed");
				e.setAttribute("type", FLASH_MIME_TYPE);
				for (var k in attObj) {
					if (attObj[k] != Object.prototype[k]) { // Filter out prototype additions from other potential libraries
						if (k == "data") {
							e.setAttribute("src", attObj[k]);
						}
						else if (k.toLowerCase() == "styleclass") { // 'class' is an ECMA4 reserved keyword
							e.setAttribute("class", attObj[k]);
						}
						else if (k != "classid") { // Filter out IE specific attribute
							e.setAttribute(k, attObj[k]);
						}
					}
				}
				for (var l in parObj) {
					if (parObj[l] != Object.prototype[l]) { // Filter out prototype additions from other potential libraries
						if (l != "movie") { // Filter out IE specific param element
							e.setAttribute(l, parObj[l]);
						}
					}
				}
	
				el.parentNode.replaceChild(e, el);
				r = e;
			}
			else { // Well-behaving browsers
	
				var o = document.createElement( OBJECT );
				
				o.setAttribute( "type", FLASH_MIME_TYPE );
				for (var m in attObj) {
					if (attObj[m] != Object.prototype[m]) { // Filter out prototype additions from other potential libraries
						if (m.toLowerCase() == "styleclass") { // 'class' is an ECMA4 reserved keyword
							o.setAttribute("class", attObj[m]);
						}
						else if (m != "classid") { // Filter out IE specific attribute
							o.setAttribute(m, attObj[m]);
						}
					}
				}
				for (var n in parObj) {
					if (parObj[n] != Object.prototype[n] && n != "movie") { // Filter out prototype additions from other potential libraries and IE specific param element
	
						this._appendObjParam( o, n, parObj[n] );
					}
				}
	
				el.parentNode.replaceChild(o, el);
				r = o;
			}
			return r;
		},

		_appendObjParam: function ( el, pName, pValue ) {
			var p = document.createElement( 'param' );
			p.setAttribute( "name", pName );	
			p.setAttribute( "value", pValue );
			el.appendChild( p );
		},

		delSWF: function(){}
    };
	//   not used?
	$.swfo.defaults = {
	    verReg: '9.0.28',
	    background: '#FFFFFF'
	};

    /* integrate a global instance of swfo object into the global jQuery object */
    $.extend( {
        swfo$: new $.swfo(),
        appendSWF:	function () { return $.swfo$.appendSWF.apply( $.swfo$, arguments ) },
        delSWF:		function () { return $.swfo$._def.apply( $.swfo$, arguments ) }
    } );

})(jQuery);

/**
 *
 * Flash Player 9 Fix (http://blog.deconcept.com/2006/07/28/swfobject-143-released/)
 *
**/
// if (window.attachEvent) {
// 	window.attachEvent("onbeforeunload", function(){
// 		__flash_unloadHandler = function() {};
// 		__flash_savedUnloadHandler = function() {};
// 	});
// }




