/* Copyright (c) 2006 Inuvia Technologies - All rights reserved. */

function INToolbarCollection()
{
    this.toolbars = new Array();

    this.addToolbar = function( toolbar ) {
        if( toolbar )
            this.toolbars[this.toolbars.length] = toolbar;
    }
    this.removeToolbar = function( toolbar ) {
        if( toolbar )
            for( var i=0; i < this.toolbars.length; i++ )
                if( this.toolbars[i] == toolbar ) {
                    for( ; i < this.toolbars.length-1; i++ )
                        this.toolbars[i] = this.toolbars[i+1];
                        
                    this.toolbars[this.toolbars.length-1] = null;
                    this.toolbars.length = this.toolbars.length-1;
                    break;
                }
    }
    
    this.findItemByElem = function( e ) {
	    var elem;
        if (!e) var e = window.event;
        if (e.target) elem = e.target;
        else if (e.srcElement) elem = e.srcElement;
        if (elem.nodeType == 3) elem = elem.parentNode;

	    while( elem && ( !elem.id || elem.id=='' ) ) elem = elem.parentElement;

        if( elem == null )
            alert('Event out of scope.');
        else {
	        var cmdid = elem.id;

	        var item = this.findItem( cmdid );
	        if( item == null )
	            alert('Missing Control - '+cmdid);
	        else
	            return item;
	    }
    }
    this.findItem = function( cmdid ) {
        for(var t = 0; t < this.toolbars.length; t++) {
            var toolbar = this.toolbars[t];
	        for(var i = 0; i < toolbar.items.length; i++) {
	            var item = toolbar.items[i];
		        if( item.cmdid == cmdid ) {
			        return item;
		        }
		    }
		}
	    return null;
    }

	function window_unload(e) {
		for(var t = 0; t < INToolbars.toolbars.length; t++)
	        INToolbars.toolbars[t].unload();
        paj_handleEvent( window, "onunload", window_unload, false); 
        
        // clear the list
        toolbars = new Array();
	}
	
    paj_handleEvent( window, "onunload", window_unload, true); 
}

var INToolbars = new INToolbarCollection();

function INToolbar( doc, id, clsprefix ) {
	this.id = id;
	this.clsprefix = clsprefix;
	this.elem = doc.createElement('TABLE');
	this.elem.cellPadding = 0;
	this.elem.cellSpacing = 0;
	this.elem.className = clsprefix + '_tb';
	this.elem.setAttribute('UNSELECTABLE', 'on');
	this.TR = this.elem.insertRow();
	this.items = new Array();
	this.lastWasSep = true;
	this.lastItem = null;
	
	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[this.items.length] = item; 
		var cell=this.TR.insertCell(); 
		cell.id = id; 
		cell.appendChild( item.elem ); 
		cell.className = 'tb';
	
	    if( this.lastItem != null ) {
	        this.lastItem.sepOnRight = item.isSep;
	        this.lastItem.redraw();
	        item.sepOnLeft = this.lastItem.isSep;
	    } else {
	        item.sepOnLeft = true;
	    }
	    item.sepOnRight = true;
	    
		//cell.valign = 'absmiddle';
		//item.toolbar = this;

        this.lastItem = item;
        		
		return item; 
	}
	this.AddSeparator = function() { 
		var item = this.Add( new INToolbarSeparator( this ) ); 
		return item;
	}
	this.OnClick = function( item ) {
		if( this.oncommand && (item.command || item.group) )
			this.oncommand( item.command, item );
	}
	this.setGroupCommandState = function( group, command, enabled )
	{
		for(var i = 0; i < this.items.length; i++) {
		    var item = this.items[i];
			if( item.group == group ) {
				item.setSelected(item.command == command);
				item.setEnabled(enabled);
				item.redraw();
			}
		}
	}
	this.setGroupEnabled = function( group, enabled )
	{
		for(var i = 0; i < this.items.length; i++) {
		    var item = this.items[i];
			if( item.group == group ) {
				item.setEnabled(enabled);
				item.redraw();
			}
		}
	}
	this.setCommandState = function( command, text, enabled )
	{
		for(var i = 0; i < this.items.length; i++) {
		    var item = this.items[i];
			if( item.cmdid == command ) {
				if( text != '' ) item.text = text;
				item.enabled = enabled;
				item.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++) {
		    var item = this.items[i];
			if( item.cmdid == cmdid ) {
				return item;
			}
		}
		return null;
	}
	
	this.unload = function() 
	{
		for(var i = 0; i < this.items.length; i++) {
		    var item = this.items[i];
		    if( item.unload != null ) item.unload();
		}
		this.elem = null; this.TR = null;
	}
	
    INToolbars.addToolbar( this );
}

