/**
 *	class:	CScrollContent
 *	author: Hans-Jürgen Hartl, 04.2005
 *	
 */
function CScrollContent( szOuterClipId, szInnerClipId, szContentTableId )
{
	this.m_szOuterClipId = szOuterClipId;
	this.m_szInnerClipId = szInnerClipId;
	this.m_szContentTableId = szContentTableId;
	this.m_nContentHeight = 0;
	this.m_nClipHeight = 0;
	this.m_nScrollableHeight = 0;
	this.m_nScrollCalcDelta = 0;
	
	this.init = function( objParent )
	{
		// content table
		this.m_objParent = objParent;
		var objContentTable = objParent.document.getElementById( this.m_szContentTableId );
		// outer clip
		var objClipDiv = objParent.document.getElementById( this.m_szOuterClipId );
		
		this.setContentHeight( objContentTable.offsetHeight );
		this.setClipHeight( objClipDiv.offsetHeight );
		this.setScrollableHeight( Math.max(  ( this.getContentHeight() - this.getClipHeight() + this.m_nScrollCalcDelta ), 0 ) );
	}
	
	this.getContentPosY = function()
	{
		var szPosRaw = this.m_objParent.document.getElementById(this.m_szInnerClipId).style.top;
		szPosRaw.replace("px","");
		var nContentPosY = parseInt(szPosRaw,10);
		if(szPosRaw == "")
		{
			return 0;
		}
		return nContentPosY;
	}
	
	this.setContentPosY = function( nPosY )
	{
		this.m_objParent.document.getElementById(this.m_szInnerClipId).style.top = nPosY + "px";
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////////
	/* setters */
	
	this.setClipHeight = function( nHeight )
	{
		this.m_nClipHeight = nHeight;
	}

	this.setContentHeight = function( nHeight ) 
	{
		this.m_nContentHeight = nHeight;
	}
	
	this.setScrollableHeight = function ( nHeight )
	{
		this.m_nScrollableHeight = nHeight;
	}

	//////////////////////////////////////////////////////////////////////////////////////////////////////
	/*	getters */
	
	this.getClipHeight = function()
	{
		return this.m_nClipHeight;
	}
	
	this.getContentHeight = function()
	{
		return this.m_nContentHeight;
	}
	
	this.getScrollableHeight = function()
	{
		return this.m_nScrollableHeight;
	}
	
	this.getInnerClipId = function()
	{
		return this.m_szInnerClipId;
	}
	
	this.setScrollCalcDelta = function( nDelta )
	{
		this.m_nScrollCalcDelta = nDelta;
	}
	
	this.getScrollCalcDelta = function()
	{
		return this.m_nScrollCalcDelta;
	}
}

/**
 *	class:	CScrollBar
 *	author: Hans-Jürgen Hartl, 04.2005
 *	
 */
function CScrollBar( szScrollBarTableId, szScrollBarId, szArrowUpId, szArrowDownId, szSliderImgId, szScrollBarDivId  )
{
	this.m_szScrollBarTableId = szScrollBarTableId;
	this.m_szScrollBarDivId = szScrollBarDivId;
	this.m_szScrollBarId = szScrollBarId;
	this.m_szArrowUpId = szArrowUpId;
	this.m_szArrowDownId = szArrowDownId;
	this.m_szSliderImgId = szSliderImgId;
	this.m_nSliderHeight = null;
	this.m_nScrollBarHeight = null;
	this.m_nDragableHeight = null;
	this.m_szSelectColor = null;
	this.m_nSliderPos = 0;
	
	this.init = function( objParent )
	{
		this.m_objParent = objParent;
		var objScrollBarTable =  objParent.document.getElementById( this.m_szScrollBarTableId );
		var objArrowUp = objParent.document.getElementById( this.m_szArrowUpId );
		var objArrowDown = objParent.document.getElementById( this.m_szArrowDownId );
		var objSliderImg = objParent.document.getElementById( this.m_szSliderImgId );
		
		// outer height of the scrollbar (including arrows)
		this.m_nScrollBarOuterHeight = objScrollBarTable.offsetHeight;

		this.m_nArrowUpHeight = objArrowUp.offsetHeight;
		this.m_nArrowDownHeight = objArrowDown.offsetHeight;
		
		// inner height of the scrollbar (between the arrows)
		this.m_nScrollBarHeight = this.m_nScrollBarOuterHeight - this.m_nArrowUpHeight - this.m_nArrowDownHeight;
		
		// relevant height for slider
		this.m_nSliderHeight = objSliderImg.offsetHeight;
	}
	
	this.draw = function()
	{
		var objScrollerImg = eval( "this.m_objDD.elements."+this.getSliderImgId() );
		var objArrowUp = eval( "this.m_objDD.elements."+this.getArrowUpId() );
		objScrollerImg.resizeTo( eval( "this.m_objDD.elements."+this.getSliderImgId()+".w" ), this.getSliderHeight(), 1);
		objScrollerImg.setCursor( CURSOR_HAND );
	}
	
	this.refresh = function()
	{
		var objScrollerImg = eval( "this.m_objDD.elements."+this.getSliderImgId() );
		objScrollerImg.moveTo ( objScrollerImg.x, this.getSliderTop()+this.getSliderPos() );
	}
	
	this.Select = function()
	{
		document.getElementById( this.m_szScrollBarId ).style.backgroundColor = this.m_szSelectColor;
	}
	
	this.Deselect = function()
	{
		document.getElementById( this.m_szScrollBarId ).style.background = "transparent";
	}
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/*  setters */
	
	this.setDD = function( objDD )
	{
		this.m_objDD = objDD;
	}
	
	this.setSelectColor = function( szColor )
	{
		this.m_szSelectColor = szColor;
	}
	
	this.setSliderHeight = function( nHeight )
	{
		this.m_nSliderHeight = nHeight;
	}
	
	this.setSliderPos = function( nPos )
	{
		this.m_nSliderPos = nPos;
	}
	
	this.setSliderByRatio = function( nRatio )
	{
		this.setSliderPos( Math.floor( nRatio * this.getDragableHeight() ) );
	}
	
	this.setDragableHeight = function( nHeight )
	{
		this.m_nDragableHeight = nHeight;
	}
	
	this.setSliderTop = function( nTop )
	{
		this.m_nSliderTop = nTop;
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////////
	/* getters */
	
	this.getScrollBarOuterHeight = function()
	{
		return this.m_nScrollBarOuterHeight;
	}
	
	this.getScrollBarHeight = function()
	{
		return this.m_nScrollBarHeight;
	}
	
	this.getSliderHeight = function()
	{
		return this.m_nSliderHeight;
	}
	
	this.getDragableHeight = function()
	{
		return this.getScrollBarHeight() - this.getSliderHeight();
	}
	
	this.getSliderPos = function()
	{
		return this.m_nSliderPos;
	}
	
	this.getScrollBarId = function()
	{
		return this.m_szScrollBarId;
	}
	
	this.getSliderImgId = function()
	{
		return this.m_szSliderImgId;
	}
	
	this.getArrowUpId = function()
	{
		return this.m_szArrowUpId;
	}
	
	this.getArrowDownId = function()
	{
		return this.m_szArrowDownId;
	}
	
	this.getDragableHeight = function()
	{
		return this.m_nDragableHeight;
	}
	
	this.getSliderTop = function()
	{
		return this.m_nSliderTop;
	}
}

/**
 *	class:	CScrolling
 *	author: Hans-Jürgen Hartl, 04.2005
 *	
 */
function CScrolling( objParent )
{
	this.m_objParent = objParent;
	this.m_objContent = null;
	this.m_objScrollBar = null;
	this.m_fStop = false;
	this.m_nSpeed = 20;
	this.m_fTop = true;
	this.m_fBottom = false;
	this.m_szId = null;
	
	this.init = function( szScrollArea )
	{
		this.m_objContent.init( this.m_objParent );
		this.m_objScrollBar.init( this.m_objParent );
		var fScrollable = false;
		if( this.isScrollable() )
		{
			this.setId( szScrollArea );
			var objContent = this.getContent();
			var objScrollBar = this.getScrollBar();
			
			var nSliderHeight = Math.floor( ( objContent.getClipHeight() / objContent.getContentHeight() ) * objScrollBar.getScrollBarHeight() );
			
			this.m_objScrollBar.setSliderHeight( nSliderHeight );
			this.m_objScrollBar.setDragableHeight( objScrollBar.getScrollBarHeight() - nSliderHeight );
			
			var nScrollBarHeight = objScrollBar.getScrollBarHeight();
			var scrollDiff = Math.floor( ( nScrollBarHeight - nSliderHeight )  );
			var szSliderImg = this.m_objScrollBar.getSliderImgId();
			var fScrollable = true;
		}
		return {DHTML: szSliderImg+MAXOFFTOP+0+MAXOFFBOTTOM+scrollDiff+VERTICAL+NO_ALT, SCROLLABLE:fScrollable};
	}
	
	this.activate = function()
	{
		if( this.isScrollable() )
		{
			// make slider dragable
			var objSlider = eval( "this.m_objParent.dd.elements."+ this.m_objScrollBar.getSliderImgId() );
			objSlider.setId( this.getId() );
			
			this.m_objSlider = objSlider;
			this.m_objSlider.setZ( 1000, null );
			this.m_objScrollBar.setSliderTop( objSlider.y  );
			
			this.m_objScrollBar.setDD( this.m_objParent.dd );
			this.m_objScrollBar.draw();
			
			var ua = navigator.userAgent.toLowerCase()
			netscape = (ua.indexOf("mozilla")!=-1 && ua.indexOf("compatible")==-1 && ua.indexOf("opera")==-1)
			ie = (ua.indexOf("msie")>-1)
			
			var szContext = this.getId();
			// register events
			// scroll bar click
			switch( szContext )
			{
					case "main":
					{
						if(ie)
						{
							this.m_objParent.document.getElementById( this.getContent().getInnerClipId() ).onmousewheel = mouseWheelTurned;
						}
						
						this.m_objParent.document.getElementById( this.getScrollBar().getScrollBarId() ).onclick = scrollBarClicked;
						// arrowUp
						this.m_objParent.document.getElementById( this.getScrollBar().getArrowUpId() ).onmousedown = arrowUpMouseDown;
						// arrowDown
						this.m_objParent.document.getElementById( this.getScrollBar().getArrowDownId() ).onmousedown = arrowDownMouseDown;
						
						this.m_objParent.document.getElementById( this.getScrollBar().getScrollBarId() ).onmouseover = scrollBarSelect;
						this.m_objParent.document.getElementById( this.getScrollBar().getScrollBarId() ).onmouseout = scrollBarDeSelect;
						this.m_objParent.document.getElementById( objScrolling.getScrollBar().getArrowUpId() ).onmouseup = stopScrolling;
						this.m_objParent.document.getElementById( objScrolling.getScrollBar().getArrowUpId() ).onmouseout = stopScrolling;
						this.m_objParent.document.getElementById( objScrolling.getScrollBar().getArrowDownId() ).onmouseup = stopScrolling;
						this.m_objParent.document.getElementById( objScrolling.getScrollBar().getArrowDownId() ).onmouseout = stopScrolling;
					}
					break;
					case "categories":
					{
						if(ie)
						{
							this.m_objParent.document.getElementById( this.getContent().getInnerClipId() ).onmousewheel = mouseWheelTurned_left;
						}
						this.m_objParent.document.getElementById( this.getScrollBar().getScrollBarId() ).onclick = scrollBarClicked_left;
						// arrowUp
						this.m_objParent.document.getElementById( this.getScrollBar().getArrowUpId() ).onmousedown = arrowUpMouseDown_left;
						// arrowDown
						this.m_objParent.document.getElementById( this.getScrollBar().getArrowDownId() ).onmousedown = arrowDownMouseDown_left;
						
						this.m_objParent.document.getElementById( this.getScrollBar().getScrollBarId() ).onmouseover = scrollBarSelect_left;
						this.m_objParent.document.getElementById( this.getScrollBar().getScrollBarId() ).onmouseout = scrollBarDeSelect_left;
						this.m_objParent.document.getElementById( objScrolling_left.getScrollBar().getArrowUpId() ).onmouseup = stopScrolling_left;
						this.m_objParent.document.getElementById( objScrolling_left.getScrollBar().getArrowUpId() ).onmouseout = stopScrolling_left;
						this.m_objParent.document.getElementById( objScrolling_left.getScrollBar().getArrowDownId() ).onmouseup = stopScrolling_left;
						this.m_objParent.document.getElementById( objScrolling_left.getScrollBar().getArrowDownId() ).onmouseout = stopScrolling_left;
					}
					break;
			}
			return true;
		}
		else
		{
			return false;
		}
	}

	this.setId = function( szId )
	{
		this.m_szId = szId;
	}
	
	this.getId = function()
	{
		return this.m_szId;
	}
	
	this.setContent = function( objContent )
	{
		this.m_objContent = objContent;
	}
	
	this.setScrollBar = function( objScrollBar )
	{
		this.m_objScrollBar = objScrollBar;
	}
	
	this.getContent = function()
	{
		return this.m_objContent;
	}
	
	this.getScrollBar = function()
	{
		return this.m_objScrollBar;
	}
	
	this.isScrollable = function()
	{
		if( this.m_objContent.getScrollableHeight() > 0 )
		{
			return true;
		}
		return false;
	}
	
	this.getSpeed = function()
	{
		return this.m_nSpeed;
	}
	
	this.setSpeed = function( nSpeed )
	{
		this.m_nSpeed = nSpeed;
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////////
	/* event processing methods */
	this.scrollBarClicked = function( nClickY )
	{
		var nRealSliderY = this.m_objScrollBar.getSliderTop() + this.m_objScrollBar.getSliderPos();
		if( nClickY > nRealSliderY)
		{
			this.scrollContentBy( -this.m_objContent.getClipHeight() );
		}
		else
		{
			this.scrollContentBy( this.m_objContent.getClipHeight() );
		}
	}
	
	this.dragSlider = function()
	{
		if( this.isScrollable() )
		{
			var nSliderY = eval( "this.m_objScrollBar.m_objDD.elements." + this.getScrollBar().getSliderImgId() + ".y" );
			var nSliderPos = nSliderY - this.m_objScrollBar.getSliderTop();
			var nSliderRatio = nSliderPos/ this.m_objScrollBar.getDragableHeight();
			var nScroll = (nSliderRatio * this.m_objContent.getScrollableHeight());
			this.scrollContentTo( nScroll * (-1) );
			
			this.top( false );
			this.bottom( false );
			
			if( nSliderRatio == 0 )
			{
				this.top( true );
			}
			
			if( nSliderRatio == 1 )
			{
				this.bottom( true );
			}
		}
	}
	
	this.mouseWheelTurned = function( nDelta )
	{
		this.scrollContentBy( nDelta );
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////////
	/* scrolling methods */
	
	this.scrollContentTo = function( nScrollTo )
	{
		this.m_objContent.setContentPosY( nScrollTo );
		this.m_objScrollBar.setSliderByRatio( Math.abs(nScrollTo)/this.getContent().getScrollableHeight() );
	}
	
	this.scrollContentBy = function ( nDelta )
	{
		var nActContentPos = this.getContent().getContentPosY();
		//alert(nActContentPos);
		var nMaxScroll = nActContentPos + nDelta;
		var nScroll = nMaxScroll;
		var fScroll = true;

		this.top( false );
		this.bottom( false );
		if( ( nMaxScroll < (this.getContent().getScrollableHeight() *(-1) )) && ( nDelta < 0 ) )
		{
			nScroll = this.getContent().getScrollableHeight() * (-1);
			fScroll = false;
			this.bottom( true );
		}
		if( ( nMaxScroll > 0 ) &&  (nDelta > 0 ))
		{
			nScroll = 0;
			fScroll = false;
			this.top( true );
		}
		
		this.scrollContentTo( nScroll );
		this.m_objScrollBar.refresh();
		return { success:fScroll, pixels:nScroll};
	}
	
	this.scrollUp = function( nPixels )
	{
		if(!this.scrollContentBy( -nPixels ).success)
		{
			this.stop();
			this.top( true );
		}
	}
	
	this.scrollDown = function( nPixels )
	{
		if(!this.scrollContentBy( nPixels ).success)
		{
			this.stop();
			this.bottom( true );
		}
	}
	
	this.top = function( fTop )
	{
		this.m_fTop = fTop;
	}
	
	this.bottom = function( fBottom )
	{
		this.m_fBottom = fBottom;
	}
	
	this.isTop = function()
	{
		return this.m_fTop;
	}
	
	this.isBottom = function()
	{
		return this.m_fBottom;
	}
	
	this.scrollUpByArrow = function()
	{
		actContext = this.getId();
		this.m_objInterval = this.m_objParent.setInterval( "scrollUp()", 50 );
	}
	
	this.scrollDownByArrow = function()
	{
		actContext = this.getId();
		this.m_objInterval = this.m_objParent.setInterval( "scrollDown()", 50 );
	}
	
	this.stop = function()
	{
		this.m_fStop = true;
		this.m_objParent.clearInterval( this.m_objInterval );
		this.m_objInterval = null;
	}
	
	this.scrollBarVisible = function( fVisible )
	{
		var szScrollbarTableId = this.getScrollBar().m_szScrollBarDivId;
		switch( fVisible )
		{
			case true:
				document.getElementById( szScrollbarTableId ).style.visiblity = "visible";
			break;
			case false:
				document.getElementById( szScrollbarTableId ).style.visibility = "hidden";
			break;
		}
	}
}

// mouse wheel event (explorer only)
function mouseWheelTurned(e)
{	
	var evt = e||window.event;
	var nDelta = evt.wheelDelta;
	objScrolling.mouseWheelTurned( nDelta );
}

function scrollBarClicked( e  )
{
	var evt = e||window.event;
	var nClickY = evt.clientY;
	objScrolling.scrollBarClicked( nClickY );
}

function arrowUpMouseDown( e )
{
	objScrolling.scrollDownByArrow();
}

function arrowDownMouseDown( e )
{
	objScrolling.scrollUpByArrow();
}

function stopScrolling()
{
	objScrolling.stop();
}

function scrollUp()
{
	switch( actContext )
	{
		case "main":
		{
			objScrolling.scrollUp( objScrolling.getSpeed() );
		}
		break;
		case "categories":
		{
			objScrolling_left.scrollUp( objScrolling_left.getSpeed() );
		}
		break;
	}
}

function scrollDown()
{
	switch( actContext )
	{
		case "main":
		{
			objScrolling.scrollDown( objScrolling.getSpeed() );
		}
		break;
		case "categories":
		{
			objScrolling_left.scrollDown( objScrolling_left.getSpeed() );
		}
		break;
	}
}

function my_DragFunc( szId )
{
	switch( szId )
	{
			case "main":
			{
				objScrolling.dragSlider();
				objScrolling.getScrollBar().Select();
			}
			break;
			case "categories":
			{
				my_DragFunc_left();
			}
			break;
	}
}
	
function my_DropFunc( szId )
{
	switch( szId )
	{
			case "main":
			{
				objScrolling.getScrollBar().Deselect();
			}
			break;
			case "categories":
			{
				my_DropFunc_left();
			}
			break;
	}
}

function scrollBarSelect()
{
	objScrolling.getScrollBar().Select();
}

function scrollBarDeSelect()
{
		objScrolling.getScrollBar().Deselect();
}

// categories calls
// mouse wheel event (explorer only)
function mouseWheelTurned_left(e)
{	
	var evt = e||window.event;
	var nDelta = evt.wheelDelta;
	objScrolling_left.mouseWheelTurned( nDelta );
}

function scrollBarClicked_left( e  )
{
	var evt = e||window.event;
	var nClickY = evt.clientY;
	objScrolling_left.scrollBarClicked( nClickY );
}

function arrowUpMouseDown_left( e )
{
	objScrolling_left.scrollDownByArrow();
}

function arrowDownMouseDown_left( e )
{
	objScrolling_left.scrollUpByArrow();
}

function stopScrolling_left()
{
	objScrolling_left.stop();
}

function scrollUp_left()
{
	objScrolling_left.scrollUp( objScrolling_left.getSpeed() );
}

function scrollDown_left()
{
	objScrolling_left.scrollDown( objScrolling_left.getSpeed() );
}

function my_DragFunc_left()
{
	objScrolling_left.dragSlider();
	objScrolling_left.getScrollBar().Select();
}
	
function my_DropFunc_left()
{
	objScrolling_left.getScrollBar().Deselect();
}

function scrollBarSelect_left()
{
	objScrolling_left.getScrollBar().Select();
}

function scrollBarDeSelect_left()
{
		objScrolling_left.getScrollBar().Deselect();
}