﻿(function($) { 
	jQuery.fn.smoothDivScroll = function(options){
		var defaults = {
		scrollingHotSpotLeft: "div.scrollingHotSpotLeft",
		scrollingHotSpotRight: "div.scrollingHotSpotRight",
		scrollWrapper: "div.scrollWrapper",
		scrollableArea: "div.scrollableArea",
		hiddenOnStart: false,
		ajaxContentURL: "",
		countOnlyClass: "",
		scrollingSpeed: 25,
		mouseDownSpeedBooster: 3,
		autoScroll: "",
		autoScrollDirection: "right",
		autoScrollSpeed: 1,
		pauseAutoScroll: "",
		visibleHotSpots: "",
		hotSpotsVisibleTime: 5,
		startAtElementId: ""
		};
		options = $.extend(defaults, options);
		return this.each(function() {
			var $mom = $(this);
			if(options.ajaxContentURL.length !== 0){
				$mom.scrollableAreaWidth = 0;
				$mom.find(options.scrollableArea).load((options.ajaxContentURL), function(){	
					$mom.find(options.scrollableArea).children((options.countOnlyClass)).each(function() {
						$mom.scrollableAreaWidth = $mom.scrollableAreaWidth + $(this).outerWidth(true);
					});
					$mom.find(options.scrollableArea).css("width", ($mom.scrollableAreaWidth + "px"));
					if(options.hiddenOnStart) {
						$mom.hide();
					}
					windowIsResized();
				});		
			}
			var scrollXpos;
			var booster;
			var motherElementOffset = $mom.offset().left;
			var hotSpotWidth = 0;
			booster = 1;
			var hasExtended = false;
			$(window).one("load",function(){
				if(options.ajaxContentURL.length === 0) {
					$mom.scrollableAreaWidth = 0;
					$mom.tempStartingPosition = 0;
					$mom.find(options.scrollableArea).children((options.countOnlyClass)).each(function() {
						if( (options.startAtElementId.length !== 0) && (($(this).attr("id")) == options.startAtElementId) ) {
						$mom.tempStartingPosition = $mom.scrollableAreaWidth;
						}
						$mom.scrollableAreaWidth = $mom.scrollableAreaWidth + $(this).outerWidth(true);
					});
					$mom.find(options.scrollableArea).css("width", $mom.scrollableAreaWidth + "px");
					if(options.hiddenOnStart) {
						$mom.hide();
					}
				}
				$mom.find(options.scrollWrapper).scrollLeft($mom.tempStartingPosition);
				if(options.autoScroll !== "") {
					$mom.autoScrollInterval = setInterval(autoScroll, 6);
				}
			});
			$mom.find(options.scrollingHotSpotRight, options.scrollingHotSpotLeft).one('mouseover',function(){
				if(options.autoScroll == "onstart") {
					clearInterval($mom.autoScrollInterval);
				}
			});	
			$(window).bind("resize",function(){
				windowIsResized();
			});
			function windowIsResized() {
				if(!(options.hiddenOnStart)) {
					$mom.scrollableAreaWidth = 0;
					$mom.find(options.scrollableArea).children((options.countOnlyClass)).each(function() {
						$mom.scrollableAreaWidth = $mom.scrollableAreaWidth + $(this).outerWidth(true);
					});
					$mom.find(options.scrollableArea).css("width", $mom.scrollableAreaWidth + 'px');
				}
				$mom.find(options.scrollWrapper).scrollLeft("0");
				var bodyWidth = $("body").innerWidth();
			}
			$(document).find(options.scrollingHotSpotRight).bind('mousemove',function(e){
				scrollXpos = 5;
			});
			$(document).find(options.scrollingHotSpotRight).bind('mouseover',function(){
				if(options.autoScroll == "onstart") {
					clearInterval($mom.autoScrollInterval);
				}
				$mom.rightScrollInterval = setInterval(doScrollRight, 6);
			});	
			$(document).find(options.scrollingHotSpotRight).bind('mouseout',function(){
				clearInterval($mom.rightScrollInterval);
				scrollXpos = 0;
			});
			$(document).find(options.scrollingHotSpotRight).bind('mousedown',function(){
				booster = options.mouseDownSpeedBooster;
			});
			$("*").bind('mouseup',function(){
				booster = 1;
			});
			var doScrollRight = function() {	
				if(scrollXpos > 0) {
					$mom.find(options.scrollWrapper).scrollLeft($mom.find(options.scrollWrapper).scrollLeft() + (scrollXpos*booster));
				}
			};
			if(options.pauseAutoScroll == "mousedown" && options.autoScroll == "always") {
				$mom.find(options.scrollWrapper).bind('mousedown',function(){
					clearInterval($mom.autoScrollInterval);
				});
				$mom.find(options.scrollWrapper).bind('mouseup',function(){
					$mom.autoScrollInterval = setInterval(autoScroll, 6);
				});
			} else if(options.pauseAutoScroll == "mouseover" && options.autoScroll == "always") {
				$mom.find(options.scrollWrapper).bind('mouseover',function(){
					clearInterval($mom.autoScrollInterval);
				});
				$mom.find(options.scrollWrapper).bind('mouseout',function(){
					$mom.autoScrollInterval = setInterval(autoScroll, 6);
				});
			}
			$mom.previousScrollLeft = 0;
			$mom.pingPongDirection = "right";
			$mom.swapAt;
			$mom.getNextElementWidth = true;
			var autoScroll = function() {	
				switch(options.autoScrollDirection) {
					case "right":
						$mom.find(options.scrollWrapper).scrollLeft($mom.find(options.scrollWrapper).scrollLeft() + options.autoScrollSpeed);
						break;
					case "left":
						$mom.find(options.scrollWrapper).scrollLeft($mom.find(options.scrollWrapper).scrollLeft() - options.autoScrollSpeed);
						break;
					case "backandforth":
						$mom.previousScrollLeft = $mom.find(options.scrollWrapper).scrollLeft();
						if($mom.pingPongDirection == "right") {
							$mom.find(options.scrollWrapper).scrollLeft($mom.find(options.scrollWrapper).scrollLeft() + options.autoScrollSpeed);
						} else {
							$mom.find(options.scrollWrapper).scrollLeft($mom.find(options.scrollWrapper).scrollLeft() - options.autoScrollSpeed);
						}
						if($mom.previousScrollLeft === $mom.find(options.scrollWrapper).scrollLeft()) {
							if($mom.pingPongDirection == "right") {
								$mom.pingPongDirection = "left";
							}
							else {
								$mom.pingPongDirection = "right";
							}
						}
						break;
					case "endlessloop":
						if($mom.getNextElementWidth) {
							if(options.startAtElementId !== "") {
								$mom.swapAt = $("#" + options.startAtElementId).outerWidth();
							}
							else {
								$mom.swapAt = $mom.find(options.scrollableArea).children(":first-child").outerWidth();
							}
							$mom.getNextElementWidth = false;
						}
						$mom.find(options.scrollWrapper).scrollLeft($mom.find(options.scrollWrapper).scrollLeft() + options.autoScrollSpeed);
						if(($mom.swapAt <= $mom.find(options.scrollWrapper).scrollLeft())) { 
							$mom.find(options.scrollableArea).append($mom.find(options.scrollableArea).children(":first-child").clone());
							$mom.find(options.scrollWrapper).scrollLeft(($mom.find(options.scrollWrapper).scrollLeft() - $mom.find(options.scrollableArea).children(":first-child").outerWidth()));
							$mom.find(options.scrollableArea).children(":first-child").remove();
							$mom.getNextElementWidth = true;
						}
						break;
					default:
						break;
				}
			};
			$(document).find(options.scrollingHotSpotLeft).bind('mousemove',function(e){
				scrollXpos = 5;
			});
			$(document).find(options.scrollingHotSpotLeft).bind('mouseover',function(){
				if(options.autoScroll == "onstart") {
					clearInterval($mom.autoScrollInterval);
				}
				$mom.leftScrollInterval = setInterval(doScrollLeft, 6);
			});	
			$(document).find(options.scrollingHotSpotLeft).bind('mouseout',function(){
				clearInterval($mom.leftScrollInterval);
				scrollXpos = 0;
			});
			$(document).find(options.scrollingHotSpotLeft).bind('mousedown',function(){
				booster = options.mouseDownSpeedBooster;
			});
			var doScrollLeft = function() {	
				if(scrollXpos > 0) {
					$mom.find(options.scrollWrapper).scrollLeft($mom.find(options.scrollWrapper).scrollLeft() - (scrollXpos*booster));
				}
			};
	});
};
})(jQuery);

