/*
 *  K O N N E K T W E B v2
 *  
 *
 *  This code is proprietary. Please do not reuse, modify or distribute.
 * 
 *  All rights reserved.
 * 
 *  Copyright (C)2003,2004,2005 Rafał Lindemann, Stamina
 */



function CSSMenu(id) {
	this.navID = id;
	this.init();
	this.root = null;
	this.closeTimer = 0;
	this.animateTimer = 0;
	this.animatePeriod = 1;
	eval("CSSMenu__"+id+"=this");
	this.items = new Array();
	this.animate = new Array();
//	this.animOpenRoot = new CSSMenuAnimResize(true, 400);
//	if (document.documentElement.currentStyle == undefined && document.documentElement.style.MozOpacity == undefined) {
  //  	this.animOpenRoot = this.animCloseRoot = this.animOpenNode = this.animCloseNode = new CSSMenuAnimOff();
//	} else {
//    	this.animOpenRoot = (document.documentElement.style.MozOpacity == undefined ? new CSSMenuAnimResize(true, 300) : new CSSMenuAnimAlpha(true, 300));
//    	this.animOpenRoot = new CSSMenuAnimResize(true, 200);
    	this.animOpenRoot = new CSSMenuAnimAlpha(true, 150);
//    	this.animOpenRoot = new CSSMenuAnimShow();
    	this.animCloseRoot = new CSSMenuAnimAlpha(false, 250);
    	this.animOpenNode = new CSSMenuAnimAlpha(true, 300);
    	this.animCloseNode = new CSSMenuAnimAlpha(false, 100);
//	}
}
CSSMenu.prototype._getElementByTag = function (root, tag) {
    for (var node = root.firstChild; node; node = node.nextSibling) {
		if (node.nodeName == tag) return node;
	}
	return null;
}
CSSMenu.prototype._getRoot = function (node) {
    for (it = node; it; it = it.parentNode) {
		if (it.id.indexOf("nav")==0) return it.menuObject;
	}
	return null;
}
CSSMenu.prototype._getParentList = function (node) {
	if (node.tagName == "LI") {
		return node.parentNode.parentNode;
	} else if (node.tagName == "UL") {
		return node.parentNode.parentNode.parentNode;
	} else { // DIV ?
		return node.parentNode;
	}
}
CSSMenu.prototype._getList = function (node) {
	if (node.tagName == "LI") {
		return CSSMenu.prototype._getElementByTag(node, "UL");
	} else if (node.tagName == "UL") {
		return node;
	} else { // DIV ?
		return node.parentNode;
	}
}
CSSMenu.prototype._getListItem = function (node) {
	if (node.tagName == "LI") {
		return node;
	} else if (node.tagName == "UL") {
		return node.parentNode;
	} else { // DIV ?
		return node.parentNode.parentNode;
	}
}
CSSMenu.prototype._getID = function (node) {
	if (node.menuID == undefined) {
		node.menuID = this.items.length;
		this.items.push(node.menuID);
	}
	return node.menuID;
}

CSSMenu.prototype.init = function () {
	if (!document.getElementById)
		return; // unsupported...
//	try {
	/* bierzemy root'a */
    var navRoot = document.getElementById(this.navID);
	this.root = navRoot;
	navRoot.menuObject = this;
	/* wyciagamy z niego UL/DIV */
	var ul = navRoot.firstChild;
	ul.menuOpened = null; // aktualnie otwarte podmenu
	this.ul = ul;
	navRoot = ul.firstChild;
    for (var node = navRoot.firstChild; node!=null; node = node.nextSibling) {
		if (node.nodeName != "LI") continue;
		this._initRootNode(node);				
	}

//	} catch (e) {
//	}

}

CSSMenu.prototype.hovering = function () {
//	document.debug.txt.value+="hovering"+this.closeTimer+"\r\n";
	if (this.closeTimer) {
		clearTimeout(this.closeTimer);
		this.closeTimer = 0;
	}
		
}
CSSMenu.prototype.hoverOut = function () {
	if (this.closeTimer) {
		clearTimeout(this.closeTimer);
	}
	this.closeTimer = setTimeout("CSSMenu.prototype.close('"+this.navID+"')", 150);
//	document.debug.txt.value+="hoverOut"+this.closeTimer+"\r\n";
}

CSSMenu.prototype.close = function (id) {
    var nav = document.getElementById(id);
	nav = nav.menuObject;
//	document.debug.txt.value+="closeALL:"+''+"\r\n";
	if (nav.ul.menuOpened && nav.ul.menuOpened.menuClose) {
		nav.ul.menuOpened.menuClose();	
	}	
//	alert("Closing " + nav.navID);
}

