/* Copyright (c) 2010 Eugene Formanenko (lublu_pivo@gmail.com)
 * Licensed under the MIT License.
 *
 * Version: 0.0.1b
 *
 */
(function(){
	var scrollPane = function(cont, options) {
		var scrollPane = this,
			$this = $(cont),
			scrollPans = options.elems!=undefined ? $this.find(options.elems) :  $this.children(),
			height = 0,
			slideBarHeight = 0,
			width,
			
			contHolder,
			slider,
			sliderPans,
			sliderConts,
			
			startMouse,
			
			scrollPercent = 0,
			_scrollPercent = 0,
		
			sliderMargin = 2,
			visible = false,
			enabled = false;			
			
			autoResize = options.autoResize || false;
		
		$.extend(this, {			
			init: function() {				
				height = $this.height();
				width = $this.width();	
				
				if(options.bgColor != undefined)
					$this.css('backgroundColor', options.bgColor)
					
				var _height;	
				scrollPans.each(function() {
					if(this.tagName != 'TD') {
						_height = $(this).outerHeight();
					}
					else {
						var tmp = $('<div>').html($(this).html())	
						$(this).append(tmp);						
						_height = tmp.outerHeight()+50;
						tmp.remove();
					}
					if(_height>height) {
						$(this).html(
							$('<div class="sp_Cont" />').css({overflow: 'hidden'}).append(
								$('<div class="sp_Pane" data-padTop="'+$(this).css('paddingTop')+'" data-padBot="'+$(this).css('paddingBottom')+'" />').css({position: 'relative'}).html($(this).html())
							)			
						)
						enabled = true;
					}						
				});	
						
				
				
				
				
				if(enabled) {
					var _html = $this.html();
					$this.css({position: 'relative'}).empty().append(
						contHolder = $('<div class="sp_ContHolder" />').html(_html),
						slider = $('<div class="sp_Slider" />').css({position: 'absolute', top:sliderMargin, right: 2, backgroundColor: '#f8c217'})
							.mousedown(function(e) {
								scrollPane.slideStart(e);
							})
					)
					sliderConts = $this.find("div.sp_Cont");
					scrollPans = $this.find("div.sp_Pane").bind('mousewheel', $.proxy(scrollPane, 'slideWheel'));
					
					
				}
	
				
				visible = true;
				
				this.calculateSize(false);
			},
			reCalculate: function() {
				height =  $this.height();
				width = $this.width();	
				
				if(enabled) {
					this.calculateSize(true);
				}
			},
			calculateSize: function(updPos) {
				
				
				if(enabled) {
					slider.height(Math.min(172, height*0.5)) 
					contHolder.css({width: width-10});
					slideBarHeight = height - slider.height()-sliderMargin*2;
					sliderConts.css({height: !autoResize ? height-20 : height});
					scrollPans.each(function() {	
						$(this).data('height',
							$(this).height()+
							parseInt($(this).data('padTop'))+
							parseInt($(this).data('padBot'))-
							$(this).parent().height()
						);
					});	
					if(updPos) {
						this.updatePos(scrollPercent);
					}
				}
			},
			slideStart: function(e) {
				startMouse = e.pageY;
				if(options.onStart && typeof options.onStart == 'function') {
					options.onStart(e);
				}
				$(document).bind('mousemove',$.proxy(scrollPane,"slide")).one('mouseup', function(e) {
					$(document).unbind('mousemove');
					scrollPercent = _scrollPercent;
					if(options.onEnd && typeof options.onEnd == 'function') {
						options.onEnd(e);
					}
				})
			},
			updatePos: function(percent) {
				slider.css({top: sliderMargin+percent*slideBarHeight});
				scrollPans.each(function(){
					$(this).css({top: 
						-(percent*$(this).data('height'))
					});
				})
			},
			slide: function(e) {
				var sel;
				if(document.selection && document.selection.empty) {
					document.selection.empty();
				}
				else if(window.getSelection) {
					sel = window.getSelection();
					if(sel && sel.removeAllRanges)
						sel.removeAllRanges();
				}
				
				_scrollPercent = scrollPercent + (e.pageY-startMouse)/slideBarHeight;					
				_scrollPercent = Math.min(Math.max(0, _scrollPercent),1);
				
				this.updatePos(_scrollPercent);
			},
			slideWheel: function(e, delta) {
				e.preventDefault();
				e.stopPropagation();
				scrollPercent = scrollPercent - delta/8;					
				scrollPercent = Math.min(Math.max(0, scrollPercent),1);				
				this.updatePos(scrollPercent);
			}	
		});
		
		this.init();	
		if(autoResize) {
			$(window).resize(function() {
				scrollPane.reCalculate();			
			});
		}
	}

	jQuery.fn.scrollPane = function(options) {
		return this.each(function(){
			if($(this).data("scrollPane") == undefined) {
				new scrollPane(this, options);
				//console.log('scrollPane inited')
				$(this).data("scrollPane",{inited: true});
			}
		});
	}
	
})(jQuery);
