/*
	Author mbarry
	URL: http://www.rakacreative.com
	Since 2010-07-01
*/
(function($) {
	TabsRotation = function(options) {
		this.options = $.extend(true,{},this.options,options);
		this.tabs = $(this.options.tabs);
		this.tabsContent = $(this.options.tabsContent);
		var fn = this;
		this.tabs.each(function(indx) {
			if (indx != fn.options.startIndex) {
				fn.hideTab(indx,false);
			} else {
				fn.showTab(indx);
			}
			$(this).bind("click",function(evt) {
				fn.showTab(indx);
				return false;
			})
		});
	};
	TabsRotation.prototype = {
		tabs: null,
		tabsContent: null,
		currentTabIndex: 0,
		options: {
			tabs: ".rotation_tab",
			tabsContent: ".rotation_tab_content",
			startIndex: 0,
			tabSelectedClass: "selected",
			showDuration: 500,
			hideDuration: 500,
			onShowTab: function(tabIndex) {},
			onHideTab: function(tabIndex) {}
		},
		showTab: function(tabIndex,animate) {
			if (this.currentTabIndex != tabIndex)
				this.hideTab(this.currentTabIndex);
			animate = (animate == undefined) ? true : animate;
			if (animate) {
				$(this.tabsContent[tabIndex]).stop();
				$(this.tabsContent[tabIndex]).animate({
					opacity: 1
				},this.options.showDuration);
				$(this.tabsContent[tabIndex]).css("display","");
			} else {
				$(this.tabsContent[tabIndex]).css("opacity",1);
				$(this.tabsContent[tabIndex]).css("display","");
			}
			$(this.tabs[tabIndex]).addClass(this.options.tabSelectedClass);
			this.options.onShowTab.call(this,tabIndex);
			this.currentTabIndex = tabIndex;
		},
		hideTab: function(tabIndex,animate) {
			animate = (animate == undefined) ? true : animate;
			if (animate) {
				$(this.tabsContent[tabIndex]).stop();
				$(this.tabsContent[tabIndex]).animate({
					opacity: 0
				},this.options.hideDuration,function() {
					$(this).css("display","none");
				});
			} else {
				$(this.tabsContent[tabIndex]).css("opacity",0);
				$(this.tabsContent[tabIndex]).css("display","none");
			}
			$(this.tabs[tabIndex]).removeClass(this.options.tabSelectedClass);
			this.options.onHideTab.call(this,tabIndex);
		}
	};
	
	TabbedSlideshow = function(options) {
		TabsRotation.call(this, options);
		var fn = this;
		if (this.options.autoPlay) {
			fn.startSlideshow();
		};
		this.tabs.bind("click.TabbedSlideshow", function() {
			fn.stopSlideshow();
		});
	};
	$.extend(true, TabbedSlideshow.prototype, TabsRotation.prototype, {
		interval: null,
		options: {
			autoPlay: true,
			interval: 8000
		},
		nextTab: function() {
			this.showTab(this.currentTabIndex + 1 > this.tabs.length - 1 ? 0 : this.currentTabIndex + 1);
		},
		stopSlideshow: function() {
			clearInterval(this.interval);
		},
		startSlideshow: function() {
			var fn = this;
			this.interval = setInterval(function() {
				fn.nextTab();
			}, this.options.interval);
		}
		
	});
})(jQuery);
