/*
 * jQuery SmoothDivScrollVert 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.smoothDivScrollVert", {
		// Default options
		options: {
			scrollingHotSpotTop: "div.scrollingHotSpotTop",
			scrollingHotSpotBottom: "div.scrollingHotSpotBottom",
			scrollableAreaVert: "div.scrollableAreaVert",
			scrollWrapperVert: "div.scrollWrapperVert",
			hiddenOnStart: false,
			ajaxContentURL: "",
			countOnlyClass: "",
			scrollStep: 15,
			scrollInterval: 15,
			mouseDownSpeedBooster: 3,
			autoScroll: "",
			autoScrollDirection: "",
			autoScrollStep: 5,
			autoScrollInterval: 10,
			visibleHotSpots: "",
			hotSpotsVisibleTime: 5,
			startAtElementId: ""
		},
		_create: function() {
			
			// Set variables
		
			var self = this, o = this.options, el = this.element;
	
			el.data("scrollWrapperVert", el.find(o.scrollWrapperVert));
			el.data("scrollingHotSpotBottom", el.find(o.scrollingHotSpotBottom));
			el.data("scrollingHotSpotTop", el.find(o.scrollingHotSpotTop));
			el.data("scrollableAreaVert", el.find(o.scrollableAreaVert));
			el.data("speedBooster", 1);
			el.data("motherElementOffset", el.offset().top);
			el.data("scrollYPos", 0);
			el.data("hotSpotHeight", el.find(o.scrollingHotSpotTop).height());
			el.data("scrollableAreaVertHeight", 0);
			el.data("startingPosition", 0);
			el.data("downScrollInterval", null);
			el.data("upScrollInterval", null);
			el.data("autoScrollInterval", null);
			el.data("hideHotSpotBackgroundsInterval", null);
			el.data("previousscrollTop", 0);
			el.data("pingPongDirection", "right");
			el.data("getNextElementWidth", true);
			el.data("swapAt", null);
			el.data("startAtElementHasNotPassed", true);
			el.data("swappedElement", null);
			el.data("originalElements", el.data("scrollableAreaVert").children(o.countOnlyClass));
			el.data("visible", true);
			el.data("initialAjaxContentLoaded", false);
			el.data("enabled", true);

			// If the user wants to have visible hotspots, here is where it's taken care of
	/*
			if (o.autoScroll !== "always") {
				switch (o.visibleHotSpots) {
					case "always":
						self.showHotSpotBackgrounds();
						break;
					case "onstart":
						self.showHotSpotBackgrounds();
						el.data("hideHotSpotBackgroundsInterval", setTimeout(function() {
							self.hideHotSpotBackgrounds("slow");
						}, (o.hotSpotsVisibleTime * 1000)));
						break;
					default:
						break;
				}
			}
			*/
			/*****************************************
			SET UP EVENTS FOR SCROLLING DOWN
			*****************************************/
			// Check the mouse Y position and calculate the relative Y position inside the bottom hotspot
			el.data("scrollingHotSpotBottom").bind("mousemove", function(e) {
				el.data("motherElementOffset", el.offset().top);
				//alert(el.data("motherElementOffset"));
				//alert(e.pageY+ "   "+this.offsetTop+ " " +el.data("motherElementOffset"));
				var y = e.pageY - (this.offsetTop + el.data("motherElementOffset"));
				
				
				el.data("scrollYPos", Math.round((y / el.data("hotSpotHeight")) * o.scrollStep));
				if (el.data("scrollYPos") === Infinity) {
					el.data("scrollYPos", 0);
				}
			});



			// mouseover bottom hotspot - scrolling
			el.data("scrollingHotSpotBottom").bind("mouseover", function() {

				// Clear autoscrolling, if it should only run on start
				if ((o.autoScroll === "onstart" && el.data("autoScrollInterval") !== null)) {
					clearInterval(el.data("autoScrollInterval"));
					el.data("autoScrollInterval", null);
					self._trigger("autoScrollIntervalStopped");
				}

				// Start the scrolling interval
				el.data("downScrollInterval", setInterval(function() {

					if (el.data("scrollYPos") > 0 && el.data("enabled")) {
						el.data("scrollWrapperVert").scrollTop(el.data("scrollWrapperVert").scrollTop() + (el.data("scrollYPos") * el.data("speedBooster")));
						self._showHideHotSpots();
					}

				}, o.scrollInterval));

				// Callback
				self._trigger("mouseOverBottomHotSpot");

			});

			// mouseout right hotspot
			el.data("scrollingHotSpotBottom").bind("mouseout", function() {
				clearInterval(el.data("downScrollInterval"));
				el.data("scrollYPos", 0);
			});

			// mousedown right hotspot (add scrolling speed booster)
			el.data("scrollingHotSpotBottom").bind("mousedown", function() {
				el.data("speedBooster", o.mouseDownSpeedBooster);
			});

			// mouseup anywhere (stop boosting the scrolling speed)
			$("body").bind("mouseup", function() {
				el.data("speedBooster", 1);
			});

			/*****************************************
			SET UP EVENTS FOR SCROLLING UP
			*****************************************/
			// Check the mouse Y position and calculate the relative Y position inside the left hotspot
			el.data("scrollingHotSpotTop").bind("mousemove", function(e) {
				el.data("motherElementOffset", el.offset().top);
				//alert(el.data("motherElementOffset"));
				var y = el.data("scrollingHotSpotTop").innerHeight() - (e.pageY - el.data("motherElementOffset"));
				
				el.data("scrollYPos", Math.round((y / el.data("hotSpotHeight")) * o.scrollStep));
				if (el.data("scrollYPos") === Infinity) {
					el.data("scrollYPos", 0);
				}

			});

			// mouseover up hotspot
			el.data("scrollingHotSpotTop").bind("mouseover", function() {

				// Clear autoscrolling, if it should only run on start

				if ((o.autoScroll === "onstart" && el.data("autoScrollInterval") !== null)) {
					clearInterval(el.data("autoScrollInterval"));
					el.data("autoScrollInterval", null);
					self._trigger("autoScrollIntervalStopped");
				}

				el.data("upScrollInterval", setInterval(function() {
					if (el.data("scrollYPos") > 0 && el.data("enabled")) {
						el.data("scrollWrapperVert").scrollTop(el.data("scrollWrapperVert").scrollTop() - (el.data("scrollYPos") * el.data("speedBooster")));

						self._showHideHotSpots();
					}

				}, o.scrollInterval));

				// Callback
				self._trigger("mouseOverUpHotSpot");
			});

			// mouseout up hotspot
			el.data("scrollingHotSpotTop").bind("mouseout", function() {
				clearInterval(el.data("upScrollInterval"));
				el.data("scrollYPos", 0);
			});

			// mousedown uphotspot (add scrolling speed booster)
			el.data("scrollingHotSpotTop").bind("mousedown", function() {
				el.data("speedBooster", o.mouseDownSpeedBooster);
			});

			/*****************************************
			SET UP EVENT FOR RESIZING THE BROWSER WINDOW
			*****************************************/
			/*
			$(window).bind("resize", function() {
				// If the scrollable area is not hidden on start, show/hide the hotspots
				if (!(o.hiddenOnStart)) {
					self._showHideHotSpots();
				}

				self._trigger("windowResized");
			});
*/

			/*****************************************
			FETCHING AJAX CONTENT ON INITIALIZATION
			*****************************************/
			// If there's an ajaxContentURL in the options, 
			// fetch the content
			if (o.ajaxContentURL.length > 0) {
				self.replaceContent(o.ajaxContentURL);
			}
			else {
				self.recalculatescrollableAreaVert();
			}
			
			if (o.autoScroll !== "always") {
				switch (o.visibleHotSpots) {
					case "always":
						self.showHotSpotBackgrounds();
						break;
					case "onstart":
						self.showHotSpotBackgrounds();
						el.data("hideHotSpotBackgroundsInterval", setTimeout(function() {
							self.hideHotSpotBackgrounds("slow");
						}, (o.hotSpotsVisibleTime * 1000)));
						break;
					default:
						break;
				}
			}



			// Should it be hidden on start?
			if (o.hiddenOnStart) {
				self.hide();
			}

			/*****************************************
			AUTOSCROLLING
			*****************************************/
			// If the user has set the option autoScroll, the scollable area will
			// start scrolling automatically. If the content is fetched using AJAX
			// the autoscroll is not started here but in recalculatescrollableAreaVert.
			// Otherwise recalculatescrollableAreaVert won't have the time to calculate
			// the width of the scrollable area before the autoscrolling starts.
			if ((o.autoScroll.length > 0) && !(o.hiddenOnStart) && (o.ajaxContentURL.length <= 0)) {
				self.startAutoScroll();
			}

		},
		/**********************************************************
		Hotspot functions
		**********************************************************/
		showHotSpotBackgrounds: function(fadeSpeed) {
			// Alter the CSS (SmoothDivScroll.css) if you want to customize
			// the look'n'feel of the visible hotspots
			var self = this, el = this.element;

			// Fade in the hotspot backgrounds
			if (fadeSpeed !== undefined) {
				// Before the fade-in starts, we need to make sure the opacity
				// is zero
				el.data("scrollingHotSpotTop").css("opacity", "0.0");
				el.data("scrollingHotSpotBottom").css("opacity", "0.0");

				el.data("scrollingHotSpotTop").addClass("scrollingHotSpotTopVisible");
				el.data("scrollingHotSpotBottom").addClass("scrollingHotSpotBottomVisible");

				// Fade in the left hotspot
				el.data("scrollingHotSpotTop").fadeTo(fadeSpeed, 0.35);

				// Fade in the right hotspot
				el.data("scrollingHotSpotBottom").fadeTo(fadeSpeed, 0.35);
			}
			// Don't fade, just show them
			else {
				// The left hotspot
				el.data("scrollingHotSpotTop").addClass("scrollingHotSpotTopVisible");
				el.data("scrollingHotSpotTop").removeAttr("style");

				// The right hotspot
				el.data("scrollingHotSpotBottom").addClass("scrollingHotSpotBottomVisible");
				el.data("scrollingHotSpotBottom").removeAttr("style");
			}
			self._showHideHotSpots();
		},
		hideHotSpotBackgrounds: function(fadeSpeed) {
			var el = this.element;

			// Fade out the hotspot backgrounds
			if (fadeSpeed !== undefined) {
				// Fade out the left hotspot
				el.data("scrollingHotSpotTop").fadeTo(fadeSpeed, 0.0, function() {
					el.data("scrollingHotSpotTop").removeClass("scrollingHotSpotTopVisible");
				});

				// Fade out the right hotspot
				el.data("scrollingHotSpotBottom").fadeTo(fadeSpeed, 0.0, function() {
					el.data("scrollingHotSpotBottom").removeClass("scrollingHotSpotBottomVisible");
				});
			}
			// Don't fade, just hide them
			else {
				el.data("scrollingHotSpotTop").removeClass("scrollingHotSpotTopVisible");
				el.data("scrollingHotSpotTop").removeAttr("style");

				el.data("scrollingHotSpotBottom").removeClass("scrollingHotSpotBottomVisible");
				el.data("scrollingHotSpotBottom").removeAttr("style");
			}

		},
		// Function for showing and hiding hotspots depending on the
		// offset of the scrolling
		_showHideHotSpots: function() {
			var self = this, el = this.element, o = this.options;
			
			// If autoscrolling is set to always, there should be no hotspots
			if (o.autoScroll !== "always") {
				
				// If the scrollable area is shorter than the scroll wrapper, both hotspots
				// should be hidden
				//alert(el.data("scrollableAreaVertHeight"));
				//alert(el.data("scrollWrapperVert").innerHeight());
				if (el.data("scrollableAreaVertHeight") <= (el.data("scrollWrapperVert").innerHeight())) {
					el.data("scrollingHotSpotTop").hide();
					el.data("scrollingHotSpotBottom").hide();
				}
				// When you can't scroll further left the left scroll hotspot should be hidden
				// and the right hotspot visible.
				else if (el.data("scrollWrapperVert").scrollTop() === 0) {
					el.data("scrollingHotSpotTop").hide();
					el.data("scrollingHotSpotBottom").show();
					// Callback
					self._trigger("scrollTopLimitReached");
					// Clear interval
					clearInterval(el.data("upScrollInterval"));
					el.data("upScrollInterval", null);
				}
				// When you can't scroll further right
				// the right scroll hotspot should be hidden
				// and the left hotspot visible
				else if (el.data("scrollableAreaVertHeight") <= (el.data("scrollWrapperVert").innerHeight() + el.data("scrollWrapperVert").scrollTop())) {
					el.data("scrollingHotSpotTop").show();
					el.data("scrollingHotSpotBottom").hide();
					// Callback
					self._trigger("scrollRightLimitReached");
					// Clear interval
					clearInterval(el.data("downScrollInterval"));
					el.data("downScrollInterval", null);
				}
				// If you are somewhere in the middle of your
				// scrolling, both hotspots should be visible
				else {
					el.data("scrollingHotSpotTop").show();
					el.data("scrollingHotSpotBottom").show();
				}
			}
			else {
				el.data("scrollingHotSpotTop").hide();
				el.data("scrollingHotSpotBottom").hide();
			}
		},
		/**********************************************************
		Moving to a certain element
		**********************************************************/
		moveToElement: function(moveTo, elementNumber) {
			var self = this, el = this.element, o = this.options, tempscrollableAreaVertHeight = 0, foundStartAtElement = false;

			switch (moveTo) {
				case "first":
					el.data("scrollYPos", 0);
					self._trigger("movedToFirstElement");
					break;
				case "start":
					// Check to see where the start-at element is at the moment.
					// This can vary if endlessloop is used for autoscroll since it
					// swaps elements around.

					el.data("scrollableAreaVert").children(o.countOnlyClass).each(function() {

						if ((o.startAtElementId.length > 0) && (($(this).attr("id")) === o.startAtElementId)) {
							el.data("startingPosition", tempscrollableAreaVertHeight);
							foundStartAtElement = true;
						}
						tempscrollableAreaVertHeight = tempscrollableAreaVertHeight + $(this).outerHeight(true);
					});

					el.data("scrollYPos", el.data("startingPosition"));
					self._trigger("movedToStartElement");
					break;
				case "last":
					el.data("scrollYPos", el.data("scrollableAreaVertHeight"));
					self._trigger("movedToLastElement");
					break;
				case "number":
					if (!(isNaN(elementNumber))) {
						// Get the total Height of all preceding elements					
						el.data("scrollableAreaVert").children(o.countOnlyClass).each(function(index) {
							//alert(tempscrollableAreaVertHeight);
							if (index === (elementNumber - 1)) {
								el.data("scrollYPos", tempscrollableAreaVertHeight);
							}
							tempscrollableAreaVertHeight = tempscrollableAreaVertHeight + $(this).outerHeight(true);
						});
						//alert(tempscrollableAreaVertHeight);
					}
					self._trigger("movedToElementNumber", null, { "elementNumber": elementNumber });
					break;
				default:
					break;
			}

			el.data("scrollWrapperVert").scrollTop(el.data("scrollYPos"));
			self._showHideHotSpots();
		},
		/**********************************************************
		Adding or replacing content
		**********************************************************/
		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("scrollableAreaVert").children(":first").before(data);
				}
				else {
					el.data("scrollableAreaVert").children(":last").after(data);
				}

				// Recalculate the total width of the elements inside the scrollable area
				self.recalculatescrollableAreaVert();

				// Determine which hotspots to show
				self._showHideHotSpots();
			});
		},
		replaceContent: function(ajaxContentURL) {
			var self = this, el = this.element;

			el.data("scrollableAreaVert").load(ajaxContentURL, function() {
				// Recalculate the total width of the elements inside the scrollable area
				self.recalculatescrollableAreaVert();
				self.moveToElement("first");
				self._showHideHotSpots();
				el.data("startingPosition", 0);
			});
		},
		/**********************************************************
		Recalculate the scrollable area
		**********************************************************/
		recalculatescrollableAreaVert: function() {

			var tempscrollableAreaVertHeight = 0, foundStartAtElement = false, o = this.options, el = this.element, self = this;

			// Add up the total width of all the items inside the scrollable area
			// and check to see if there's a specific element to start at
			el.data("scrollableAreaVert").children(o.countOnlyClass).each(function() {
				var this_height = $(this).outerHeight(true);
				// Check to see if the current element in the loop is the one where the scrolling should start
				/*
				if ((o.startAtElementId.length > 0) && (($(this).attr("id")) === o.startAtElementId)) {
					el.data("startingPosition", tempscrollableAreaVertHeight);
					foundStartAtElement = true;
				}
				*/
				//tempscrollableAreaVertHeight = tempscrollableAreaVertHeight + $(this).outerHeight(true);
				tempscrollableAreaVertHeight += this_height;
			});
			tempscrollableAreaVertHeight	= el.data("scrollableAreaVert").children(o.countOnlyClass).length*59;
			
		
			//alert(tempscrollableAreaVertHeight);
			// If the element with the ID specified by startAtElementId
			// is not found, reset it
			if (!(foundStartAtElement)) {
				el.data("startAtElementId", "");
			}

			// Set the width of the scrollable area
			el.data("scrollableAreaVertHeight", tempscrollableAreaVertHeight);
			//alert("Recalc"+el.data("scrollableAreaVertWidth"));
			el.data("scrollableAreaVert").height(el.data("scrollableAreaVertHeight"));

			// Move to the starting position
			el.data("scrollWrapperVert").scrollTop(el.data("startingPosition"));
			el.data("scrollYPos", el.data("startingPosition"));

			// If the content of the scrollable area is fetched using AJAX
			// during initialization, it needs to be done here. After it has
			// been loaded a flag variable is set to indicate that the content
			// has been loaded already and shouldn
			if (!(el.data("initialAjaxContentLoaded"))) {
				if ((o.autoScroll.length > 0) && !(o.hiddenOnStart) && (o.ajaxContentURL.length > 0)) {
					self.startAutoScroll();
					el.data("initialAjaxContentLoaded", true);
				}
			}

		},
		/**********************************************************
		Stopping, starting and doing the autoscrolling
		**********************************************************/
		stopAutoScroll: function() {
			var self = this, el = this.element;

			clearInterval(el.data("autoScrollInterval"));
			el.data("autoScrollInterval", null);

			// Check to see which hotspots should be active
			// in the position where the scroller has stopped
			self._showHideHotSpots();

			self._trigger("autoScrollStopped");

		},
		startAutoScroll: function() {
			var self = this, el = this.element, o = this.options;

			self._showHideHotSpots();

			// Stop any running interval
			clearInterval(el.data("autoScrollInterval"));
			el.data("autoScrollInterval", null);

			// Callback
			self._trigger("autoScrollStarted");

			// Start interval
			el.data("autoScrollInterval", setInterval(function() {

				// If the scroller is not visible or
				// if the scrollable area is shorter than the scroll wrapper
				// any running autoscroll interval should stop.
				if (!(el.data("visible")) || (el.data("scrollableAreaVertHeight") <= (el.data("scrollWrapperVert").innerHeight()))) {
					// Stop any running interval
					clearInterval(el.data("autoScrollInterval"));
					el.data("autoScrollInterval", null);
				}
				else {
					// Store the old scrollTop value to see if the scrolling has reached the end
					el.data("previousscrollTop", el.data("scrollWrapperVert").scrollTop());


					switch (o.autoScrollDirection) {
						case "right":
							el.data("scrollWrapperVert").scrollTop(el.data("scrollWrapperVert").scrollTop() + o.autoScrollStep);
							if (el.data("previousscrollTop") === el.data("scrollWrapperVert").scrollTop()) {
								self._trigger("autoScrollRightLimitReached");
								clearInterval(el.data("autoScrollInterval"));
								el.data("autoScrollInterval", null);
								self._trigger("autoScrollIntervalStopped");
							}
							break;

						case "left":
							el.data("scrollWrapperVert").scrollTop(el.data("scrollWrapperVert").scrollTop() - o.autoScrollStep);
							if (el.data("previousscrollTop") === el.data("scrollWrapperVert").scrollTop()) {
								self._trigger("autoscrollTopLimitReached");
								clearInterval(el.data("autoScrollInterval"));
								el.data("autoScrollInterval", null);
								self._trigger("autoScrollIntervalStopped");
							}
							break;

						case "backandforth":
							if (el.data("pingPongDirection") === "right") {
								el.data("scrollWrapperVert").scrollTop(el.data("scrollWrapperVert").scrollTop() + (o.autoScrollStep));
							}
							else {
								el.data("scrollWrapperVert").scrollTop(el.data("scrollWrapperVert").scrollTop() - (o.autoScrollStep));
							}

							// If the scrollTop hasnt't changed it means that the scrolling has reached
							// the end and the direction should be switched
							if (el.data("previousscrollTop") === el.data("scrollWrapperVert").scrollTop()) {
								if (el.data("pingPongDirection") === "right") {
									el.data("pingPongDirection", "left");
									self._trigger("autoScrollRightLimitReached");
								}
								else {
									el.data("pingPongDirection", "right");
									self._trigger("autoscrollTopLimitReached");
								}
							}
							break;

						case "endlessloopright":
							// Get the width of the first element. When it has scrolled out of view,
							// the element swapping should be executed. A true/false variable is used
							// as a flag variable so the swapAt value doesn't have to be recalculated
							// in each loop.

							if (el.data("getNextElementWidth")) {
								if ((o.startAtElementId.length > 0) && (el.data("startAtElementHasNotPassed"))) {
									el.data("swapAt", $("#" + o.startAtElementId).outerWidth(true));
									el.data("startAtElementHasNotPassed", false);
								}
								else {
									el.data("swapAt", el.data("scrollableAreaVert").children(":first").outerWidth(true));
								}

								el.data("getNextElementWidth", false);
							}

							// Do the autoscrolling
							el.data("scrollWrapperVert").scrollTop(el.data("scrollWrapperVert").scrollTop() + o.autoScrollStep);

							// Check to see if the swap should be done
							if (el.data("swapAt") <= el.data("scrollWrapperVert").scrollTop()) {
								el.data("swappedElement", el.data("scrollableAreaVert").children(":first").detach());
								el.data("scrollableAreaVert").append(el.data("swappedElement"));
								el.data("scrollWrapperVert").scrollTop(el.data("scrollWrapperVert").scrollTop() - el.data("swappedElement").outerWidth(true));
								el.data("getNextElementWidth", true);
							}
							break;
						case "endlessloopleft":
							// Get the width of the first element. When it has scrolled out of view,
							// the element swapping should be executed. A true/false variable is used
							// as a flag variable so the swapAt value doesn't have to be recalculated
							// in each loop.

							if (el.data("getNextElementWidth")) {
								if ((o.startAtElementId.length > 0) && (el.data("startAtElementHasNotPassed"))) {
									el.data("swapAt", $("#" + o.startAtElementId).outerWidth(true));
									el.data("startAtElementHasNotPassed", false);
								}
								else {
									el.data("swapAt", el.data("scrollableAreaVert").children(":first").outerWidth(true));
								}

								el.data("getNextElementWidth", false);
							}

							// Do the autoscrolling
							el.data("scrollWrapperVert").scrollTop(el.data("scrollWrapperVert").scrollTop() - o.autoScrollStep);

							// Check to see if the swap should be done
							if (el.data("scrollWrapperVert").scrollTop() === 0) {
								el.data("swappedElement", el.data("scrollableAreaVert").children(":last").detach());
								el.data("scrollableAreaVert").prepend(el.data("swappedElement"));
								el.data("scrollWrapperVert").scrollTop(el.data("scrollWrapperVert").scrollTop() + el.data("swappedElement").outerWidth(true));
								el.data("getNextElementWidth", true);
							}
							break;
						default:
							break;

					}
				}
			}, o.autoScrollInterval));

		},
		restoreOriginalElements: function() {
			var self = this, el = this.element;

			// Restore the original content of the scrollable area
			el.data("scrollableAreaVert").html(el.data("originalElements"));
			self.recalculatescrollableAreaVert();
			self.moveToElement("first");
		},
		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("autoScrollInterval"));
			clearInterval(el.data("downScrollInterval"));
			clearInterval(el.data("upScrollInterval"));
			clearInterval(el.data("hideHotSpotBackgroundsInterval"));

			// Set enabled to false
			el.data("enabled", false);
		},
		destroy: function() {
			var el = this.element;

			// Clear all running intervals
			clearInterval(el.data("autoScrollInterval"));
			clearInterval(el.data("downScrollInterval"));
			clearInterval(el.data("upScrollInterval"));
			clearInterval(el.data("hideHotSpotBackgroundsInterval"));

			// Remove all element specific events
			el.data("scrollingHotSpotBottom").unbind("mouseover");
			el.data("scrollingHotSpotBottom").unbind("mouseout");
			el.data("scrollingHotSpotBottom").unbind("mousedown");

			el.data("scrollingHotSpotTop").unbind("mouseover");
			el.data("scrollingHotSpotTop").unbind("mouseout");
			el.data("scrollingHotSpotTop").unbind("mousedown");

			// Restore the original content of the scrollable area
			el.data("scrollableAreaVert").html(el.data("originalElements"));

			// Remove the width of the scrollable area
			el.data("scrollableAreaVert").removeAttr("style");
			el.data("scrollingHotSpotBottom").removeAttr("style");
			el.data("scrollingHotSpotTop").removeAttr("style");

			el.data("scrollWrapperVert").scrollTop(0);
			el.data("scrollingHotSpotTop").removeClass("scrollingHotSpotTopVisible");
			el.data("scrollingHotSpotBottom").removeClass("scrollingHotSpotBottomVisible");
			el.data("scrollingHotSpotBottom").hide();
			el.data("scrollingHotSpotTop").hide();

			// Call the base destroy function
			$.Widget.prototype.destroy.apply(this, arguments);

		}
	});
})(jQuery);
