/**
 * Flash Video Player plugin for jQuery
 * 
 * Dependencies:
 * 		jquery.js
 * 		swfobject.js
 * 		player.swf
 * 
 * @author Simplimation
 * @version 1.0
 * 
 */


	jQuery.fn.videoPlayer = function(opt){
		
		// get options
		var options = jQuery.extend({
			 autostart: "false",
			 playerpath: "",		// relative path to player.swf (e.g. /js/)
			 playerfile: "player.swf",	// filename of Flash player
			 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: "bottom",	// bottom|over|false
			 volume: 50,			// player default volume
			 mute: "false",			// default mute setting
			 resizing: "true",		// resize to fit container
			 frontcolor: "",		// control bar foreground color
			 backcolor: "",			// control bar background color
			 lightcolor: "",		// progress and volume bar color
			 screencolor: "",		// screen bg color
			 flashbgcolor: "000000",	// flash background color
			 logo: "",				// logo overlay image
			 skin: "",				// JW Player skin file (.swf file)
			 link: "",				// link button url
			 thumbnail: "",			// default thumbnail (applied to all videos)
			 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
				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);
				jQuery(el).attr("id",playerId);
				//jQuery(el).replaceWith("<div class='FlashVideoPlayer' id='" + playerId + "'></div>");
				log(playerId);
			// create flash object
				flashvars = {};
				flashvars.id = playerId;	
				flashvars.file = videoFile;
				flashvars.image = imageFile;
				flashvars.logo = options.logo;
				flashvars.skin = options.skin;
				flashvars.link = options.link;	
				flashvars.autostart = options.autostart;
				flashvars.repeat = options.repeat;
				flashvars.displayclick = options.displayclick;	
				flashvars.icons = options.icons;	
				flashvars.quality = options.quality;
				flashvars.controlbar = options.controlbar;	
				flashvars.volume = options.volume;	
				flashvars.mute = options.mute;
				flashvars.resizing = options.resizing;	
				flashvars.frontcolor = options.frontcolor;
				flashvars.backcolor = options.backcolor;	
				flashvars.lightcolor = options.lightcolor;
				flashvars.screencolor = options.screencolor;
				params = {
					allowfullscreen: options.fullscreen,
					allowscriptaccess: options.scriptable,
					bgcolor: "#" + options.flashbgcolor
				};
				atts = {};
				swfobject.embedSWF(options.playerpath + options.playerfile, playerId, options.width, options.height, "9.0.0", options.playerpath + "expressInstall.swf", flashvars, params, atts);
		});
	};
