﻿/**
* jQuery.smoothDivScroll - Smooth div scrolling using jQuery.
* This plugin is for turning a set of HTML elements's into a smooth scrolling area.
*
* Copyright (c) 2009 Thomas Kahn - thomas.kahn(at)karnhuset(dot)net
*
* This plugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This plugin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details. <http://www.gnu.org/licenses/>.
*
* Date: 2009-05-23
* @author Thomas Kahn
* @version 0.8
*
* Changelog
* ---------------------------------------------
* 0.9.1 - Added functionality for horizontal scroll (edwin.vlieg@sqills.com)
*
* 0.9	- Bugfixes: Problem with multiple autoscrollers on the same page - the intervals
*		  where global which resulted in the wrong autoscroller stopping on mouseOver or
*		  mouseDown.
*		  Error in calculation in autoscrolling mode that made the autoscrolling grind
*		  to a halt after a number of loops.
*
* 0.8   - Major update. New parameter setup. Lots of new autoscrolling capabilities and 
*		  new parameters for controlling the scrolling speed. Made it possible to start 
*		  the scroller at a specific element.
* 
* 0.7   - Added support for autoscrolling after the page has loaded. 
*         Added support for making the hot spots visible at start for X number of seconds
*         or visible all the time.
*
* 0.6   - First version.
*/

