/* Copyright (c) 2011 Roland Kujundzic (http://web-to-print.eu)
 * Version: 1.0.0
 * Requires: jquery.mousewheel.js
 *
 * Enable horizontal or vertical scrolling via mousewheel or button within 
 * overflow:auto|hidden element. Scroll buttons must have same id as scoll
 * block with suffix _up|_down or _left|_right.
 *
 * Options are:
 *	- type: ='horizonal'|'vertical' 
 *	- size: int=15 (scroll step)
 *	- timer: int=30 (scroll speed when scroll button is pressed)
 *  - no_scroll: ='hidden'|'none'|''|function() { ... } 
 *			(execute when button exists and no scrolling necessary)
 *	- overflow: ='hidden'
 */
(function($) {

$.fn.rkSimpleScroll = function(options) {

var settings = {
	'type': 				'horizonal',
  'overflow':			'hidden',
  'no_scroll': 		'hidden',
  'timer':				30,
	'size': 				15 
};


var _timerScrollX = 0, _timerScrollY = 0;


return this.each(function() {        

  // Maintaining Chainability 
  var $this = $(this);

  // If options exist, lets merge them with our default settings
  if (options) {
		$.extend(settings, options);
  }

  if (settings.overflow) {
		$this.css('overflow', settings.overflow);
	}

  // bind button ...
  var prefix = '#' + $this.context.id;

  if (settings.type == 'horizonal' && $(prefix + '_left').length && $(prefix + '_right').length) {

	  function _lr_scroll(suffix, direction) {
  	  var btn = $(prefix + suffix);

    	btn.click(function(e) { e.preventDefault(); });

      if (settings.no_scroll == '' || $this.context.scrollWidth > $this.outerWidth()) {
	    	btn.mouseup(function() { clearInterval(_timerScrollX); });

	    	btn.mousedown(function() { 
  	    	_timerScrollX = setInterval(function() { 
    	   		$this.scrollLeft($this.scrollLeft() + direction * settings.size); }, settings.timer); 
	    	});
			}
      else if (typeof settings.no_scroll == 'function') {
				settings.no_scroll();
				settings.no_scroll = '';
			}
			else if (settings.no_scroll == 'none') {
				btn.css('display', 'none');
			}
			else if (settings.no_scroll == 'hidden') {
				btn.css('visibility', 'hidden');
			}
  	}

    _lr_scroll('_left', -1);
    _lr_scroll('_right', 1);
  }

  if (settings.type == 'vertical' && $(prefix + '_up').length && $(prefix + '_down').length) {

		function _ud_scroll(suffix, direction) {
  		var btn = $(prefix + suffix);

  		btn.click(function(e) { e.preventDefault(); });

/* console.log($this[0].scrollHeight + ': ' + $this.context.scrollHeight + ' ? ' + 
$this.outerHeight()); */

      if ($this.context.scrollHeight > $this.outerHeight()) {
    		btn.mouseup(function() { clearInterval(_timerScrollY); });

  		  btn.mousedown(function() { 
    		  _timerScrollY = setInterval(function() { 
      			$this.scrollTop($this.scrollTop() + direction * settings.size); }, settings.timer); 
  			});
			}
      else if (typeof settings.no_scroll == 'function') {
				settings.no_scroll();
				settings.no_scroll = '';
			}
			else if (settings.no_scroll == 'none') {
				btn.css('display', 'none');
			}
			else if (settings.no_scroll == 'hidden') {
				btn.css('visibility', 'hidden');
			}
		}

    _ud_scroll('_up', -1);
    _ud_scroll('_down', 1);
  }

	$this.bind('mousewheel', function(event, delta) {

		if (settings.type == 'horizonal') {
			$this.scrollLeft($this.scrollLeft() - delta * settings.size);
		}
    else if (settings.type == 'vertical') {
			$this.scrollTop($this.scrollTop() - delta * settings.size);
		}

	  // return false to avoid selecting text and dragging links within the scroll window
  	return false;
	});

});

};
})(jQuery);

