
function ilikeVisualInit()
{
	if ( this.parent == null )
	{
		this.parent = document.getElementById( this.parentId );
		this.x      = ilikeVisualGetX( this.parent );
		this.y      = ilikeVisualGetY( this.parent ) + this.parent.offsetHeight;
		this.width  = this.parent.offsetWidth;
		if ( this.height == null )
		{
			this.height = '';
		}
	}
}



function ilikeVisualInitReference()
{
	if ( this.reference == null )
	{
		if ( this.id != null && document.getElementById( this.id ) != null )
		{
			this.reference = document.getElementById( this.id );
		}
		else
		{
			var newDiv               = document.createElement( 'DIV' );
			newDiv.style.display     = 'none';
			this.reference = document.body.appendChild( newDiv );
			if ( this.id != null )
			{
				this.reference.id    = this.id;
			}
		}
		// pridani reakce na klavesu "sipka dolu" v pripade, ze je zobrazen selectbox
		if ( this.parent.nodeName == "INPUT" )
		{
			for( var i = 0; i < this.reference.childNodes.length; i++ )
			{
				if ( this.reference.childNodes[ i ].nodeType == 1 && this.reference.childNodes[ i ].nodeName == "SELECT" )
				{
					ilikeJavascriptAddEvent( this.parent, "onkeyup"
						, "if ( window." + this.name + ".visible ) "
						+ "{if ( ilikeJavascriptCheckEventKeyCode( event, new Array( 39, 40 ) ) ) "
						+ " {window." + this.name + ".reference.childNodes[" + i + "].focus(); "
						+ "  try{ window." + this.name + ".reference.childNodes[" + i + "].selectedIndex = 0; } catch(e) {}; "
						+ "  return false; }} "
					);
					ilikeJavascriptAddEvent( this.reference.childNodes[ i ], ( window.opera ? "onkeypress" : "onkeydown" )
						, "if ( this.selectedIndex == 0 ) "
						+ "{if ( ilikeJavascriptCheckEventKeyCode( event, new Array( 37, 38 ) ) ) "
						+ " {window." + this.name + ".parent.focus(); "
						+ "  try{ this.childNodes[0].selected = false; } catch(e) {}; "
						+ "  return false; } };"
						+ "if ( ilikeJavascriptCheckEventKeyCode( event, new Array( 13, 39 ) ) ) "
						+ "{window." + this.name + ".parent.value = this.getElementsByTagName('option')[this.selectedIndex].childNodes[0].data; "
						+ ( this.parent2Id != null ? " document.getElementById('" + this.parent2Id + "').value=this.getElementsByTagName('option')[this.selectedIndex].value;" : "" )
						+ " window." + this.name + ".hide(); "
						+ " window." + this.name + ".parent.focus(); "
						+ " return false; } "
//						+ "else { window." + this.name + ".parent.onkeyup(42); return false; }; "
					);
					ilikeJavascriptAddEvent( this.reference.childNodes[ i ], "onclick"
						, "window." + this.name + ".parent.value = this.getElementsByTagName('option')[this.selectedIndex].childNodes[0].data; "
						+ ( this.parent2Id != null ? "document.getElementById('" + this.parent2Id + "').value=this.getElementsByTagName('option')[this.selectedIndex].value;" : "" )
						+ "window." + this.name + ".hide(); "
						+ "window." + this.name + ".parent.focus(); "
					);
					
					// pokud select ztrati focus, 
					ilikeJavascriptAddEvent( this.reference.childNodes[ i ], "onblur"
						, "window." + this.name + ".blurTime = (new Date()).getTime(); "
						+ "{ setTimeout( 'window." + this.name + ".blurHide()',300); }"
					);
					// pokud select ziska focus
					// toto je v IE volano prvni
					ilikeJavascriptAddEvent( this.reference.childNodes[ i ], "onfocus"
						, "window." + this.name + ".focusTime = (new Date()).getTime();"
						+ "if ( window." + this.name + ".visible ) "
						+ "{ window." + this.name + ".show(); }"
					);
					
					// pokud input ztrati focus
					// toto je v IE volano druhe
					ilikeJavascriptAddEvent( this.parent, "onblur"
						, "window." + this.name + ".blurTime = (new Date()).getTime(); "
						+ "{ setTimeout( 'window." + this.name + ".blurHide()',300); }"
					);
					ilikeJavascriptAddEvent( this.parent, "onfocus"
						, "window." + this.name + ".focusTime = (new Date()).getTime();"
						, "window." + this.name + ".blurTime = 0; "
						+ "if ( window." + this.name + ".visible ) "
						+ "{window." + this.name + ".show(); }"
					);
				}
			}
		}
	}
}


//function ilikeVisual

