/**********************************************************************/
/**********************************************************************/
/***   Copyright (c) 2006 - 2008, PLAN e d.o.o.											***/
/***   Vse pravice pridržane. All rights reserved.									***/
/***   v 1.008																											***/
/**********************************************************************/
/**********************************************************************/

var Spry;
if (!Spry) Spry = {};
if (!Spry.Widget) Spry.Widget = {};

Spry.Widget.MenuNavigation = function(btnLevel,element,issOpen,anmEnb,anmType,xOffSet,yOffSet,clickURL,clickTarget) {
	this.root = document.getElementById("menuNavGroup");
	document[element] = this;
	if(!this.root.clickOffset)
	this.root.clickOffset = 0;
	
	this.init(btnLevel,element,issOpen,anmEnb,anmType,xOffSet,yOffSet,clickURL,clickTarget);
	this.attachBehaviors();
	
	//this.element.style.visibility = "visible";
	this.element.style.display = "block";
};

Spry.Widget.MenuNavigation.prototype.init = function(btnLevel,element,issOpen,anmEnb,anmType,xOffSet,yOffSet,clickURL,clickTarget) {
	
/*
stringObject.charAt(index)  									Returns the character at a specified position
stringObject.charCodeAt(index) 								Returns the Unicode of the character at a specified position
stringObject.concat(string1,"string2")			 	Joins two or more strings

indexOf() 																		Returns the position of the first occurrence of a specified string value in a string
lastIndexOf() 																Returns the position of the last occurrence of a specified string value, searching backwards from the specified position in a string

match("string") 					 					 					Searches for a specified value in a string
replace("findstring","newstring") 			 			Replaces some characters with some other characters in a string
search(/string/i) 				 				 				 		Searches a string for a specified value (i = not case sensitive

slice(start,end) 		 				 				 					Extracts a part of a string and returns the extracted part in a new string
split("sepstring", howmany) 									Splits a string into an array of strings
substr(start,length) 			 				 						Extracts a specified number of characters in a string, from a start index
substring(start,end) 	 				 				 				Extracts the characters in a string between two specified indices

toSource() 																		Represents the source code of an object
valueOf() 											 							Returns the primitive value of a String object


*/
	this.urlpause = 0;
	
	this.idName = element;
	this.element = this.getElement(element);
	this.content = this.getContent() ? this.getContent():false;
	this.hasSub = this.content ? true:false;
	this.btn = this.getBtn();
	this.xOffSet = xOffSet;
	this.yOffSet = yOffSet;
	this.btnIsOpen = issOpen;
	this.btnLevel = btnLevel;
	this.anmType = anmType;
	this.anmEnable = anmEnb;
	this.animator = null;
	this.clickURL = clickURL;
	this.clickTarget = clickTarget;
	
	var str = new String(this.idName);
	var splitNum = str.search(/_/i)+1;
	var newStr = str.substring(splitNum);
	
	var levelArray = newStr.split("_")
	var setIndex = levelArray[this.btnLevel];
	
	if(this.btnIsOpen)
	this.root["lastIndex"+this.btnLevel] = setIndex;
	this.index = setIndex;
	
	
	if(this.btnLevel==0) {
		this.textCol = this.getTableColumn(1);
		this.eventCol = this.getTableColumn(0);
		
		this.openedClass = "MainNavigationOpened";
		this.closedClass = "MainNavigationClosed";
	} else {
		this.textCol = this.getTableColumn(1);
		this.eventCol = this.getTableColumn(0);	
		
		this.openedClass = "SubNavigationOpened";
		this.closedClass = "SubNavigationClosed";
	}
	
	this.btnClass = "Btn";
	this.btnHoverClass = "BtnHover";
	this.textColClass = "TextCol";
	this.textColHoverClass = "TextColHover";
	
	if(!this.hasSub) {
		this.eventColClass = "EventColDisabled";
		this.eventColHoverClass = "EventColDisabledHover";
	} else {
		this.eventColClass = "EventCol";
		this.eventColHoverClass = "EventColHover";
	}
	
	if(this.btnIsOpen) {
		this.root["subRoot"+this.btnLevel] = this;
		this.root.mLength=this.btnLevel;
	}
};

