(function($){

	$.netscroller = function(el, options){
		
		var base = this;
		// Access to jQuery and DOM versions of element
		base.$el = $(el);
		base.el = el;
		// Add a reverse reference to the DOM object
		base.$el.data("netscroller", base);
		
		base.properties = new Object();
		base.properties.scroller_enable = true;
		base.ie6 = ($.browser.msie && ($.browser.version == "6.0")) ? true : false;
		
		base.init = function() {
			base.options = $.extend({},$.netscroller.defaultOptions, options);
			base.init_content();
			if( base.properties.scroller_enable == true ) {
				base.init_obj();
				//base.debug_obj();
				base.get_properties();
				base.init_style_css();
				base.add_event();
			}
		};
		
		base.init_content = function() {
			base.div_contener = base.$el;
			//base.content_html = base.$el.html();
			if( base.div_contener.css( 'display') != 'none' ) {
				if( base.div_contener.children( '.netscroller_content').size() == 0 ) {
					base.html = '';
					//base.html += '<div class="netscroller_content">'+base.content_html+'</div>';
					base.div_contener.children().wrapAll( '<div class="netscroller_content"></div>');
				}
				if( base.div_contener.children( '.netscroller_control').size() == 0 ) {
					base.ctrl = '';
					base.ctrl += '<div class="netscroller_control">';
					base.ctrl += '<div class="netscroller_control_up"></div>';
					base.ctrl += '<div class="netscroller_control_bar"><div class="netscroller_control_puce"></div></div>';
					base.ctrl += '<div class="netscroller_control_down"></div>';
					base.ctrl += '</div>';
					base.div_contener.append( base.ctrl);
				}
				//base.div_contener = base.$el.html( base.html);
			} else {
				base.properties.scroller_enable = false;
			}
		};
		
		base.init_obj = function() {
			base.div_contener = base.$el;
			base.div_content = base.div_contener.children('.netscroller_content');
			base.div_control = base.div_contener.children('.netscroller_control');
			base.div_control_up = base.div_control.children('.netscroller_control_up');
			base.div_control_bar = base.div_control.children('.netscroller_control_bar');
			base.div_control_puce = base.div_control_bar.children('.netscroller_control_puce');
			base.div_control_down = base.div_control.children('.netscroller_control_down');
			base.div_control_puce.draggable({
				addClasses: false, 
				axis: 'y', 
				containment: 'parent', 
				drag: function() {
					base.move_content();
				}
			});
			
		};
		
		base.get_properties = function() {
			
			/*var img = base.div_content.find( 'img');
			var comp = true;
			var max = 100;
			console.log( "length : "+img.length);
			for( var i = 0 ; i < max ; i++ ) {
				comp = true;
				for( var j = 0 ; j < img.length ; j++ ) {
					//console.log( img[j].complete + '   |||   ' + $(img[j]).height());
					if( img[j].complete != true ) {
						//console.log( img[j].complete);
						comp = false;
					}
				}
				if( comp == true ) {
					console.log( "ok : "+i);
					i = max;
				}
			}
			console.log( base.div_contener.height() );*/
			
			base.properties.contener_width = base.div_contener.width();
			base.properties.contener_height = base.div_contener.height();
			//base.properties.contener_height = parseInt(base.div_contener.css( "height"));
			base.properties.content_width = base.div_content.width();
			base.properties.content_height = base.div_content.height();
			//alert( base.properties.content_height);
			//base.properties.content_height = parseInt(base.div_content.css( "height"));
			base.properties.bar_height = base.div_control_bar.height();
			//base.properties.bar_height = parseInt(base.div_control_bar.css( "height"));
			base.properties.content_pos = (parseInt(base.div_content.css( 'top')) < 0)?parseInt(base.div_content.css( 'top')):0;
			//alert( parseInt(base.div_content.css( 'top')));
			base.properties.content_pos_max = ( base.properties.content_height - base.properties.contener_height );
			base.properties.puce_height = base.get_puce_height();
			base.properties.puce_pos = parseInt(base.div_control_puce.css( 'top'));
			base.properties.puce_pos_max = (( base.properties.bar_height - base.properties.puce_height)+1);
			//base.properties.scroller_enable = true;
			//console.log( base.properties);
			if( base.properties.content_height < base.properties.contener_height ) {
				base.properties.scroller_enable = false;
			}
			//base.debug_prop();
		};
		
		base.init_style_css = function() {
			if( base.properties.scroller_enable == false ) {
				base.div_control.css('display', 'none');
			}
			base.div_contener.css('overflow', 'hidden');
			base.div_contener.css('position', 'relative');
			base.div_content.css('position', 'absolute');
			base.div_content.css('top', '0');
			base.div_content.css('left', '0');
			base.div_control.css('height', base.properties.contener_height);
			var h = ( parseInt( base.properties.puce_height) > 0 )?parseInt( base.properties.puce_height):0;
			base.div_control_puce.css('height', h+'px' );
			base.div_control_up.css('cursor', 'pointer' );
			base.div_control_down.css('cursor', 'pointer' );
			base.div_control_puce.css('cursor', 'pointer' );
		};
		
		base.get_puce_height = function() {
			base.pourcent_aff = ( base.properties.contener_height * 100 ) / base.properties.content_height;
			var h = ( ( base.properties.bar_height * base.pourcent_aff ) / 100 );
			if( h > base.properties.bar_height ) h = base.properties.bar_height;
			//h = math.ceil(h+1);
			return h;
		};
		
		base.move_content = function() {
			base.properties.puce_pos = parseInt( base.div_control_puce.css( 'top'));
			var pourcent = ( base.properties.puce_pos * 100 ) / base.properties.puce_pos_max;
			var next_pos = parseInt( ( pourcent * base.properties.content_pos_max ) / 100) * (-1);
			base.properties.content_pos = parseInt( next_pos);
			base.div_content.css('top', base.properties.content_pos+'px');
		}
		
		base.move_pas = function( val) {
			var next_pos = base.properties.content_pos + ( base.options.pas * val );
			if( next_pos > 0 ) next_pos = 0;
			if( next_pos < -base.properties.content_pos_max ) next_pos = -base.properties.content_pos_max;
			base.properties.content_pos = parseInt( next_pos);
			base.div_content.css('top', base.properties.content_pos+'px');
			base.fixe_puce();
		}
		
		base.fixe_puce = function() {
			var pourcent = ( -base.properties.content_pos * 100 ) / base.properties.content_pos_max;
			var next_pos_puce = parseInt( ( pourcent * base.properties.puce_pos_max ) / 100);
			if( next_pos_puce < 0 ) next_pos_puce = 0;
			if( next_pos_puce > base.properties.puce_pos_max ) next_pos_puce = base.properties.puce_pos_max;
			base.properties.puce_pos = next_pos_puce;
			base.div_control_puce.css( 'top', next_pos_puce);
		}
		
		base.add_event = function() {
			if( base.properties.scroller_enable == true ) {
				base.event = new Object();
				// bt_up_click
				base.event.event_up = false;
				base.div_control_up.mousedown( function() { base.init_up(); } );
				base.div_control_up.mouseup( function() { base.release_up(); } );
				// bt_up_click
				base.event.event_down = false;
				base.div_control_down.mousedown( function() { base.init_down(); } );
				base.div_control_down.mouseup( function() { base.release_down(); } );
				// Mouse Whell
				base.div_contener.bind('mousewheel', function (e, delta) {
					base.mouse_well( delta);
					return false;
				});
				// puce move
				base.event.event_puce_move = false;
				base.div_control_down.mousedown( function() { base.init_puce_move(); } );
				base.div_control_down.mouseup( function() { base.release_puce_move(); } );
			}
		};
		
		// function traitement event
		// ********** UP ***********
		base.init_up  = function() {
			base.event.event_up = true;
			base.action_up();
		};
		base.action_up  = function() {
			if( base.event.event_up ) {
				base.move_pas( 1);
				setTimeout( function() { base.action_up(); }, base.options.repeat_delay);
			}
		};
		base.release_up  = function() {
			base.event.event_up = false;
		};
		
		// ********* DOWN **********
		base.init_down  = function() {
			base.event.event_down = true;
			base.action_down();
		};
		base.action_down  = function() {
			if( base.event.event_down ) {
				base.move_pas( -1);
				setTimeout( function() { base.action_down(); }, base.options.repeat_delay);
			}
		};
		base.release_down  = function() {
			base.event.event_down = false;
		};
		
		// ********* PUCE **********
		base.init_puce_move  = function() {
			base.event.event_puce_move = true;
			
		};
		base.puce_move  = function() {
			if( base.event.event_down ) {
				base.move_pas( -1);
				setTimeout( function() { base.action_down(); }, base.options.repeat_delay);
			}
		};
		base.release_puce_move  = function() {
			base.event.event_puce_move = false;
		};
		
		// ******* MOUSE *********
		base.mouse_well = function( val) {
			base.move_pas( val);
		}
		
		base.debug_prop = function() {
			var txt = '';
			txt += 'contener_width : '+base.properties.contener_width+"\n";
			txt += 'contener_height : '+base.properties.contener_height+"\n";
			txt += 'content_width : '+base.properties.content_width+"\n";
			txt += 'content_height : '+base.properties.content_height+"\n";
			txt += 'bar_height : '+base.properties.bar_height+"\n";
			txt += 'content_pos : '+base.properties.content_pos+"\n";
			txt += 'content_pos_max : '+base.properties.content_pos_max+"\n";
			txt += 'puce_height : '+base.properties.puce_height+"\n";
			txt += 'puce_pos : '+base.properties.puce_pos+"\n";
			txt += 'puce_pos_max : '+base.properties.puce_pos_max+"\n";
			txt += 'scroller_enable : '+base.properties.scroller_enable+"\n";
			alert( txt);
		}
		
		base.debug_obj = function() {
			var txt = '';
			txt += 'div_contener : '+base.div_contener+"\n";
			txt += 'div_content : '+base.div_content+"\n";
			txt += 'div_control : '+base.div_control+"\n";
			txt += 'div_control_up : '+base.div_control_up+"\n";
			txt += 'div_control_down : '+base.div_control_down+"\n";
			txt += 'div_control_bar : '+base.div_control_bar+"\n";
			txt += 'div_control_puce : '+base.div_control_puce+"\n";
			alert( txt);
		}
		
		
		
	};

	$.netscroller.defaultOptions = {
		pas: 10, 
		repeat_delay: 100
  };

	$.fn.netscroller = function( options){
    return this.each(function(){
    	$(this).bind( 'display_change', function( e) {
				e.stopPropagation();
				(new $.netscroller(this, options)).init();
				//alert( e+' : display_change : '+this);
			});
      (new $.netscroller(this, options)).init();
    });
  };

})(jQuery)
