(function($) {
	$.fn.carousel = function(options){

		// default configuration properties
		var defaults = {
		};

		var options = $.extend(defaults, options);

		return this.each(function() {
			var mainObj = $(this);
			var timeOut = 0;
			var iterator = 1;
			var wrapperObj = $('div.carouselWrapper', mainObj);
			var videoItems = $('div.item', mainObj);
			var rotateItems = $('div.rotate a', mainObj);
			var currentObj = $(rotateItems[0]);
			var currentNum = 0;
			$(rotateItems[0]).addClass('selected');

			for(var i=0; i<videoItems.length; i++){
				$(rotateItems[i]).attr('pos', i);
				if(i!=0){
					$(rotateItems[i]).bind('click', function(){
						toggle($(this));
					});
				}
			}                                     
			timeOut = setTimeout(function(){
				timeRotate();
			}, 5000);

			function timeRotate(){
				if(currentNum==rotateItems.length-1){
					iterator=-1;			
				}       					
				if(currentNum==0){
					iterator=1;			
				}
				currentNum = currentNum+iterator;
				currentObj = $(rotateItems[currentNum]);
				toggle(currentObj);
			}

			function toggle(obj){
				clearTimeout(timeOut);
				$('div.rotate a.selected', mainObj).bind('click', function(){
					toggle($(this));
				});
				$('div.rotate a.selected', mainObj).removeClass('selected');
				obj.addClass('selected');
				obj.unbind('click');
				wrapperObj.animate({'marginLeft': (-292)*obj.attr('pos')}, 500);
				currentObj = obj;
				timeOut = setTimeout(function(){
					timeRotate();
				}, 5000);
			}
		});

	};

})(jQuery);

