(function($) {
	//public functions
	var methods = {
		init : function(options) {
			var defaults = {},			
			opts = $.extend(defaults, options);
			$.extend(this, opts);
			
			var $w	= $(window), goingLeft = true, animating = false, mouseX = 0, prevTouchX = 0,
			wWidth 	= $w.width(),
			quarter	= wWidth/4, threeQuarter = quarter*3,
			
			$wineList		= $(this),
			$wrapper 		= $("#content"),			
			$wines			= $wineList.find("li"),
			$divs 			= $wines.find("div"),
			minMarginLeft	= $wrapper.width() - $wineList.width()+110, 
			maxMarginLeft	= 110,
			currMargin 		= Number($wineList.css("margin-left").replace("px","")),
			scrollInterval = 0;
						
			$divs.not(":eq(2)").css({opacity: 0});
			$wines.children("a").mouseover(function(){
				var $t = $(this);
				$divs.css({opacity: 0});
				$t.children("div").css({opacity: 1});
			});
			
			try{
				$wineList[0].addEventListener("touchstart", onTouchStart, true);
				$wineList[0].addEventListener("touchmove", onTouchMove, true);
			}catch(e){}
			
			$w.resize(function(){
				wWidth 	= $w.width();
				quarter	= wWidth/4;
				threeQuarter = quarter*3;
			});
			$(document).mousemove(onMouseMove);
			
			function onMouseMove(e){
				mouseX = e.clientX;
				if(mouseX <= quarter){
					if(animating && goingLeft){return;}
					goingLeft = true;				
					animating = true;				
					scrollInterval = setInterval(function(){
						scroll(false);						
					}, 16);
					
				}else if(mouseX >= threeQuarter){
					if(animating && !goingLeft){return;}
					goingLeft = false;				
					animating = true;				
					scrollInterval = setInterval(function(){
						scroll(false);						
					}, 16);				
					
				}else{		
					animating = false;
					clearInterval(scrollInterval);					
				}
			}	
			
			function scroll(useDistance, distance){
				if(useDistance !== true){
					currMargin += (goingLeft)?8:-8;
				}else{
					currMargin += distance;
				}
				
				currMargin = (currMargin < minMarginLeft)?minMarginLeft:currMargin;
				currMargin = (currMargin > maxMarginLeft)?maxMarginLeft:currMargin;
				
				$wineList.css("margin-left", currMargin); 
				
				
				if(currMargin == minMarginLeft) {
					$('#leftArrow').css('display','inherit');
					$('#rightArrow').css('display','none');					
				} else if(currMargin == maxMarginLeft) {
					$('#leftArrow').css('display','none');
					$('#rightArrow').css('display','inherit')					
				} else {
					$(".arrow").css("display","inherit");
				}
				
				
				
				
				//should we be changing the current wine
				$wines.each(function(){
					var $t = $(this), offX = ($t.offset()).left;
					
					
					if(mouseX > offX && mouseX < offX + $t.width()){
						$t.children("a").mouseover();
					}
				});
			}
			
			function onTouchStart(event){	
				var touches = event.changedTouches;
				
				if(touches.length !== 1){
					return;
				}
				var first = touches[0];
				prevTouchX = first.pageX;
				//event.preventDefault();
			}
			
			function onTouchMove(event){
				var touches = event.changedTouches;
				
				if(touches.length !== 1){
					return;
				}
		
		
				var first = touches[0];
				scroll(true, first.pageX-prevTouchX);
				prevTouchX = first.pageX;
						
				event.preventDefault();
			}
			
			return this;
		}
	};
	
	/*
		private vars
	*/
	
	
	/*
		private functions
	*/
	
	$.fn.wineList = function(method) {
		//Method Calling Login
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' + method + ' does not exist on jQuery.');
		}
	};
})(jQuery);
