﻿function PhScrollBar()
{
	this.m_oTrack = null;
	this.m_oThumb = null;
	
	this.m_oScrollPanel = null;
	
	this.m_bIsScrolling = false;
	this.m_bEnabled		= true;
	this.m_iMinValue	= 0;
	this.m_iMaxValue	= 0;
	this.m_iPanelHeight	= 0;
	this.m_iFrameHeight	= 0;
	this.m_iYPos		= 0;

	this.InitObject();
}

PhScrollBar.prototype = new PhControl();

PhScrollBar.prototype.AttachToElement = function( parent, insert_after_elm )
{
	if( this.m_oTrack == null )
	{
		alert( 'scrollbar not yet created' );
		return;
	}
	
	this.m_oParent = (typeof( parent ) == 'string')? document.getElementById( parent ) : parent;

	if( this.m_oParent != null )
	{
		this.m_oParent.appendChild( this.m_oTrack );
		this.m_oParent.appendChild( this.m_oThumb );
	}
	else
		alert( 'failed to find parent object ('+parent+')' );
}

PhScrollBar.prototype.InitObject = function()
{
	var me = this;
	
	var t1 = new Image(); t1.src = 'controls/scroll_bar/images/thumb_norm.png';
	var t2 = new Image(); t2.src = 'controls/scroll_bar/images/thumb_over.png';
	var t3 = new Image(); t3.src = 'controls/scroll_bar/images/thumb_down.png';
	
	this.m_oTrack = document.createElement( 'div' );
	this.m_oThumb = document.createElement( 'div' );
	
	this.m_oTrack.style.backgroundImage = 'url( controls/scroll_bar/images/track.png )';
	this.m_oTrack.style.width = '8px';
	this.m_oTrack.style.height = '384px';
	this.m_oTrack.style.position = 'absolute';
	this.m_oTrack.className = 'png';
	this.m_oTrack.style.zIndex = 10;
	
	this.m_oThumb.style.backgroundImage = 'url( controls/scroll_bar/images/thumb_norm.png )';	
	this.m_oThumb.style.width = '30px';
	this.m_oThumb.style.height = '32px';
	this.m_oThumb.style.position = 'absolute';
	this.m_oThumb.style.cursor = 'pointer';
	this.m_oThumb.className = 'png';
	this.m_oThumb.style.zIndex = 11;
	
	this.m_oThumb.onmousedown	= function(e){ me.MouseDown( (e)?e:event ); }
	this.m_oThumb.onmouseup		= function(e){ me.MouseUp(); }
	this.m_oThumb.onmouseover	= function(e){ me.MouseOver(); }
	this.m_oThumb.onmouseout	= function(e){ me.MouseOut( (e)?e:event ); }
	
	g_oClient.DisableSelection( this.m_oTrack );
	g_oClient.DisableSelection( this.m_oThumb );
}

PhScrollBar.prototype.SetPosition = function( x, y )
{
	this.m_iYPos				= y-10;
	
	this.m_oTrack.style.left	= x-4+'px';
	this.m_oTrack.style.top		= y+'px';
		
	this.m_iMinValue			= y-10;
	this.m_iMaxValue			= y+384-32+10;
	
	this.m_oThumb.style.left	= x-15+'px';
	this.m_oThumb.style.top		= this.m_iMinValue+'px';
}

PhScrollBar.prototype.SetScrollPanel = function( scroll_panel )
{
	this.m_oScrollPanel = scroll_panel;
}

PhScrollBar.prototype.MouseDown = function( evt )
{
	var me = this;
	
	this.m_oThumb.style.backgroundImage = 'url( controls/scroll_bar/images/thumb_down.png )';
		
	document.body.onmousemove	= function(e){ me.Scroll( (e)?e:event ); };
	document.body.onmouseup		= function(e){ me.EndScroll( (e)?e:event ); };
}

PhScrollBar.prototype.MouseUp = function()
{
	this.m_oThumb.style.backgroundImage = 'url( controls/scroll_bar/images/thumb_over.png )';
}

PhScrollBar.prototype.MouseOver = function()
{
	if( !this.m_bIsScrolling )
		this.m_oThumb.style.backgroundImage = 'url( controls/scroll_bar/images/thumb_over.png )';
}

PhScrollBar.prototype.MouseOut = function( evt )
{
	if( !this.m_bIsScrolling )
		this.m_oThumb.style.backgroundImage = 'url( controls/scroll_bar/images/thumb_norm.png )';
}

PhScrollBar.prototype.Scroll = function( evt )
{
	var pos = null;
	var val = 0;
	
	if( g_oClient.CheckLeftMouseButton( evt ) )
	{
		pos = g_oClient.GetCursorPosition( evt, this.m_oParent );
		val = pos.Y-16;
		
		if( val < this.m_iMinValue )
			val = this.m_iMinValue;
		else if( val > this.m_iMaxValue )
			val = this.m_iMaxValue;
			
		if( this.m_bEnabled && this.m_oScrollPanel != null )
		{
			var d1 = this.m_iPanelHeight-this.m_iFrameHeight;
			var d2 = this.m_iMaxValue-this.m_iMinValue;
			var offset	= ((val-this.m_iYPos)/d2)*d1;
			
			this.m_oScrollPanel.style.top = -offset+'px';
		}
			
		this.m_oThumb.style.top = val+'px';
		this.m_bIsScrolling		= true;
		
		evt.cancelBubble = true;
	}
	else
		document.body.onmouseup( null );
}

PhScrollBar.prototype.EndScroll = function( evt )
{
	document.body.onmousemove	= null;
	document.body.onmouseup		= null;
	document.body.onmouseout	= null;
	
	this.m_oThumb.style.backgroundImage = 'url( controls/scroll_bar/images/thumb_norm.png )';
	this.m_bIsScrolling = false;
}

PhScrollBar.prototype.Enable = function()
{
	this.m_oThumb.style.visibility = 'visible';
	this.m_bEnabled = true;
}

PhScrollBar.prototype.Disable = function()
{
	this.m_bEnabled = false;
	this.m_oThumb.style.visibility = 'hidden';
}

PhScrollBar.prototype.Update = function()
{
	if( this.m_oScrollPanel != null )
	{
		this.m_iPanelHeight = this.m_oScrollPanel.clientHeight;
		this.m_iFrameHeight = this.m_oParent.clientHeight;
		
		if( this.m_iPanelHeight < this.m_iFrameHeight )
			this.Disable();
		else
			this.Enable();
		
		this.m_oScrollPanel.style.top	= '0px';
		this.m_oThumb.style.top			= this.m_iMinValue+'px';
	}
}