/**********************************************************************/
/**********************************************************************/
/***   Copyright (c) 2006 - 2008, PLAN e d.o.o.											***/
/***   Vse pravice pridržane. All rights reserved.									***/
/***   v 1.008																											***/
/**********************************************************************/
/**********************************************************************/
adminTipOpened = 0;

adminTipClose = false;


var Spry;
if (!Spry) Spry = {};
if (!Spry.Widget) Spry.Widget = {};

Spry.Widget.adminMenuNav = function(element, issOpen, anmEnb, anmType, clickEnb, mouseOutEnb, xOffSet, yOffSet, ttDirecrion, ttMode, clickURL, clickTarget, opts)
{
	
	this.init(element, issOpen, anmEnb, anmType, ttMode);
	//this.element.style.visibility = "visible";
	this.element.style.display = "block";
	this.toolDirect = ttDirecrion;
	this.xOffSet = xOffSet;
	this.yOffSet = yOffSet;
	this.tabOpen = issOpen;
	this.idName = element;
	
	document[element] = this;
	
	if(issOpen) {
		adminTipID = this;
		adminTipClose = true;

		adminTipOpened = 1;
		adminTipRoot = this;
	}
		
	Spry.Widget.adminMenuNav.setOptions(this, opts);
	
	this.attachBehaviors(clickEnb, mouseOutEnb, clickURL, clickTarget);
};

Spry.Widget.adminMenuNav.prototype.init = function(element, issOpen, anmEnb, anmType, ttMode)
{
	this.element = this.getElement(element);
	//alert(this.element.style.zIndex)
	this.focusElement = null;
	
	if(ttMode == 1) {
		this.hoverClass = "adminMenuNavTabHover1";
		this.openClass = "adminMenuNavOpen1";
		this.closedClass = "adminMenuNavClosed1";
		this.focusedClass = "adminMenuNavFocused1";
	} else {
		this.hoverClass = "adminMenuNavTabHover";
		this.openClass = "adminMenuNavOpen";
		this.closedClass = "adminMenuNavClosed";
		this.focusedClass = "adminMenuNavFocused";
	}
	this.enableAnimationOpen = anmEnb;
	this.enableAnimationClose = anmEnb;

	this.enableKeyboardNavigation = true;
	this.animator = null;
	this.hasFocus = false;
	this.contentIsOpen = issOpen;
	
	this.anmType = anmType;
	

};

Spry.Widget.adminMenuNav.prototype.getElement = function(ele)
{
	if (ele && typeof ele == "string")
		return document.getElementById(ele);
	return ele;
};

Spry.Widget.adminMenuNav.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.adminMenuNav.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.adminMenuNav.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.adminMenuNav.prototype.setDisplay = function(ele, display)
{
	if( ele ) {
		ele.style.display = display;
	}
};

Spry.Widget.adminMenuNav.setOptions = function(obj, optionsObj, ignoreUndefinedProps)
{
	if (!optionsObj)
		return;
	for (var optionName in optionsObj)
	{
		if (ignoreUndefinedProps && optionsObj[optionName] == undefined)
			continue;
		obj[optionName] = optionsObj[optionName];
	}
};

Spry.Widget.adminMenuNav.prototype.onTabMouseOver = function()
{
	if(!this.contentIsOpen) {
		this.addClassName(this.getTab(), this.hoverClass);
	}
	
	
};

Spry.Widget.adminMenuNav.prototype.onTabMouseOut = function()
{
	this.removeClassName(this.getTab(), this.hoverClass);
	if(!this.contentIsOpen) {
	}
};
Spry.Widget.adminMenuNav.prototype.onTabClick = function()
{
		
	if (this.isOpen() && !this.tabOpen) {
		
		this.close(true);
	} else {
		this.open();
this.removeClassName(this.getTab(), this.hoverClass);
	}
	this.tabOpen = null;
	//this.focus();
};
Spry.Widget.adminMenuNav.prototype.onTabOver = function()
{
		
		this.open();
	//this.focus();
};
Spry.Widget.adminMenuNav.prototype.onTabOut = function()
{
		
		this.close(true);
		this.tabOpen = null;
	//this.focus();
};

