/*
 *JQuery scroller plugin. Creates a custom scrollbar for div 
 *
 *@author Alexey playboyko Boyko playboyko.spb@gmail.com
 *@version 1.0rc1
 *@requires JQuery 1.2.6
 *@requires JQueryUI
 *
 */
; ( function( $ ) {
    $.fn.scroller = function( options ) {
	var settings = {
	    orientation: 'vertical'
	    , el: 'div:first'
	    , scrollClass: null
	    , scrollBarClass: null
	    , speed: 15
	    , scrollWidth: 8
	    , autoWidth: true
	    , height: null
	}
	if( options ) {
	    $.extend( settings, options );
	}
	return this.each(
	    function( i ) {
		var self = $(this);
		var tmp = self.find( settings.el );
		self.css( 'overflow', 'hidden' );
                var container = $('<div></div>').prependTo(self).html(tmp);
		//tmp.remove();
		
		var canvas      = container.find( settings.el );
		var scrollCnt   = $('<div></div>').appendTo(self);
		var scroll      = $('<div style="height: 100%"></div>').appendTo(scrollCnt);
		var itemsHeight = null;
		var margina	= 'margin-top'
		if( settings.orientation == 'vertical' ) {
		    scrollCnt.addClass( 'scroller-scroll' );
		    var width     = self.width() - settings.scrollWidth;
		    if( settings.scrollClass ) {
			scrollCnt.addClass( settings.scrollClass );
			if( settings.autoWidth ) {
			    width = self.width() - scrollCnt.width();
			}
		    }
		    var height    = self.height();
		    container.width(width).addClass( 'scroller-container' );
		    self.height(height);
		    
		    self.addClass( 'scroller' );
		    $('<div style="clear: both"></div>').appendTo(self);
		    itemsHeight =  canvas.innerHeight() - self.height();
		    if( itemsHeight <= 0 ) {
			itemsHeight = self.height() - canvas.innerHeight();
		    }
		    if( settings.height ) {
			itemsHeight = settings.height;
		    }
		    //console.log( itemsHeight, canvas.innerHeight(), self.height() );
		} else {
		    if( settings.scrollClass ) {
			scrollCnt.addClass( settings.scrollClass );
		    }
		    width = self.parent().parent().width();
		    
		    
		    if( settings.height ) {
			self.width( settings.height );
		    }
		    itemsHeight =  self.width() - width;
		    //IE big hack.. Using ONLY for this project!!
		    if( $.browser.msie ) {
			itemsHeight = 564;
		    }
		    //console.log( self.parent().width(), canvas.innerWidth(), self.width(), itemsHeight );
		    margina	= 'margin-left';
		    scroll.css({ height: '8px', width: width - 8 });
		   /* if( $.browser.msie ) {
			itemsHeight = -itemsHeight;
			scroll.width(self.parent().width() - 8);
		    }*/
		}
		if( settings.scrollBarClass ) {
		    scroll.addClass( settings.scrollBarClass );
		}
		//canvas.css('width', '100%');
		
		if( $.browser.mozilla ) {
		    self.bind( 'DOMMouseScroll', function(e) {
			wheel( e, scroll, settings.orientation, settings.speed );
		    });
		} else {
		    self[0].onmousewheel = function(e) {
			wheel( e, scroll, settings.orientation, settings.speed );
		    }
		}
		/*console.log(self.height());
		console.log(container.height());
		console.log( canvas.innerHeight(), self.height(), itemsHeight );*/
		scroll.slider({
		    orientation: settings.orientation,
		    min: 0,
		    max: itemsHeight,
		    value: settings.orientation == 'vertical' ? itemsHeight : 0,
		    stop: function (event, ui) {
			var value = null;
			if( settings.orientation == 'vertical' ) {
			    value = itemsHeight - ui.value;
			} else {
			    value = ui.value;
			}
			canvas.animate( {margina : value * -1}, 500 );
		    },
		    slide: function (event, ui) {
			var value = null;
			if( settings.orientation == 'vertical' ) {
			    value = itemsHeight - ui.value;
			} else {
			    value = ui.value;
			}
			canvas.css( margina, value * -1 );
		    }
		    , change: function( event, ui ) {
			var value = null;
			if( settings.orientation == 'vertical' ) {
			    value = itemsHeight - ui.value;
			} else {
			    value = ui.value;
			}
			canvas.css( margina, value * -1 );
		    }
		});

	    }
	);

    }

    function wheel( event, scroll, orientation, speed ) {
	var wheelDelta = 0;
	
	if (!event) {
	    event = window.event;
	}
	if (event.wheelDelta) {
	    // В IE и Opera при сдвиге колеса на один шаг event.wheelDelta принимает значение 120
	    // Значения сдвига в этих двух браузерах совпадают по знаку.
	    wheelDelta = event.wheelDelta/120;
	} 
	else if (event.detail) {
	    // В Mozilla, значение wheelDelta отличается по знаку от значения в IE.
	    // Сдвиг колеса на один шаг соответствует значению 3 параметра event.detail          
	    wheelDelta = -event.detail/3;
	}
	// При скроллинге вверх wheelDelta > 0
	// При скролинге вниз - wheelDelta < 0
	if (wheelDelta) {
	    var value = scroll.slider('value');
	    //console.log(value);
	    
	    if( orientation == 'vertical' ) {
		if( wheelDelta > 0) {
		    value += speed;
		} else {
		    value -= speed;
		}
	    } else {
		if( wheelDelta > 0) {
		    value -= speed;
		} else {
		    value += speed;
		}
	    }
	    scroll.slider('value', value );
	}
	if (event.preventDefault) {
	    event.preventDefault();
	}
	event.returnValue = false;
	blockEvent(event);
    }
    
    function blockEvent(event ) {
	if ( !event ) {
	    event = window.event;
	}
	if( event.stopPropagation ) event.stopPropagation();
	    else event.cancelBubble = true;
	if( event.preventDefault ) event.preventDefault();
	    else event.returnValue = false;
    }


})(jQuery);