/* Javascript March 1, 2010*/                             //////////////////////
 ////////////////////////////   N E W  L O O K	  ////////
//                          //////////////////////   	 //
//	BHScroll Version 0.40                  			     //
//  (c) Copyright 2006-2008 Howard Brown                // 
//      			                                   //
///////////////////////////////////////////////////////
function BHScroll(bh)
{
	bhUI.createObject('Scroll',this);
	this.work=0;
	this.width=0;
	this.height=0;
	this.defaultDuration=2;
	this.defaultStep=11;
	this.defaultDelay=20;
	this.vscroll={step:1,content:0,bar:0,button:0};
	this.hscroll={step:1,content:0,bar:0,button:0};
	var f,s,b,i,e,a,tb,tr,td,img,n,j;

	f='/newlook/theme/default/img/arrow/';
	b='/newlook/img/clear.gif';
	s=['Up','Down'];
	c=['clearTimeout(this.tmo);this.tmo=setTimeout("'+this.Tx+'.onButtonMove(',');",60);'];
	j='<img src="'+b+'" width="1" height="1" style="display:none;">';
	
	if(!bh.skin)bh.skin='';
	
	this.vscroll.bar=e=bhUI.createElement();
	e.className=bh.skin+'scrollbar-v';
	e.style.position='absolute';
	e.style.overflow='hidden';
	tb=e.addElement('TABLE');
	tb.cellPadding='1';
	tb.cellSpacing='2';
	tb=tb.addElement('TBODY');
	for(i=0;i<2;i++)
	{
		tr=tb.addElement('TR');
		a=tr.addElement('TD').addElement('A');
		a.onmousedown=new Function(this.Tx+".scroll"+s[i]+"(0);");
		a.onmouseup=new Function(this.Tx+".scrollCancel();");
		img=a.addElement('IMG');
		img.src=f+s[i].toLowerCase()+'.gif';
	}
	this.vscroll.button=e=e.addElement('IMG');
	e.src=b;
	e.className='button';
	e.style.position='absolute';
	e.style.width='5px';e.style.height='0px';
	e.style.top='24px';e.style.left='3px';
	e.bhondrag=new Function(c[0]+1+c[1]);
	e.bhondragstart=function(evt)
	{
		evt.bhdataTransfer.dragMode=1;
		var drag=evt.bhdataTransfer.drag;
		drag.parent=this.parentNode;	
		drag.left=3;drag.right=4;
		drag.top=24;drag.bottom=2;
	}
	
	this.hscroll.bar=e=bhUI.createElement();
	e.className=bh.skin+'scrollbar-h';
	e.style.position='absolute';e.style.overflow='hidden';
	tb=e.addElement('TABLE');tb.cellPadding='1';tb.cellSpacing='2';
	tb=tb.addElement('TBODY');tr=tb.addElement('TR');
	s=['Left','Right'];
	for(i=0;i < 2;i++)
	{	
		a=tr.addElement('TD').addElement('A');
		a.onmousedown=new Function(this.Tx+".scroll"+s[i]+"(0);");
		a.onmouseup=new Function(this.Tx+".scrollCancel();");
		img=a.addElement('IMG');
		img.src=f+s[i].toLowerCase()+'.gif';
	}
	this.hscroll.button=e=e.addElement('IMG');
	e.src=b;
	e.className='button';
	e.style.position='absolute';
	e.style.width='0px';e.style.height='5px';
	e.style.top='3px';e.style.left='24px';
	e.bhondrag=new Function(c[0]+0+c[1]);
	e.bhondragstart=function(evt)
	{
		evt.bhdataTransfer.dragMode=1;
		var drag=evt.bhdataTransfer.drag;
		drag.parent=this.parentNode;	
		drag.left=24;drag.right=2;
		drag.top=3;drag.bottom=4;
	}
	if(bh.id){
		bh.view=bhUI.element(bh.id+'-view');
		bh.content=bhUI.element(bh.id+'-content');
	}
    if(bh.view){
		bh.view.appendChild(this.vscroll.bar);
		bh.view.appendChild(this.hscroll.bar);
		bh.view.style.position='relative';
		bh.view.style.overflow='hidden';
		if(bh.content[0]){
			this.hscroll.content=bh.content[0];
			this.vscroll.content=bh.content[1];
		}else{
			this.hscroll.content=this.vscroll.content=bh.content;
		}
		this.vscroll.content.style.position='absolute';
	    this.vscroll.content.style.top='0px';
		this.vscroll.content.style.left='0px';
		this.hscroll.content.style.position='absolute';
	    this.hscroll.content.style.top='0px';
		this.hscroll.content.style.left='0px';
	}
	if(bh.flags){
		if(bh.flags&0x01)this.auto=true;
	}
	this.down=this.scrollDown;
	this.up=this.scrollUp;
	this.left=this.scrollLeft;
	this.right=this.scrollRight;
	this.content=this.vscroll.content;
}