function INToolbarLabel( tb, html ) {
	this.group = null;
	this.cmdid = null;
	this.toolbar = tb;
	this.elem = tb.elem.document.createElement('SPAN');
	this.elem.className = this.toolbar.clsprefix+'_lbl';
	this.elem.setAttribute('UNSELECTABLE', 'on'); 
	this.elem.innerHTML = html;

    this.redraw = function() {}
}

function INToolbarSeparator( tb ) {
	this.group = null;
	this.cmdid = null;
	this.toolbar = tb;
	this.elem = tb.elem.document.createElement('SPAN');
	this.elem.className = this.toolbar.clsprefix+'_sep';
	this.elem.setAttribute('UNSELECTABLE', 'on'); 
	this.elem.innerHTML = "&nbsp;";
	this.isSep = true;

    this.redraw = function() {}
}

function INToolbarButton( tb, href, alt, text, command, group, confirmtext ) {
	this.isSep = false;
	this.selected = false;
	this.enabled = false;
	this.lockenabled = false;
	this.highlighted = false;
	this.down = 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;

    this.confirmtext = confirmtext;
    
	// build the DHTML content
	this.elem = tb.elem.document.createElement('SPAN');
	this.elem.id = this.cmdid;
	this.elem.setAttribute('UNSELECTABLE', 'on');
	this.anchor = tb.elem.document.createElement('A');
	this.anchor.id = this.cmdid;
	this.anchor.className = 'button';
	this.anchor.setAttribute('UNSELECTABLE', 'on');
	this.anchor.title = alt;
	this.elem.appendChild(this.anchor);
	if( !href || href == '' ) href = 'Icons/spacer.gif';
	this.img = tb.elem.document.createElement('IMG');
	this.img.className = 'icon';
	this.img.src = href;
	this.img.align = 'absmiddle';
	this.anchor.appendChild(this.img);
	if( this.text && this.text != '' ) {
		var label = tb.elem.document.createElement('SPAN');
		label.className = 'lbl';
		label.setAttribute('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.setDown = function(down) {
		this.down = down;
	}
	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.text == null || this.text == '' )
		{
		    if( this.sepOnLeft ) {
		        if( this.sepOnRight )
		            clsname += '_lr';
		        else
		            clsname += '_l';
		    } else {
		        if( this.sepOnRight )
		            clsname += '_r';
		        else
		            clsname += '_c';
		    }
		}
		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.down )
		        clsname += '_sel';
			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 ); 
	}
	
	this.unload = function() {
	    if( this.elem != null )
            $clearHandlers( this.elem );
        if( this.anchor != null )
            $clearHandlers( this.anchor );
	    
	    this.elem = null; this.anchor = null; this.img = null;
	}

    this.elem_enter = function(e) { this.setHighlight(true); this.redraw(); }	
    this.elem_leave = function(e) { this.setHighlight(false); this.setDown(false); this.redraw(); }
    this.elem_down = function(e) { this.setDown(true); this.redraw(); return true; }
    this.elem_up = function(e) { this.setDown(false); this.redraw(); return true; }
    this.anchor_click = function(e) { if( this.enabled && (this.confirmtext == null || window.confirm(this.confirmtext) ) ) this.onclick(); }

	// events
	$addHandlers( this.elem, { "mouseenter": this.elem_enter,
	             "mouseleave": this.elem_leave,
	             "mousedown": this.elem_down, 
	             "mouseup": this.elem_up }, this );
	             
	$addHandlers( this.anchor, { "click": this.anchor_click }, this );

	this.redraw();	
}

function INToolbarStaticButton(tb, href, alt, text, command, group ) {
	var self = new INToolbarButton(tb,href,alt,text,command,group);
	self.setEnabled(true);
	self.lockEnabled(true);
	self.redraw();
	return self;
}

