/**
 * Copyright (c) 2008 Andreas Timm | symmetrics gmbh (http://symmetrics.de)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $CreateDate: 2008-07-30 16:22:00 +0001 (Wen, 30 Jul 2008) $
 * $LastChangedDate: 2008-07-30 16:22:00 +0001 (Wen, 30 Jul 2008) $
 *
 * Version: 0.01
 *
 * @author Andreas Timm <at@symmetrics.de>
 *
 * Requires: jQuery 1.2.4+, mScrollInertia, dAux
 **/
(function($){

	/**
	 * Number	top:
	 * Number	width:
	 * Number	height:
	 * Number	cont_height:
	 * Number	c_height:
	 **/
	$.etxtscroll = new Object ;

	$.etxtscroll_default =
	{
		init: false,
		top: 416,
		width: 27,
		height: 100,
		pos: 0,
		cont_dom: 0,
		cont_delta: 0,
		c_max_height: 120,
		scroll_shift: 12,
		field_down: false
	} ;


	$.etxtscroll_param =
	{
		up_dn_height: 12
 	} ;

	/**
	 * etxtscroll
	 *
	 * @param	Object		v:
	 * 				String		id:
	 * 				.......    ...: parameters
	 **/
	$.etxtscroll_conf = function ( v )
	{
		var i1 ;
		var i2 ;
		for ( i1 in v )
		{
			if ( $.etxtscroll[i1] === undefined )
				$.etxtscroll[i1] = dAux.clone_obj ( $.etxtscroll_default ) ;
			for ( i2 in v[i1] )
			{
				$.etxtscroll[i1][i2] = v[i1][i2] ;
			}
		}
	};


	/**
	 * etxtscroll position
	 *
	 * @param	String		i:
	 * @param	Number		position: [0-1]
	 **/
	$.etxtscroll_position = function ( id, position )
	{
		position ({ id: id, pos: position }) ;
	};


	/**
	 * update
	 *
	 * @param	String		id:
	 **/
	$.etxtscroll_update = function ( id )
	{
		var el = $($.etxtscroll[id].scroller) ;

		var params = $.etxtscroll[id] ;

		$.etxtscroll[id].cont_height = params.cont_height =
			$("#"+id+" "+$.etxtscroll[id].cont_dom).height() ;

		$.etxtscroll[id].height = params.height =
		 	$("#"+id).height() ;

		if ( params.cont_height  == 0 || params.cont_height <= params.height )
		{
			el.css("display","none");
			return ;
		}
			el.css("display","block");

		params.c_height =
			$.etxtscroll[id].c_max_height
			* ( ( 1 - params.cont_height / params.height ) / ( 5 - 1 ) + 1 ) ;

		if ( params.c_height < $.etxtscroll_param.up_dn_height )
			params.c_height = $.etxtscroll_param.up_dn_height ;

		$.etxtscroll[id].c_height = params.c_height ;
	} ;



	$.fn.extend ({
		/**
		 * etxtscroll
		 *
		 * @param	Object		v:
		 * 				String		cont: id or class of content body
		 * 				String		cursor:
		 * 				Function	cont_down:
		 * 				Function	cont_up:
		 * 				String		inertia_id:
		 */
		etxtscroll: function( v )
		{
			var id = $(this).attr("id") ;
			if ( $.etxtscroll[id] === undefined )
				$.etxtscroll[id] = dAux.clone_obj ( $.etxtscroll_default ) ;

			$.etxtscroll[id].inertia_id =
				v.inertia_id === undefined ? "etxscroll" : v.inertia_id;

			$.etxtscroll[id].cont_dom = v.cont;

			$.etxtscroll[id].scroller = v.scroller;

			$.etxtscroll[id].cursor_down = v.cursor_down !== undefined ?
				v.cursor_down : "pointer" ;
			$.etxtscroll[id].cursor_up = v.cursor_up !== undefined ?
				v.cursor_up : "pointer" ;

			if ( v.cont_down !== undefined )
				$.etxtscroll[id].cont_down = v.cont_down;
			if ( v.cont_up !== undefined )
				$.etxtscroll[id].cont_up = v.cont_up;

			if ( ! $.etxtscroll[id].init )
				ets_init( id );

			$.etxtscroll_update ( id );

			mScrollInertia.stop ( $.etxtscroll[id].inertia_id );
			position ({ id: id, pos:0 }) ;
		}
	});



	/**
	 * init
	 *
	 * @param	String		id: (default=ets_{parent.id})
	 **/
	function ets_init ( id )
	{
		$.etxtscroll[id].init = true ;

		$($.etxtscroll[id].scroller).mousedown(function( event ){

			$.etxtscroll[id].field_down = true ;

			var first_click = event.pageY ;
			var first_pos = $.etxtscroll[id].pos ;

			$("#reference_scroll").css("cursor",$.etxtscroll[id].cursor_down);
			dAux.mouse_over_box ({
				action: "init",
				cursor: $.etxtscroll[id].cursor_down,
				func_move: function ( event )
				{
					position
					({
						id: id,
						first_pos: first_pos,
						delta_scr: event.pageY - first_click
					}) ;
				},
				func_up: function ( event )
				{
					$("#reference_scroll").css("cursor",$.etxtscroll[id].cursor_up);
					$.etxtscroll[id].field_down = false ;
//					References.checkHideOut(event);
					dAux.mouse_over_box ({ action: "remove" }) ;
				}
			});

		}) ;


		$("#"+id+" "+$.etxtscroll[id].cont_dom).mousedown(function( event ){

			var first_y = event.pageY ;
			var first_pos = $.etxtscroll[id].pos ;
			$.etxtscroll[id].cont_delta = 0 ;

			$.etxtscroll[id].field_down = true ;

			if ( $.etxtscroll[id].cont_down !== undefined )
				$.etxtscroll[id].cont_down () ;

			//---- inertia 01 ----------------------------------------
			mScrollInertia.speed_calc1 ({
				id: $.etxtscroll[id].inertia_id,
				action: "start",
				get_values: function ()
				{
					return [ $.etxtscroll[id].cont_delta ] ;
				}
			}) ;
			mScrollInertia.vars[$.etxtscroll[id].inertia_id].sc_param1_temp =
				first_y - event.pageY ;
			//---- end of inertia 01 ---------------------------------

			$("#references_box").css("cursor",$.etxtscroll[id].cursor_down);
			dAux.mouse_over_box ({
				action: "init",
//				cursor: $.etxtscroll[id].cursor,
				cursor: $.etxtscroll[id].cursor_down,
				func_move: function ( event )
				{
					$.etxtscroll[id].cont_delta = first_y - event.pageY ;
					position
					({
						id: id,
						delta: $.etxtscroll[id].cont_delta,
						first_pos: first_pos
					}) ;

					//---- inertia 02 ------------------
					mScrollInertia.speed_calc2 ({
						id: $.etxtscroll[id].inertia_id,
						param1: $.etxtscroll[id].cont_delta
					}) ;
					//---- end of inertia 02 -----------

				},
				func_up: function ( event )
				{
					$("#references_box").css("cursor",$.etxtscroll[id].cursor_up);
					$.etxtscroll[id].field_down = false ;
					References.checkHideOut(event);
					mScrollInertia.init({
						id: $.etxtscroll[id].inertia_id,
						param1: $.etxtscroll[id].cont_delta,
						move: function ( data )
						{
							position
							({
								id: id,
								delta: data.param1,
								first_pos: first_pos
							}) ;
						}
					});

					mScrollInertia.speed_calc1 ({
						id: $.etxtscroll[id].inertia_id,
						action: "stop"
					}) ;

					dAux.mouse_over_box ({ action: "remove" }) ;

					if ( $.etxtscroll[id].cont_up !== undefined )
						$.etxtscroll[id].cont_up ({ delta: $.etxtscroll[id].cont_delta }) ;
				}
			});

		}) ;

	}



	/**
	 * position
	 *
	 * there are 3 options:
	 *    1) v.pos = [0-1]
	 *    2) v.y = left scroller from 0 to ... end
	 *    3) v.delta and v.first_pos = delta of y and first pos
	 *
	 * @param	Object		v:
	 * 				String		id:
	 * 				Number		pos: [0-1]
	 * 				Number		y:
	 * 				Number		delta:
	 * 				Number		first_pos:
	 **/
	function position ( v )
	{
		if ( v.delta !== undefined )
			v.pos =
				v.first_pos
				+ v.delta
					/ ( $.etxtscroll[v.id].cont_height - $.etxtscroll[v.id].height );
		else if ( v.delta_scr !== undefined )
			v.pos = v.first_pos + v.delta_scr / ( $.etxtscroll[v.id].height - 34 );

		if ( v.pos < 0 )
			v.pos = 0 ;
		else if ( v.pos > 1 )
			v.pos = 1 ;

		$.etxtscroll[v.id].pos = v.pos ;

		//scroller position
		$($.etxtscroll[v.id].scroller).css
		(
			"top",
			Math.round ( v.pos * ( $.etxtscroll[v.id].height - 34 ) )
			 + $.etxtscroll[v.id].scroll_shift
		);

		$("#"+v.id+" "+$.etxtscroll[v.id].cont_dom).css
		(
			"top",
			- Math.round
			(
				v.pos
				* ( $.etxtscroll[v.id].cont_height - $.etxtscroll[v.id].height )
			)
		) ;
	}

})(jQuery);