// *** JSF DropMenu
// *** (c) feel by fish 
// *** http://www.imagen.pl
// *** 2001.02.13
// *** GNU GPL distribution

// * based on DynAPI
// * by Dan Steinman, http://www.dansteinman.com/dynapi/
// * libs: dynlayer, dynlayer-common

// -> wysuwane menu

function JSFDropMenu(layername) {
	// nazwa warstwy i obiektu
	this.layername = layername;
	this.objname = layername + "dmenu";
	eval(this.objname + "=this");

	this.opened = false;
	this.busy = false;
	this.run = null;
	this.blocked = false;
	
	this.time = 5;
	this.inc = 10;
	
	this.onOpen = null;
	this.onClose = null;

	// init	
	this.lay = new DynLayer(this.layername, null);
	this.laybody = new DynLayer(this.layername + "body", this.layername);
	this.w = this.laybody.getContentWidth();
	this.h = this.laybody.getContentHeight();
	this.dy = -this.h;
	this.zindex = this.lay.css.zIndex;
	
	this.laybody.moveBy(0,-this.h);
	this.laybody.show();
}

// metody obiektu: otworz/zamknij po wskazaniu pola aktywnego, otworz, zamknij, zmien stan, 
JSFDropMenu.prototype.msshow = JSFDropMenuMSShow;
JSFDropMenu.prototype.mshide = JSFDropMenuMSHide;
JSFDropMenu.prototype.show = JSFDropMenuShow;
JSFDropMenu.prototype.hide = JSFDropMenuHide;
JSFDropMenu.prototype.toggle = JSFDropMenuToggle;
JSFDropMenu.prototype.inbound = JSFDropMenuInbound;

function JSFDropMenuMSShow() {
	// menu nieczule na zamkniecie
	this.blocked = true;
	this.show();
}

function JSFDropMenuMSHide() {
	// menu czule na zamkniecie
	this.blocked = false;
}

function JSFDropMenuShow() {
	if ((!this.opened && this.busy) || (this.opened && !this.busy)) return;
	
	this.busy = true;
	this.opened = false;
	this.lay.css.zIndex = 9;

	if (this.run) clearTimeout(this.run);
	this.run = setTimeout("_jsfdmenushow(\"" + this.objname + "\")", this.time);
}

function JSFDropMenuHide() {
	if ((!this.opened && !this.busy) || (this.opened && this.busy)) return;
	if (this.blocked) return;

	this.busy = true;
	this.opened = true;
	this.lay.css.zIndex = this.zindex;

	if (this.run) clearTimeout(this.run);
	this.run = setTimeout("_jsfdmenuhide(\"" + this.objname + "\")", this.time);
}

function JSFDropMenuToggle() {
	//if (!this.opened) this.show(); else this.hide();
	if ((!this.opened && this.busy) || (this.opened && !this.busy)) this.hide(); else this.show();
}

function JSFDropMenuInbound(x, y) {
	y += 2;
	if ( x>=this.lay.x && x<=(this.lay.x + this.w) && y>=this.lay.y && y<=(this.lay.y + this.h) )
		return true;
	return false;
}


// --- funkcje pomocnicze
function _jsfdmenushow(objname) {
	o = eval(objname);
	o.dy += o.inc;
	if (o.dy >= 0) {
		o.dy = 0;
		o.laybody.moveTo(0, 0);
		clearInterval(o.run);
		o.run = null; o.busy = false; o.opened = true;
		o.lay.show();
		if (o.onOpen) o.onOpen();
	}		
	else {
		o.laybody.moveTo(0, o.dy);
		o.run = setTimeout("_jsfdmenushow(\"" + o.objname + "\")", o.time);
	}
}

function _jsfdmenuhide(objname) {
	o = eval(objname);
	o.dy -= o.inc;
	if (o.dy <= -o.h ) {
		o.dy = -o.h;
		o.laybody.moveTo(0, -o.h);
		clearInterval(o.run);
		o.run = null; o.busy = false; o.opened = false;
		o.lay.hide();
		if (o.onClose) o.onClose();
	}		
	else {
		o.laybody.moveTo(0, o.dy);
		o.run = setTimeout("_jsfdmenuhide(\"" + o.objname + "\")", o.time);
	}
}

// modfish: load part
if (window.JSFModules!=null) {
	JSFModules[JSFModules.length] = "jsf-dropmenu";
	loadJSFModules();
}