function ilikeVisualShow()
{
	this.init();
	this.initReference();
	
	this.reference.style.position    = 'absolute';
	this.reference.style.left        = this.x + 'px';
	this.reference.style.top         = this.y + 'px';
	if ( this.backgroundColor != null )
	{
		this.reference.style.backgroundColor = this.backgroundColor;
	}
	if ( this.content != null )
	{
		this.reference.innerHTML = this.content;
	}
	if ( this.border != null )
	{
		this.reference.style.border = this.border;
	}

	// sirka boxu = sirka rodice - border boxu
	var w = this.width;
	if ( this.reference.style.borderLeftWidth != null && parseInt( this.reference.style.borderLeftWidth ) > 0 )
	{
		w -= parseInt( this.reference.style.borderLeftWidth );
	}
	if ( this.reference.style.borderRightWidth != null && parseInt( this.reference.style.borderRightWidth ) > 0 )
	{
		w -= parseInt( this.reference.style.borderRightWidth );
	}
	this.reference.style.width       = w + 'px';
	for( var i = 0; i < this.reference.childNodes.length; i++ )
	{
		if ( this.reference.childNodes[ i ].nodeType == 1 && this.reference.childNodes[ i ].nodeName == "SELECT" )
		{
//		alert( i );
			this.reference.childNodes[ i ].style.width = w + 'px';
		}
	}
	if ( this.height != "" )
	{
		this.reference.style.height      = this.height + 'px';
	}
	
//	this.reference.innerHTML         = 'sdfsd';
	this.reference.style.display = 'inline';
	this.reference.style.zIndex  = 199;
	this.visible                 = true;
}

function ilikeVisualHide()
{
	this.init();
	if ( this.reference != null )
	{
		this.reference.style.display = 'none';
	}
	this.visible = false;
}
function ilikeVisualBlurHide()
{
	if ( this.focusTime + 600 < (new Date()).getTime() )
	{
		this.hide();
	}
}

function ilikeVisualToggle()
{
	if ( this.visible )
	{
		this.hide();
	}
	else
	{
		this.show();
	}
}


function ilikeVisualGetX( o )
{
	var x = 0;
	while ( o != null && o.offsetParent )
	{
		x += o.offsetLeft
		o = o.offsetParent;
	}
	return x;
}

function ilikeVisualGetY( o )
{
	var y = 0;
	while ( o.offsetParent )
	{
		y += o.offsetTop
		o = o.offsetParent;
	}
	return y;
}

function ilikeVisualSetHeight( n )
{
	this.height = n;
}
function ilikeVisualSetContent( n )
{
	this.content = n;
}
function ilikeVisualSetBorder( n )
{
	this.border  = n;
}
function ilikeVisualSetBackgroundColor( n )
{
	this.backgroundColor  = n;
}


var ilikeVisualUniqueNameIndex = 0;
/* vytvori unikatni jmeno v globalnim prostoru jmen
 */
function ilikeVisualGetUniqueName()
{
	ilikeVisualUniqueNameIndex++;
	return "ilikeVisualUniqueNameIndex" + ilikeVisualUniqueNameIndex;
}


function ilikeVisualBox( id, boxId, id2 )
{
	// unikatni jmeno objektu v globalnim prostoru, pres ktere se pristupuje k objektu
	// napr. pri registrovani udalosti se priradi zavolani metody objektu s timto jmenem
	this.name          = ilikeVisualGetUniqueName();
	this.x             = null;
	this.y             = null;
	this.width         = null;
	this.height        = null;

	this.content       = null;
	this.border        = null;
	this.backgroundColor = null;
	
	this.parentId      = id;
	this.parent2Id     = id2;
	this.parent        = null;
	this.visible       = false;
	this.reference     = null;
	this.id            = null;
	
	// cas kdy prvek ztratil fokus (pouzivano k autozavirani a autootevirani)
	this.blurTime      = 0;
	this.focusTime     = 100000000000;

	// inicializuje odkaz na vytvoreny (popr. predany box )
	this.initReference = ilikeVisualInitReference;
	// inicializuje velikost a polohu boxu
	this.init          = ilikeVisualInit;
	this.show          = ilikeVisualShow;

	this.blurHide      = ilikeVisualBlurHide;
	this.hide          = ilikeVisualHide;
	this.toggle        = ilikeVisualToggle;
	// nastaveni ruznych parametru pro box
	this.setHeight     = ilikeVisualSetHeight;
	this.setContent    = ilikeVisualSetContent;
	this.setBorder     = ilikeVisualSetBorder;
	this.setBackgroundColor = ilikeVisualSetBackgroundColor;
	
	if ( boxId != null )
	{
		this.id        = boxId;
	}

	// vytvoreni globalniho odkazu na tento objekt
	// napr. pri vytvoreni udalosti se prida zavolani metody objektu s timto jmenem
	eval( "window." + this.name + " = this" );
}