(function(a){jQuery.fn.smoothDivScroll=function(b){var c={horizontal:true,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:""};b=a.extend(c,b);return this.each(function(){var e=a(this);if(b.ajaxContentURL.length!==0){e.scrollableAreaWidth=0;e.find(b.scrollableArea).load((b.ajaxContentURL),function(){e.find(b.scrollableArea).children((b.countOnlyClass)).each(function(){e.scrollableAreaWidth=e.scrollableAreaWidth+a(this).outerWidth(true)});if(b.horizontal){e.find(b.scrollableArea).css("width",e.scrollableAreaWidth+"px")}else{e.find(b.scrollableArea).css("height",e.scrollableAreaHeight+"px")}if(b.hiddenOnStart){e.hide()}q();n()})}var k;var j;if(b.horizontal){var t=e.offset().left}else{var t=e.offset().top}var u=0;j=1;var o=false;a(window).one("load",function(){if(b.ajaxContentURL.length===0){e.scrollableAreaWidth=0;e.tempStartingPosition=0;e.find(b.scrollableArea).children((b.countOnlyClass)).each(function(){if((b.startAtElementId.length!==0)&&((a(this).attr("id"))==b.startAtElementId)){e.tempStartingPosition=e.scrollableAreaWidth}if(b.horizontal){e.scrollableAreaWidth=e.scrollableAreaWidth+a(this).outerWidth(true)}else{e.scrollableAreaWidth=e.scrollableAreaWidth+a(this).outerHeight(true)}});if(b.horizontal){e.find(b.scrollableArea).css("width",e.scrollableAreaWidth+"px")}else{e.find(b.scrollableArea).css("height",e.scrollableAreaWidth+"px")}if(b.hiddenOnStart){e.hide()}}if(b.horizontal){e.find(b.scrollWrapper).scrollLeft(e.tempStartingPosition)}else{e.find(b.scrollWrapper).scrollTop(e.tempStartingPosition)}if(b.autoScroll!==""){e.autoScrollInterval=setInterval(h,6)}if(b.autoScroll=="always"){p();l()}switch(b.visibleHotSpots){case"always":i();break;case"onstart":i();e.hideHotSpotBackgroundsInterval=setInterval(d,(b.hotSpotsVisibleTime*1000));break;default:break}});e.find(b.scrollingHotSpotRight,b.scrollingHotSpotLeft).one("mouseover",function(){if(b.autoScroll=="onstart"){clearInterval(e.autoScrollInterval)}});a(window).bind("resize",function(){q()});function q(){if(!(b.hiddenOnStart)){e.scrollableAreaWidth=0;e.find(b.scrollableArea).children((b.countOnlyClass)).each(function(){if(b.horizontal){e.scrollableAreaWidth=e.scrollableAreaWidth+a(this).outerWidth(true)}else{e.scrollableAreaWidth=e.scrollableAreaWidth+a(this).outerHeight(true)}});if(b.horizontal){e.find(b.scrollableArea).css("width",e.scrollableAreaWidth+"px")}else{e.find(b.scrollableArea).css("height",e.scrollableAreaHeight+"px")}}e.find(b.scrollWrapper).scrollLeft("0");var v=a("body").innerWidth();if(b.autoScroll!=="always"){if(e.scrollableAreaWidth<v){p();l()}else{m()}}}function p(){e.find(b.scrollingHotSpotLeft).hide()}function l(){e.find(b.scrollingHotSpotRight).hide()}function s(){e.find(b.scrollingHotSpotLeft).show();if(u<=0){if(b.horizontal){u=e.find(b.scrollingHotSpotLeft).width()}else{u=e.find(b.scrollingHotSpotLeft).height()}}}function r(){e.find(b.scrollingHotSpotRight).show();if(u<=0){if(b.horizontal){u=e.find(b.scrollingHotSpotLeft).width()}else{u=e.find(b.scrollingHotSpotLeft).height()}}}function n(){jQuery.each(jQuery.browser,function(v,w){if(v=="msie"&&jQuery.browser.version.substr(0,1)=="6"){e.find(b.scrollingHotSpotLeft).css("height",(e.find(b.scrollableArea).innerHeight()));e.find(b.scrollingHotSpotRight).css("height",(e.find(b.scrollableArea).innerHeight()))}})}e.find(b.scrollingHotSpotRight).bind("mousemove",function(w){if(b.horizontal){var v=w.pageX-(this.offsetLeft+t);k=Math.round((v/u)*b.scrollingSpeed)}else{var v=w.pageY-(this.offsetTop+t);if(v<0){v=15}k=Math.round((v/u)*b.scrollingSpeed)}if(k===Infinity){k=0}});e.find(b.scrollingHotSpotRight).bind("mouseover",function(){if(b.autoScroll=="onstart"){clearInterval(e.autoScrollInterval)}e.rightScrollInterval=setInterval(g,6)});e.find(b.scrollingHotSpotRight).bind("mouseout",function(){clearInterval(e.rightScrollInterval);k=0});e.find(b.scrollingHotSpotRight).bind("mousedown",function(){j=b.mouseDownSpeedBooster});a("*").bind("mouseup",function(){j=1});var g=function(){if(k>0){if(b.horizontal){e.find(b.scrollWrapper).scrollLeft(e.find(b.scrollWrapper).scrollLeft()+(k*j))}else{e.find(b.scrollWrapper).scrollTop(e.find(b.scrollWrapper).scrollTop()+(k*j))}}m()};if(b.pauseAutoScroll=="mousedown"&&b.autoScroll=="always"){e.find(b.scrollWrapper).bind("mousedown",function(){clearInterval(e.autoScrollInterval)});e.find(b.scrollWrapper).bind("mouseup",function(){e.autoScrollInterval=setInterval(h,6)})}else{if(b.pauseAutoScroll=="mouseover"&&b.autoScroll=="always"){e.find(b.scrollWrapper).bind("mouseover",function(){clearInterval(e.autoScrollInterval)});e.find(b.scrollWrapper).bind("mouseout",function(){e.autoScrollInterval=setInterval(h,6)})}}e.previousScrollLeft=0;e.pingPongDirection="right";e.swapAt;e.getNextElementWidth=true;var h=function(){if(b.autoScroll=="onstart"){m()}switch(b.autoScrollDirection){case"right":if(b.horizontal){e.find(b.scrollWrapper).scrollLeft(e.find(b.scrollWrapper).scrollLeft()+b.autoScrollSpeed)}else{e.find(b.scrollWrapper).scrollTop(e.find(b.scrollWrapper).scrollTop()+b.autoScrollSpeed)}break;case"left":if(b.horizontal){e.find(b.scrollWrapper).scrollLeft(e.find(b.scrollWrapper).scrollLeft()-b.autoScrollSpeed)}else{e.find(b.scrollWrapper).scrollTop(e.find(b.scrollWrapper).scrollTop()-b.autoScrollSpeed)}break;case"backandforth":e.previousScrollLeft=b.horizontal?e.find(b.scrollWrapper).scrollLeft():e.find(b.scrollWrapper).scrollTop();if(e.pingPongDirection=="right"){if(b.horizontal){e.find(b.scrollWrapper).scrollLeft(e.find(b.scrollWrapper).scrollLeft()+b.autoScrollSpeed)}else{e.find(b.scrollWrapper).scrollTop(e.find(b.scrollWrapper).scrollTop()+b.autoScrollSpeed)}}else{if(b.horizontal){e.find(b.scrollWrapper).scrollLeft(e.find(b.scrollWrapper).scrollLeft()-b.autoScrollSpeed)}else{e.find(b.scrollWrapper).scrollTop(e.find(b.scrollWrapper).scrollTop()-b.autoScrollSpeed)}}scrollLeft=b.horizontal?e.find(b.scrollWrapper).scrollLeft():e.find(b.scrollWrapper).scrollTop();if(e.previousScrollLeft===scrollLeft){if(e.pingPongDirection=="right"){e.pingPongDirection="left"}else{e.pingPongDirection="right"}}break;case"endlessloop":if(e.getNextElementWidth){if(b.startAtElementId!==""){e.swapAt=b.horizontal?a("#"+b.startAtElementId).outerWidth():a("#"+b.startAtElementId).outerHeight()}else{e.swapAt=b.horizontal?e.find(b.scrollableArea).children(":first-child").outerWidth():e.find(b.scrollableArea).children(":first-child").outerHeight()}e.getNextElementWidth=false}e.find(b.scrollWrapper).scrollLeft(e.find(b.scrollWrapper).scrollLeft()+b.autoScrollSpeed);if((e.swapAt<=e.find(b.scrollWrapper).scrollLeft())){e.find(b.scrollableArea).append(e.find(b.scrollableArea).children(":first-child").clone());e.find(b.scrollWrapper).scrollLeft((e.find(b.scrollWrapper).scrollLeft()-e.find(b.scrollableArea).children(":first-child").outerWidth()));e.find(b.scrollableArea).children(":first-child").remove();e.getNextElementWidth=true}break;default:break}};e.find(b.scrollingHotSpotLeft).bind("mousemove",function(w){if(b.horizontal){var v=e.find(b.scrollingHotSpotLeft).innerWidth()-(w.pageX-t)}else{var v=e.find(b.scrollingHotSpotLeft).innerHeight()-(w.pageY-t)}k=Math.round((v/u)*b.scrollingSpeed);if(k===Infinity){k=0}});e.find(b.scrollingHotSpotLeft).bind("mouseover",function(){if(b.autoScroll=="onstart"){clearInterval(e.autoScrollInterval)}e.leftScrollInterval=setInterval(f,6)});e.find(b.scrollingHotSpotLeft).bind("mouseout",function(){clearInterval(e.leftScrollInterval);k=0});e.find(b.scrollingHotSpotLeft).bind("mousedown",function(){j=b.mouseDownSpeedBooster});var f=function(){if(k>0){if(b.horizontal){e.find(b.scrollWrapper).scrollLeft(e.find(b.scrollWrapper).scrollLeft()-(k*j))}else{e.find(b.scrollWrapper).scrollTop(e.find(b.scrollWrapper).scrollTop()-(k*j))}}m()};function m(){scrollLeft=b.horizontal?e.find(b.scrollWrapper).scrollLeft():e.find(b.scrollWrapper).scrollTop();scrollableWidth=b.horizontal?(e.scrollableAreaWidth)<=(e.find(b.scrollWrapper).innerWidth()+scrollLeft):(e.scrollableAreaHeight)<=(e.find(b.scrollWrapper).innerHeight()+scrollLeft);if(scrollLeft===0){p();r()}else{if(scrollableWidth){l();s()}else{r();s()}}}function i(){e.find(b.scrollingHotSpotLeft).addClass("scrollingHotSpotLeftVisible");e.find(b.scrollingHotSpotRight).addClass("scrollingHotSpotRightVisible")}function d(){clearInterval(e.hideHotSpotBackgroundsInterval);e.find(b.scrollingHotSpotLeft).fadeTo("slow",0,function(){e.find(b.scrollingHotSpotLeft).removeClass("scrollingHotSpotLeftVisible")});e.find(b.scrollingHotSpotRight).fadeTo("slow",0,function(){e.find(b.scrollingHotSpotRight).removeClass("scrollingHotSpotRightVisible")})}})}})(jQuery);
