/* $Id: gui_button.js,v 1.2 2003/12/10 11:38:27 olaf Exp $ */

	function class_scrollPanel() {
		this._scrollField	= null;
		this._scrollBar		= null;
		this._buttonUp		= null;
		this._buttonDown	= null;
	}

// -----------------------------------------------------------------------------------------------------------------------

	function class_button( layerId, scrollPanelObj ) {
		this._scrollPanel = scrollPanelObj;
		this.initialize = defaultInitialize;
		this.initialize( layerId );
		this.actualize = function() {};
//		this.show();
	}

// -----------------------------------------------------------------------------------------------------------------------

	function class_scrollBar( layerId, scrollPanelObj ) {
		this._scrollPanel = scrollPanelObj;

		this.initialize = defaultInitialize;
		this.initialize( layerId );

		this.l_mark = new class_layer( layerId + "Mark" );
		this.layers[1] = this.l_mark;

		this.actionType = "none";
		this.dragging = scrollBar_dragging;

		this.actualize = scrollBar_actualize;
		this.actualize();

		if ( parseInt(this._scrollPanel._scrollField.l_main.css.height) > this._scrollPanel._scrollField.areaHeight ) {
			this._scrollPanel._buttonUp.show();
			this._scrollPanel._buttonDown.show();
			this.show();
		}

	}

	function scrollBar_dragging( eventName, x, y ) {

		if ( y - this.dragShift + parseInt(this.l_mark.css.height) > this.top + this.height ) {
			this._scrollPanel._scrollBar.l_mark.css.top = this.top + this.height - parseInt(this.l_mark.css.height);
		}
		else if ( y - this.dragShift  < this.top ) {
			this._scrollPanel._scrollBar.l_mark.css.top = this.top;
		}
		else {
			this._scrollPanel._scrollBar.l_mark.css.top = y - this.dragShift;
		}
		var knobHeight = ( this._scrollPanel._scrollField.areaHeight/this._scrollPanel._scrollField.height ) * this.height ;
		var knobHeightDif = parseInt(this.l_mark.css.height) - knobHeight;
		var scrollerDif = ( knobHeightDif / this.height ) * this._scrollPanel._scrollField.height;
		var mnoznik = ( parseInt(this.l_mark.css.top) - this.top ) / ( this.height - parseInt(this.l_mark.css.height) );
		var over = ( parseInt(this.l_mark.css.top) - this.top ) / this.height;
			over = Math.round( ( over * this._scrollPanel._scrollField.height ) + ( scrollerDif * mnoznik ) );
		this._scrollPanel._scrollField.scrollTo( over );

	}

	function scrollBar_actualize( eventName, x, y ) {
		if ( this._scrollPanel._scrollField.areaHeight > this._scrollPanel._scrollField.height ) return;
		switch( eventName ) {
			case "down" :
				if (
						x >= parseInt(this.l_mark.css.left)
					&&	y >= parseInt(this.l_mark.css.top)
					&&	x <= ( parseInt(this.l_mark.css.left) + parseInt(this.l_mark.css.width) ) 
					&&	y <= ( parseInt(this.l_mark.css.top) + parseInt(this.l_mark.css.height) )
				) {
					this.actionType = "drag";
					this.dragShift  = y - parseInt(this.l_mark.css.top);
				}
				else {
					if ( y > ( parseInt(this.l_mark.css.top) + parseInt(this.l_mark.css.height) ) ) {
						this.actionType = "scrolldown";
						this._scrollPanel._scrollField.scrollMe = true;
						this._scrollPanel._scrollField.scrollDown();
					}
					else {
						this.actionType = "scrollup";
						this._scrollPanel._scrollField.scrollMe = true;
						this._scrollPanel._scrollField.scrollUp();
					}
				}
				break;
			case "up" :
				this.actionType = "none";
				this._scrollPanel._scrollField.stopScroll();
				break;
			case "mouseout" :
				if (this.mouseDown) this._scrollPanel._scrollField.stopScroll();
				break;
			case "out" :
				if (this.mouseDown && this.actionType == "drag") this.dragging( eventName, x, y );
				break;
			case "rolling" :
				if (this.mouseDown && this.actionType == "drag") this.dragging( eventName, x, y );
				break;
			case "mouseover" :
				if (this.mouseDown) {
					switch( this.actionType ) {
						case "scrolldown" :
							this._scrollPanel._scrollField.scrollMe = true;
							this._scrollPanel._scrollField.scrollDown();
							break;
						case "scrollup" :
							this._scrollPanel._scrollField.scrollMe = true;
							this._scrollPanel._scrollField.scrollUp();
							break;
						case "drag" :
							break;
					}
				}
				break;
			case "scrolled" :
				var over = this._scrollPanel._scrollField.l_main.clipTop / this._scrollPanel._scrollField.height;
				var	knobTop = this.top + this.height * over;
				var knobHeight = Math.round( ( this._scrollPanel._scrollField.areaHeight/this._scrollPanel._scrollField.height ) * this.height ) ;
				var knobHeightDif = parseInt(this.l_mark.css.height) - knobHeight;
				var mnoznik = this._scrollPanel._scrollField.l_main.clipTop / ( this._scrollPanel._scrollField.height - this._scrollPanel._scrollField.areaHeight );
				this._scrollPanel._scrollBar.l_mark.css.top = Math.round( knobTop - knobHeightDif*mnoznik );
				break;
		}
	}

