/*******************************
*
* elcNavigation.js
* 
* navigation and menuing class
*
* Author: DoublePrime
* References: ELCI Clinique navigation.js created by Razorfish
* Version: 0.2
*
* Comments
*
*******************************/
DEBUG = (window.location.search.indexOf('debug=1') > -1) ? true : false;
var defaultTimeoutDuration = 10000;

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;
	elcNavigation.Items[elcNavigation.Items.length] = this;
}

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

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

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

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

elcNavigation.prototype.addImgItem = function(oImage,sLayerName,isDefault,sNavType){
	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);
	oItem.isDefault = (isDefault)?1:0;
	if (isDefault && !oItem.image.state) {
		oItem.image.setOn();
		if (typeof this.hSetOn == 'function') {
			this.hSetOn(oItem);
		}
	}
}

elcNavigation.prototype.addNS4Item = function(oLayer,sLayerName,isDefault){
	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);
	oItem.isDefault = isDefault;
	if (isDefault && !oItem.image.state) oItem.image.setOn();
}

elcNavigation.prototype.setDefaultItem = function(key){
	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
	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+"')",defaultTimeoutDuration);
		}
	}
	if (typeof this.hSetOn == 'function') {
		this.hSetOn(oItem);
	}
}

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

elcNavigation.prototype.getItem = function(key){
	for (i=0;i<this.Items.length;i++){
		oItem = this.Items[i];
		if (oItem == key) {
			return oItem;
		}
	}
	return null;
}

elcNavigation.prototype.setOn = function(key){
	var oItem = this.getItem(key);
	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 );
			alert('oItem info: ' + oItem );
			return
		}
	}
	if (oItem.type=="ns4css2"){
		oItem.onSubLayer.show();
	} else {
		oItem.image.setOn();
		if (typeof this.hSetOn == 'function') {
			this.hSetOn(oItem);
		}
	}
	if(oItem.layer){
		oItem.layer.show();
		if(this.hasTimeout){
			clearTimeout(eval(this.name+".timeout"));
			this.timeout = setTimeout(this.name+".setOff('"+key+"')",defaultTimeoutDuration);
		}
	} 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.setOn: missing object ' + key );
			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) oItem.layer.hide();
	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.sendURL = function(url){
	location.href = url;
}
