(function(){

/*
	Class: SliderBar 
	The base class for a collection of navigation components 
*/
function SliderBar()
{
	this.contents = new window['SB']['util']['List'];
	this.htmlObj = null;
	this.spacer = null;
	this.newButtonHtmlObj = null;
	
	//SButil.EventManager.addListener("BoxDeleted", onBoxDeleted, this);
	//SButil.EventManager.addListener("ContainerDeleted", onContainerDeleted, this);
}

SliderBar.prototype.toString = function() { return "[SB.ui.SliderBar]"; }

//Group: Basic Methods 
/*
	Function: setConfig
	Establishes the basic setup for the SliderBar.
	
	inConfig - A configuration array (key/value)

	Configuration Array Options:
	id - the id of the div to use as a template (default:none)
	content_cl - the css class name for the content area (default:none)
	page_left_cl - the css class name for the left pager (default:none)
	page_right_cl - the css class name for the right pager (default:none)
*/
SliderBar.prototype.setConfig = function( inConfig )
{
	if( inConfig.id )
		this.htmlObj = YUIDom.get( inConfig.id );

	if( inConfig.spacer )
		this.spacer = inConfig.spacer;
	
	//Page left
	var arr = YUIDom.getElementsByClassName( "page_left", "div", this.htmlObj );
	if( arr[0] ) 
	{ 	
		this.pageLeftHtmlObj = arr[0];
		this.pageLeftHtmlObj.style.visibility = "hidden";	
		YUIEvent.addListener(this.pageLeftHtmlObj, 'click', this.pageLeft, this, true);
	}

	//Create content obj
	var arr = YUIDom.getElementsByClassName( "barcontent", "div", this.htmlObj );
	if( arr[0] ) 
	{ 	
		this.contentHtmlObj = arr[0];
		this.contentHtmlObj.style.width = "5px";
	
			//Making space on the left for the pager
		var leftOffset = (this.pageLeftHtmlObj.offsetLeft + this.pageLeftHtmlObj.offsetWidth) + 3;
		this.contentHtmlObj.style.marginLeft = leftOffset + "px";
		this.contentHtmlObj.setAttribute( "leftOffset", leftOffset );
	}

	//Page right
	var arr = YUIDom.getElementsByClassName( "page_right", "div", this.htmlObj );
	if( arr[0] ) 
	{ 	
		this.pageRightHtmlObj = arr[0];
		this.pageRightHtmlObj.style.visibility = "hidden";	
		YUIEvent.addListener(this.pageRightHtmlObj, 'click', this.pageRight, this, true);
	}

	//New Button
	var arr = YUIDom.getElementsByClassName( "new_button", "div", this.htmlObj );
	if( arr[0] ) 
	{ 	
		this.newButtonHtmlObj = arr[0];
	}
	//Add dynamic sizing
	YUIEvent.addListener( window, "resize", this.resize, this, true );	
	


};

/*
	Function: resize 
	Automatically adjusts bar to screen width
*/
SliderBar.prototype.resize = function()
{
	this.htmlObj.style.width = YUIDom.getViewportWidth() - 75 +"px";	

	if( (this.contentHtmlObj.offsetWidth + parseInt(this.contentHtmlObj.style.marginLeft)) > parseInt(this.htmlObj.style.width) )
		this.pageRightHtmlObj.style.visibility="visible";
	else
		this.pageRightHtmlObj.style.visibility="hidden";
};	


/*
	Function: addItem
	A simple method to add a <BarItem> into the Bar
*/
SliderBar.prototype.addItem = function( inItem )
{
	this.contents.append( inItem ); 

	if( this.newButtonHtmlObj != null )
	{
		if( this.contentHtmlObj.offsetWidth < this.newButtonHtmlObj.offsetWidth )
			this.contentHtmlObj.style.width = this.newButtonHtmlObj.offsetWidth + 8 + "px";

		this.contentHtmlObj.insertBefore( inItem.getHtmlObj(), this.newButtonHtmlObj ); 
		var oWidth = this.contentHtmlObj.offsetWidth + inItem.getWidth() + "px";
		this.contentHtmlObj.style.width = oWidth;	

		if( parseInt(oWidth) > YUIDom.getViewportWidth() )
			this.pageRightHtmlObj.style.visibility = "visible";	
		
		if( inItem.attach ) inItem.attach();

		inItem.bar = this;
	}
	else
	{
		this.contentHtmlObj.appendChild( inItem.getHtmlObj() ); 
		var oWidth = this.contentHtmlObj.offsetWidth + inItem.getWidth() + "px";
		this.contentHtmlObj.style.width = oWidth;	
	}

};	

/*
	Function: addItemToFront
	A simple method to add a <BarItem> into the front of the Bar
*/
SliderBar.prototype.addItemToFront = function( inItem )
{
	this.contents.insertAt( inItem, 0 ); 
	this.contentHtmlObj.insertBefore( inItem.getHtmlObj(), this.contentHtmlObj.firstChild ); 
	var oWidth = this.contentHtmlObj.offsetWidth + inItem.getWidth() + "px";
	this.contentHtmlObj.style.width = oWidth;	

	if( inItem.attach ) inItem.attach();
	
	inItem.bar = this;
};

/*
	Function: removeItemInternal
	A simple method to remove a <BarItem> from the Bar
*/
SliderBar.prototype.removeItemInternal = function( inItem )
{
	this.contents.remove( inItem );
};	


/*
	Function: removeItem 
	A simple method to remove a <BarItem> from the Bar
*/
SliderBar.prototype.removeItem = function( inItem )
{
	this.contents.remove( inItem );
	if( inItem.getHtmlObj() && inItem.getHtmlObj().parentNode )
		inItem.getHtmlObj().parentNode.removeChild( inItem.getHtmlObj() );
};	

/*
	Function: removeItemsAfter 
	A simple method to remove a <BarItem> from the Bar
*/
SliderBar.prototype.removeItemsAfter = function( inItem )
{

	var index = this.contents.find( inItem );
	if( index >= 0 )
	{
		this.contents.removeAfter( index );
		var children = YUIDom.getChildren( this.htmlObj );	

		var removeNow = false;	

		//Get rid of everything after inItem
		for( el in children )
		{
			if( children[el] == this.newButtonHtmlObj ) 
				continue; 

			if( removeNow )
				this.contentHtmlObj.removeChild( children[el] ); 

			if( children[el] == inItem.getHtmlObj() )
				removeNow = true;					
		}
	}
};	

/*
	Function: removeAllItems
	A simple method to remove all <BarItem> from the Bar
*/
SliderBar.prototype.removeAllItems = function( )
{
	this.pageRightHtmlObj.style.visibility = "hidden";	
	this.pageLeftHtmlObj.style.visibility = "hidden";	

	this.contentHtmlObj.style.width = "0px";	

	for( var i = 0; i < this.contents.size(); i++ )
	{
		this.removeItemInternal( this.contents.get(i) );
	}	

	var arr = YUIDom.getChildren( this.contentHtmlObj );
	for( el in arr )
	{
		if( arr[el] != this.newButtonHtmlObj )
		{
			if( arr[el].id == "sb_ui_baritem_box" )
				this.contentHtmlObj.removeChild( arr[el] );
		}
	}

};	

/*
	Function: switchItems 
	A simple method to remove all <BarItem> from the Bar and add all items from inList
*/
SliderBar.prototype.switchItems = function( inList )
{
	this.removeAllItems();

	var i = 0;
	for( ; i < inList.length; i++ )
	{
	}
};

/*
	Function: setActive 
	Trivial function for indicating whether the bar has valid contents or not.  This will also toggle the display of the new button.

	inVal - A boolean value
*/
SliderBar.prototype.setActive = function( inVal )
{
	this.is_active = inVal;

	if( this.newButtonHtmlObj )
		this.newButtonHtmlObj.style.display = (inVal)?"block":"none";
};

//Group: Delegate Methods 
/*
	Function: addItemTransition 
	A stub function which is called just after <addItem> is called
*/
SliderBar.prototype.addItemTransition = function()
{
};

/*
	Function: removeItemTransition 
	A stub function which is called just after <removeItem> or <removeAllItems> is called
*/
SliderBar.prototype.removeItemTransition = function()
{
};

/*
	Function: pageLeft 
	Shifts the contents of the bar to the left. 
	Default is by one box step.	
*/
SliderBar.prototype.pageLeft = function( inBar )
{
	var cOffset = parseInt(this.contentHtmlObj.style.marginLeft);
	if(cOffset >= 0) 
	{
		this.pageLeftHtmlObj.style.visibility = "hidden";	
		return;
	}

	var leftOffset = (this.pageLeftHtmlObj.offsetLeft + this.pageLeftHtmlObj.offsetWidth) + 3;
	var rightOffset = this.pageRightHtmlObj.offsetLeft;
	var boxWidth = (this.newButtonHtmlObj.offsetWidth + 8);
	
	var pageSize = rightOffset - leftOffset;
	var chankSize = (this.newButtonHtmlObj.offsetLeft+boxWidth) - leftOffset;

	var cnt = Math.floor( pageSize / boxWidth );	
	var moveTo = (cOffset + (boxWidth*cnt));
	if( moveTo > leftOffset ) moveTo = leftOffset;

	if( 2*(moveTo) >= 0 )
		this.pageLeftHtmlObj.style.visibility = "hidden";	

	this.pageRightHtmlObj.style.visibility = "visible";	
	//this.contentHtmlObj.style.marginLeft = moveTo +"px";

	var attributes = { marginLeft:{ to:moveTo} };	
	var anim = new YUIAnim( this.contentHtmlObj, attributes, 0.2, YUIEasing.easeIn);
	anim.animate();

};	

/*
	Function: pageRight
	Shifts the contents of the bar to the right	
	Default is by one box step	
*/
SliderBar.prototype.pageRight = function( inBar ) 
{
	
	var cOffset = parseInt(this.contentHtmlObj.style.marginLeft);
	var leftOffset = (this.pageLeftHtmlObj.offsetLeft + this.pageLeftHtmlObj.offsetWidth) + 3;
	var rightOffset = this.pageRightHtmlObj.offsetLeft;
	var boxWidth = (this.newButtonHtmlObj.offsetWidth + 8);
	
	var pageSize = rightOffset - leftOffset;
	var chankSize = (this.newButtonHtmlObj.offsetLeft+boxWidth) - leftOffset;

	var cnt = Math.floor( pageSize / boxWidth );	
	var moveTo = (cOffset - (boxWidth*cnt));

	if( (-1*moveTo) > this.contentHtmlObj.offsetWidth ) 
	{
		this.pageRightHtmlObj.style.visibility = "hidden";	
		return;
	}

	if( 2*(-1*moveTo) > this.contentHtmlObj.offsetWidth )
		this.pageRightHtmlObj.style.visibility = "hidden";	

	this.pageLeftHtmlObj.style.visibility = "visible";	
	//this.contentHtmlObj.style.marginLeft = moveTo + "px";

	var attributes = { marginLeft:{ to:moveTo} };	
	var anim = new YUIAnim( this.contentHtmlObj, attributes, 0.2, YUIEasing.easeIn);
	anim.animate();


};	

/*
 * Process custom events
 */



window['SB']['ui']['SliderBar'] = SliderBar;

})();
