//===============================[ToolBar Class]==============================\\


/**
 * Initializes the object.
 * @param name The name of the toolbar.
 * @param caption The caption of the toolbar.
 */
function ToolBar(name, caption)
{
	this.up = null;
	this.name = name;
	this.caption = caption;
	this.toolbars = new Array();
	this.buttons = new Array();
	this.click='';
	this.root=null;
}



ToolBar.root = new ToolBar("");



/*
 * Function: addButton
 * Adds the specified button to the root.
 */
ToolBar.addButton = function(arg1, arg2, arg3, arg4, arg5)
{
	return ToolBar.root.addButton(arg1, arg2, arg3, arg4, arg5);
}

/**
 * Add the specified toolbar to the root.
 * @param arg1 
 */
ToolBar.addToolBar = function(name, caption)
{
	return ToolBar.root.addToolBar(name, caption);
}


/*
 * Function: addToolBar
 * Adds the specified toolbar (as an object or properties) to the toolbar list.
 */
ToolBar.prototype.addToolBar = function(arg1, arg2)
{
	if (arg1 instanceof ToolBar)
	{
		this.toolbars.push(arg1);
		arg1.up=this;
	}
	else
	{
		if (arg1 == "")
		{
			window.alert("ToolBar error: Name parameter is mandatory!");
		}
		else
		{
			var toolbar = new ToolBar(this.name+arg1, arg2);
			this.toolbars.push(toolbar);
			toolbar.up = this;
			if(this.root==null) toolbar.root=this; else toolbar.root=this.root;
			return toolbar;
		}
	}
}

/**
 * Add the specified button to the button list.
 * @param arg1 The button which should be added.
 * @param arg1 The name of the 
 */
ToolBar.prototype.addButton = function(arg1, arg2, arg3, arg4, arg5)
{
	if (arg1 instanceof ToolButton)
	{
		this.buttons.push(arg1);
	}
	else
	{
		var button = new ToolButton(arg1, arg2, arg3, arg4,arg5);
		this.buttons.push(button);
		return button;
	}
}

/**
 * Add a separator button to the button list.
 */
ToolBar.prototype.addSeparator = function()
{
	var button = new ToolSeparator();
	this.buttons.push(button);
	return button;
}


/**
 * Add a dropdown list to the button list.
 */
ToolBar.prototype.addDropDown = function(name, hint, url, data)
{
	var button = new ToolDropDown(name, hint, url, data);
	this.buttons.push(button);
	return button;
} 
 
/*
 * Function: collapse
 * Hide all child toolbar recursively except the first.
 */
ToolBar.prototype.collapse = function()
{
	if (this.toolbars.length>0) 
	{
		this.toolbars[0].select();
		this.toolbars[0].show();
	}
	for (var i=0;i<this.toolbars.length;i++) 
	{
		if (i>0) 
		{
			this.toolbars[i].hide();
			this.toolbars[i].deselect();
		}
		this.toolbars[i].collapse();
	}
}


/*
 * Function: hideAll
 * Hide and deselect all child toolbar.
 */
ToolBar.prototype.hideAll = function(toolbar)
{
	for (var i=0;i<this.toolbars.length;i++) 
	{
		this.toolbars[i].hide();
		this.toolbars[i].deselect();
	}
}


/*
 * Function: hideChildren
 * Hide and deselect all child toolbar recursively.
 */
ToolBar.prototype.hideChildren = function(toolbar)
{
	for (var i=0;i<this.toolbars.length;i++) 
	{
		this.toolbars[i].hide();
		this.toolbars[i].hideAll();
	}
}



/*
 * Function: getLevel
 * Return the current level in the whole three.
 */
ToolBar.prototype.getLevel = function()
{
	var level = 0;
	var toolbar = this;
	while (toolbar != null)
	{
		level++;
		toolbar = toolbar.up;
	}
	return level-1;
}


/*
 * Function: getChildLevel
 * Return the level of the last children of this toolbar.
 */
ToolBar.prototype.getChildLevel = function(toolbar)
{
	var level=this.getLevel(); 
	var toolbar = this;
	
	for (var i=0;i<this.toolbars.length;i++) 
	{
		var current = this.toolbars[i].getChildLevel();
		if (current > level) current = level;
	}

	return level;
}

/*
 * Function: hide
 * Hide the contents.
 */
ToolBar.prototype.hide = function()
{
	var tname = this.name+'_content';
	document.getElementById(tname).style.display='none';
	for (var i=0;i<this.toolbars.length;i++) 
	{
		document.getElementById(this.toolbars[i].name).style.display='none';
	}	
}


/*
 * Function: select
 * Set the style to 'selected'.
 */
