// JavaScript Document

var menuItems = new Array();

function tMMi(itemID) {	// toggle menu item
/*	
	// Ce je animiran menu ok, lahko tole vržeš ven...

	var objChildrenBlock = document.getElementById('mmiB'+itemID);
	if (objChildrenBlock != null) {
		if (objChildrenBlock.style.display == 'block') objChildrenBlock.style.display = 'none';
		 else objChildrenBlock.style.display = 'block';
	}
	return false;

	// Ce je animiran menu ok, lahko tole vržeš ven...		*/	
	var menuItemsCount = menuItems.length;
	for (var i=0; i<menuItemsCount; i++) {
		if (menuItems[i].id == itemID) {
			menuItems[i].toggle();
			return false;
		}
	}
	menuItems[menuItemsCount] = new TMenuItem(itemID, 'menuItems['+menuItemsCount+']');
	menuItems[menuItemsCount].toggle();
	return false;
}

function TMenuItem(itemID, arrayHandle) {
	this.id = itemID;
	this.childrenBlock = null;
	this.currentTop = 0;
	this.contentsHeight = 0;
	this.minTop = 0;
	this.maxTop = 0;
	this.animationTimer = null;
	this.direction = 0;
	this.defaultStep = 20;
	this.step = this.defaultStep;
	this.maxStep = 150;
	this.arrayItem = arrayHandle;
	
	this.getHandles = function () {
		this.childrenBlock = document.getElementById('mmiB'+this.id);
	}
	
	this.startAnimation = function (direction) {
		clearTimeout(this.animationTimer);
		this.direction = direction;
/*		// Zaenkrat ne preverjamo ce so handles definirane, ker ni variante, da bi se ta funkcija poklicala ob nedefiniranih handles...
		if (!this.plank || !this.contentsLayer) this.getHandles();
		if (!this.plank || !this.contentsLayer) return false;		*/
		this.animationTimer = setInterval(this.arrayItem+'.animate();', 100);
//		this.refreshToggleLink(this.direction>0);
	}
	
	this.stopAnimation = function () {
//		this.refreshToggleLink(this.direction>0);
		this.direction = 0;
		this.step = this.defaultStep;
		clearTimeout(this.animationTimer);
	}

	this.animate = function () {
		this.currentTop += this.step * this.direction;
		this.step += this.defaultStep;
		if (this.step > this.maxStep) this.step = this.maxStep;
		if (this.currentTop < this.maxTop) {	// marginTop je vedno negativen; kar pomeni, da je maxTop takrat, ko je blok najbolj "dvignjen"; Torej fullUp, contracted.
			this.currentTop = this.maxTop;
			this.childrenBlock.style.display = 'none';
			this.stopAnimation();
		}
		if (this.currentTop > this.minTop) {
			this.currentTop = this.minTop;
			this.stopAnimation();
		}
		this.childrenBlock.style.marginTop = (this.currentTop)+'px';
	}
	
	this.toggle = function () {
		if (!this.childrenBlock) this.getHandles();
		if (!this.childrenBlock) {
// alert('function toggle\nthis.childrenBlock == null');
			return;
		}
		if (this.childrenBlock.style.display == 'none') {		// this.contentsLayer.style != null && this.contentsLayer.style.display != null &&
			// Zacetna inicializacija; Hidden Elements (CSS display: none) imajo offsetHeight 0, zato moramo block narediti visible in še enkrat prebrati offsetHeight
			this.childrenBlock.style.display = 'block';
			this.getBounds();
			// Nato pa nastavimo marginTop na contractedPosition; ker pac blok do sedaj ni bil viden
			this.currentTop = this.maxTop;
			this.childrenBlock.style.marginTop = (this.currentTop)+'px';
			this.startAnimation(1);
		}
		 else if ((parseInt(this.childrenBlock.style.marginTop) || 0) <= 0) {	// ce damo ... == 0 potem se (ker je zacetni margin-top par pixlov v minus), menu s trenutno pozicijo (ki ga rendramo odprtega), ne zapre;
				this.getBounds();	// Dodano 18.3.2010; Ker je lahko obiskovalec vmes odprl še kak podmenu in zacetna getBounds() ni vec veljavna, ker je zaradi odprtega podmenuja container višji.
			 	this.startAnimation(-1);
		 }
		  else this.startAnimation(1);
	}

	this.getBounds = function () {
		this.currentTop = parseInt(this.childrenBlock.style.marginTop) || 0;
		this.contentsHeight = this.childrenBlock.offsetHeight;
		this.getLimits();
	}

	this.getLimits = function () {
//		this.minTop = 0 * (-1);
		this.maxTop = this.contentsHeight * (-1);
	}
}

