/* * jQuery SmoothDivScroll 1.1 * * Copyright (c) 2010 Thomas Kahn * Licensed under the GPL license. * * http://www.maaki.com/thomas/SmoothDivScroll/ * * Depends: * jquery.ui.widget.js * */(function($) {	$.widget("thomaskahn.smoothDivScroll", {		// Default options		options: {			scrollingHotSpotLeft: "div.scrollingHotSpotLeft", 			scrollingHotSpotRight: "div.scrollingHotSpotRight", 			scrollableArea : "div.scrollableArea",			scrollWrapper: "div.scrollWrapper", 			hiddenOnStart: false, 			ajaxContentURL: "",			scrollStep: 15,			scrollInterval: 10,			mouseDownSpeedBooster: 5, 			hotSpotsVisibleTime: 5		},		_create: function() {			// Set variables			var self = this, o = this.options, el = this.element;			el.data("scrollWrapper", el.find(o.scrollWrapper));			el.data("scrollingHotSpotRight", el.find(o.scrollingHotSpotRight));			el.data("scrollingHotSpotLeft", el.find(o.scrollingHotSpotLeft));			el.data("scrollableArea", el.find(o.scrollableArea));			el.data("speedBooster", 1);			el.data("motherElementOffset", el.offset().left);			el.data("scrollXPos", 0);			el.data("hotSpotWidth", el.find(o.scrollingHotSpotLeft).width());			el.data("scrollableAreaWidth", 0);			el.data("startingPosition", 0);			el.data("scrollIntervalId", null);			el.data("previousScrollLeft", 0);			el.data("getNextElementWidth", true);			el.data("visible",true);			el.data("initialAjaxContentLoaded", false);			el.data("enabled", true);			el.data("scrollWrapper").scrollLeft(el.data("startingPosition"));			el.data("scrollingHotSpotRight").bind("mousedown", function(){						var nextOffset = 5;						el.data("scrollIntervalId", setInterval(function(){			if(el.data("enabled")){                            el.data("scrollWrapper").scrollLeft(el.data("scrollWrapper").scrollLeft() + nextOffset);                            nextOffset += el.data("speedBooster");                        }	                    }, o.scrollInterval));			self._trigger("mouseOverRightHotSpot");						});		el.data("scrollingHotSpotRight").bind("mouseout", function(){                    clearInterval(el.data("scrollIntervalId"));		});			el.data("scrollingHotSpotRight").bind("mouseup", function(){                    clearInterval(el.data("scrollIntervalId"));                });			el.data("scrollingHotSpotLeft").bind("mousedown", function(){				var nextOffset = 5;				el.data("scrollIntervalId", setInterval(function(){					if(el.data("enabled")) {						el.data("scrollWrapper").scrollLeft(el.data("scrollWrapper").scrollLeft() - nextOffset);						nextOffset += el.data("speedBooster");					}							}, o.scrollInterval));				self._trigger("mouseOverLeftHotSpot");			});			el.data("scrollingHotSpotLeft").bind("mouseout", function(){				clearInterval(el.data("scrollIntervalId"));			});			el.data("scrollingHotSpotLeft").bind("mouseup", function(){				clearInterval(el.data("scrollIntervalId"));			});			//$("*").bind("mouseup", function(){		//		clearInterval(el.data("scrollIntervalId"));		//	});						$(window).bind("resize", function(){				self._trigger("windowResized");			});			if(o.ajaxContentURL.length > 0){				self.replaceContent(o.ajaxContentURL);			}else{				self.recalculateScrollableArea();			}			if(o.hiddenOnStart) { 				self.hide();			}		},		/**********************************************************		 Moving to a certain element - MODIFIED !!!		**********************************************************/		moveToElement: function(moveTo, elementNumber) {			var self = this, el = this.element, o = this.options, tempScrollableAreaWidth = 0, foundStartAtElement = false; 			var ew = 0, pew = 0; 			switch(moveTo){				case "first":					el.data("scrollXPos", 0);					self._trigger("movedToFirstElement");					break;				case "last":					el.data("scrollXPos", el.data("scrollableAreaWidth"));					self._trigger("movedToLastElement");					break;				case "number":					if(!(isNaN(elementNumber))) {						// Get the total width of all preceding elements											el.data("scrollableArea").children().each(function(index) {							if(index === (elementNumber-1)) {								pew = $(this).outerWidth(true);							} else if(index === (elementNumber))	{								el.data("scrollXPos", tempScrollableAreaWidth);								ew = $(this).outerWidth(true);							}							tempScrollableAreaWidth = tempScrollableAreaWidth + $(this).outerWidth(true);						});					}					self._trigger("movedToElementNumber", null, {"elementNumber": elementNumber});					break;				default:					break;			}			var ow = el.data("scrollWrapper").outerWidth(true);			var sl = el.data("scrollWrapper").scrollLeft();			var sx = el.data("scrollXPos");			if (sx<=sl) {				el.data("scrollWrapper").animate({scrollLeft : sx-ow+ew+pew}, 600);			}else if ((sx + ew) >= (sl+ow)) {				var sp = 				el.data("scrollWrapper").animate({scrollLeft : sx - ew> el.data("scrollableAreaWidth") ? 0 : sx - ew}, 600);			}		},		addContent: function(ajaxContentURL, addWhere){			var self = this, el = this.element;			$.get(ajaxContentURL, function(data){				// Add the loaded content first or last in the scrollable area				if(addWhere === "first") {					el.data("scrollableArea").children(":first").before(data);				}else {					el.data("scrollableArea").children(":last").after(data);				}				self.recalculateScrollableArea();							});		},		replaceContent: function(ajaxContentURL){			var self = this, el = this.element;			el.data("scrollableArea").load(ajaxContentURL, function(){				// Recalculate the total width of the elements inside the scrollable area				self.recalculateScrollableArea();				self.moveToElement("first");				el.data("startingPosition", 0);			});		},		/**********************************************************		 Recalculate the scrollable area		**********************************************************/		recalculateScrollableArea: function() {			var tempScrollableAreaWidth = 0, el = this.element, self = this;			// Add up the total width of all the items inside the scrollable area//			el.data("scrollableArea").children().each(function() {//				tempScrollableAreaWidth = tempScrollableAreaWidth + $(this).outerWidth(true);//			});                        var elmnts=el.data("scrollableArea").children();                        for(i=0;elmnts[i];i++){                            if(i==1){                               tempScrollableAreaWidth = tempScrollableAreaWidth + parseInt($(elmnts[i]).css("margin-left").replace("px",""));                            }                            tempScrollableAreaWidth = tempScrollableAreaWidth + $(elmnts[i]).outerWidth(true);                        }			// Set the width of the scrollable area			el.data("scrollableAreaWidth", tempScrollableAreaWidth);			el.data("scrollableArea").width(el.data("scrollableAreaWidth"));		},		show: function(){			var el = this.element;			el.data("visible", true);			el.show();		},		hide: function(){			var el = this.element;			el.data("visible", false);			el.hide();		},		enable: function(){			var el = this.element;						// Set enabled to true			el.data("enabled", true);		},		disable: function(){			var el = this.element;						// Clear all running intervals			clearInterval(el.data("scrollIntervalId"));						// Set enabled to false			el.data("enabled", false);		}	});})(jQuery);