ToolBar.prototype.select= function(name)
{
	var tname = this.name;
	cpt=document.getElementById(tname);
	cpt.className='tbCaptionSelected'+this.getLevel();
	
	cpt=document.getElementById(tname+'_a');
	this.click=cpt.onclick;
	cpt.onclick='';
	cpt.removeAttribute('href');
	
	cpt=document.getElementById(tname+'_before');
	cpt.className='tbCaptionBeforeSelected'+this.getLevel();
	
	cpt=document.getElementById(tname+'_after');
	cpt.className='tbCaptionAfterSelected'+this.getLevel();
	
	cpt=document.getElementById(this.root+'tbContentPart');
	var i=0;
	while (cpt==null && i<10000)
	{
		cpt=document.getElementById(this.root+'tbContentPartSelected'+i);
		i++;
	}
	if (cpt != null) cpt.id=this.root+'tbContentPartSelected'+this.getChildLevel();
}

/*
 * Function: deselect
 * Set the style to 'deselected'.
 */
ToolBar.prototype.deselect= function(name)
{
	var tname = this.name;
	cpt=document.getElementById(tname);
	cpt.className='tbCaption';
	cpt=document.getElementById(tname+'_a');
	cpt.onclick=this.click;
	cpt.setAttribute("href","#null");
	
	cpt=document.getElementById(tname+'_before');
	cpt.className='tbCaptionBefore';
	
	cpt=document.getElementById(tname+'_after');
	cpt.className='tbCaptionAfter';

	cpt=document.getElementById(this.root+'tbContentPart');
	var i=0;
	while (cpt==null && i<10000)
	{
		cpt=document.getElementById(this.root+'tbContentPartSelected'+i);
		i++;
	}
	if (cpt != null)  cpt.id=this.root+'tbContentPart';
}

/*
 * Function: showAll
 * Show the contents of child toolbars.
 */
ToolBar.prototype.showAll= function(name)
{
	for (var i=0;i<this.toolbars.length;i++) 
	{
		this.toolbars[i].show();
	}
}


/*
 * Function: showChildren
 * Show the contents of child toolbars recursively.
 */
ToolBar.prototype.showChildren= function(name)
{
	for (var i=0;i<this.toolbars.length;i++) 
	{
		this.toolbars[i].show();
		this.toolbars[i].showChildren();
	}
}

/*
 * Function: show
 * Show the contents.
 */
ToolBar.prototype.show= function(name)
{
	var tname = this.name+'_content';
	document.getElementById(tname).style.display='';
	for (var i=0;i<this.toolbars.length;i++) 
	{
		document.getElementById(this.toolbars[i].name).style.display='';
	}
}

/*

/*
 * Function: realizeCaption
 * Write the caption to the document.
 */
ToolBar.prototype.realizeCaption = function(click)
{
	var str="";
		if (click==undefined) click='ToolBar.switchTo('+"'"+this.name+"')";else click=click+this.name+"');";
		str+=('<div id="'+this.name+'" class="tbCaption">');
		str+=('<div id="'+this.name+'_before" class="tbCaptionBefore">&nbsp;</div>');
		str+=('<a id="'+this.name+'_a" href="#null" onclick="'+click+'"><b>'+this.caption+'</b></a>');
		str+=('<div id="'+this.name+'_after" class="tbCaptionAfter">&nbsp;</div>');
		str+=('</div>');
		document.writeln(str);
		cpt=document.getElementById(this.name+'_a');
		this.click = cpt.onclick;
}


/*
 * Function: realizeAllCaption
 * Write the caption to the document recursively.
 */
ToolBar.prototype.realizeAllCaption = function(level, click)
{
	if (level == undefined) level=1;
	for (var i=0;i<this.toolbars.length;i++) this.toolbars[i].realizeCaption(click);
	if (this.toolbars.length>0) 
	{
	//	document.writeln("<br/>");
		document.writeln('<div class="tbCaptionRow'+level+'">');
		for (var i=0;i<this.toolbars.length;i++) this.toolbars[i].realizeAllCaption(level+1);
		document.writeln('</div>');
	}
}

/*
* Function: realizeButtons
* Write the buttons to the document.
*/
ToolBar.realizeButtons = function(toolbar)
{
	var tb = (toolbar==undefined)?ToolBar.root:toolbar;
	for (var i=0;i<tb.buttons.length;i++) tb.buttons[i].realize();
}



/**
 * Function: init
 * Initialize the toolbar, write the HTML tags to the document. 
 * If toolbar specified, it will be initialized. If no toolbar specified, the root will be initialized.
 */
