/* extends elcNavigation */

/* safety check for onmouseover handler */
elcNavigation.prototype.verifyMenuSetOn = function(sImageName,sLayerName,isDefault,sSliderBasename) {
	var oItem = this.getItem(sImageName);
	if (oItem) {
		if (oItem.layerName) {
			if (oItem.layer) {
				this.setOn(sImageName);
			} else {
				if (elcLayer(oItem.layerName)) {
					oItem.layer = new elcLayer(oItem.layerName);
					this.addSlidingLayer(oItem, sSliderBasename);
					this.setOn(sImageName);
				}
			}
		} else {
			this.setOn(sImageName);
		}
	}
}

elcNavigation.prototype.addSlidingLayer = function(oItemObj, sSliderBasename) {
	if (oItemObj && oItemObj.layer) {
		oItemObj.hasSlidingLayer = true;
		oItemObj.slider = new SlidingNavLayer(sSliderBasename,oItemObj,this);
	}
}

elcNavigation.prototype.slide = function(key, sDir) {
	var oItemObj = this.getItem(key);
	if (oItemObj && oItemObj.hasSlidingLayer) {
		oItemObj.slider.slide(sDir);
	}
}

elcNavigation.prototype.stop = function(key) {
	var oItemObj = this.getItem(key);
	if (oItemObj && oItemObj.hasSlidingLayer) {
		oItemObj.slider.stop();
	}
}

elcNavigation.prototype.addSliderArrow = function(key, oImgObj, sDir) {
	this.sliderArrows[this.sliderArrows.length] = new sliderArrow(this.getItem(key),key, oImgObj, sDir);

	function sliderArrow(oItemObj, oItemName, oImgObj, sDir) {
		if (oItemObj) {
			this.parentItem = oItemObj;
			this.parentItemName = oItemName;
			this.imgObj = new elcImage(oImgObj,null,true);
			this.dir = sDir;
			return this;
		}
		return null;
	}
}

elcNavigation.prototype.getSliderArrow = function(key, sDir) {
	for (var i=0; i<this.sliderArrows.length; i++) {
		if ((this.sliderArrows[i].parentItemName == key) && (this.sliderArrows[i].dir == sDir)) {
			return this.sliderArrows[i];
		}
	}
	return null;
}

function SlidingNavLayer(sSliderBasename, oParentObj, oParentNavObj) {
	this.sliderBasename = sSliderBasename;
	this.slideIncrement = (typeof(window.NAV_SLIDE_INCREMENT) != "undefined")? window.NAV_SLIDE_INCREMENT : 1;
	this.slideTimeout = (typeof(window.NAV_SLIDE_TIMEOUT) != "undefined")? window.NAV_SLIDE_TIMEOUT : 25;
	this.maxLoops = (typeof(window.NAV_MAX_LOOPS) != "undefined")? window.NAV_MAX_LOOPS : 500;
	this.curLoops = 0;
	this.clipHeight = (typeof(window.NAV_CLIP_HEIGHT) != "undefined")? window.NAV_CLIP_HEIGHT : 77;
	this.contentLayerOffsetTop = 28;
	this.parentObj = oParentObj;
	this.parentNavObj = oParentNavObj;
	this.contentLayer = new elcLayer(this.sliderBasename + "slide");
	this.contentLayer.defaultY = this.contentLayer.y;
	this.clipLayer = new elcLayer(this.sliderBasename + "menu");
	this.canSlideUp = false;
	this.canSlideDown = false;
	this.upArrow = null;
	this.downArrow = null;
}


SlidingNavLayer.prototype.slide = function(sDir) {
	if (this.isSliding) {
		window.clearInterval(this.slideTimer);
	}
	if (this.upArrow && this.downArrow) {
		if ((typeof(sDir != "undefined")) && (sDir == 'up') && this.canSlideUp) {
			if (this.parentObj.slider) {
				eval('this.slideTimer = window.setInterval("' + this.parentNavObj.name + '.getItem(\'' + this.sliderBasename + '\').slider._slideLoop(' + this.slideIncrement + ');",this.slideTimeout);');
				this.upArrow.setOver();
				this.upArrow.isOver = true;
				this.isSliding = true;
			}
		} else if ((typeof(sDir != "undefined")) && (sDir == 'down') && this.canSlideDown) {
			if (this.parentObj.slider) {
				eval('this.slideTimer = window.setInterval("' + this.parentNavObj.name + '.getItem(\'' + this.sliderBasename + '\').slider._slideLoop(' + -this.slideIncrement + ');",this.slideTimeout);');
				this.downArrow.setOver();
				this.downArrow.isOver = true;
				this.isSliding = true;
			}
		}
	}
}

