/* Copyright (c) 2006 Inuvia Technologies - All rights reserved. */

function INToolbarCollection()
{
    this.toolbars = new Array();

    this._window_unload = Function.createDelegate(this, this.window_unload);
    
    $addHandler(window, "unload", this._window_unload);
}

INToolbarCollection.prototype = {
    addToolbar: function(toolbar) {
        if (toolbar)
            this.toolbars[this.toolbars.length] = toolbar;
    },

    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;
        }
    },

    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.parentNode;

        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;
        }
    },

    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;
    },

    window_unload: function(e) {
        for (var t = 0; t < INToolbars.toolbars.length; t++)
            INToolbars.toolbars[t].unload();

        $removeHandler(window, "unload", this._window_unload);
            
        // clear the list
        toolbars = new Array();
    }
};

var INToolbars = new INToolbarCollection();

function INToolbar( doc, id, clsprefix ) {
	this.id = id;
	this.clsprefix = clsprefix;
	this.elem = document.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(0);
	this.items = new Array();
	this.lastWasSep = true;
	this.lastItem = null;

	INToolbars.addToolbar(this);
}

INToolbar.prototype = {
	Show : function() { this.elem.style.visibility = 'visible'; },
	Hide : function() { this.elem.style.visibility = 'hidden'; },
	Clear : function() { 
		while( this.TR.cells.length > 0 ) 
			this.TR.deleteCell(); 
	},
	Add : function( item ) { 
		this.items[this.items.length] = item; 
		var cell=this.TR.insertCell(this.TR.cells.length); 
		cell.id = this.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; 
	},
	AddSeparator : function() { 
		var item = this.Add( new INToolbarSeparator( this ) ); 
		return item;
	},
	OnClick : function( item ) {
		if( this.oncommand && (item.command || item.group) )
			this.oncommand( item.command, item );
	},
	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();
			}
		}
	},
	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();
			}
		}
	},
	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();
			}
		}
	},
	findItemByElem : function( e )
	{
		var elem = e.srcElement;
		while( elem && ( !elem.id || elem.id=='' ) ) elem = elem.parentNode;
		
		var cmdid = elem.id;
		return this.findItem( cmdid );
	},
	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;
	},
	
	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;
	}
}

function INToolbarLabel( tb, html ) {
	this.group = null;
	this.cmdid = null;
	this.toolbar = tb;
	this.elem = document.createElement('SPAN');
	this.elem.className = this.toolbar.clsprefix+'_lbl';
	this.elem.setAttribute('UNSELECTABLE', 'on');
	this.elem.innerHTML = html;
	this.panename = null;
}

INToolbarLabel.prototype = {
    redraw : function() {}
}

function INToolbarSeparator( tb ) {
	this.group = null;
	this.cmdid = null;
	this.toolbar = tb;
	this.elem = document.createElement('SPAN');
	this.elem.className = this.toolbar.clsprefix+'_sep';
	this.elem.setAttribute('UNSELECTABLE', 'on'); 
	this.elem.innerHTML = "&nbsp;";
	this.isSep = true;
	this.panename = null;
}

INToolbarSeparator.prototype = {
    redraw : function() {}
}

function INToolbarButton(tb, href, alt, text, command, group, confirmtext) {
    if (tb == undefined ) return;
    
	this.isSep = false;
	this.selected = false;
	this.enabled = false;
	this.lockenabled = false;
	this.highlighted = false;
	this.down = false;

	this.toolbar = tb;

	this.className = '_button';

    if( command != null )
	    this.cmdid = tb.id+'_'+command;
	this.command = command;
	this.group = group;
	this.panename = null;
	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 = document.createElement('SPAN');
	this.elem.id = this.cmdid;
	this.elem.setAttribute('UNSELECTABLE', 'on');
	this.anchor = 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 = 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 = document.createElement('SPAN');
		label.className = 'lbl';
		label.setAttribute('UNSELECTABLE', 'on');
		label.innerHTML = this.text;
		this.anchor.appendChild( label );
	}

	// events
	$addHandlers(this.elem, { "mouseover": this.elem_enter,
	    "mouseout": this.elem_leave,
	    "mousedown": this.elem_down,
	    "mouseup": this.elem_up
	}, this);

	$addHandlers(this.anchor, { "click": this.anchor_click }, this);

	this.redraw();
};

