/*******************************
*
* elcNavigation.js
* 
* navigation and menuing class
*
* Author: DoublePrime
* References: ELCI Clinique navigation.js created by Razorfish
* Version: 0.4
*
* 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.
*
*
*******************************/
/*DEBUG = (window.location.search.indexOf('debug=1') > -1) ? true : false;*/

suppressErrors()
var DEBUG = true;

elcNavigation.defaultTimeoutDuration = 10000;
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){
	window.location.href = wsmlMakeComponentHref(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();
	}
}

elcNavigation.hasItemWithName = elcNavigation_hasItemWithName;
elcNavigation.setAllLastOff = elcNavigation_setAllLastOff;
elcNavigation.sendURL = elcNavigation_sendURL;

elcNavigation.prototype.addItem = function(oImage,sLayerName,isDefault,sNavType,isSlidingNav,sOrientation){
	if (oImage.layers) {
		this.addNS4Item(oImage,sLayerName,isDefault,sNavType,isSlidingNav,sOrientation);
	} else {	
		this.addImgItem(oImage,sLayerName,isDefault,sNavType,isSlidingNav,sOrientation);
	}
}

elcNavigation.prototype.addNS4Item = function(oLayer,sLayerName,isDefault,isSlider,sOrient){
	idx = this.Items.length;
	oItem = this.Items[this.Items.length] = new String(oLayer.name);
	oItem.type='ns4css2';
	oItem.onSubLayer = new elcLayer(oLayer.name+'on',1);
	oItem.offSubLayer = new elcLayer(oLayer.name+'off',1);
	if (this.offLayer && !this.off){
		this.off = new elcLayer(this.offLayer,1);
	}
	if (sLayerName){
		oItem.layer = new elcLayer(sLayerName,1);
		(isSlider)?this.addSlidingLayer(oItem, sOrient):oItem.layerType='static';
	}
	oItem.isDefault = (isDefault)?1:0;
	if (isDefault && !oItem.image.state) oItem.image.setOn();
}

elcNavigation.prototype.addImgItem = function(oImage,sLayerName,isDefault,sNavType,isSlider,sOrient){
	idx = this.Items.length;
	oItem = this.Items[this.Items.length] = new String(oImage.name);
	oItem.type = 'image';
	oItem.image = new elcImage(oImage);
	if (sNavType == 'dom') {
		oItem.myLayer = new elcLayer(oImage.name,0);
	}
	if (this.offLayer && !this.off){
		this.off = new elcLayer(this.offLayer,1);
	}
	if (sLayerName) {
		oItem.layer = new elcLayer(sLayerName,1);
		(isSlider)?this.addSlidingLayer(oItem, sOrient):oItem.layerType='static';
	}
	oItem.isDefault = (isDefault)?1:0;
	if (isDefault && !oItem.image.state) oItem.image.setOn();
}

elcNavigation.prototype._processNS4SetOn = function(oItemObj){
	oItemObj.onSubLayer.show();
}

elcNavigation.prototype._processDOMSetOn = function(oItemObj){
	oItemObj.image.setOn();
	if (typeof this.hSetOn == 'function') this.hSetOn(oItemObj);
}

elcNavigation.prototype._processSubLayerSetOn = function(key){
	var obj = this.getItem(key);
	var isSlider = (typeof this.hasSlidingLayer == 'function' && this.hasSlidingLayer(key));
	(isSlider)?this._processSlidingSubLayerSetOn(key):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);
	var isSlider = (typeof this.hasSlidingLayer == 'function' && this.hasSlidingLayer(key));
	(isSlider)?this._processSlidingSubLayerSetOff(key):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
		}
	}
	if (oItem.type=="ns4css2"){
		this._processNS4SetOn(oItem);
	} else if (oItem.type=="image" || oItem.type=="dom"){
		this._processDOMSetOn(oItem);
	} else {
		// type not recognized
		return false;
	}
	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){
		if (oItem.type=="ns4css2"){
			oItem.onSubLayer.hide();
		} else {
			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;
}