CSSMenu.prototype._initBaseNode = function (node) { // wspólna inicjalizacja...
	var ul = this._getElementByTag(node, "UL"); 
	if (ul == null) { // nie ma podmenu...
	} else {
		ul.menuOpened = null; // aktualnie otwarte podmenu...
		var div = this._getElementByTag(ul, "DIV");
		div.onmouseover = this.onMouseOverDIV;		
		div.onmouseout = this.onMouseOutDIV;		
	}
	node.onmouseover = this.onMouseOverLI;
	return ul != null;	
}

CSSMenu.prototype._initRootNode = function (node) {
	if (this._initBaseNode(node)) {
    	node.menuOpen = this._openRootNode;
    	node.menuClose = this._closeRootNode;
    	// inicjalizujemy wszystkie podmenu...
    	list = node.getElementsByTagName("LI");
    	for (var i=0; i<list.length; i++) {
    		this._initNode(list[i]);
    	}
	}
}
CSSMenu.prototype._initNode = function (node) {
	if (this._initBaseNode(node)) {
    	node.menuOpen = this._openNode;
    	node.menuClose = this._closeNode;
	}
}

CSSMenu.prototype.onMouseOverLI = function () {
	var parentUl = CSSMenu.prototype._getParentList(this);
	var root = CSSMenu.prototype._getRoot(this);
	root.hovering();	
//	document.debug.txt.value+="mu="+parentUl.menuOpened+"\r\n";
	if (parentUl.menuOpened == this) {
		return;
	} else {
    	if (parentUl.menuOpened && parentUl.menuOpened.menuClose) {
    		parentUl.menuOpened.menuClose();
    	}
	}
	if (this.menuOpen) {
		this.menuOpen();
	}
}


CSSMenu.prototype.onMouseOverDIV = function () {
	var li = CSSMenu.prototype._getListItem(this);
	var root = CSSMenu.prototype._getRoot(this);
	root.hovering();	
}
CSSMenu.prototype.onMouseOutDIV = function () {
	var li = CSSMenu.prototype._getListItem(this);
	var root = CSSMenu.prototype._getRoot(this);
	root.hoverOut();
}


/* Funkcje animacji */
function AAANIMATE(what) {
	what._animateQueue();
}
CSSMenu.prototype._animateQueue = function() {
	var keepArray = false;
	this.animateTimer = 0;
	if (this.animate.length == 0) return;
	
	for (var i=0; i<this.animate.length; ++i) {
		if (this.animate[i].animate()) {
			keepArray = true;
		}
	}
	if (keepArray) {
		this.animateTimer = setTimeout("CSSMenu__"+this.navID+"._animateQueue()", this.animatePeriod);
//		this.animateTimer = setTimeout("CSSMenu_e_"+this.navID+"._animateQueue()", 1);
	} else {
		this.animate = new Array(); // czystka...
//		document.debug.txt.value+="animStop\r\n";
	}
}
CSSMenu.prototype._animateNode = function(node, animProto) {
	var id = this.animate.length;
	if ( !animProto.oneStep || !animProto.oneStep(node) ) { 
		var anim = animProto.getCopy(node);
		this.animate.push(anim);
	}
	if (!this.animateTimer)
		this._animateQueue();
	
}
function CSSMenuAnimOff() {
}
CSSMenuAnimOff.prototype.oneStep = function (node) {
	return true;
}

function CSSMenuAnimShow() {
}
CSSMenuAnimShow.prototype.oneStep = function (node) {
	var ul = CSSMenu.prototype._getList(node);
	//ul.style.display = 'block';
	ul.style.visibility = 'visible';
	return true;
}
function CSSMenuAnimHide() {
}
CSSMenuAnimHide.prototype.oneStep = function (node) {
	var ul = CSSMenu.prototype._getList(node);
	//ul.style.display = 'none';
	ul.style.visibility = 'hidden';
	return true;
}

