/*
* Scroll it! v0.01 - jQuery menu widget
* originally by Andres Pi - andres(at)dreamsiteweb.com
* heavily edited by mark creer to allow jump to selected item
*
*/

(function($) {
		  
jQuery.fn.scrollIt = function(settings){
	return this.each(function(i) {
			$this = $(this);
			settings = jQuery.extend({
						   //set the container for ul
						   menuHeight : 190,
						   menuWidth : 700,
						   //set each item of list
						   lineHeight : 25,
						   //duration of animation overridden in html action
						   scrollDuration : 10,
						   //class for container
						   scrollItwrapper : 'scrollItwrapper'
					   }, settings);
									 
			
			//set tempo of intervalo according to tempo of  animation
			var scrollInterval = settings.scrollDuration + 0.1;
		   
			/*add  class to  menu with other css styles to set desired height for menu*/
			$this.addClass('scroll-menu').css('height',settings.menuHeight);
			$this.addClass('scroll-menu').css('width',settings.menuWidth);
			
			//add a parent to wrap after the arrows
			$this.wrap('<div class=' + settings.scrollItwrapper + '></div>');
			
			//add the arrows
			$this.parent().prepend('<div class="scroll-it-up scroll-menu-up-' + i + '" ></div>');
			$this.parent().append('<div class="scroll-it-down scroll-menu-down-' + i + '"></div>');
			
			//capture  element to move
			var menuList = $($this.children());
			//get  margin-top and save in a variable
			//var menuListMargin = parseInt(menuList.css('margin-top'));
			//set margin  to 0 as initial value 
			var menuListMargin = 0;
			/* get the difference between the height of the list and its container to find the limit of scroll*/
			var menuListHeight = menuList.height() - $this.height();
			var menuListWidth = menuList.width() - $this.width();
	
			var resetmarg=0;
			var dbug=0;	
		
			//functions for up - down / left - right
			function scrollMenuUp(){
				if(dbug)console.log("up: "+menuListMargin);
				if(menuListMargin < 0){
					//set  new margin 
					menuListMargin += settings.lineHeight;
					//move list
					//recheck for greater than zero as that will overscroll
					if(menuListMargin > 0)menuListMargin=0;
					menuList.animate({ 
						marginTop: menuListMargin
					}, settings.scrollDuration );				
				}else{
					//clear interval if not needed
					clearInterval(intervalo);
				}
			}
			
			function scrollMenudown(){
				//check margin is not greater than set width
				if(dbug)console.log("dn: "+menuListMargin);
				if(Math.abs(menuListMargin) < menuListHeight){
					//set  new margin 
					menuListMargin -= settings.lineHeight;
					//move list
						//recheck for greater than list height to stop overscroll
					if(Math.abs(menuListMargin) > menuListHeight)menuListMargin="-"+menuListHeight;
					menuList.animate({ 
						marginTop: menuListMargin
					}, settings.scrollDuration );
				}else{
					//clear interval if not needed
					clearInterval(intervalo);
				}
			}
		
			//actions mouse
			$("." + settings.scrollItwrapper + " .scroll-menu-down-" + i ).hover(
				function () {
					
					if(!resetmarg){
					menuListMargin=	parseInt(document.getElementById('uljq').style.marginTop,10);
					
					if(dbug)console.log("hoverdn: "+menuListMargin);
					resetmarg=1;
					}
					//set interval when you call the function
					intervalo = setInterval(scrollMenudown, scrollInterval);
					//add class to change button image  
					$(this).addClass('arrow-hover');
				},
				function(){
					//add class to change button image and clear interval
					$(this).removeClass('arrow-hover');
					clearInterval(intervalo);
					resetmarg=0;
				}
			);
					
			$("." + settings.scrollItwrapper + " .scroll-menu-up-" + i ).hover(
				function () {
					if(!resetmarg){
					menuListMargin=	parseInt(document.getElementById('uljq').style.marginTop,10);
					if(dbug)console.log("hoverup: "+menuListMargin);
					resetmarg=1;
					}
					//set interval when you call the function
					intervalo = setInterval(scrollMenuUp, scrollInterval);
					//add class to change button image 
					$(this).addClass('arrow-hover');
				},
				function(){
					//add class to change button image and clear interval
					$(this).removeClass('arrow-hover');
					clearInterval(intervalo);
					resetmarg=0;
				}
			);	
	});
};
	

})(jQuery);