Spry.Widget.adminMenuNav.prototype.open = function()
{
	var content = this.admGetContent();
	var direction = this.toolDirect;
	var yOffSet = this.yOffSet;
	var tab = this.getTab();

	this.contentIsOpen = true;
	
	
	
	if (this.enableAnimationOpen) {
		if (this.animator)
			this.animator.stop();
			
			this.animator = new Spry.Widget.adminMenuNav.PanelAnimator(this, true);
			this.animator.start();
			
			if(adminTipOpened==1 && adminTipRoot!= this) {
				adminTipRoot.close(false);
				
			}

	} else {
		this.setDisplay(content, "block");
		if(direction=="T") {
			var mT = content.offsetHeight+tab.offsetHeight+yOffSet;
			content.style.marginTop = -mT+"px";
		} else
			content.style.marginTop = yOffSet+"px";
			
		if(adminTipOpened==1 && adminTipRoot!= this) {
			adminTipRoot.close(false);
			
		}
	}
	this.element.style.zIndex = 1000;
	
	this.removeClassName(this.element, this.closedClass);
	this.addClassName(this.element, this.openClass);
	
	adminTipRoot = this;
	adminTipOpened = 1;
			
	//alert(this.element.style.zIndex+" == "+this.element.zRestore)
};

Spry.Widget.adminMenuNav.prototype.close = function(enab)
{
	var element = this.element;
	var tab = this.getTab();
	var content = this.admGetContent();
	
	this.contentIsOpen = false;
	
	
	if (this.enableAnimationClose && enab) {
		
		element.style.zIndex = element.zRestore;
		if (this.animator)
			this.animator.stop();
		this.animator = new Spry.Widget.adminMenuNav.PanelAnimator(this, false);
		this.animator.start();
		
		this.removeClassName(element, this.openClass);
		this.addClassName(element, this.closedClass);
	} else {
		
		element.style.zIndex = element.zRestore;
		this.setDisplay(content, "none");
	
		
		this.removeClassName(element, this.openClass);
		this.addClassName(element, this.closedClass);
		
	}
};
Spry.Widget.adminMenuNav.prototype.onFocus = function(e)
{
	this.hasFocus = true;
	this.addClassName(this.element, this.focusedClass);
};
Spry.Widget.adminMenuNav.prototype.onBlur = function(e)
{
	this.hasFocus = false;
	this.removeClassName(this.element, this.focusedClass);
};

Spry.Widget.adminMenuNav.ENTER_KEY = 13;
Spry.Widget.adminMenuNav.SPACE_KEY = 32;

