(function($){
	//global scope only in the plugin for variables
	var options={};
	var detectFlashPlayerVersion=[];
	var reqFlashPlayerVersion=[];
	$.fn.swf = function(url,settings){
		// build main options before element iteration
		options = $.extend({}, $.fn.swf.defaults, settings, {src:url});
		//overwrite alternateContent settings
		if($(options._content).length==1){
			if(options._getHtml){
				options._content = $(options._content).html();
			}
		}
		//check if version is valid
		if(supportVersion(options._flashPlayerVersion)){
			return this.each(function(){
				$this =$(this);
				createSWFObject($this);
			});
		}
		else{
			//check if expressInstall enabled
			if(options._expressInstall && checkVersion(detectFlashPlayerVersion,[6,0,65])){
				var MMPlayerType = ($.browser.msie) ? "ActiveX" : "PlugIn";
				var MMredirectURL = window.location;
				document.title = document.title.slice(0, 47) + " - Flash Player Installation";
				var MMdoctitle = document.title;
				options.FlashVars = "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"";
				options.src = options._path;
				return this.each(function(){
					$this =$(this);
					createSWFObject($this);
				});
			}
			else{//write alternateContent
				return this.each(function(){
						$this =$(this);
						write($this);
					}
				);
			}
		}
	};
	//Public Methods
	//plugin defaults
	$.fn.swf.defaults = {
		id : '',
		name : '',
		width : '100%',
		height : '100%',
		quality : 'high',
		allowScriptAccess : 'sameDomain',
		bgcolor : '#FFFFFF',
		wmode : '',
		type : 'application/x-shockwave-flash',
		pluginspage : 'http://www.adobe.com/go/getflashplayer',
		_flashPlayerVersion : '9.0.115', //latest version
		_expressInstall : false,
		_path : 'playerProductInstall.swf',
		_content: 'This site requires Adobe Flash Player',
		_getHtml : true,
		_replaceMethod : 'append',
		_wrap : ''

	};
	$.fn.swf.replaceMethod = {
		append:function($this){
			if(options._wrap!=''){
				$($this).append('<'+options._wrap+'>'+options._content+'</'+options._wrap+'>');
			}
			else{
				$($this).append(options._content);
			}
		}
	}
	$.fn.swf.replaceMethod.html = function($this){
		if(options._wrap!=''){
			$($this).html('<'+options._wrap+'>'+options._content+'</'+options._wrap+'>');
		}
		else{
			$($this).html(options._content);
		}		
	}
	$.fn.swf.replaceMethod.externalInterface = function($this){
		if(options._wrap!=''){
			document.getElementById($this.attr("id")).innerHTML = '<'+options._wrap+'>'+options._content+'</'+options._wrap.split(' ')[0]+'>';
		}
		else{
			document.getElementById($this.attr("id")).innerHTML = options._content;
		}		

	}
	//private functions
	function checkVersion(detectFlashPlayerVersion,reqFlashPlayerVersion){
		if(Number(detectFlashPlayerVersion[0])>Number(reqFlashPlayerVersion[0])){	
			return true;
		}
		else if(Number(detectFlashPlayerVersion[0])==Number(reqFlashPlayerVersion[0])){
			if(Number(detectFlashPlayerVersion[1])>Number(reqFlashPlayerVersion[1])){
				return true;
			}
			else if(Number(detectFlashPlayerVersion[1])==Number(reqFlashPlayerVersion[1])){				
				if(Number(detectFlashPlayerVersion[2])>=Number(reqFlashPlayerVersion[2])){
					return true;
				}
				else{
					return false;
				}
			}
			else{
				return false;
			}
		}
		else{
			return false;
		}
	}
	function setDetectFlashPlayerVersion(arrVersion){
		detectFlashPlayerVersion[0]=arrVersion[0];
		detectFlashPlayerVersion[1]=arrVersion[1];
		detectFlashPlayerVersion[2]=arrVersion[2];
	}
	function supportVersion(flashPlayerVersion){
		reqFlashPlayerVersion = flashPlayerVersion.split('.');
		if(navigator.plugins && navigator.mimeTypes.length){
			var x = navigator.plugins["Shockwave Flash"];
			if(x && x.description) {
				setDetectFlashPlayerVersion(x.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split("."));
			}
			else{
				setDetectFlashPlayerVersion([0,0,0]);
			}
		}
		else if (navigator.userAgent && navigator.userAgent.indexOf("Windows CE") >= 0){ // if Windows CE
		var axo = 1;
		var counter = 3;
			while(axo) {
				try {
					counter++;
					axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+ counter);
				} catch (e) {
					axo = null;
				}
			}
			setDetectFlashPlayerVersion([counter,0,0]);
		}
		else {
			try{
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
			}catch(e){
				try {
					var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
					axo.AllowScriptAccess = "always"; // error if player version < 6.0.47 (thanks to Michael Williams @ Adobe for this code)
				} catch(e) {
						setDetectFlashPlayerVersion([6,0,21]);
				}
				try {
					axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
				} catch(e) {}
			}
			if (axo != null) {
				setDetectFlashPlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));
			}
		}
		return checkVersion(detectFlashPlayerVersion,reqFlashPlayerVersion)
	}
	function createSWFObject($this){		
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) { // netscape plugin architecture
			var embedAttrs='<embed ';
			for(i in options){
				if(!i.search(/_/)==0 && options[i]!=''){//filter the custom settings and blank settings, only allow settings by object and embed tags					
					embedAttrs += i + '="' + options[i] + '" ';
				}
			}
			embedAttrs += '></embed>';
			options._content = embedAttrs;
			write($this);
		}
		else{
			var objAttrs = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
			if(options.id!=''){
				objAttrs+=' id="'+options.id;
			}
			objAttrs +='" width="'+options.width+'" height="'+options.height+'" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">';
			objAttrs+='<param name="movie" value="'+options.src+'" />';
			for(i in options){
				if((i!='src'&&i!='width'&&i!='height'&&i!='id'&&i!='type'&&i!='pluginspage'&&i!='name')&&(!i.search(/_/)==0 && options[i]!='')){
					objAttrs+='<param name="'+i+'" value="'+options[i]+'" />';
				}
			}
			objAttrs+='</object>';
			options._content = objAttrs;
			write($this);
		}
	}
	function write($this){
		var replaceMethod = $.fn.swf.replaceMethod[options._replaceMethod];
		if($.isFunction(replaceMethod)){
			replaceMethod($this);
		}
		else{							
			var replaceMethod = $.fn.swf.replaceMethod[$.fn.swf.defaults._replaceMethod];
			if($.isFunction(replaceMethod)){
				replaceMethod($this);
			}
			else{
				$($this).append(options._content);
			}
		}
	}
})(jQuery);
