/**
 * Flash Video Player plugin for jQuery
 * 
 * Dependencies:
 * 		jquery.js
 * 		swfobject.js
 * 		player.swf
 * 
 * usage:
 * 	<a class="video" href="videoFilename.swf"><img src="thumbnailFilename.jpg" alt=""/></a>
 * 
 * @author Simplimation
 * @version 1.0
 * 
 */


	jQuery.fn.videoPlayer = function(opt){
		
		// get options
		var options = jQuery.extend({
			 autostart: "false",
			 id: "",		// manually set element ID attribute for Javascript 
			 playerfile: "/mediaplayer.swf",	// filename of Flash player
			 filename: "", // video filename
			 thumbnail: "",			// default thumbnail (applied to all videos)
			 width: "100%",		// player width
			 height: "100%",		// player height
			 repeat: "none",		// none|list|always|single
			 displayclick: "play",	// play|link|fullscreen|mute|next|none
			 icons: "true",			// show buffering indicator
			 quality: "false", 		// true = enable hi-quality video
			 controlbar: "false",	// bottom|over|false
			 volume: 50,			// player default volume
			 mute: "false",			// default mute setting
			 resizing: "true",		// resize to fit container
			 frontcolor: "cccccc",		// control bar foreground color
			 backcolor: "000000",			// control bar background color
			 lightcolor: "ffffff",		// progress and volume bar color
			 screencolor: "000000",		// screen bg color
			 flashbgcolor: "000000",	// flash background color
			 logo: "",				// logo overlay image
			 skin: "",				// JW Player skin file (.swf file)
			 link: "",				// link button url
			 fullscreen: "true",	// allow full screen mode
			 scriptable: "always",	// allow Javascript access to player controls
			 log: false
		}, opt);
		
		// log function
		function log(val){
			if(options.log){
				jQuery.log(val);
			}
		}
		
		// step through each element
		jQuery(this).each(function(i,el){
			// set video filename
				if(options.filename){
					var videoFile = options.filename;	
				} else {
					// get from href
					var videoFile = jQuery(el).attr("href");
				}
				log(videoFile);
			// set thumbnail filename
				var imageFile = jQuery(el).find("img").attr("src");
				// override with options
				if (options.thumbnail) {
					imageFile = options.thumbnail;
				}
				log(imageFile);
			// replace selector with container
				var playerId = "FlashVideoPlayer" + (i+1);
				// override with options if set
				if(options.id){
					playerId = options.id;
				}
				jQuery(el).attr("id",playerId);
				//jQuery(el).replaceWith("<div class='FlashVideoPlayer' id='" + playerId + "'></div>");
				log(playerId);
				log(jQuery("#"+playerId));
			// create flash object
				var so = new SWFObject(options.playerfile,playerId,options.width,options.height,'8');
				so.addParam('allowscriptaccess', options.scriptable);
				so.addParam('allowfullscreen',options.fullscreen);
				so.addParam('bgcolor',"#" + options.flashbgcolor);
				so.addParam('name','audioP');
				so.addVariable('height',options.height);
				so.addVariable('width',options.width);
				so.addVariable('file',videoFile);
				so.addVariable('image',imageFile);
				so.addVariable('backcolor',options.backcolor);
				so.addVariable('frontcolor',options.frontcolor);
				so.addVariable('lightcolor',options.lightcolor);
				so.addVariable('screencolor',options.screencolor);
				so.addVariable('displayheight',options.height);
				so.addVariable('displaywidth',options.width);
				so.addVariable('javascriptid',playerId);
				so.addVariable('showicons',options.icons);
				so.addVariable('shownavigation',options.controlbar);
				so.addVariable('showstop','false');
				so.addVariable('showdigits','false');
				so.addVariable('autostart',options.autostart);
				//so.addVariable('repeat',options.repeat);
				so.addVariable('volume',options.volume);
				//so.addVariable('thumbsinplaylist','false');
				//so.addVariable('shuffle','false');
				so.addVariable('enablejs','true');
				// write Flash to #audioplayer
				so.write(playerId);
		});
	};

