/*******************************
*
* elcNavigation.js
* 
* navigation and menuing class
*
* Author: DoublePrime
* References: ELCI Clinique navigation.js created by Razorfish
* Version: 0.5
*
* Comments
* Added defaultItem for Px.
* 0.3: Class method setAllLastOff:
* to allow single off layer to turn off all
* registered elcNavigation objects last layer off
* 0.4: Allow for sliding navs using helper file
* elcSlidingNav,js if the functions are defined.
* 0.5: Removed NS4 support and prepared for LM sliding nav
*
*******************************/
DEBUG = (window.location.search.indexOf('debug=1') > -1) ? true : false;

elcNavigation.defaultTimeoutDuration = 2000;
elcNavigation.Items = new Array();

function elcNavigation(name){
	this.name = name;
	this.Items = new Array();
	this.last = null;
	this.offLayer = null;
	this.hasTimeout = false;
	this.timeout = null;
	this.defaultItemKey = null;
	this.parent = null;
	this.children = new Array();
	if (elcNavigation.hasItemWithName(this)) {
		eval(this.name+".addChild(this)");
	}
	elcNavigation.Items[elcNavigation.Items.length] = this;
}

function elcNavigation_sendURL(url){
	location.href = url;
}

function elcNavigation_hasItemWithName(nav){
	if (nav.name) {
		var obj = eval(nav.name);
		if (typeof obj == "object" && obj.constructor == elcNavigation) return true;
	}
	return false;
}

function elcNavigation_setAllLastOff(){
	for (var i=0; i<elcNavigation.Items.length; i++){
		if (elcNavigation.Items[i].last) elcNavigation.Items[i].setLastOff();
	}
}

function elcNavigation_hideAllOff(){
	for (var i=0; i<elcNavigation.Items.length; i++){
		if (elcNavigation.Items[i].off) elcNavigation.Items[i].off.hide();
	}
}

elcNavigation.hasItemWithName = elcNavigation_hasItemWithName;
elcNavigation.setAllLastOff = elcNavigation_setAllLastOff;
elcNavigation.hideAllOff = elcNavigation_hideAllOff;
elcNavigation.sendURL = elcNavigation_sendURL;

elcNavigation.prototype.addItem = function(oImage,sLayerName,isDefault){	
	oItem = this.Items[this.Items.length] = new String(oImage.name);
	oItem.type = 'image';
	oItem.image = new elcImage(oImage);
	oItem.hasSlidingLayer = false;
	if (this.offLayer && !this.off){
		this.off = new elcLayer(this.offLayer);
	}
	if (typeof(sLayerName) != "undefined"){
		oItem.layerName = sLayerName;
		oItem.layer = (elcLayer(oItem.layerName)) ? new elcLayer(oItem.layerName) : null;
	}
	oItem.isDefault = (isDefault) ? true : false;
	if (isDefault && !oItem.image.state) oItem.image.setOn();
}

elcNavigation.prototype._processDOMSetOn = function(oItemObj){
	if (typeof this.hSetOn == 'function') this.hSetOn(oItemObj);
	oItemObj.image.setOn();
}

elcNavigation.prototype._processSubLayerSetOn = function(key){
	var obj = this.getItem(key);
	if (obj.layer) {
		obj.layer.show();
	}
	if(this.hasTimeout){
		clearTimeout(eval(this.name+".timeout"));
		this.timeout = setTimeout(this.name+".setOff('"+key+"')",elcNavigation.defaultTimeoutDuration);
	}
}

elcNavigation.prototype._processSubLayerSetOff = function(key){
	var obj = this.getItem(key);
	if (obj.layer) {
		obj.layer.hide();
	}
}

elcNavigation.prototype.setOn = function(key){
	var oItem = this.getItem(key);
	this.childrenSetOff();
	if(this.last && this.last != oItem) this.setLastOff();
	if(this.defaultItemKey && this.thisDefaultItemKey != key) this.setOff(this.defaultItemKey);
	if (DEBUG) {
		if (oItem == "undefined" || oItem == null || oItem.image == "undefined"){
			alert('Error in elcNavigation.setOn: missing object ' + key + ' in nav object ' + this.name)
			return
		}
	}
	this._processDOMSetOn(oItem);
	if(oItem.layer){
		this._processSubLayerSetOn(oItem);
	} else if(this.defaultItemKey) {
		this.setDefaultItemOn();
	}

	if(this.off) this.off.show();
	this.last = oItem;
}

elcNavigation.prototype.setOff = function(key){
	oItem = this.getItem(key);
	if (DEBUG) {
		if (oItem == "undefined" || oItem == null || oItem.image == "undefined") {
			alert('Error in elcNavigation.setOff: missing object ' + key + ' in nav object ' + this.name)
			return;
		}
	}
	if (!oItem.isDefault){
		oItem.image.setOff();
		if (typeof this.hSetOff == 'function') {
			this.hSetOff(oItem);
		}
	}
	if (oItem.layer) this._processSubLayerSetOff(oItem);
	if (this.off) this.off.hide();
}

elcNavigation.prototype.setLastOff = function(){
	if(this.defaultItemKey) {
		if(this.last != this.defaultItemKey) {
			this.setOff(this.last);
			this.setDefaultItemOn();
		} else {
			this.setDefaultItemOff();
		}
	} else {
		this.setOff(this.last);
	}
	this.last = null;
}

elcNavigation.prototype.showOffLayer = function(key){
	for(i=0; i<this.Items.length;i++){
		oItem = this.last;
		if (this.off) this.off.show();
	}
}

elcNavigation.prototype.hideOffLayer = function(){
	for (i=0; i<this.Items.length; i++){
		oItem = this.last;
		if (this.off) this.off.hide();
	}
}

elcNavigation.prototype.clrTimeout = function(){
	if (this.hasTimeout) clearTimeout(eval(this.name+".timeout"));
}

elcNavigation.prototype.addChild = function(navObj){
	this.children[this.children.length] = navObj;
}

elcNavigation.prototype.hasChildren = function(){
	return (this.children.length>0)?1:0;
}

elcNavigation.prototype.setDefaultItem = function(key){
	var oItem = this.getItem(key);
	if (DEBUG) {
		if (oItem == "undefined" || oItem == null || oItem.image == "undefined"){
			alert('Error in elcNavigation.setDefaultItem: missing object with key: ' + key )
			return;
		} else {
			alert('Setting the defaultItem for the nav: ' + this.name + ' to ' + key );
		}
	}
	this.defaultItemKey = key;
	this.setDefaultItemOn();
}

elcNavigation.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+"')",elcNavigation.defaultTimeoutDuration);
		}
	}
}

elcNavigation.prototype.setDefaultItemOff = function(){
	// does not call the 'hide' layer method
	var oItem = this.getItem(this.defaultItemKey);
	oItem.image.setOff();
}

elcNavigation.prototype.getItem = function(key){
	for (var i=0;i<this.Items.length;i++){
		var oItem = this.Items[i];
		if (oItem == key) {
			return oItem;
		}
	}
	return null;
}

elcNavigation.prototype.childrenSetOff = function(){
	if (this.children.length) {
		for (var i=0; i<this.children.length; i++) {
			if (this.children[i].last) this.children[i].setOff(this.children[i].last);
		}
	}
}

/* not sure this is needed */
elcNavigation.prototype.setName = function(name){
	var oldName = this.name;
	this.name = name;
	return oldName;
}