Spry.Widget.adminMenuNav.prototype.onKeyDown = function(e)
{
	var key = e.keyCode;
	if (!this.hasFocus || (key != Spry.Widget.adminMenuNav.ENTER_KEY && key != Spry.Widget.adminMenuNav.SPACE_KEY))
		return true;
	
	if (this.isOpen())
		this.close(true);
	else
		this.open();

	if (e.stopPropagation)
		e.stopPropagation();
	if (e.preventDefault)
		e.preventDefault();

	return false;
};
Spry.Widget.adminMenuNav.prototype.getURL = function(clickEnb, mouseOutEnb, clickURL, clickTarget)
{
	if (!clickEnb)
		if(clickURL)
			window.open(clickURL, clickTarget)	
	
};
Spry.Widget.adminMenuNav.prototype.attachPanelHandlers = function(clickEnb, mouseOutEnb, clickURL, clickTarget)
{
	var panel = this.element;
	var tab = this.getTab();
	var content = this.admGetContent();
	
	if (!tab)
		return;

	var self = this;	
	
	if(clickEnb) {
		Spry.Widget.adminMenuNav.addEventListener(tab, "click", function(e) { return self.onTabClick(); }, false);
		Spry.Widget.adminMenuNav.addEventListener(tab, "mouseover", function(e) { return self.onTabMouseOver(); }, false);
		Spry.Widget.adminMenuNav.addEventListener(tab, "mouseout", function(e) { return self.onTabMouseOut(); }, false);
	} else {
		Spry.Widget.adminMenuNav.addEventListener(tab, "click", function(e) { return self.getURL(clickEnb, clickURL, clickTarget); }, false);
		Spry.Widget.adminMenuNav.addEventListener(panel, "mouseover", function(e) { return self.onTabOver(); }, false);
		
		if(mouseOutEnb)
		Spry.Widget.adminMenuNav.addEventListener(panel, "mouseout", function(e) { return self.onTabOut(); }, false);
		
	}

	if (this.enableKeyboardNavigation)
	{
		// XXX: IE doesn't allow the setting of tabindex dynamically. This means we can't
		// rely on adding the tabindex attribute if it is missing to enable keyboard navigation
		// by default.

		// Find the first element within the tab container that has a tabindex or the first
		// anchor tag.
		
		var tabIndexEle = null;
		var tabAnchorEle = null;

		this.preorderTraversal(tab, function(node) {
			if (node.nodeType == 1 /* NODE.ELEMENT_NODE */)
			{
				var tabIndexAttr = tab.attributes.getNamedItem("tabindex");
				if (tabIndexAttr)
				{
					tabIndexEle = node;
					return true;
				}
				if (!tabAnchorEle && node.nodeName.toLowerCase() == "a")
					tabAnchorEle = node;
			}
			return false;
		});

		if (tabIndexEle)
			this.focusElement = tabIndexEle;
		else if (tabAnchorEle)
			this.focusElement = tabAnchorEle;

		if (this.focusElement)
		{
			//Spry.Widget.adminMenuNav.addEventListener(this.focusElement, "focus", function(e) { return self.onFocus(e, this); }, false);
			Spry.Widget.adminMenuNav.addEventListener(this.focusElement, "blur", function(e) { return self.onBlur(e, this); }, false);
			Spry.Widget.adminMenuNav.addEventListener(this.focusElement, "keydown", function(e) { return self.onKeyDown(e); }, false);
		}
	}
};

Spry.Widget.adminMenuNav.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) {}
};

Spry.Widget.adminMenuNav.prototype.preorderTraversal = function(root, func)
{
	var stopTraversal = false;
	if (root)
	{
		stopTraversal = func(root);
		if (root.hasChildNodes())
		{
			var child = root.firstChild;
			while (!stopTraversal && child)
			{
				stopTraversal = this.preorderTraversal(child, func);
				try { child = child.nextSibling; } catch (e) { child = null; }
			}
		}
	}
	return stopTraversal;
};

Spry.Widget.adminMenuNav.prototype.attachBehaviors = function(clickEnb, clickURL, clickTarget)
{
	var panel = this.element;
	var tab = this.getTab();
	var content = this.admGetContent();
	var direction = this.toolDirect;
	var xOffSet = this.xOffSet;
	var yOffSet = this.yOffSet;
	

	content.style.marginLeft = xOffSet+"px";
	if(direction=="T") {
		
		var mT = content.offsetHeight+tab.offsetHeight+yOffSet;
		content.style.marginTop = -mT+"px";
	} 
	else
		content.style.marginTop = yOffSet+"px";
		
	if (this.contentIsOpen || this.hasClassName(panel, this.openClass))
	{
		panel.zRestore = panel.style.zIndex;
		
		panel.style.zIndex = 1000;
		this.addClassName(panel, this.openClass);
		this.setDisplay(content, "block");
		this.contentIsOpen = true;
	}
	else
	{	
		panel.zRestore = panel.style.zIndex;
		this.removeClassName(panel, this.openClass);
		this.addClassName(panel, this.closedClass);
		this.setDisplay(content, "none");
		this.contentIsOpen = false;
	}
	
		this.attachPanelHandlers(clickEnb, clickURL, clickTarget);
	
};

Spry.Widget.adminMenuNav.prototype.getTab = function()
{
	return this.getElementChildren(this.element)[0];
};

Spry.Widget.adminMenuNav.prototype.admGetContent = function()
{
	return this.getElementChildren(this.element)[2];
};

Spry.Widget.adminMenuNav.prototype.isOpen = function()
{
	return this.contentIsOpen;
};

