
function helper_handleEvent( obj, eventname, eventfn, onoff )
{
	if( obj.addEventListener ) {
		if( onoff )
			obj.addEventListener( eventname, eventfn, true );
		else
			obj.removeEventListener( eventname, eventfn, true );			
	} else {
		if( onoff )
			obj.attachEvent(eventname, eventfn);
		else
			obj.detachEvent(eventname, eventfn);
	}
}

function INToolbar( doc, id, clsprefix ) {
	this.id = id;
	this.clsprefix = clsprefix;
	this.elem = doc.createElement('<TABLE border=0 height=10 cellpadding=0 cellspacing=0 class="' + clsprefix + '_tb" UNSELECTABLE="on">');
	this.TR = this.elem.insertRow();
	this.items = new Array();
	
	this.Show = function() { this.elem.style.visibility = 'visible'; }
	this.Hide = function() { this.elem.style.visibility = 'hidden'; }
	this.Clear = function() { 
		while( this.TR.cells.length > 0 ) 
			this.TR.deleteCell(); 
	}
	this.Add = function( item ) { 
		this.items.push(item); 
		var cell=this.TR.insertCell(); 
		cell.id = id; 
		cell.appendChild( item.elem ); 
		cell.className = clsprefix + '_tb';
		//cell.valign = 'absmiddle';
		//item.toolbar = this;
		
		return item; 
	}
	this.AddSeparator = function() { 
		return this.Add( new INToolbarSeparator( this ) ); 
	}
	this.OnClick = function( item ) {
		if( this.oncommand && item.command )
			this.oncommand( item.command, item );
	}
	this.setGroupCommandState = function( group, command, enabled )
	{
		for(var i=0; i<this.items.length; i++)
			if( this.items[i].group == group ) {
				this.items[i].setSelected(this.items[i].command == command);
				this.items[i].setEnabled(enabled);
				this.items[i].redraw();
			}
	}
	this.setGroupEnabled = function( group, enabled )
	{
		for(var i=0; i<this.items.length; i++)
			if( this.items[i].group == group ) {
				this.items[i].setEnabled(enabled);
				this.items[i].redraw();
			}
	}
	this.setCommandState = function( command, text, enabled )
	{
		for(var i=0; i<this.items.length; i++)
			if( this.items[i].cmdid == command ) {
				if( text != '' ) this.items[i].text = text;
				this.items[i].enabled = enabled;
				this.items[i].redraw();
			}
	}
	this.findItemByElem = function( e )
	{
		var elem = e.srcElement;
		while( elem && ( !elem.id || elem.id=='' ) ) elem = elem.parentElement;
		
		var cmdid = elem.id;
		return this.findItem( cmdid );
	}
	this.findItem = function( cmdid )
	{
		for(var i=0; i<this.items.length; i++)
			if( this.items[i].cmdid == cmdid ) {
				return this.items[i];
			}
		return null;
	}
}

function INToolbarSeparator( tb ) {
	this.group = null;
	this.cmdid = null;
	this.toolbar = tb;
	this.elem = tb.elem.document.createElement('<SPAN class="'+this.toolbar.clsprefix+'_sep"  UNSELECTABLE="on">'); 
}

function INToolbarButton( tb, href, text, command, group ) {
	this.selected = false;
	this.enabled = false;
	this.lockenabled = false;
	this.highlighted = false;
	
	this.toolbar = tb;

	this.cmdid = tb.id+'_'+command;
	this.command = command;
	this.group = group;
	this.text = text;
	this.href = href;
	this.href_highlighted = href;
	this.href_selected = href;
	
	this.commandArgs = null;

	// build the DHTML content
	this.elem = tb.elem.document.createElement('<SPAN id="'+this.cmdid+'"  UNSELECTABLE="on">');
	this.anchor = tb.elem.document.createElement('<A id="'+this.cmdid+'" class="'+this.toolbar.clsprefix+'_button"  UNSELECTABLE="on">');
	this.elem.appendChild(this.anchor);
	if( !href || href == '' ) href = 'Icons/spacer.gif';
	this.img = tb.elem.document.createElement('<IMG class="'+this.toolbar.clsprefix+'_button" src="' + href + '" align="absmiddle">');
	this.anchor.appendChild(this.img);
	if( this.text && this.text != '' ) {
		var label = tb.elem.document.createElement('<SPAN class="'+this.toolbar.clsprefix+'_button_text" UNSELECTABLE="on">');
		label.innerHTML = this.text;
		this.anchor.appendChild( label );
	}
	
	this.setCommandArgs = function( args ) {
		this.commandArgs = args;
	}
	this.setHref = function( href ) {
		this.img.src = href;
		this.href = href;
	}
	this.setText = function( text ) {
		this.text = text;
	}
	this.setHighlight = function(highlight) {
		this.highlighted = highlight;
	}
	this.setEnabled = function( enabled ) {
		if( !this.lockenabled )
			this.enabled = enabled;
	}
	this.getEnabled = function() {
		return this.enabled;
	}
	this.lockEnabled = function( locked ) {
		this.lockenabled = locked;
	}
	this.setSelected = function( selected )	{
		this.selected = selected;
	}
	this.redraw = function() {
		var clsname = this.toolbar.clsprefix + '_button'
		if( !this.enabled ) {
			clsname += '_dis';
		} else if( this.selected ) {
			if( this.highlighted ) 
				clsname += '_over_sel';
			else
				clsname += '_sel';
			this.img.src = this.href_selected;
		} else {
			if( this.highlighted ) 
				clsname += '_over';
			else
				;
			this.img.src = this.href_highlighted;
		}
		this.elem.className = clsname
	}	
	this.onclick = function() {	
		if( this.enabled ) 
			this.toolbar.OnClick( this ); 
	}
	
	// events	
	helper_handleEvent( this.elem, "onmouseenter", function(e) { var btn=plp_toolbar.findItemByElem(e); btn.setHighlight(true); btn.redraw(); }, true); 
	helper_handleEvent( this.elem, "onmouseleave", function(e) { var btn=plp_toolbar.findItemByElem(e); btn.setHighlight(false); btn.redraw(); }, true); 
	helper_handleEvent( this.anchor, "onclick", function(e) { plp_toolbar.findItemByElem(e).onclick(); }, true );

	this.redraw();	
	//tb_commands.push(this);
}

function INToolbarStaticButton(tb, href, text, command, group ) {
	var self = new INToolbarButton(tb,href,text,command,group);
	self.setEnabled(true);
	self.lockEnabled(true);
	self.redraw();
	return self;
}

function INToolbarShowPopupButton(tb,href,text,popup_elem) {
	var self = new INToolbarButton(tb,href,text,'','')

	self.setEnabled(true);
	self.lockEnabled(true);
	self.popup_elem = popup_elem;
	self.onclick = function() {
		plp_ShowPopup( popup_elem, this.elem, PLP_TOGGLE );
	}
	self.redraw();
	return self;	
}

function INToolbarLinkPopupButton(tb,href,text,command,group,url) {
	var self = new INToolbarButton(tb,href,text,command,group)

	self.setEnabled(true);
	self.lockEnabled(true);
	self.url = url;
	self.onclick = function() {
		window.open( self.url, text, "resizable=1,channelmode=0,directories=0,location=0,menubar=0,scrollbars=0,status=0,toolbar=0", true );
	}
	self.redraw();
	return self;	
}

