// mb controller

function MultiboxController(tabs, menuboxid, bottomid)
{
	this.tabs = tabs;
	this.menuboxId = menuboxid;
	this.bottomId = bottomid;
}

MultiboxController.newTab = function(contentId, bottomType)
{
	return {
		id: contentId,
		fx: bottomType
	};
}

MultiboxController.prototype.setClass = function(e, c)
{
	if( e.className !== undefined )
		e.className = c;
	else if( e.setAttribute )
		e.setAttribute( "class", c );
}

MultiboxController.prototype.getClass = function(e)
{
	if( e.className !== undefined )
		return e.className;
	else if( e.getAttribute )
		return e.getAttribute( "class" );

	return false;
}

MultiboxController.prototype.open = function(i)
{
	for( var j = 0; j < this.tabs.length; j ++ ) {
		var element = document.getElementById( this.tabs[j].id );
		if( !element ) continue;
		if( j == i ) {
			element.style.display = "block";
			
			if( this.bottom ) {
				this.setClass( this.bottom, this.tabs[j].fx );
			}
		} else {
			element.style.display = "none";
		}
	}

	var ll = this.menubox.getElementsByTagName( "A" );
	
	for( var j = 0; j < ll.length; j ++ ) {
		var cc = this.getClass( ll[j] );
		var x = cc.indexOf( "_" );
		
		if( x > 0 ) {
			var ccbase = cc.substr( 0, x );
			cc = ccbase;
		}
		
		var selected = false;
		var addr = "";

        if( ll[j].href !== undefined )
			addr = ll[j].href;
        else if( ll[j].getAttribute )
            addr = ll[j].getAttribute( "href" );
            
        var y;

        x = addr.lastIndexOf("(");
        y = addr.lastIndexOf(")");
        
        if( (x > 0) && (y > 0) ) {
        	var xx = addr.substr( x+1, (y-x)-1 );
        	if( xx == i )
        		selected = true;
        }

		if( selected ) {
			this.setClass( ll[j], cc + "_selected" );
		} else {
			this.setClass( ll[j], cc );
		}
	}
}

MultiboxController.prototype.init = function()
{
	this.menubox = document.getElementById( this.menuboxId );
	this.bottom = document.getElementById( this.bottomId );
}