Spry.Widget.MenuNavigation.prototype.attachBehaviors = function() {
	
	if(this.hasSub) {
		this.content.style.marginLeft = this.xOffSet+"px";
		this.content.style.marginTop = this.yOffSet+"px";
	}
	
	this.addClassName(this.btn, this.btnClass);
	this.addClassName(this.textCol, this.textColClass);
	this.addClassName(this.eventCol, this.eventColClass);
	
	if ((this.btnIsOpen || this.hasClassName(this.element, this.openedClass))) {
		this.addClassName(this.element, this.openedClass);
		
		if(this.hasSub) {
			this.setDisplay(this.content, "block");
		}
		this.btnIsOpen = true;
	}	else {	
		this.addClassName(this.element, this.closedClass);
		
		if(this.hasSub) {
			this.setDisplay(this.content, "none");
		}
		this.btnIsOpen = false;
	}

	this.attachPanelHandlers();
	
};

Spry.Widget.MenuNavigation.prototype.attachPanelHandlers = function() {
	if (!this.btn)
		return;

	var self = this;
	
	if(this.hasSub) {
		Spry.Widget.MenuNavigation.addEventListener(this.eventCol, "click", function(e) { return self.onBtnClick(true); }, false);
		
		Spry.Widget.MenuNavigation.addEventListener(this.textCol, "click", function(e) { return self.onBtnClick(); }, false);
	} else if(this.btnLevel==0)
		Spry.Widget.MenuNavigation.addEventListener(this.textCol, "click", function(e) { return self.onBtnClick(); }, false);
		
	if(this.clickURL) {
		Spry.Widget.MenuNavigation.addEventListener(this.textCol, "click", function(e) { return self.getURL(self.clickURL, self.clickTarget); }, false);
	} 
	
	
	Spry.Widget.MenuNavigation.addEventListener(this.btn, "mouseover", function(e) { return self.onBtnMouseOver("btnGroup"); }, false);
	Spry.Widget.MenuNavigation.addEventListener(this.btn, "mouseout", function(e) { return self.onBtnMouseOut("btnGroup"); }, false);

	Spry.Widget.MenuNavigation.addEventListener(this.textCol, "mouseover", function(e) { return self.onBtnMouseOver("textCol"); }, false);
	Spry.Widget.MenuNavigation.addEventListener(this.textCol, "mouseout", function(e) { return self.onBtnMouseOut("textCol"); }, false);

	Spry.Widget.MenuNavigation.addEventListener(this.eventCol, "mouseover", function(e) { return self.onBtnMouseOver("eventCol"); }, false);
	Spry.Widget.MenuNavigation.addEventListener(this.eventCol, "mouseout", function(e) { return self.onBtnMouseOut("eventCol"); }, false);
	
};

Spry.Widget.MenuNavigation.prototype.onBtnMouseOver = function(e) {
	if(e=="btnGroup") {
		this.addClassName(this.btn, this.btnHoverClass);
	} else if(e=="textCol") {
		this.addClassName(this.textCol, this.textColHoverClass);
	} else if(e=="eventCol") {
		this.addClassName(this.eventCol, this.eventColHoverClass);
	}
};

Spry.Widget.MenuNavigation.prototype.onBtnMouseOut = function(e) {
	if(e=="btnGroup") {
		this.removeClassName(this.btn, this.btnHoverClass);
	} else if(e=="textCol") {
		this.removeClassName(this.textCol, this.textColHoverClass);
		//if(!this.btnIsOpen)
	} else if(e=="eventCol") {
		this.removeClassName(this.eventCol, this.eventColHoverClass);
	}
};

