(function(){
	
	$.fn.catalogProducts = function(options) {
		var isMethodCall = (typeof options == "string") || false;
		var args = arguments;
		if (!isMethodCall)
			var options = $.extend({}, options, {});
		
		$(this).each(function(){
			var cat = $(this).data("catalogProducts");
			if (isMethodCall && cat) {
				if (cat[options] && typeof cat[options] == "function"){
					cat[options].apply(cat, $.makeArray(args).slice(1));
				}
				return;
			} else {
				cat = new $.catalogProducts(this, options);
				$(this).data("catalogProducts", cat);
			}
		});
		return this;
	};
	
	$.catalogProducts = function(elem, options) {
		this.elem = elem;
		this.options = $.extend({}, options, $.catalogProducts.defaults);
		this.init();
	};
	
	$.extend($.catalogProducts, {
		defaults: {
			itemWidth: 98,
			itemWidthOpen: 212,
			visibleItem: 5,
			shiftInDuration: 600,
			shiftOutDuration: 600,
			showDuration: 400,
			hideDuration: 400,
			onMouseOver: function(obj, event) {
				if ($(this).is(".hover")) return false;
				var idx = obj.items.index(this);
				
				$(this).addClass("hover");
				
				obj.options.shift = false;
				if (idx >= obj.curIndex + obj.options.visibleItem - 1) {
					obj.options.shift = true;
					
					obj.curIndex++;
					if (obj.curIndex > obj.itemsCount-1) obj.curIndex = obj.itemsCount-1;
					var x = obj.curIndex * (obj.options.itemWidth+obj.itemPad) * -1;
					$(obj.elem).animate({left: x + "px"}, {queue:false, duration:obj.options.shiftInDuration});
					
				}
				
				$(this).animate({width: obj.options.itemWidthOpen + "px"}, {queue:false, duration:obj.options.showDuration});
			},
			onMouseOut: function(obj, event) {
				if (!$(this).is(".hover")) return false;
				
				$(this).removeClass("hover");
				
				if (obj.options.shift) {
					obj.options.shift = false;
					
					obj.curIndex--;
					if (obj.curIndex < 0) obj.curIndex = 0;
					var x = obj.curIndex * (obj.options.itemWidth+obj.itemPad) * -1;
					$(obj.elem).animate({left: x + "px"}, {queue:false, duration:obj.options.shiftOutDuration});
					
				}
				
				$(this).animate({width: obj.options.itemWidth + "px"}, {queue:false, duration:obj.options.hideDuration});
			}
		},
		prototype: {
			init: function() {
				var self = this;
				this.parent = $(this.elem).parent();
				this.items = $(">li", this.elem);
				this.itemsCount = this.items.length;
				this.shortCircuit = false;
				
				if (!this.itemsCount) {
					this.shortCircuit = true;
					return;
				}
				
				this.itemPad = parseInt($(this.items[0]).css("padding-left")) || 0;
				this.itemPad += parseInt($(this.items[0]).css("padding-right")) || 0;
				
				//make width large enough
				$(this.elem).css({width: (this.options.itemWidthOpen * this.itemsCount)+"px"});
				
				this.items.each(function(){
					$(this).bind("mouseover", function(event){
						self.options.onMouseOver.apply(this, [self, event]);
					});
					$(this).bind("mouseout", function(event){
						self.options.onMouseOut.apply(this, [self, event]);
					});
				});
				
				this.curIndex = 0;
			},
			
			move: function(idx) {
				if (idx != this.curIndex) {
					this.curIndex = idx;
					var x = this.curIndex * (this.options.itemWidth+this.itemPad) * -1;
					$(this.elem).animate({left: x + "px"}, {queue:false, duration:this.options.shiftInDuration});
				}
			}
		}
	});
})(jQuery);