(function($){
	
	var bannerThumb = function(_this, options){
		this._obj = _this;
		this.bannerThumb();
	};
	
	bannerThumb.prototype = {

		options: {
			speed: "normal"	,
			timeout: 3500,
		},
		
		_obj: null,

		timer: null,
		
		current: null,

		bannerThumb: function(){
			this.current = $("", this._obj)[0];
			this.bind();
		},
	
		bind: function(){
			var self = this;
	
			$("tr.thumb > td", this._obj).bind("click", function(){

				if(self.current == this){
					return;
				}
				clearTimeout(self.timer);				
				self.active(this);
			});
		
			$("td:first", this._obj)
				.css(
					"height", 
					$("td:first", this._obj).height()
				);
			
			$("td:first").bind("mouseover", function(){
				clearTimeout(self.timer);
			});
			
			$("td:first").bind("mouseout", function(){
				self.animate();
			});
			
			$("tr.thumb > td:eq(0)").trigger("click");
		},
		
		animate: function(){
			
			if(!this.options.timeout) return;
	
			var self = this;

			clearTimeout(self.timer);

			this.timer = setTimeout(function(){
				
				var td = $("tr.thumb > td", this._obj);
				
				for(var i=0; i<td.length; i++){
					if(self.current == $(td).eq(i)[0]){
						i = (i == td.length - 1) ? 0 : i + 1;
						break;
					}
				}

				$(td).eq(i).trigger("click");

			}, this.options.timeout);
		},
		
		active: function(_this){
			
			$("div", this._obj).removeClass("active");
			$("div", _this).addClass("active");
			
			this.current = _this;

			var self = this;
	
			$("td > div:first", this._obj).fadeOut(this.options.speed, function(){
				$(this)
					.html(
						$(".thumb-post", self.current).html()
					)
					.fadeIn(self.options.speed, function(){
						self.animate();
					});
			});
			
		}
			
	};
	
	$.fn.bannerThumb = function(options){
		
		return this.each(function(){
			this.bannerThumb = new bannerThumb(this, options);			
		});

	};
	
})(jQuery);