Spry.Widget.MenuNavigation.prototype.onBtnClick = function(type) {
	if(this.btnLevel==0) {
		var n = new Number(this.index)
		var l = new Number(this.root["lastIndex"+this.btnLevel])
		
		if(n>l) {
			var mOffset = new Number((this.root["subRoot"+this.btnLevel].content)?this.root["subRoot"+this.btnLevel].content.offsetHeight:0);
			var scrollH = new Number(this.getScrollXY());
			var doMath = new Number(scrollH-mOffset);
			var newScroll = (doMath>0)?doMath:0;
			var newOffset = new Number(this.root.clickOffset+mOffset);
			
			//this.root.style.marginTop=newOffset+"px";
			//alert(scrollH)
			
			
			window.scrollTo(0,newScroll);

			
			//trace(1, newScroll)
			this.root.clickOffset = newOffset;
		}
		
		
	} 
	if(type) {
		if(this.btnIsOpen) {
			this.open(true);
			this.addClassName(this.btn, this.btnHoverClass);
		} else {
			this.open();
		}
	} 
	if(!type&&!this.btnIsOpen) {
		this.open();

	}
	this.root["lastIndex"+this.btnLevel] = this.index;
	
};
Spry.Widget.MenuNavigation.prototype.getScrollXY = function() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfY;
}
Spry.Widget.MenuNavigation.prototype.getURL = function(clickURL, clickTarget) {
	if(this.urlpause)
		return;
		
	
	if(clickURL) {
		if(clickTarget)
		window.open(clickURL, clickTarget)
		else
		window.open(clickURL, "_self")
	}
};
/*
this.openedClass //this.element
this.closedClass //this.element

this.btnClass //this.btn
this.btnHoverClass //this.btn

this.textColClass //this.textCol
this.textColHoverClass //this.textCol

this.eventColClass //this.eventCol
this.eventColHoverClass //this.eventCol
*/

Spry.Widget.MenuNavigation.prototype.open = function(ll) {
	this.btnIsOpen = true;
	
	if (this.anmEnable)	{
		if (this.animator)
			this.animator.stop();
		this.animator = new Spry.Widget.MenuNavigation.PanelAnimator(this, true);
		this.animator.start();
	}	else {
		this.setDisplay(this.content, "block");
	} 	
	
	this.removeClassName(this.element, this.closedClass);
	this.addClassName(this.element, this.openedClass);
	
	for(var i = this.root.mLength; i>=this.btnLevel; i--) {
			if(this.root["subRoot"+i]) {
				this.root["subRoot"+i].close();
			}
	}
	this.root["subRoot"+this.btnLevel] = this;
	if(ll)
	this.root.mLength = this.btnLevel-1;
	else
	this.root.mLength = this.btnLevel;
	

};
Spry.Widget.MenuNavigation.prototype.close = function() {
	this.btnIsOpen = false;
	if (this.anmEnable) {
		if (this.animator)
			this.animator.stop();
		this.animator = new Spry.Widget.MenuNavigation.PanelAnimator(this, false);
		this.animator.start();
	} else {
		this.setDisplay(this.content, "none");
	} 	
	
	this.removeClassName(this.element, this.openedClass);
	this.addClassName(this.element, this.closedClass);
};










Spry.Widget.MenuNavigation.PanelAnimator = function(panel, doOpen, opts)
{
	this.timer = null;
	this.interval = 0;
	this.stepCount = 0;
	this.fps = 0;
	this.steps = 20;
	this.duration = 300;
	this.onComplete = null;
	
	var doOpen = doOpen;
	
	document["globalCon"+this.btnLevel] = this.content;

	if (this.fps > 0) {
		this.interval = Math.floor(1000 / this.fps);
		this.steps = parseInt((this.duration + (this.interval - 1)) / this.interval);
	} else if (this.steps > 0)
		this.interval = this.duration / this.steps;

	var c = this.content;
	var curHeight = c.offsetHeight ? c.offsetHeight : 0;
	
	if (doOpen && c.style.display == "none") {
		this.fromHeight = 0;
	} else {
		this.fromHeight = curHeight;
	}

	if (!doOpen) {
		this.toHeight = 0;
		
	} else {
		if (c.style.display == "none") {
			c.style.visibility = "hidden";
			c.style.display = "block";
		}
		c.style.height = "";
		this.toHeight = c.offsetHeight;
		
	}
	if (this.anmType == "slide") {
		
		this.slideIncrement = (this.toHeight - this.fromHeight) / this.steps;
		c.style.height = this.fromHeight + "px";
	} else if (this.anmType == "fade") {
		//Kao fade
	}
	
	this.overflow = c.style.overflow;
	c.style.overflow = "hidden";
	c.style.visibility = "visible";
	
	c.style.display = "block";
};

Spry.Widget.MenuNavigation.PanelAnimator.prototype.start = function()
{
	var self = this;
	this.timer = setTimeout(function() { self.stepAnimation(); }, this.interval);
};