ToolBar.init = function(toolbar, click)
{

	if (toolbar == undefined)
	{
			document.writeln('<div id="tbCaptionPart">');
			document.writeln('<div class="tbCaptionRow0">');
			ToolBar.realizeButtons();
			ToolBar.root.realizeAllCaption(1);
			document.writeln('</div>');
			document.writeln('</div>');
			document.writeln('<div id="tbContentPart">');
			ToolBar.realizeContent();
			document.writeln('</div>');
			
		//	ToolBar.root.collapse();
			if (ToolBar.root.toolbars.length>0) 
			{
				ToolBar.switchTo(ToolBar.root.toolbars[0].name);
			}
	}
	else
	{
		document.writeln('<div id="'+toolbar.name+'tbCaptionPart" class="tbCaptionPart">');
		document.writeln('<div class="tbCaptionRow0">');
		ToolBar.realizeButtons(toolbar);
		toolbar.realizeAllCaption(1,click);
		document.writeln('</div>');
		document.writeln('</div>');
		document.writeln('<div id="'+toolbar.name+'tbContentPart" class="tbContentPart">');
		ToolBar.realizeContent(toolbar);
		document.writeln('</div>');
		
	//	ToolBar.root.collapse();
		if (toolbar.toolbars.length>0) 
		{
			ToolBar.switchTo(toolbar.toolbars[0].name, toolbar);
		}		
	}
}

/*
 * Function: realizeContent
 * Write the root content to the document.
 */
ToolBar.realizeContent = function(toolbar)
{
	var tb = (toolbar==undefined)?ToolBar.root:toolbar;
	document.writeln('<div id="'+this.name+'_content'+'" class="tbContent">');
	for (var i=0;i<tb.toolbars.length;i++) tb.toolbars[i].realizeContent();
	document.writeln('</div>');

}


/*
 * Function: realizeContent
 * Write the content to the document.
 */
ToolBar.prototype.realizeContent = function()
{
	
	document.writeln('<div id="'+this.name+'_content'+'" class="tbContent">');
	for (var i=0;i<this.buttons.length;i++) this.buttons[i].realize();
	for (var i=0;i<this.toolbars.length;i++) this.toolbars[i].realizeContent();
	document.writeln('</div>');

}



/*
 * Function: switchTo
 * Switch to the specified toolbar.
 */
ToolBar.switchTo = function(name,tbar)
{
	var tb = ToolBar.getToolBarByName(name, tbar);
	var toolbar = tb.up;
	while (toolbar != null)
	{
		toolbar = toolbar.up;
	}
//	tb.collapse();

	tb.up.hideAll();
	tb.show();
	tb.select();
	tb.collapse();
}

/*
 * Function: getToolBarByName
 * Find the specified toolbar.
 */
ToolBar.getToolBarByName = function(name, tbar)
{

	var found = 0;
	var toolbar = null;

	if (tbar == undefined) toolbar = ToolBar.root; else toolbar = tbar;


	if (toolbar.name == name && toolbar.name!="") return toolbar;

	if (toolbar == null) alert('ToolBar error: You must specify root toolbar!');
	for (var i=0;i<toolbar.toolbars.length;i++)
	{
		found = ToolBar.getToolBarByName(name, toolbar.toolbars[i]);
		if (found != null) return found;
	}

	return null;
}

ToolBar.getPositionX = function(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;

}


//================================[Button Class]==============================\\



/**
 * Initialize the new button
 * @param name The name of the button.
 * @param src The source URL of the button.
 * @param hint The hint text of the button.
 */
function ToolButton(name, src, hint, url, size)
{
	if (size == undefined) this.size=25; else this.size=size;
	this.name = name;
	this.src = src;
	this.hint = hint;
	this.url = url;

}

ToolButton.prototype.realize = function()
{
	document.writeln('<div id="'+this.name+'" class="toolbutton">');
	document.writeln('<a href="'+this.url+'"><img src="'+this.src+'" title="'+this.hint+'" '+((this.size>0)?('width="'+this.size+'" height="'+this.size+'"'):'')+ ' alt="'+this.hint+'" border="0"/></a>');
	document.writeln('</div>');
}



//================================[DropDown Class]==============================\\


/**
 * Initialize the new button
 * @param name The name of the button.
 * @param src The source URL of the button.
 * @param hint The hint text of the button.
 */
function ToolDropDown(name, hint, fnc, data)
{
	this.name = 'DropDown'+name+ToolDropDown.TBCounter++;;
	this.hint = hint;
	this.fnc = fnc;
	this.options = new Array();
	this.values = new Array();
	this.selected = 1;
	this.display = '';
	ToolDropDown.list[ToolDropDown.list.length] = this;
	this.width=0;
	this.data=data;
}
ToolDropDown.list = new Array();
ToolDropDown.eventadded = false;
ToolDropDown.open = false;