function CSSMenuAnimResize(show, time) {
	this.node = null;
	this.time = time; // czas pełnej animacji...
	this.show = show;	
	this.mX = 100;
	this.mY = 1;
	this.getCopy = function(node) {
    	var obj = new CSSMenuAnimResize(this.show, this.time);
    	obj.node = node;
		obj.el = CSSMenu.prototype._getList(node);
//		obj.el = CSSMenu.prototype._getElementByTag(obj.ul, "DIV");
		obj.sX = 0;
		obj.sY = 12;
		obj.clipY = 5;
		obj.clipX = 0;
		obj.sW = obj.el.clientWidth; 
		obj.sH = obj.el.clientHeight;
		obj.start = Number(new Date); // poczštek animacji...
		obj.mX = this.mX;
		obj.mY = this.mY;
		obj.started = false;
		obj.finished = false;
		return obj;
	}
	this.animate = function() {
		if (this.finished)
			return false;
		var pos = (Number(new Date) - this.start) / this.time;
		if (pos > 0.9) {
			pos = 1;
			this.finished = true;
			if (!this.show) {
				CSSMenuAnimHide.prototype.oneStep(this.node);
        		this.el.style.top = '';
        		this.el.style.left = '';
        		this.el.style.clip = "rect(auto,auto,auto,auto)";
				return false;
			}
		} 
		var m = (this.show ? pos : 1 - pos);
		m = Math.max(Math.sin(m * Math.PI / 2) , 0.01);
		var cX = Math.min(1, m*this.mX);
		var cY = Math.min(1, m*this.mY);
		var x = (cX == 1 ? this.sX : (-this.sW + Math.round(this.sW*cX) + this.sW));
		var y = (cY == 1 ? this.sY : (-this.sH + Math.round(this.sH*cY) + this.sY));
		this.el.style.top = y + 'px';
		this.el.style.left = x + 'px';
		this.el.style.clip = "rect("+(cY==1?'auto':(-y+this.sY+this.clipY)+'px')+",auto,auto,"+(cX==1?'auto':(-x+this.sX+this.clipX)+'px')+")";
//		document.debug.txt.value+=this.el.style.clip+":"+pos+" // "+m+"\r\n";
		this.el.style.filter = "";
		this.el.style.MozOpacity = 0.99;
		if (!this.started && this.show) {
			CSSMenuAnimShow.prototype.oneStep(this.node);
			this.started = true;
		}
		return pos<=1;
	}
}

function CSSMenuAnimAlpha(show, time) {
	this.node = null;
	this.time = time; // czas pełnej animacji...
	this.show = show;	
	this.getCopy = function(node) {
    	var obj = new CSSMenuAnimAlpha(this.show, this.time);
    	obj.node = node;
		obj.el = CSSMenu.prototype._getList(node);
//		obj.el = CSSMenu.prototype._getElementByTag(obj.ul, "DIV");
		obj.start = Number(new Date); // poczštek animacji...
		obj.started = false;
		obj.finished = false;
		return obj;
	}
	this.animate = function() {
		if (this.finished)
			return false;
		var pos = (Number(new Date) - this.start) / this.time;
		if (pos > 0.9) {
			pos = 1;
			this.finished = true;
			if (!this.show) {
				CSSMenuAnimHide.prototype.oneStep(this.node);
				return false;
			}
		} 
		var m = (this.show ? pos : 1 - pos);
		m = Math.max(Math.sin(m * Math.PI / 2) , 0.01);
        this.el.style.filter = "alpha(opacity="+Math.round(Math.max(Math.min(m, 0.95),0.1)*100)+")";
        this.el.style.MozOpacity = Math.min(m, 0.99);
		if (!this.started && this.show) {
			CSSMenuAnimShow.prototype.oneStep(this.node);
			this.started = true;
		}
//		document.debug.txt.value+=CSSMenu.prototype._getList(this.node).style.visibility+" o="+Math.round(Math.max(Math.min(m, 0.9),0.2)*100)+"\r\n";
		return pos<1;
	}
}



/* Funkcje elementow */

CSSMenu.prototype._openBaseNode = function (node) { 
	var parentUl = CSSMenu.prototype._getParentList(node);
	parentUl.menuOpened = node;
	var ul = CSSMenu.prototype._getList(node);
	ul.menuOpened = null;
    if (node.menuCloseTimer) {
	    clearTimeout(node.menuCloseTimer);
    	node.menuCloseTimer = 0;
	}
}
CSSMenu.prototype._closeBaseNode = function (node) {
	var parentUl = CSSMenu.prototype._getParentList(node);
	parentUl.menuOpened = null;
	var ul = CSSMenu.prototype._getList(node);
	if (ul.menuOpened)
		ul.menuOpened.menuClose();
    if (node.menuCloseTimer) {
	    clearTimeout(node.menuCloseTimer);
    	node.menuCloseTimer = 0;
	}
}

CSSMenu.prototype._openRootNode = function () { // this jest ELEMENTEM LI!!!
	CSSMenu.prototype._openBaseNode(this);
	var root = CSSMenu.prototype._getRoot(this);
	root._animateNode(this, root.animOpenRoot);
}
CSSMenu.prototype._closeRootNode = function () {
	var root = CSSMenu.prototype._getRoot(this);
	root._animateNode(this, root.animCloseRoot);
	CSSMenu.prototype._closeBaseNode(this);
}

CSSMenu.prototype._openNode = function () {
	CSSMenu.prototype._openBaseNode(this);
	var root = CSSMenu.prototype._getRoot(this);
	root._animateNode(this, root.animOpenNode);
}
CSSMenu.prototype._closeNode = function () {
	var root = CSSMenu.prototype._getRoot(this);
	root._animateNode(this, root.animCloseNode);
	CSSMenu.prototype._closeBaseNode(this);
}