BHScroll.prototype.redraw=function()
{
	var d,j,r,e,b,w,h;
	
	if(this.auto){
		j=this.vscroll.content;
		e=j.parentNode;
		d=(bhUI.charWidth+2);
		w=e.offsetWidth-d-2;
		h=e.offsetHeight-d-2;
		this.width=w+8;
		this.height=h+8;
		
		if((j.offsetWidth-w)<16){
			h=e.offsetHeight;
		}
		b=this.vscroll.bar;
		b.style.top='0px';
		b.style.left=w+'px';
		b.style.width=d+'px';
		b.style.height=(h>2?h-2:0)+'px';
		
		if((j.offsetHeight-h)<16){
			w=e.offsetWidth;
		}
		b=this.hscroll.bar;
		b.style.left='0px';
		b.style.top=h+'px';
		b.style.width=(w>2?w-2:0)+'px';
		b.style.height=d+'px';
			
	}
	e=this.vscroll;
	j=this.hscroll;
	d=e.bar.offsetHeight-30;
	h=((e.content.offsetHeight-this.height));
	e.step=8+parseInt(h/d);
	r=(d-parseInt(h/e.step));
	d=r<d?r:d;
	e.button.style.height=(d>0?d:0)+'px';
	
	d=j.bar.offsetWidth-30;
	w=((j.content.offsetWidth-this.width));
	j.step=8+parseInt(w/d);
	r=(d-parseInt(w/j.step));
	d=r<d?r:d;
	j.button.style.width=(d>0?d:0)+'px';

	if(this.auto)
	{	
		e.bar.style.visibility=h<16?'hidden':'inherit';
		j.bar.style.visibility=w<16?'hidden':'inherit';
	}
}

BHScroll.prototype.set=function(x,y)
{   
	var k;
	if(y){
		k=this.vscroll;y--;
		k.content.style.top=y+'px';
		k.button.style.top=24+((y/k.step))+'px';
	}
	if(x){
		k=this.hscroll;x--;
		k.content.style.left=x+'px';
		k.button.style.left=(24+(x/k.step))+'px';
	}
}

BHScroll.prototype.to=function(x,y,callback)
{
	var p,n;
	if(x)
	{	
		n=this.hscroll.step;x--;
		p=Math.round((~(parseInt(this.hscroll.content.style.left)-1))/n);
		x=Math.round(x/n);
		if(x!=p)
		{
			this.cancel=true;this.stop();
			if(x>p)	this.scrollRight(x-p,n);
			else this.scrollLeft(p-x,n);
		}
	}
	if(y)
	{
		n=this.hscroll.step;y--;
		p=Math.round((~(parseInt(this.vscroll.content.style.top)-1))/n);
		y=Math.round(y/n);
		if(y!=p)
		{	
			this.cancel=true;this.stop();
			if(y>p)	this.scrollDown(y-p,n);
			else this.scrollUp(p-y,n);
		}
	}
}


BHScroll.prototype.onButtonMove=function(button)
{
	var n,p,b;
	if(button)
	{
		b=this.vscroll;
		n=parseInt(b.button.style.top)-24;
		p=~(parseInt(b.content.style.top)-1)/b.step;
		if(n!=p)
		{
			this.cancel=true;this.stop();
			if(p>n)this.scrollUp(p-n,b.step,false);
			else this.scrollDown(n-p,b.step,false);
		}
	}
	else
	{
		b=this.hscroll;
		n=parseInt(b.button.style.left)-26;
		p=~(parseInt(b.content.style.left)-1)/b.step;
		if(n!=p)
		{
			this.cancel=true;this.stop();
			if(p>n)this.scrollLeft(p-n,b.step,false);
			else this.scrollRight(n-p,b.step,false);
		}
	}
}

BHScroll.prototype.scrollInit=function(a)
{
	if(a&&a.length>1)
	{
		this.duration=a[0];
		this.step=a[1];
		this.mode=a[2]===false?false:true;
		var n,d=Math.floor(60-((this.duration/this.step)));
		d=d>20?d:20;n=60-d;n+=(n%2)
		this.duration/=n;this.step*=n;this.delay=d;
		this.cancel=true;
	}
	else
	{
		this.duration=this.defaultDuration;
		this.step=this.defaultStep; 
		this.delay=this.defaultDelay;
		if(!this.work)
		{
			this.cancel=false;
			this.mode=true;
			this.count=0;
		}
		else
		{
			this.count+=4;
			this.delay-=this.count;
			if(this.delay<1)this.delay=1;
		}
	}
	this.vscroll.content.parentNode.scrollTop=0;  
	this.hscroll.content.parentNode.scrollTop=0;  
}