INToolbarButton.prototype = {
    setCommandArgs: function(args) {
        this.commandArgs = args;
    },
    setHref: function(href) {
        this.img.src = href;
        this.href = href;
    },
    setText: function(text) {
        this.text = text;
    },
    setAlt: function(alt) {
        this.anchor.title = alt;
    },
    setHighlight: function(highlight) {
        this.highlighted = highlight;
    },
    setDown: function(down) {
        this.down = down;
    },
    setEnabled: function(enabled) {
        if (!this.lockenabled)
            this.enabled = enabled;
    },
    getEnabled: function() {
        return this.enabled;
    },
    lockEnabled: function(locked) {
        this.lockenabled = locked;
    },
    setSelected: function(selected) {
        this.selected = selected;
    },
    redraw: function() {
        var clsname = this.toolbar.clsprefix + this.className;
        
        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;
    },
    onclick: function() {
        if (this.enabled)
            this.toolbar.OnClick(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;
    },

    elem_enter: function(e) { this.setHighlight(true); this.redraw(); },
    elem_leave: function(e) { this.setHighlight(false); this.setDown(false); this.redraw(); },
    elem_down: function(e) { this.setDown(true); this.redraw(); return true; },
    elem_up: function(e) { this.setDown(false); this.redraw(); return true; },
    anchor_click: function(e) { if (this.enabled && (this.confirmtext == null || window.confirm(this.confirmtext))) this.onclick(); }
}

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.className = '_dropbutton';
	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.panename = null;
	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 = 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 = 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 = document.createElement('SPAN');
		label.className = 'lbl';
		label.setAttribute('UNSELECTABLE', 'on');
		label.innerHTML = this.text;
		this.elem.appendChild( label );
	}

	this.edit = document.createElement('INPUT');
	this.edit.type = 'text';
	this.edit.className = 'edit';
	this.elem.appendChild(this.edit);
    this.button = 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.recursecheck = false;

	// events
	this.combo.add_change( Function.createDelegate(this,this.select_change) );
	this.combo.add_fill( Function.createDelegate(this,this.select_fill) );
	
	this.redraw();	
}

INToolbarCombobox2.prototype = {
	_addOption : function( optioninfo ) {
	    this.addOption(optioninfo.text, optioninfo.id);
	},
	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();
	},
	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;
	    }
	},
	addOptions : function( optioninfos ) {
	    for( var optioninfo in optioninfos )
	        this.addOption(optioninfo);
	},

	setCommandArgs : function( args ) {
		this.commandArgs = args;
	},
	clearOptions : function() {
	    this.combo.clear_options();
	    this.filled = false;
	},
	
	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();
	},
	
	setHref : function( href ) {
		this.img.src = href;
		this.href = href;
	},
	setText : function( text ) {
		this.text = text;
	},
	setHighlight : function(highlight) {
		this.highlighted = highlight;
	},
	setEnabled : function( enabled ) {
		if( !this.lockenabled )
			this.enabled = enabled;
	},
	getEnabled : function() {
		return this.enabled;
	},
	lockEnabled : function( locked ) {
		this.lockenabled = locked;
	},
	setSelectedCommand : function( command ) {
	    if( this.onfill != undefined ) {
	        this.setOnlyOption( command, command, this.enabled );
	    } else {
		    this.command = command;
		    this.combo.set_value(command);
		}
	},
	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;
	},
	
	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 ); 
			    }
			}
	},
	
	unload : function() {
	    this.elem = null; this.anchor = null; this.img = null;
	},

    select_change : function(e) { 
        if( !this.recursecheck && (this.confirmtext == null || window.confirm(this.confirmtext)) ) 
            this.onclick(); 
    },
            
    select_fill : function(e) { if( this.onfill ) this.onfill( this ); this.filled = true; }
};

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

