(function($){
	$.fn.carousel = function(){
		return this.each(function(){
			carousel($(this));
		});
	}
	$.fn.mcarousel = function(p){
		return this.each(function(){
			mcarousel($(this), p);
		});
	}
	var carousel = function(gallery){
		gitem 			= gallery.find('li');
		gitem.width 	= gitem.width();
		gitem.height	= gitem.height();
		
		gallery.width(gitem.width*gitem.length);
		
		gtime = 400;
		gpause = 20000;
		var completenext = function(){button.bind('click', nextclick);h = setInterval(auto, gpause);}
		var completeprev = function(){button.bind('click', prevclick);h = setInterval(auto, gpause);}
		var animate = function(v, cb){
			gallery.animate({marginLeft: v}, gtime, cb);
		}
		var nowpos = function(){return parseFloat(gallery.css('margin-left'));}
		var gmax = 0;
		var gmin = -gitem.width*(gitem.length-1);
		
		var nextclick = function(){
			clearInterval(h);
			button = $(this).unbind('click', nextclick);
			if(nowpos() > gmin){
				animate('-='+gitem.width+'px', completenext);
			} else if (nowpos() == gmin){ animate(gmax, completenext);}
		}
		var prevclick = function(){
			clearInterval(h);
			button = $(this).unbind('click', prevclick);
			if(nowpos() < gmax){
				animate('+='+gitem.width+'px', completeprev);
			} else if (nowpos() == gmax){ animate(gmin, completeprev);}
		}
		var auto = function(){$next.click();}
		
		var $next = $('.gcontrol .next');
		var $prev = $('.gcontrol .prev');
		
		$next.bind('click', nextclick);
		$prev.bind('click', prevclick);
		
		h = setInterval(auto, gpause);
	}
	var mcarousel = function($this,p){
		var params = {
 			visibleCount: 5,
 			scrollCount:1,
			next: $('.products .next'),
			prev: $('.products .prev'),
			wrap1: 'mcarousel',
			wrap2: 'mcarousel-clip'
  		};
		
		$this.wrap('<div class="'+params.wrap1+'"><div class="'+params.wrap2+'"></div></div>');
			li = $this.find('li');
			$clip = $('.'+params.wrap2);
			width = Math.round($clip.width()/params.visibleCount);
			widthn = width+parseFloat(li.css('padding-left'))+parseFloat(li.css('padding-right'));
			count = li.length;
		
		$this.width(widthn*count);
		li.width(width);
		var nowposul = function(){return parseFloat(ul.css('marginLeft'));}
		var maxleft = widthn*(li.length-params.visibleCount);
		
		var scrollingRight = function(){
			ul = $clip.children('ul');
			li = ul.find('li');
			var scrolling = function(){
				if(nowposul()>=-maxleft)
					ul.css({marginLeft:nowposul()-1});
			}
			sR = setInterval(scrolling, 1);
		}
		var scrollingRightStop = function(){clearInterval(sR);}
		
		var scrollingLeft = function(){
			ul = $clip.children('ul');
			li = ul.find('li');
			var scrolling = function(){
				if(nowposul()<=-1)
					ul.css({marginLeft:nowposul()+1});
			}
			sL = setInterval(scrolling, 1);
		}
		var scrollingLeftStop = function(){	clearInterval(sL);	}
		var nextclick = function(){
			if(nowposul() <= -maxleft){
				$this.append(p[0],p[1],p[2], p[3], p[4], p[5]);
				$this.width(widthn*(count+6));
				maxleft = widthn*(li.length+6-params.visibleCount);
				li = $this.find('li');
				li.width(width);
			}
		}
		
		$next = params.next;
		$prev = params.prev;
		
		$next.bind('mouseover', scrollingRight);
		$next.bind('mouseout', scrollingRightStop);
		$next.bind('click', nextclick);
		$prev.bind('mouseover', scrollingLeft);
		$prev.bind('mouseout', scrollingLeftStop);
	}
})(jQuery);