Spry.Widget.MenuNavigation.PanelAnimator.prototype.stop = function()
{
	if (this.timer)	{
		clearTimeout(this.timer);
		if (this.stepCount < this.steps)
			this.content.style.overflow = this.overflow;
	}
	this.timer = null;
};
Spry.Widget.MenuNavigation.PanelAnimator.prototype.stepAnimation = function() {
	++this.stepCount;
	
	if(this.anmType == "slide") {
		this.animate();
	} else if(this.anmType == "fade")  {
		//this.fade();
	}
	
	if (this.stepCount < this.steps)
		this.start();
};
Spry.Widget.MenuNavigation.PanelAnimator.prototype.animate = function() {
	if (this.stepCount >= this.steps)  {
		if (!this.doOpen)
		this.content.style.display = "none";
		this.content.style.overflow = this.overflow;
		if(IEE && version<=6) {
			this.content.style.height=this.toHeight + "px";
		} else {
			if(IEE && version==7)
			this.content.style.minHeight = this.toHeight + "px";
			this.content.style.height="auto";
			
		}
	} else {
		this.fromHeight += this.slideIncrement;
		if(IEE && version<=6) {
			this.content.style.height = this.fromHeight + "px";
		} else {
			this.content.style.height = this.fromHeight + "px";
			//alert(this.slideIncrement)
		}
	}
};

Spry.Widget.MenuNavigation.PanelAnimator.prototype.fade = function() {
	if (this.stepCount >= this.steps) {
		if (!this.doOpen)
			this.content.style.display = "none";
		this.content.style.overflow = this.overflow;
		this.content.style.opacity = this.toOpacity;
		this.content.style.filter = 'alpha(opacity=' + this.toOpacity*100 + ')';
	} else {
		this.fromOpacity += this.fadeIncrement;
		this.content.style.opacity = this.fromOpacity;
		this.content.style.filter = 'alpha(opacity=' + this.fromOpacity*100 + ')';
	}
};

Spry.Widget.MenuNavigation.prototype.getElement = function(ele) {
	if (ele && typeof ele == "string")
		return document.getElementById(ele);
	return ele;
};
Spry.Widget.MenuNavigation.prototype.getElementChildren = function(element) {
	var children = [];
	var child = element.firstChild;
	while (child)	{
		if (child.nodeType == 1 /* Node.ELEMENT_NODE */)
			children.push(child);
		child = child.nextSibling;
	}
	return children;
};
Spry.Widget.MenuNavigation.prototype.getBtn = function() {
	return this.getElementChildren(this.element)[0];
};
Spry.Widget.MenuNavigation.prototype.getTableColumn = function(which) {
	var tab = this.btn;
	var obj = tab.getElementsByTagName("td")[which];
	
	//alert(element+" : "+obj)	
	return obj;
};
Spry.Widget.MenuNavigation.prototype.getContent = function() {
	return this.getElementChildren(this.element)[1];
};

Spry.Widget.MenuNavigation.prototype.isOpen = function() {
	return this.btnIsOpen;
};



Spry.Widget.MenuNavigation.prototype.addClassName = function(ele, className) {
	if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) != -1))
		return;
	ele.className += (ele.className ? " " : "") + className;
};

Spry.Widget.MenuNavigation.prototype.removeClassName = function(ele, className) {
	if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) == -1))
		return;
	ele.className = ele.className.replace(new RegExp("\\s*\\b" + className + "\\b", "g"), "");
};

Spry.Widget.MenuNavigation.prototype.hasClassName = function(ele, className) {
	if (!ele || !className || !ele.className || ele.className.search(new RegExp("\\b" + className + "\\b")) == -1)
		return false;
	return true;
};

Spry.Widget.MenuNavigation.prototype.setDisplay = function(ele, display) {
	if( ele ) {
		ele.style.display = display;
	}
};




Spry.Widget.MenuNavigation.addEventListener = function(element, eventType, handler, capture) {
	try {
		if (element.addEventListener)
			element.addEventListener(eventType, handler, capture);
		else if (element.attachEvent)
			element.attachEvent("on" + eventType, handler);
	}
	catch (e) {}
};