/*
 * Copyright (c) 2008 FIZON GmbH
 * All rights reserved.
 *
 * $Id: pmenuitem.js 1739 2008-08-04 07:50:26Z as $
 */
PMenuItem = function(title, onclick)
{
	/**
	 * construct()
	 */
	this.construct = function(title, onclick)
	{
		if (typeof PMenuItem.Objs == 'undefined')
			PMenuItem.Objs = [];

		this.id = PMenuItem.Objs.length;
		PMenuItem.Objs[this.id] = this;

		this.menu = PMenu.Get();

		this.title = title;
		this.onclick = this.construct.arguments.length > 1 ? onclick : false;
		
		this.displayed = false;
		this.parent = false;
		this.items = [];

		this.list = document.createElement('ul');
		this.list.style.position = 'fixed';
		this.li = document.createElement('li');
		this.a = document.createElement('a');
		this.a.href = '#';
		this.a.innerHTML = this.title;
		this.a.setAttribute('onclick', 'PMenu.Click('+this.id+'); return false;');
		this.a.setAttribute('onmouseover', 'PMenuItem.Mouseover('+this.id+');');

		this.li.appendChild(this.a);
	}

	
	/**
	 * destroy()
	 */
	this.destroy = function()
	{
		PMenuItem.Destroy(this.id);
	}


	/**
	 * setTitle()
	 */
	this.setTitle = function(title)
	{
		this.a.innerHTML = title;
	}


	/**
	 * addItem()
	 */
	this.addItem = function(title, onclick)
	{
		var id, item;

		// neue Kinder-ID
		id = this.items.length;
		// item erstellen
		item = this.items[id] = new PMenuItem(title, onclick);
		// ElternObj ist dieses hier
		item.parent = this;
		// den neuen item mit in die Liste einhangen
		this.list.appendChild(item.li);
		// wenn es das erste Kinderobjekt ist
		// markieren wir dieses Item als "Knoten"
		if (this.items.length == 1)
			this.a.innerHTML += ' >';

		return item;
	}


	/**
	 * click()
	 */
	this.click = function()
	{
		/* wenn das item keine weiteren kinder hat
		 * schliessen wir das gesamte menu und 
		 * fuehren bei einem vorhandenem "onclick"
		 * diesen aus */
		if (this.items.length == 0) {
			this.menu.click(0);
			return this.onclick ? eval(this.onclick) : false;
		}
		/* ansonsten, wenn es geoefnet ist schliessen
		 * wir es oder eben anders rum */
		else {
			return this.displayed ? this.hide() : this.show();
		}
	}


	/**
	 * mouseover()
	 */
	this.mouseover = function()
	{
		if (this.parent) 
			for (var i = 0; i < this.parent.items.length; i++) 
				this.parent.items[i].hide();
				
		return this.displayed ? this.hide() : this.show();
	}


	/**
	 * show()
	 */
	this.show = function()
	{
		if (this.displayed)
			return true;

		if (this.items.length == 0)
			return false;

		var x = 0;
		var y = 23;

		if (this.parent) {
			for (var i = 0; i < this.parent.items.length; i++)
				this.parent.items[i].hide();
			this.li.className = 'active';

			x = this.parent.list.clientWidth + this.parent.list.offsetLeft;
			y = window.innerHeight
				- this.parent.list.offsetTop - this.li.offsetTop - this.li.offsetHeight;
		}

		this.list.style.left = x+'px';
		this.list.style.bottom = y+'px';

		this.displayed = true;
		this.menu.menuDiv.appendChild(this.list);
	}


	/**
	 * hide()
	 */
	this.hide = function()
	{
		if (! this.displayed)
			return true;

		this.displayed = false;
		for (var i = 0; i < this.items.length; i++)
			this.items[i].hide();
		this.menu.menuDiv.removeChild(this.list)
		if (this.parent)
			this.li.className = '';
	}


	this.construct(title, onclick);
}


/**
 * PMenuItem.Get()
 */
PMenuItem.Get = function(id)
{
	return PMenuItem.Objs[id];
}


/**
 * PMenuItem.Click()
 */
PMenuItem.Click = function(id)
{
	PMenuItem.Get(id).click();
}


/**
 * PMenuItem.Mouseover()
 */
PMenuItem.Mouseover = function(id)
{
	PMenuItem.Get(id).mouseover();
}


/**
 * PMenuItem.Destroy()
 */
PMenuItem.Destroy = function(id)
{
	var item = PMenuItem.Get(id);

	for (var i = 0; i < item.items.length; i++)
		PMenuItem.Destroy(item.items[i].id);

	item.parent.list.removeChild(item.li);
	item = null;
}
