(function(jQuery){ 
	jQuery.fn.jmscroller = function(options){
				
		var scrollerInstance = this;
		
		scrollerInstance.defaults = {
					movingBox: '.absoluteBox',
					imagesHolder: '.product_grid_display',
					cycleTime: 1500,
					imageBoxes: '.product_grid_item',
					textHolder: '.product_text',
					opacityValue: 0.7,
					hideText: 1,
					direction: "right2left"
		}
														
		scrollerInstance.container = jQuery(scrollerInstance);
		scrollerInstance.o = jQuery.extend(scrollerInstance.defaults, options);
		
		/*scrollerInstance.imagePreloads = new Array();
		scrollerInstance.i = 0;
		jQuery(scrollerInstance.o.imageBoxes + " img").each(function(){
					scrollerInstance.imagePreloads[scrollerInstance.i] = new Image();
					scrollerInstance.imagePreloads[scrollerInstance.i].src = this.src;
					scrollerInstance.i++;
		});*/
		
		if(scrollerInstance.o.direction == "left2right"){
					scrollerInstance.theWay = "right";
		}else if(scrollerInstance.o.direction == "top2bottom"){
					scrollerInstance.theWay = "bottom";
		}else if(scrollerInstance.o.direction == "bottom2top"){
					scrollerInstance.theWay = "top";
		}else{
					scrollerInstance.theWay = "left";
		}
		
		scrollerInstance.o.container = scrollerInstance.container;
		scrollerInstance.cssObject = Object();
		jQuery(scrollerInstance.o.movingBox, scrollerInstance).css({'position' : "absolute"});
		
		jQuery(scrollerInstance.o.imageBoxes, scrollerInstance).css({'position' : "absolute"});
		
		//if(scrollerInstance.o.opacityValue != "false")
					jQuery(scrollerInstance.o.imageBoxes, scrollerInstance).css({opacity : scrollerInstance.o.opacityValue});
					
		if(scrollerInstance.o.hideText != "false")
					jQuery(scrollerInstance.o.textHolder, scrollerInstance).css({opacity : 0});
		
		scrollerInstance.distFromLeft, scrollerInstance.timeLeft, scrollerInstance.distRemaining, scrollerInstance.pixelsPerMS;
		scrollerInstance.imageHolders = new Array(); //to hold original image holders
		scrollerInstance.clones = new Array(); //to hold clone image holders
		
		scrollerInstance.totalOriginalIHs = jQuery(scrollerInstance.o.movingBox + " " + scrollerInstance.o.imageBoxes, scrollerInstance).size(); //get the amount of original image holders
		
		scrollerInstance.o.cycleTime = scrollerInstance.totalOriginalIHs * scrollerInstance.o.cycleTime;
		
		scrollerInstance.runningWidthTotal = 0; //initiate width at 0
		
		for(var i=0; i<scrollerInstance.totalOriginalIHs; i++){
					scrollerInstance.imageHolders[i] = jQuery(scrollerInstance.o.movingBox + " " + scrollerInstance.o.imageBoxes + ":eq(" + i + ")", scrollerInstance); //get all original image holders
					scrollerInstance.cssObject[scrollerInstance.theWay] = scrollerInstance.runningWidthTotal;
					scrollerInstance.imageHolders[i].css(scrollerInstance.cssObject); //set original image holders left values
					
					scrollerInstance.clones[i] = scrollerInstance.imageHolders[i].clone(); //make clones
					
					if(scrollerInstance.o.direction == "left2right"){
								scrollerInstance.runningWidthTotal += scrollerInstance.imageHolders[i].outerWidth(true);
					}else if(scrollerInstance.o.direction == "top2bottom"){
								scrollerInstance.runningWidthTotal += scrollerInstance.imageHolders[i].outerHeight(true);
					}else if(scrollerInstance.o.direction == "bottom2top"){
								scrollerInstance.runningWidthTotal += scrollerInstance.imageHolders[i].outerHeight(true);
					}else{
								scrollerInstance.runningWidthTotal += scrollerInstance.imageHolders[i].outerWidth(true); //get next left value amount
					}
					
					
		}
		
		scrollerInstance.totalOriginalWidth = scrollerInstance.runningWidthTotal; //get total width of original image holders
		
		scrollerInstance.pixelsPerMS = scrollerInstance.totalOriginalWidth/scrollerInstance.o.cycleTime;
		
		for(var i=0; i<scrollerInstance.totalOriginalIHs; i++){
					scrollerInstance.cssObject[scrollerInstance.theWay] = (parseInt(scrollerInstance.clones[i].css(scrollerInstance.theWay), 10) + scrollerInstance.totalOriginalWidth);
					scrollerInstance.clones[i].css(scrollerInstance.cssObject); //set left value at original plus total width of originals                                    
					
					jQuery(scrollerInstance.o.imagesHolder, scrollerInstance).append(scrollerInstance.clones[i]); //add the clones to the marquee
		}
		
		function startMovement(){
					scrollerInstance.cssObject[scrollerInstance.theWay] = "0px";
					scrollerInstance.movement.css(scrollerInstance.cssObject); //reset the position to 0 at end of first cycle
					scrollerInstance.cssObject[scrollerInstance.theWay] = -(scrollerInstance.totalOriginalWidth);
					scrollerInstance.movement
								.animate(
											scrollerInstance.cssObject, //animate to left again
											scrollerInstance.o.cycleTime, //8 seconds to move by above left amount again
											"linear", //constant speed again
											startMovement
					);
		}
	
		scrollerInstance.cssObject[scrollerInstance.theWay] = -(scrollerInstance.totalOriginalWidth);
		scrollerInstance.movement = jQuery(scrollerInstance.o.movingBox, scrollerInstance).animate( //start the animation of container of the image holders
		scrollerInstance.cssObject, //animate to the left
		scrollerInstance.o.cycleTime, //8 seconds to move by the above left amount
		"linear", //constant speed instead of swinging speed
		startMovement //start the recursion
		);
											
		scrollerInstance.o.container.hover(function(){
					scrollerInstance.movement.stop();
					scrollerInstance.movement.dequeue();
		},
		function(){
					scrollerInstance.distFromLeft = -(parseFloat(scrollerInstance.movement.css(scrollerInstance.theWay)));
					scrollerInstance.distRemaining = (scrollerInstance.totalOriginalWidth) - scrollerInstance.distFromLeft;
					scrollerInstance.timeLeft = scrollerInstance.distRemaining/(scrollerInstance.pixelsPerMS);
					scrollerInstance.newPxPerMS = scrollerInstance.distRemaining/scrollerInstance.timeLeft;
					scrollerInstance.cssObject[scrollerInstance.theWay] = -(scrollerInstance.totalOriginalWidth);
					scrollerInstance.movement = jQuery(scrollerInstance.o.movingBox, scrollerInstance).animate( //start the animation of container of the image holders
								scrollerInstance.cssObject, //animate to the left
								scrollerInstance.timeLeft, //8 seconds to move by the above left amount
								"linear", //constant speed instead of swinging speed
								startMovement //start the recursion
					);
					
		});
		if(scrollerInstance.o.opacityValue != "false"){
					jQuery(scrollerInstance.o.imageBoxes, scrollerInstance).hover(function(){
								if(scrollerInstance.o.hideText != "false"){
											jQuery(scrollerInstance.o.textHolder, this).stop().animate({ opacity: 1 }, 200);
								}
								jQuery(this).stop().animate({ opacity: 1 }, 200);
					},
					function(){
								if(scrollerInstance.o.hideText != "false"){
											jQuery(scrollerInstance.o.textHolder, this).stop().animate({ opacity: 0 }, 100);
								}
								jQuery(this).stop().animate({ opacity: scrollerInstance.o.opacityValue }, 100);
					});                    
		}                       
	}
})(jQuery);