ToolDropDown.hideList = function(name)
{
	var list = document.getElementById(name+'list');
	list.style.visibility='hidden';
}

ToolDropDown.hideAllLists = function()
{
	if (!ToolDropDown.open)
	{
		for (var i=0;i<ToolDropDown.list.length;i++)
		{
			ToolDropDown.hideList(ToolDropDown.list[i].name);
		}
	}
	ToolDropDown.open=false;
}

ToolDropDown.getDropDown = function(name)
{
	for (var i=0;i<ToolDropDown.list.length;i++)
	{
		if (ToolDropDown.list[i].name == name) return ToolDropDown.list[i];
	}
}

ToolDropDown.showList = function(name)
{
	var list = document.getElementById(name+'list');
	list.style.visibility='visible';
	ToolDropDown.open=true;

}

ToolDropDown.invertList = function(name)
{
	var list = document.getElementById(name+'list');
	list.style.visibility=(list.style.visibility=='visible')?'hidden':'visible';
	ToolDropDown.open=!	ToolDropDown.open;
	if (list.style.visibility=='visible')
	{
		ToolDropDown.deselectAll(name);
		var dd = ToolDropDown.getDropDown(name);
		var item = document.getElementById(name+'listItem'+dd.selected);
		item.className = 'dropdownlistitem_selected';
	}
}

ToolDropDown.select = function(name, selected)
{
	var dd = ToolDropDown.getDropDown(name);
	var label = document.getElementById(name+'LabelAnchor');
	label.innerHTML =dd.options[selected]; 
	dd.selected = selected;
	document.getElementById(name+'Label').style.width=dd.width+'px';
}

ToolDropDown.deselectAll = function(name)
{
	var dd = ToolDropDown.getDropDown(name);
	for (var i=0;i<dd.options.length;i++)
	{
		var item = document.getElementById(name+'listItem'+i);
		item.className='dropdownlistitem';
	}
}

ToolDropDown.prototype.resetWidth = function()
{
	var maxw=0;
	for (var i=0;i<this.options.length;i++)
	{
		var width=document.getElementById(this.name+'listItem'+i).clientWidth;
		if (width>maxw) maxw = width;
	}
	document.getElementById(this.name+'Label').style.width=maxw+'px';
	this.width=maxw;
}

ToolDropDown.prototype.addOption = function(label, value)
{
	this.options.push(label);
	this.values.push(value);
}


ToolDropDown.exec = function(name)
{
	var dd = ToolDropDown.getDropDown(name);
	dd.fnc(dd.options[dd.selected], dd.values[dd.selected], dd.data);
}

ToolDropDown.prototype.realize = function()
{
	document.write('<div class="dropdown_wrapper">');
	document.write('<div id="'+this.name+'" class="dropdown">');
	document.write('<div class="dropdown_left"> </div><div class="dropdown_center"><span id="'+this.name+'Label" class="dropdown_label"><a id ="'+this.name+'LabelAnchor" href="javascript:ToolDropDown.exec(\''+this.name+'\')">'+this.options[this.selected]+'</a></span></div>');
	document.write('<div class="dropdown_right" onclick="ToolDropDown.invertList(\''+this.name+'\');"> </div>');
	document.writeln('</div>');
	document.write('<div id="'+this.name+'list" class="dropdownlist">');
	for (var i=0;i<this.options.length;i++)
	{
		var events = 'onclick="ToolDropDown.select(\''+this.name+'\','+i+');" onmouseover="ToolDropDown.deselectAll(\''+this.name+'\');this.className=\'dropdownlistitem_selected\';"';
		document.write('<div id="'+this.name+'listItem'+i+'" class="dropdownlistitem" '+events+'>');
		document.write(this.options[i]);
		document.write('</div>');
	}
	document.writeln('</div>');
	document.writeln('</div>');
	ToolDropDown.hideList(this.name);
	if (!ToolDropDown.eventadded)
	{
		if (document.all)
		{
			document.attachEvent('click', ToolDropDown.hideAllLists );
		}
		else
		{
			document.addEventListener('click', ToolDropDown.hideAllLists, false );
		}
		ToolDropDown.eventadded = true;
	}


	//ToolDropDown.showList(this.name);
	this.resetWidth();
	

}
ToolDropDown.TBCounter=0;

//================================[Separator Class]==============================\\

function ToolSeparator()
{
	this.size=25;
	this.name = 'Separator'+ToolSeparator.TBCounter++;
	this.src = '';
	this.hint = '';
	this.url = '';

}

ToolSeparator.TBCounter=0;

ToolSeparator.prototype.realize = function()
{
	document.writeln('<div id="'+this.name+'" class="tbSeparator">');
	document.writeln('</div>');
}