Spry.Widget.adminMenuNav.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.adminMenuNav.prototype.focus = function()
{
	if (this.focusElement && this.focusElement.focus)
		this.focusElement.focus();
};

/////////////////////////////////////////////////////

Spry.Widget.adminMenuNav.PanelAnimator = function(panel, doOpen, opts)
{
	this.timer = null;
	this.interval = 0;
	this.stepCount = 0;

	this.fps = 0;
	this.steps = 15;
	if (this.anmType == "fade")
		this.duration = 500;
	else
		this.duration = 100;
	
	this.onComplete = null;

	this.panel = panel;
	this.content = panel.admGetContent();
	this.tab = panel.getTab();
	this.panelData = [];
	this.doOpen = doOpen;
	this.direction = this.panel.toolDirect;
	var xOffSet = this.xOffSet;
	var yOffSet = this.yOffSet;
	this.anmType = panel.anmType;

	Spry.Widget.adminMenuNav.setOptions(this, opts);
	
	
	// If caller specified speed in terms of frames per second,
	// convert them into steps.

	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;
	var curOpacity = 1 ? 1 : 0;
	
	if (doOpen && c.style.display == "none") {
		this.fromHeight = 0;
		this.fromOpacity = 0;
		
	} else {
		this.fromHeight = curHeight;
		this.fromOpacity = curOpacity;
		
	}

	if (!doOpen) {
		this.toHeight = 0;
		this.toOpacity = 0;
		
	} else {
		if (c.style.display == "none") {
			c.style.visibility = "hidden";
			c.style.display = "block";
			
		}
		c.style.height = "";
		this.toHeight = c.offsetHeight;
		
		//c.style.opacity = "";
		//c.style.filter = 'alpha(opacity= )';
		this.toOpacity = 1;
	}
	if (this.anmType == "slide") {
	this.slideIncrement = (this.toHeight - this.fromHeight) / this.steps;
	c.style.height = this.fromHeight + "px";
	
	} else if (this.anmType == "fade") {
		this.fadeIncrement = (this.toOpacity - this.fromOpacity) / this.steps;
		c.style.height = this.toHeight + "px";
		c.style.opacity = this.fromOpacity;
		c.style.filter = 'alpha(opacity='+this.fromOpacity*100+')';
	
	}
	
	this.overflow = c.style.overflow;
	c.style.overflow = "hidden";
	c.style.visibility = "visible";
	
	c.style.display = "block";
};

Spry.Widget.adminMenuNav.PanelAnimator.prototype.start = function()
{
	
	var self = this;
	this.timer = setTimeout(function() { self.stepAnimation(); }, this.interval);
};

Spry.Widget.adminMenuNav.PanelAnimator.prototype.stop = function()
{
	if (this.timer)
	{
		clearTimeout(this.timer);
	
		// If we're killing the timer, restore the overflow
		// properties on the panels we were animating!

		if (this.stepCount < this.steps)
			this.content.style.overflow = this.overflow;
	}

	this.timer = null;
	
};
Spry.Widget.adminMenuNav.PanelAnimator.prototype.stepAnimation = function()
{
	++this.stepCount;
	//alert(this.anmType);
	
	if(this.anmType == "slide") {
		this.animate();
		
	} else if(this.anmType == "fade")  {
		this.fade();

	}
	
	
	if (this.stepCount < this.steps)
		this.start();
	
};


Spry.Widget.adminMenuNav.PanelAnimator.prototype.animate = function()
{
	
	if (this.stepCount >= this.steps)
	{
		if (!this.doOpen)
		this.content.style.display = "none";
		this.content.style.overflow = this.overflow;
		this.content.style.height = this.toHeight + "px";
		
	}
	else
	{
		this.fromHeight += this.slideIncrement;
		this.content.style.height = this.fromHeight + "px";
	}
};
Spry.Widget.adminMenuNav.prototype.setOpacity = function(ele, value) {
	//alert("asdasdasd");
 	ele.style.opacity = value / 100;
 	ele.style.filter = 'alpha(opacity=' + value + ')';
}
Spry.Widget.adminMenuNav.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 + ')';
		
	}
};


