(function($) {

	$.fn.viddlerWidget = function() {
		
		this.each(function() {
			
			viddlerWidgetUI.init(this);
	
		}); 
		
		
	};
	
	var viddlerWidgetUI = {
		
		// Control function
		init: function(widget){
			
			// DOM selection of THIS passed in from EACH function.
			// Changed back into a jQuery selection.
			// Passed back into functions below as $widget to
			// refer to EACH instance as a jQuery selection.
			widget = $(widget);
			
			// Global variables (for sliding the inline widget)
			$itemsContainer = widget.find('.items');
			numberOfItems = widget.find('.item').length;
			itemPadding = 24; // .width() isn't including padding
			itemWidth = widget.find('.item').width() + itemPadding;
			itemsWidth = (widget.find('.item').width() + itemPadding) * numberOfItems;
			viewableItemArea = widget.find('.items-container').width();
			offsetValue = 88; // unused but available space in the viewable area
			slideOffset = viewableItemArea - offsetValue;
			numberOfClicks = parseInt(itemsWidth/slideOffset);
			clickCounter = 0;
			
			// Main functions call
			this.prepWidget(widget);
			this.initialShow(widget);
			this.clickFunctions(widget);
			
		},
		
		// Main functions
		prepWidget: function($widget) {

			if ($widget.children().hasClass('slider')) {
				if (!$itemsContainer.parent().parent().hasClass('alt-layout')) {
					$itemsContainer.css('width', itemsWidth);
				}
				if (numberOfClicks > 0) {
					$widget.find('span.next a').css({opacity:1.0});
				}
			}
			
		},
		
		initialShow: function($widget){
			
			var $initialFlag = $widget.find('span.initial-show');
			var initialVideo = $initialFlag.attr('title');
			
			if ($widget.children().hasClass('static')) {
				this.showVideo($widget, initialVideo);
			}
			
			// doing something unknown to fix unload error in ie7 www.fusioncharts.com/forum/FindPost12189.aspx
			if ($.browser.msie && (jQuery.browser.version > 6.9999) && (jQuery.browser.version < 7.9999)) {
				window.attachEvent("onbeforeunload", fcRemoveChart);
				function fcRemoveChart(){
					var arr = document.all.tags("object");
					if (arr && arr.length) {
						for (var i = arr.length - 1; 0 <= i; i--) {
							arr[i].removeNode(true);
						}
					}
				}
			}
			
		},
		
		clickFunctions: function($widget) {
			
			var $videoAnchor = $widget.find('a.movie');
			var $nextAnchor = $widget.find('span.next a');
			var $prevAnchor = $widget.find('span.prev a');
			
			$videoAnchor.click(function(){
				
				var thisVideo = $(this).find('span').attr('title');
				if ($widget.children('div').hasClass('static')) {
					viddlerWidgetUI.showVideo($widget, thisVideo);
					return false;
				} else {
					viddlerWidgetUI.showThickboxVideo($widget, thisVideo, $(this));
				}
				
			});
			
			$nextAnchor.click(function(){
				
				//alert('click counter =' + clickCounter + ' number of clicks available = ' + numberOfClicks);
				if (clickCounter < numberOfClicks) {
					$widget.find('span.next a').animate({opacity:0.0}, 500);
					$widget.find('span.prev a').animate({opacity:1.0}, 500);
					$itemsContainer.animate({"left": "-=" + slideOffset}, 1500);
					clickCounter++;
				}
				return false;
				
			});
			
			$prevAnchor.click(function(){
				
				if (clickCounter > 0) {
					//alert('click counter =' + clickCounter + ' number of clicks available = ' + numberOfClicks);
					$widget.find('span.prev a').animate({opacity:0.0}, 500);
					$widget.find('span.next a').animate({opacity:1.0}, 500);
					$itemsContainer.animate({"left": "+=" + slideOffset}, 1500);
					clickCounter--;
				}
				return false;
			
			});
				
		},
		
		// Nested Functions
		showVideo: function($widget, whichVideo) {
			
			$widget.find('.movie-box span.embed-code').html(whichVideo)	
			
		},
		
		showThickboxVideo: function($widget, whichVideo, $anchor) {
			
			$widget.find('div#hiddenVideoContent').html(whichVideo);
			$anchor.addClass('thickbox');
			$anchor.attr('href', '#TB_inline?inlineId=hiddenVideoContent');
			
		}
		
	}

})(jQuery);

$(document).ready(function() {
	
	$('.viddler-widget').viddlerWidget();
	
});
