;(function($) {
	
	$.fn.extend({
		videoThumbs: function(options) {
			return this.each(function() {
				new $.VideoThumbs(this, options);
			});
		}
	});
	
	$.VideoThumbs = function(element, options) {
		
		var defaults = {
			selectedClassValue: 'selected',
			videoPlayDelay: 2000, 
			videoObject: {
				source: '/text/flashsource/www/swfs/videoplayer.swf', 
				instanceName: 'video1', 
				instanceWidth: 607, 
				instanceHeight: 382, 
				minFlashPlayerVersion: '9.0', 
				bgColor: '#000000', 
				wmode: 'transparent',
				defaultSkinSource: '/text/flashsource/www/swfs/SportsChaletSkin.swf', 
				failoverSource: '/text/flashsource/www/swfs/expressinstall.swf', 
				videoInfoContentContainerClassBase: 'video-resources', 
				videoDisplayContainerId: 'video01', 
				videoThumbImageClass: 'video-thumb'
			}
		};
		
		if(options) {
			var extendedVideoObject = $.extend({}, defaults.videoObject, options.videoObject || {});
			options.videoObject = extendedVideoObject;
		}
		
		this.options			= $.extend({}, defaults, options || {});
		this.element			= $(element);
		this.linksCollection	= $("." + this.options.videoObject.videoThumbImageClass, this.element);
		this.vObj				= this.instantiateVideoObject();
						
		// Add onclick events to the video links
		var self = this;
		$(this.linksCollection).bind('click', function() {
			self.updateStage(this);
			if (self.FlashCommunication(self.options.videoObject.instanceName)) {
				t = setTimeout( function() {
					self.FlashCommunication(self.options.videoObject.instanceName).PlayMovie();
				}, self.options.videoPlayDelay);	
			}
			return false;
		});
				
		this.updateStage(this.linksCollection[0]);
	};
	
	$.extend($.VideoThumbs.prototype, {
		
		FlashCommunication:	function(targetInstanceName) {
			
			this.id = targetInstanceName;
						
			this.Send = function (args) {				
				var Flash = this.Flash();
				if(Flash[args['method']]) {
					Flash[args['method']](args['variables']);			
				}
			};
			
			this.PlayMovie = function () {
				var data = "data = {variables:{},method:'PlayMovie'}";
				data = Object(eval(data));
				this.Send(Object(data));
			}
			
			this.StopMovie = function () {
				var data = "data = {variables:{},method:'StopMovie'}";
				data = Object(eval(data));
				this.Send(Object(data));
			}
			
			this.Flash = function () {
				var isIE = navigator.appName.indexOf("Microsoft") != -1;
				return (isIE) ? window[this.id] : document[this.id];
			}
			
			return this;
		}, 
		
		instantiateVideoObject: function() {
			
			var self = this;
			
			var vObj = new SWFObject(
				self.options.videoObject.source,
				self.options.videoObject.instanceName, 
				self.options.videoObject.instanceWidth,
				self.options.videoObject.instanceHeight, 
				self.options.videoObject.minFlashPlayerVersion,
				self.options.videoObject.bgColor
			);
			
			vObj.addParam(
				"wmode",
				self.options.videoObject.wmode
			);
			
			vObj.addVariable(
				"playerSkinURL",
				self.options.videoObject.defaultSkinSource
			);
			
			// Failover case - if the client's browser doesn't have the minimum Flash version, 
			// then display a Flash object for downloading the latest Flash version
			vObj.useExpressInstall(self.options.videoObject.failoverSource);
			
			return vObj;
		}, 
		
		updateStage: function(element) {
			
			this.updateLinks(element);
			this.loadVideo(element);
		}, 
		
		updateLinks: function(element) {
			
			$(this.linksCollection).parents('li').removeClass(this.options.selectedClassValue);
			$(element).parents('li').addClass(this.options.selectedClassValue);
		}, 
		
		loadVideo: function(element) {
			
			var resourceContainer	= $(element).parent().siblings('.' + this.options.videoObject.videoInfoContentContainerClassBase);
			var videoWidth			= resourceContainer.children(".videoWidth").text();
			var videoHeight			= resourceContainer.children(".videoHeight").text();
			var startImageURL		= resourceContainer.children(".startImageURL").text();
			var playerSkinURL		= resourceContainer.children(".playerSkinURL").text();
			var playerSourceURL		= resourceContainer.children(".playerSourceURL").text();
			
			this.vObj.addVariable("videoWidth", videoWidth);
			this.vObj.addVariable("videoHeight", videoHeight);
			this.vObj.addVariable("startImageURL", startImageURL);
			this.vObj.addVariable("playerSourceURL", playerSourceURL);
			
			if (playerSkinURL != "") {
				this.vObj.addVariable("playerSkinURL", playerSkinURL);
			}
			this.vObj.write(this.options.videoObject.videoDisplayContainerId);
		}
		
	});
	
})(jQuery);
