/*
 * iScrollBar
 * Examples and documentation at: http://
 * Version: 1.0.0 (05/01/2011)
 * Copyright © 2011 
 * Licensed under the MIT License: http://en.wikipedia.org/wiki/MIT_License
 * Requires: jQuery v1.4.4+
*/
;(function($) {

	$.fn.iScrollBar = function( options ){
		
		var defaults = {
			deltaWeight : 30,
			enterFrameSeconds : 50,
			viewScrollButtons : false,
			viewScrollBar : true,
			fixWidth : false
		};
		
		var options = $.extend( {}, defaults, options );

		this.each(function( i, o ){
		
			var _root = $(o);
			var margintop = 0;

			var iScrollBar_Box = $( ".iscrollbar_box:eq(0)", _root );
			var iSBC = $( ".isbc:eq(0)", iScrollBar_Box );

			var iScrollBar_Controller = $("<div class='iscrollbar_controller'><div class='iscrollbar_scrolltop'><p></p></div><div class='iscrollbar_scrollbottom'><p></p></div><div class='trackline'><div class='seeker'><div class='face'><span></span><p></p></div></div></div></div>").appendTo( iScrollBar_Box );
			
			var iScrollButton_Top = $( ".iscrollbar_scrolltop:eq(0)", iScrollBar_Controller ).hide();
			var iScrollButton_Bot = $( ".iscrollbar_scrollbottom:eq(0)", iScrollBar_Controller ).hide();
			
			var iTrackLine = $( ".trackline:eq(0)", iScrollBar_Controller );
			var iSeeker = $( ".seeker:eq(0)", iTrackLine );
			var iSeekerP = $( "p:eq(0)", iSeeker );
			var iSeekerIcon = $( "span:eq(0)", iSeeker );
			
			var difference = iSBC.outerHeight(true) - _root.outerHeight(true);

			if( difference > 0 )
			{
	
				//iScrollBar_Box.width( iScrollBar_Box.parent().outerWidth(true) );
				//iSBC.width( iScrollBar_Box.width() - iScrollBar_Controller.outerWidth(true) - 4 );
				
				iScrollBar_Controller.height( _root.outerHeight(true) );
				
				if( options.viewScrollButtons )
				{
					var iScrollVector = 0;

					setInterval(function(){
						if( iScrollVector )
						{
							scrollByVector( iScrollVector, true );
						}
					}, options.enterFrameSeconds);
					
					iTrackLine.css({
						'position' : 'absolute',
						'height' : _root.height() - iScrollButton_Top.height() - iScrollButton_Bot.height(),
						'top' : iScrollButton_Top.height()
					});
					
					iScrollButton_Top.css({
						'top':0
					}).mousedown(function(){
						iScrollVector = 1
					}).mouseleave(function(){
						iScrollVector = 0
					}).mouseup(function(){
						iScrollVector = 0
					}).show();
					
					iScrollButton_Bot.css({
						'bottom':0
					}).mousedown(function(){
						iScrollVector = -1
					}).mouseleave(function(){
						iScrollVector = 0
					}).mouseup(function(){
						iScrollVector = 0
					}).show();

				}
				else
				{
					iTrackLine.height( _root.outerHeight(true) );
				}
				
				setInterval(function(){
					
					if( _root.css('display') != 'none' )
					{
						var iSeekerHeight = Math.round( _root.outerHeight(true) / iSBC.outerHeight(true) * iTrackLine.outerHeight(true) );
						
						iSeeker.height( iSeekerHeight );
	
						iSeekerIcon.css({
							'top' : Math.round( ( iSeekerHeight + iSeekerP.outerHeight(true) ) / 2 - iSeekerIcon.outerHeight(true) / 2 )
						});
						
						iScrollBar_Box.height( iSBC.outerHeight(true) );
						
						var SubSeekerSpace = iTrackLine.outerHeight(true) - ( iSeeker.outerHeight(true) + parseInt( iSeeker.css('top').split('px')[0]) ) - iSeekerP.outerHeight(true);
						
						if( SubSeekerSpace < 0 )
						{
							scrollByVector( 1, true );
						}
					}
				
				}, options.enterFrameSeconds);
	
				iSeeker.css({
					'top' : 0
				})
				.bind( 'dragstart', function( event ){ $(this).addClass( 'seeker_drag' ) })
				.bind( 'dragend', function( event ){ $(this).removeClass( 'seeker_drag' ) })
				.bind( 'drag', function( event ){
	
					var _this = $(this);
					var top = parseInt( _this.css('top').split('px')[0]);
					
					var FreeScrollSpace = iTrackLine.outerHeight(true) - _this.outerHeight(true) - iSeekerP.outerHeight(true);

					if( top > -1 )
					{
						top = event.offsetY - iTrackLine.offset().top;
					}
					if( top < 0 )
					{
						top = 0;
					}

					var SubSeekerSpace = iTrackLine.outerHeight(true) - ( _this.outerHeight(true) + top ) - iSeekerP.outerHeight(true);
					
					if( SubSeekerSpace < 0 )
					{
						top = FreeScrollSpace;
					}

					_this.css({ 'top' : top });

					var percent = top / FreeScrollSpace;

					var difference = iSBC.outerHeight(true) - _root.outerHeight(true);
	
					iScrollBar_Box.stop().css({ 'marginTop' : margintop = -Math.round( difference * percent ) });

				});

				_root.mousewheel(function( event, vector ){ scrollByVector( vector, false ); return false});
			
				function scrollByVector( vector, css )
				{

					var FreeScrollSpace = iTrackLine.outerHeight(true) - iSeeker.outerHeight(true) - iSeekerP.outerHeight(true);
					
					//$(".priceline p").text(iTrackLine.outerHeight(true) - iSeeker.outerHeight(true) - iSeekerP.outerHeight(true));
					
					margintop += vector * options.deltaWeight;
						
					var difference = iSBC.outerHeight(true) - _root.outerHeight(true);
						
					if( ( difference + margintop ) < 0 ) { margintop = -difference }
					if( margintop > 0 ) { margintop = 0 }
		
					iScrollBar_Box.stop().css({ 'marginTop' : margintop });
		
					var percent =  margintop / difference ;

					if( css )
					{
						iSeeker.addClass( 'seeker_drag' ).stop()
						.css({ 'top' : -Math.round( FreeScrollSpace * percent ) })
						.removeClass( 'seeker_drag' );
					}
					else
					{
						iSeeker.addClass(  'seeker_drag' ).stop()
						.animate({ 'top' : -Math.round( FreeScrollSpace * percent ) }, 400, 'swing', function(){ $(this).removeClass('seeker_drag') });
					}
				}

			}
			else
			{
				iScrollBar_Controller.hide();
				if( options.fixWidth )
				iScrollBar_Box.width( iScrollBar_Box.parent().width() );
				//iScrollBar_Box.css({"width":"auto"});
			}
			
		});

	};
})(jQuery);