SlidingNavLayer.prototype.stop = function(iFinalY) {
	if (this.isSliding) {
		this.isSliding = false;
		this.curLoops = 0;
		if (this.upArrow) {
			this.upArrow.isOver = null;
		}
		if (this.downArrow) {
			this.downArrow.isOver = null;
		}
		window.clearInterval(this.slideTimer);
	}
	if (typeof(iFinalY) != "undefined") {
		this.contentLayer.moveTo(this.contentLayer.x,iFinalY);
	}
	this._resetArrows();
}

SlidingNavLayer.prototype.set = function() {
	this._setArrows();
}

SlidingNavLayer.prototype.reset = function() {
	this.stop(this.contentLayer.defaultY);
}

SlidingNavLayer.prototype._setArrows = function() {
	if (!this.upArrow) {
		var oSliderUpArrow = this.parentNavObj.getSliderArrow(this.sliderBasename, "up");
		this.upArrow = ((oSliderUpArrow) && (oSliderUpArrow.imgObj))? oSliderUpArrow.imgObj : null;
	}
	if (!this.downArrow) {
		var oSliderDownArrow = this.parentNavObj.getSliderArrow(this.sliderBasename, "down");
		this.downArrow = ((oSliderUpArrow) && (oSliderDownArrow.imgObj))? oSliderDownArrow.imgObj : null;
	}
	this._resetArrows();
}

SlidingNavLayer.prototype._positionCheck = function() {
	var layerHeightDiff = (this._getContentHeight() - this.contentLayerOffsetTop) - this.clipHeight;
	var layerYdiff = this.contentLayer.defaultY - this.contentLayer.y;
	this.canSlideUp = (this.contentLayer.y < this.contentLayer.defaultY);
	this.canSlideDown = ((layerHeightDiff > 0) && (layerYdiff < layerHeightDiff));
}

SlidingNavLayer.prototype._resetArrows = function() {
	this._positionCheck();
	if (this.upArrow && (!this.upArrow.isOver)) {
		if (this.canSlideUp) {
			this.upArrow.setOn();
		} else {
			this.upArrow.setOff();
		}
	}
	if (this.downArrow && (!this.downArrow.isOver)) {
		if (this.canSlideDown) {
			this.downArrow.setOn();
		} else {
			this.downArrow.setOff();
		}
	}
}

SlidingNavLayer.prototype._getContentHeight = function() {
	var topAnchorY = eval('getAnchorPosition("' + this.sliderBasename + '_top").y;');
	var bottomAnchorY = eval('getAnchorPosition("' + this.sliderBasename + '_bottom").y;');
	return (bottomAnchorY - topAnchorY);
}

SlidingNavLayer.prototype._slideLoop = function(iSlideIncrement) {
	if ((this.curLoops < this.maxLoops) && ((this.canSlideUp && (iSlideIncrement > 0)) || (this.canSlideDown && (iSlideIncrement < 0)))) {
		this.curLoops++;
		var layerHeightDiff = (this._getContentHeight() - this.contentLayerOffsetTop) - this.clipHeight;
		var layerYdiff = this.contentLayer.defaultY - (this.contentLayer.y + iSlideIncrement);
		var willMaxUp = ((this.contentLayer.y + iSlideIncrement) >= this.contentLayer.defaultY);
		var willMaxDown = (layerYdiff >= layerHeightDiff);
		
		if (willMaxUp || willMaxDown) {
			if (willMaxUp) {
				this.stop(this.contentLayer.defaultY);
			}
			if (willMaxDown) {
				this.stop(this.contentLayer.defaultY + this.clipHeight - (this._getContentHeight() - this.contentLayerOffsetTop));
			}
		} else {
			this.contentLayer.moveBy(0,iSlideIncrement);
			this._resetArrows();
		}
	}
}
