elcNavBar.defaultTimeoutDuration = 300;
elcNavBar.Items = new Array();

function elcNavBar(name){
	this.name = name;
	this.Items = new Object;
	this.last = null;
	this.hasTimeout = false;
	this.timeout = null;
	this.defaultItemKey = null;
	this.layer = null;
	this.activeLayer = null;
	this.activeLayerCanClose = false;
	elcNavBar.Items[elcNavBar.Items.length] = this;
}

function elcNavBar_sendURL(url){
	window.location.href = wsmlMakeComponentHref(url);
}

function elcNavBar_setAllLastOff(){
	for (var i=0; i<elcNavBar.Items.length; i++){
		if (elcNavBar.Items[i].last) elcNavBar.Items[i].setLastOff();
	}
}

elcNavBar.setAllLastOff = elcNavBar_setAllLastOff;
elcNavBar.sendURL = elcNavBar_sendURL;

elcNavBar.prototype.addItem = function(oImage,sLayerName,isDefault){
	var oItem = this.Items[oImage.name] = new String(oImage.name);
	oItem.type = 'image';
	oItem.image = new elcImg(oImage);
	if (sLayerName) oItem.layer = new elcLayer(sLayerName);
	oItem.isDefault = (isDefault)?1:0;
	if (isDefault && !oItem.image.state) oItem.image.setOn();
}

elcNavBar.prototype.setOn = function(key){
	var oItem = this.getItem(key);
	if (!oItem) {return};
	if(this.last && this.last != oItem) this.setLastOff();
	if(this.defaultItemKey && this.thisDefaultItemKey != key) this.setOff(this.defaultItemKey);
	oItem.image.setOn();
	if (typeof this.hSetOn == 'function') this.hSetOn(oItem);
	if(oItem.layer){
		this._processSubLayerSetOn(oItem);
	} else if(this.defaultItemKey) {
		this.setDefaultItemOn();
	}
	this.last = oItem;
}

elcNavBar.prototype.setOff = function(key,force){
	var oItem = this.getItem(key);
	if (!oItem) {return};
	if (oItem.layer && this.activeLayer && !force) {
		// if a layer is active we defer closing
		this.activeLayerCanClose = true;
		clearTimeout(eval(this.name+".timeout"));
		this.timeout = setTimeout(this.name+".setLayerOff('"+this.activeLayer+"')",elcNavBar.defaultTimeoutDuration);
		return;
	}
	if (!oItem.isDefault){
		oItem.image.setOff();
		if (typeof this.hSetOff == 'function') this.hSetOff(oItem);
	}
	if (oItem.layer) {
		if (typeof this.hSetOffExtra == 'function') this.hSetOffExtra();
		this._processSubLayerSetOff(oItem);
	}
}

elcNavBar.prototype._processSubLayerSetOn = function(key){
	var oItem = this.getItem(key);
	oItem.layer.show();
	this.activeLayer = key;
	this.activeLayerCanClose = false;
}

elcNavBar.prototype._processSubLayerSetOff = function(key){
	var oItem = this.getItem(key);
	oItem.layer.hide();
}

elcNavBar.prototype.setLayerOff = function(key){
	if (this.activeLayer && this.activeLayerCanClose) this.setOff(this.activeLayer,true);
}

elcNavBar.prototype.setLayerOver = function(){
	this.activeLayerCanClose = false;
}

elcNavBar.prototype.setLayerOut = function(){
	this.activeLayerCanClose = true;
	clearTimeout(eval(this.name+".timeout"));
	this.timeout = setTimeout(this.name+".setLayerOff('"+this.activeLayer+"')",elcNavBar.defaultTimeoutDuration);
}

elcNavBar.prototype.setLastOff = function(){
	this.activeLayer = null;
	this.activeLayerCanClose = true;
	if(this.defaultItemKey) {
		if(this.last != this.defaultItemKey) {
			this.setOff(this.last,true);
			this.setDefaultItemOn();
		} else {
			this.setDefaultItemOff();
		}
	} else {
		this.setOff(this.last,true);
	}
	this.last = null;
}

elcNavBar.prototype.clrTimeout = function(){
	if (this.hasTimeout) clearTimeout(eval(this.name+".timeout"));
}

elcNavBar.prototype.setDefaultItem = function(key){
	var oItem = this.getItem(key);
	this.defaultItemKey = key;
	this.setDefaultItemOn();
}

elcNavBar.prototype.setDefaultItemOn = function(){
	// does not call the oItem.image.setOn() method
	var oItem = this.getItem(this.defaultItemKey);
	if(oItem.layer){
		oItem.layer.show();
		if(this.hasTimeout){
			clearTimeout(eval(this.name+".timeout"));
			this.timeout = setTimeout(this.name+".setOff('"+key+"')",elcNavBar.defaultTimeoutDuration);
		}
	}
}

elcNavBar.prototype.setDefaultItemOff = function(){
	// does not call the 'hide' layer method
	var oItem = this.getItem(this.defaultItemKey);
	oItem.image.setOff();
}

elcNavBar.prototype.getItem = function(key){
	return this.Items[key];
}