// -----------------------------------------------------------------------------------------------------------------------

	function class_scrollField( name, layerId, areaWidth, areaHeight, scrollPanelObj ) {
		this._scrollPanel = scrollPanelObj;
		this.initialize = defaultInitialize;
		this.initialize( layerId );

		this.name = name;

		this.moveBy = 30;
		this.areaWidth = areaWidth;
		this.areaHeight = areaHeight;

		this.scrollMe = false;

		// methods
		this.scrollTo   = scrollTo;
		this.scrollDown = scrollDown;
		this.scrollUp   = scrollUp;
		this.stopScroll = stopScroll;

		this.l_main.setClip( 0, this.areaWidth, this.areaHeight, 0 );

	}

	function scrollDown() {
		if ( this.areaHeight > this.height ) return;
		if ( !this.scrollMe ) return;
		if ( parseInt(this.l_main.css.height) != this.l_main.clipBottom ) {
			if ( ( this.l_main.clipBottom + this.moveBy ) > parseInt(this.l_main.css.height) ) {
				var moveBy = parseInt(this.l_main.css.height) - this.l_main.clipBottom;
				this.l_main.css.top = parseInt(this.l_main.css.top) - moveBy;
					var top = this.l_main.clipTop + moveBy;
					var bottom = parseInt(this.l_main.css.height);
				this.l_main.setClip( top, this.areaWidth, bottom, 0 );
				this._scrollPanel._scrollBar.actualize( "scrolled", moveBy );
				this.scrollMe = false;
				}
			else {
				this.l_main.css.top = parseInt(this.l_main.css.top) - this.moveBy;
					var top = this.l_main.clipTop + this.moveBy;
					var bottom = this.l_main.clipBottom + this.moveBy;
				this.l_main.setClip( top, this.areaWidth, bottom, 0 );
				this._scrollPanel._scrollBar.actualize( "scrolled", this.moveBy );
				this.scrollingLoop = setTimeout( this.name + ".scrollDown();", 30 );
			}
		}
	}

	function scrollUp() {
		if ( this.areaHeight > this.height ) return;
		if ( !this.scrollMe ) return;
		if ( this.l_main.clipTop != 0 ) {
			if ( this.l_main.clipTop < this.moveBy ) {
				var moveBy = this.l_main.clipTop;
				this.l_main.css.top = parseInt(this.l_main.css.top) + this.l_main.clipTop;
					var top = 0;
					var bottom = this.areaHeight;
				this.l_main.setClip( top, this.areaWidth, bottom, 0 );
				this._scrollPanel._scrollBar.actualize( "scrolled" );
				this.scrollMe = false;
				}
			else {
				this.l_main.css.top = parseInt(this.l_main.css.top) + this.moveBy;
					var top = this.l_main.clipTop - this.moveBy;
					var bottom = this.l_main.clipBottom - this.moveBy;
				this.l_main.setClip( top, this.areaWidth, bottom, 0 );
				this._scrollPanel._scrollBar.actualize( "scrolled" );
				this.scrollingLoop = setTimeout( this.name + ".scrollUp();", 30 );
			}
		}
	}

	function scrollTo( yPossition ) {
				this.l_main.css.top = this.top - yPossition;
				this.l_main.setClip( yPossition, this.areaWidth, yPossition + this.areaHeight, 0 );
	}

	function stopScroll() {
		this._scrollPanel._scrollField.scrollMe = false;
		clearTimeout( this.scrollingLoop );
	}

