/***Copyright and coded by Dakusan, please do not steal, but you may study and learn... - See http://www.castledragmire.com/Copyright for more information. ***/
//Force an element to constrain to a certain height
var ScrollInt=0, ScrollStartY, MouseState=false, MouseY;
function MinimizeBox(El, FinalHeight, TheHtml, TheAction)
{
	//Try inserting the HTML and see if it can exist without already overlapping, and if so, leave as is
	if(TheHtml==undefined)
		TheHtml=El.innerHTML;
	else if(El.innerHTML!=TheHtml) //Only set if not already equal - to save on some browser parsing time
		El.innerHTML=TheHtml;
	if(El.scrollHeight<=FinalHeight+2)
		if(!(TheAction instanceof Array))
			return false;
		else
			FinalHeight=El.scrollHeight;
		
	$CP(El, {'style.height':FinalHeight+'px', 'style.overflow':'hidden'}); //Set our main div to show the maximum amount of data

	//If a simple popup, only a few properties to set	
	if(TheAction=='PopupBox' || TheAction=='PopupBoxMove')
		return CreatePB(El, TheAction, TheHtml, El.offsetWidth);

	//Otherwise we need to do complex actions
	var MyOs=[El], HiddenDiv=$CE('DIV', {'style.position':'absolute'}); //An absolutly positioned div to store any needed data [the 3 scroll objects (that are all relative)]
	if(TheAction=='Scrollers') //For custom scroll bar boxes
	{
		El.parentNode.insertBefore(HiddenDiv, El);
		//The scroll objects
		var Scrollers=['Up','Scroll','Down',0,-1,I(El.clientHeight)-16];
		for(var i=0;i<3;i++)
			MyOs[i+1]=$CE('DIV', {
				'style.backgroundImage':'url(layout/'+Scrollers[i]+'.png)',
				'style.position':'relative',
				'style.left':I(El.clientWidth)-8+'px',
				'style.top':Scrollers[i+3]+'px',
				'style.width':'7px',
				'style.height':(i==1?7:4)+'px',
				onmousedown:DynamicFunc('return ScrollBox(Type, Objs, event);', {Type:i, Objs:MyOs})
			}, HiddenDiv);
		//Scroll object boundary values
		MyOs.push(0, I(El.clientHeight)-16, MyOs[0].scrollHeight-MyOs[0].clientHeight);
		
		//Position the objects at top of content
		MouseState=ScrollInt=1;
		ScrollBox(0, MyOs);
		MouseState=ScrollInt=0;
		return false;
	}

	//For Open/ViewAll news boxes
	//TheAction=Array(Link, ExtraLeft, ExtraTop, NeedsView)
	El.insertBefore(HiddenDiv, El.firstChild);
	var MD=$CE('DIV', {className:'MinBoxOptions'}, HiddenDiv);
	$CE('A', {href:TheAction[0], innerHTML:'Open'}, MD);
	if(TheAction[3])
	{
		MD.appendChild(document.createTextNode(' | '));
		CreatePB($CE('SPAN', {innerHTML:'View All', 'style.fontWeight':'bold'}, MD), 'PopupBox', TheHtml, El.offsetWidth);
	}
	$CP(MD, {'style.left':El.offsetWidth-MD.offsetWidth+I(TheAction[1])+'px', 'style.top':El.offsetHeight-MD.offsetHeight+I(TheAction[2])+'px'});
	return false;
}

function ScrollBox(Type, D, event)
{
	//D=[ScrollingDiv/SD,UpButton/UB,ScrollerButton/SB,DownButton/DB,ScrollerStart/SS,ScrollerEnd/SE,ScrollSpace/SSp];
	var SD=0,UB=1,SB=2,DB=3,SS=4,SE=5,SSp=6;
	switch(Type)
	{
		case 0: //Requested to move either up or down
		case 2:
			if(ScrollInt==0)
			{
				MouseState=true;
				ScrollInt=setInterval(function() { ScrollBox(Type,D); }, 100);
			}
			if(!MouseState)
				break;
			D[SD].scrollTop=Math.max(Math.min(I(D[SD].scrollTop)+(Type==0 ? -5 : 5), D[SSp]), 0);
			D[SB].style.top=D[SS]+(I(D[SD].scrollTop)/D[SSp])*(D[SE]-D[SS])+'px';
			return false;
		case 1: //Prepare for moving scroller
			var E=(event==undefined?window.event:event);
			MouseState=true;
			ScrollStartY=I(E.clientY)-I(D[SB].style.top);
			ScrollInt=setInterval(function() { ScrollBox(3,D); }, 5);
			E.clientbubble=false;
			if(!IsIE)
			{
				E.stopPropagation();
				E.preventDefault();
			}
			else
				E.cancelBubble=true;
			return false;
		case 3: //Scrolling bar
			D[SB].style.top=Math.max(Math.min(I(MouseY)-ScrollStartY, D[SE]), D[SS])+'px';
			D[SD].scrollTop=I((I(D[SB].style.top)-D[SS])/(D[SE]-D[SS])*D[SSp]);
			if(MouseState)
				return false;
			break;
	}

	//Stop work
	clearInterval(ScrollInt);
	ScrollInt=0;
	return false;
}

document.onmouseup=function(event) { MouseState=false; };
document.onmousemove=function(event) { MouseY=(event==undefined?window.event:event).clientY; };
