/*
 * FeatureList - simple and easy creation of an interactive "Featured Items" widget
 * Examples and documentation at: http://jqueryglobe.com/article/feature_list/
 * Version: 1.0.0 (01/09/2009)
 * Copyright (c) 2009 jQueryGlobe
 * Licensed under the MIT License: http://en.wikipedia.org/wiki/MIT_License
 * Requires: jQuery v1.3+
*/
;(function($) {
	$.fn.featureList = function(options) {
		var tabs	= $(this);
		//var video	= $('#output');
		//var play	= $('.vjs-big-play-button');
		var output	= $(options.output);

		new jQuery.featureList(tabs, output, options/*, video, play*/);

		return this;	
	};

	$.featureList = function(tabs, output, options /*, video, play*/) {
		function slide(nr) {
			if (typeof nr == "undefined") {
				nr = visible_item + 1;
				nr = nr >= total_items ? 0 : nr;
			}

			tabs.removeClass('current').filter(":eq(" + nr + ")").addClass('current');

			output.stop(true, true).filter(":visible").fadeOut();
			output.filter(":eq(" + nr + ")").fadeIn(function() {
				visible_item = nr;	
			});
		}

		var options			= options || {}; 
		var total_items		= tabs.length;
		var visible_item	= options.start_item || 0;

		options.pause_on_hover		= options.pause_on_hover		|| true;
		options.transition_interval = options.transition_interval == null ? 5000 : options.transition_interval; 

		output.hide().eq( visible_item ).show();
		tabs.eq( visible_item ).addClass('current');
		
		/*video.click(function() {
			clearInterval( timer );
		});
		
		play.click(function() {
			clearInterval( timer );
		});
		*/
		tabs.click(function() {
			if ($(this).hasClass('current')) {
				return false;	
			} 
			slide( tabs.index( this) );

			//$('.video-js')[0].player.pause();
			
				 var html = '<!-- Using the Video for Everybody Embed Code http://camendesign.com/code/video_for_everybody -->'+
				    '<video class="video-js" width="558" height="257" controls preload poster="video/index-test-poster.jpg">'+
				      '<source src="video/MAA-video.mp4" type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\' />'+
				      '<source src="video/MAA-video.ogg" type=\'video/ogg; codecs="theora, vorbis"\' />'+
				      '<!-- Flash Fallback. Use any flash video player here. Make sure to keep the vjs-flash-fallback class. -->'+
				      '<object id="video" class="vjs-flash-fallback" width="558" height="257" type="application/x-shockwave-flash"'+
				        'data="http://releases.flowplayer.org/swf/flowplayer-3.2.6.swf">'+
				        '<param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.6.swf" />'+
				        '<param name="allowfullscreen" value="true" />'+
				        '<param name="flashvars" value=\'config={"playlist":["http://maa.dev/video/index-test-poster.jpg", {"url": "http://maa.dev/video/MAA-video.flv","autoPlay":false,"autoBuffering":true}]}\' />'+
				        '<!-- Image Fallback. Typically the same as the poster image. -->'+
				        '<img src="video/index-test-poster.jpg" width="558" height="257" alt="Poster Image" title="No video playback capabilities." />'+
				      '</object>'+
				    '</video>';
				   $('.video-js-box').html(html);
				   VideoJS.setupAllWhenReady();
			
		});

		if (options.transition_interval > 0) {
			var timer = setInterval(function () {
				slide();
			}, options.transition_interval);

			if (options.pause_on_hover) {
				tabs.mouseenter(function() {
					clearInterval( timer );
				}).mouseleave(function() {
					clearInterval( timer );
					timer = setInterval(function () {
						slide();
					}, options.transition_interval);
				});
			}
		}
	};
})(jQuery);