function INToolbarShowPopupButton(tb,href,alt,text,popup_elem) {
	var self = new INToolbarButton(tb,href,alt,text,text,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,alt,text,command,group,url) {
	var self = new INToolbarButton(tb,href,alt,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=1,status=0,toolbar=0", true );
	}
	self.redraw();
	return self;	
}

function INToolbarCombobox2(tb,href,alt,text,command,group,confirmtext) {
	this.selected = false;
	this.enabled = false;
	this.lockenabled = false;
	this.highlighted = false;
	
	this.toolbar = tb;

	this.cmdid = tb.id+'_'+group;
	this.command = command;
	this.group = group;
	this.text = text;
	this.href = href;
	this.href_highlighted = href;
	this.href_selected = href;
	this.selectedIndex = -1;
	
	this.commandArgs = null;

    this.confirmtext = confirmtext;
	this.debug = '';
	this.filled = false;

	// build the DHTML content
	this.elem = tb.elem.document.createElement('SPAN');
	this.elem.id = this.cmdid;
	this.elem.setAttribute('UNSELECTABLE', 'on');
	this.elem.title = alt;
    this.elem.className = this.toolbar.clsprefix+'_select';

	if( href && href != '' ) {
	    this.img = tb.elem.document.createElement('IMG');
	    this.img.className = 'icon';
	    this.src = href;
	    this.align = 'absmiddle';
	    this.elem.appendChild(this.img);
	}
	if( this.text && this.text != '' ) {
		var label = tb.elem.document.createElement('SPAN');
		label.className = 'lbl';
		label.setAttribute('UNSELECTABLE', 'on');
		label.innerHTML = this.text;
		this.elem.appendChild( label );
	}

	this.edit = tb.elem.document.createElement('INPUT');
	this.edit.type = 'text';
	this.edit.className = 'edit';
	this.elem.appendChild(this.edit);
    this.button = tb.elem.document.createElement('IMG');
    this.button.src = 'images/spacer.gif';
    this.button.className = 'button';
	this.button.align = 'absmiddle';
	this.elem.appendChild(this.button);
    
    this.combo = $create(WFControls.Combobox, {
            "autovalidate":false,
            "button":this.button,
            "container":this.elem,
            "options":[],
            "size":0,
            "value":"",
            "valuename":""}, null, null, this.edit);

	
	this._addOption = function( optioninfo ) {
	    this.addOption(optioninfo.text, optioninfo.id);
	}
	this.addOption = function( text, value ) {
	    if( value == null || value == undefined ) value = text;
	    
	    var selected = value?(this.command==value):(this.command==text);
        var index = this.combo.add_option( { "Text": text, "Value": value, "Selected": selected, "Enabled": true } );
	    if(selected) this.selectedIndex = index;

	    this.combo.refresh_options();
	}
	this.setOption = function( index, text, value ) {
	    if( value == null || value == undefined ) value = text;
    
        if( index >= 0 && index < this.combo.get_options_count() ) {
            this.combo.get_option(index).Value = value;
            this.combo.get_option(index).Text = text;

	        var selected = value?(this.command==value):(this.command==text);
            this.combo.get_option(index).Selected = selected;
	        
	        if(selected) this.selectedIndex = index;
	    }
	}
	this.addOptions = function( optioninfos ) {
	    for( var optioninfo in optioninfos )
	        this.addOption(optioninfo);
	}

	this.setCommandArgs = function( args ) {
		this.commandArgs = args;
	}
	this.clearOptions = function() {
	    this.combo.clear_options();
	    this.filled = false;
	}
	
	this.setOnlyOption = function( optiontext, optionvalue, enabled ) {
	    this.clearOptions();
	        
  	    this.command = optionvalue;
	    if( this.combo.get_options().length > 0 ) {
	        this.combo.get_options()[0].Text = optionstext;
	    } else {
	        this.addOption( optiontext, optionvalue );
	    }
	    this.combo.set_selectedIndex( 0 );
	    this.combo.refresh_options();
        this.enabled = enabled;
        this.redraw();
	}
	
	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.setSelectedCommand = function( command ) {
	    if( this.onfill != undefined ) {
	        this.setOnlyOption( command, command, this.enabled );
	    } else {
		    this.command = command;
		    this.combo.set_value(command);
		}
	}
	this.redraw = function() {
	    var clsname = this.toolbar.clsprefix + '_select';
	
	    if( !this.enabled ) {
		    clsname += '_dis';
	    } else {
		    if( this.highlighted ) 
			    clsname += '_over';
		    else
			    ;
            if( this.img )
		        this.img.src = this.href_highlighted;
	    }
		
		this.combo.set_Enabled( this.enabled );
        this.elem.className = clsname;
		
	}
	
	this.onclick = function() {
		if( this.enabled && !this.recursecheck )
		    if( this.onfill != undefined && this.filled && this.combo.get_selectedIndex() > -1 ) {
		        var option = this.combo.get_option(this.combo.get_selectedIndex());
		        if( option.Value != this.command ) {
		            this.command = option.Value;
			        this.toolbar.OnClick( this ); 
			    }
			} else {
		        if( this.combo.get_value() != this.command ) {
		            this.command = this.combo.get_value();
			        this.toolbar.OnClick( this ); 
			    }
			}
	}
	
	this.unload = function() {
	    this.elem = null; this.anchor = null; this.img = null;
	}

    this.recursecheck = false;
    this.select_change = function(e) { 
        if( !this.recursecheck && (this.confirmtext == null || window.confirm(this.confirmtext)) ) 
            this.onclick(); 
    }
            
    this.select_fill = function(e) { if( this.onfill ) this.onfill( this ); this.filled = true; }
    
	// events
	this.combo.add_change( Function.createDelegate(this,this.select_change) );
	this.combo.add_fill( Function.createDelegate(this,this.select_fill) );
	
	this.redraw();	
}

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
