/**
 * @file: menuanime.js
 * @desc: menuanime javascript
 *
**/

/**
 * @class: MenuAnime
 * @desc: animate menu
**/

function MenuAnime()
{
}
	MenuAnime.prototype.set = function (instance, idPrefix)
	{
		/* constants */
		this.CLOSING_COMPLETE = 0;
		this.OPENING_COMPLETE = 1;
		this.CLOSING = 3;
		this.OPENING = 4;
		this.TIMER_ANIME = 10;

		/* designation */
		this.instance = instance;
		this.idPrefix = idPrefix;

		/* work */
		this.indexes = new Array();
		this.counts = new Array();
		this.status = new Array();
		this.timers = new Array();
	}

	MenuAnime.prototype.setIndexes = function (id, count, visible)
	{
		var idx = this._initIndex(id, count);
		var idOption = this.idPrefix + '_' + this.indexes[idx] + '_';
		for ( var i = this.counts[idx] - 1; i >= 0; i-- )
		{
			document.getElementById(idOption + i.toString()).style.display = visible ? '' : 'none';
			if ( visible )
			{
				this.status[idx] = this.OPENING_COMPLETE;
			}
		}
	}

	MenuAnime.prototype.startOpen = function (id)
	{
		var idx = this._getIndex(id);
		if ( this.status[idx] != this.OPENING_COMPLETE )
		{
			this.status[idx] = this.OPENING;
			this.anime(this.instance, idx);
		}
		return false;
	}

	MenuAnime.prototype.startClose = function (id)
	{
		var idx = this._getIndex(id);
		if ( this.status[idx] != this.CLOSING_COMPLETE )
		{
			this.status[idx] = this.CLOSING;
			this.anime(this.instance, idx);
		}
		return false;
	}

	MenuAnime.prototype.anime = function (instance, idx)
	{
		var animeObject = eval(instance);
		animeObject._anime(idx);
	}

	MenuAnime.prototype._anime = function (idx)
	{
		var i = 0;
		var idOption = this.idPrefix + '_' + this.indexes[idx] + '_';

		if ( this.timers[idx] !== false )
		{
			window.clearTimeout(this.timers[idx]);
			this.timers[idx] = false;
		}

		var found = false;
		switch ( this.status[idx] )
		{
			case this.OPENING:
				for ( i = this.counts[idx] - 1; i >= 0; i-- )
				{
					if ( document.getElementById(idOption + i.toString()).style.display == 'none' )
					{
						document.getElementById(idOption + i.toString()).style.display = '';
						found = true;
						break;
					}
				}
				if ( !found )
				{
					this.status[idx] = this.OPENING_COMPLETE;
				}
			break;

			case this.CLOSING:
				for ( i = 0; i < this.counts[idx]; i++ )
				{
					if ( document.getElementById(idOption + i.toString()).style.display != 'none' )
					{
						document.getElementById(idOption + i.toString()).style.display = 'none';
						found = true;
						break;
					}
				}
				if ( !found )
				{
					this.status[idx] = this.CLOSING_COMPLETE;
				}
			break;
		}
		if ( found )
		{
			this.timers[idx] = window.setTimeout(this.instance + '.anime(' + this.instance + ', ' + idx + ')', this.TIMER_ANIME);
		}
	}

	MenuAnime.prototype._initIndex = function (id, count)
	{
		var idx = this._getIndex(id);
		if ( idx == -1 )
		{
			idx = this.indexes.length;
			this.indexes[idx] = id;
			this.counts[idx] = count;
			this.status[idx] = this.CLOSING_COMPLETE;
			this.timers[idx] = false;
		}
		return idx;
	}

	MenuAnime.prototype._getIndex = function (id)
	{
		var idx = -1;
		for ( var i = 0; i < this.indexes.length; i++ )
		{
			if ( this.indexes[i] == id )
			{
				idx = i;
				break;
			}
		}
		return idx;
	}