BHScroll.prototype.scrollCancel=function()
{
	this.cancel=true;	
}

BHScroll.prototype.stop=function()
{
	clearTimeout(this.work);
	this.duration=0;this.work=0;
	if(this.callback)
	{	var f=this.callback;
		this.callback=0;f(this.cancel);
	}
}

BHScroll.prototype.scrollDown=function()
{
	if(arguments.length){
		if(this.work)return;
		this.scrollInit(arguments);
	}
	else if(!this.work)return;		
	
	var top,e,n,vs=this.vscroll;
	e=vs.content;top=parseInt(e.style.top);
	n=(e.offsetHeight*(-1)+this.height);
	if(top>=n){
		e.style.top=((top-this.step)>n?(top-this.step):n)+'px';
		if(this.mode){
			vs.button.style.top=24+(~(top-1)/vs.step)+'px';
		}
		if(this.duration){
			this.duration--;
			if(this.duration<=0){
				if(this.cancel){
					this.stop();return;
				}
				this.scrollInit();
			}
		}
		this.work=setTimeout(this.Tx+'.scrollDown()',this.delay);
	}
	else this.stop();
}


BHScroll.prototype.scrollUp=function()
{	
	if(arguments.length)
	{
		if(this.work)return;
		this.scrollInit(arguments);
	}
	else if(!this.work)return;		
	
    var top,e,vs=this.vscroll;
	e=vs.content;
	top=parseInt(e.style.top);
	if(top<0)
	{		
		top+=this.step;
		e.style.top=(top<1?top:0)+'px';
		if(this.mode)
		{
			vs.button.style.top=24+(~(top-1)/vs.step)+'px';
		}
		if(this.duration)
		{
			this.duration--;
			if(this.duration<=0)
			{
				if(this.cancel)
				{
					this.stop();return;
				}
				this.scrollInit();	
			}
		}
		this.work=setTimeout(this.Tx+'.scrollUp()',this.delay);
   	}
	else this.stop();
}

BHScroll.prototype.scrollLeft=function()
{
	if(arguments.length)
	{
		if(this.work)return;
		this.scrollInit(arguments);
	}
	else if(!this.work)return;		
	
	var left,e,hs=this.hscroll;
	e=hs.content;
	left=parseInt(e.style.left);
	if(left<0)
	{
		left+=this.step;
		e.style.left=(left<1?left:0)+'px';
		if(this.mode)
		{
			hs.button.style.left=24+(~(left-1)/hs.step)+'px';
		}
		if(this.duration)
		{
			this.duration--;
			if(this.duration<=0)
			{
				if(this.cancel)
				{
					this.stop();return;
				}
				this.scrollInit();
			}
		}
		this.work=setTimeout(this.Tx+'.scrollLeft()',this.delay);
   	}
	else this.stop();
}

BHScroll.prototype.scrollRight=function()
{	
	if(arguments.length)
	{
		if(this.work)return;
		this.scrollInit(arguments);
	}
	else if(!this.work)return;
	
	var left,e,hs=this.hscroll;
	e=hs.content;  
	left=parseInt(e.style.left);
	if(left>=(e.offsetWidth*(-1)+this.width))
	{
		e.style.left=left-this.step+'px';
		if(this.mode)
		{
			hs.button.style.left=24+(~(left-1)/hs.step)+'px';
		}
		if(this.duration)
		{
			this.duration--;
			if(this.duration<=0)
			{
				if(this.cancel)
				{
					this.stop();return;
				}
				this.scrollInit();
			}
		}
		this.work=setTimeout(this.Tx+'.scrollRight()',this.delay);
	}
	else this.stop();
}

BHScroll.prototype.show=function()
{
	if(arguments[0]===false)
	{
		var h,v;
		h=this.hscroll;v=this.vscroll;
		h.content.parentNode.style.width=h.bar.parentNode.offsetWidth+'px';
		h.content.parentNode.style.height=v.content.parentNode.style.height=(this.height+v.bar.offsetHeight)+'px';
		h.bar.style.display=v.bar.style.display="none";
	}
}


BHScroll.prototype.showHBar=function()
{
	if(arguments[0]===false)
	{
		var h=this.hscroll;
		h.content.parentNode.style.width=h.bar.parentNode.offsetWidth+'px';
		h.bar.style.display="none";
	}
}

