/*
 *               ____
 *              /\   \
 *             ___\   \___
 *            /\          \
 *            \ \___    ___\
 *        ____ \/__/\   \__/
 *       /\   \    \ \___\    klof  |  innovative web technology
 *      ___\   \___ \/___/
 *     /\          \          klib3/dynamic.js
 *     \ \___    ___\         HTML Element manipulation
 *      \/__/\   \__/
 *          \ \___\           Copyright 2003-2006, klof
 *           \/___/           http://www.klof.net/k.lib3/
 *
 *                            requirements: klib3/style.js
 */


/* Create the master klib3 object if she doesn't exist */
if ( typeof klib3 != "object" )
	klib3 = new ( function(){ this._child = 0 } )();


klib3.dynamic = function( mElement )
{
	this._version = "1.0.0";
	if ( typeof mElement != "undefined" )
		this.init( mElement );
};
klib3.dynamic.prototype.init = function( mElement )
{
	if ( typeof mElement == "string" && document.getElementById )
		this._object = document.getElementById( mElement );
	else if ( typeof mElement == "object" )
		this._object = mElement;
	else
		return false;

	this._id      = typeof this._object.id == "string" && this._object.id != "" ? this._object.id : "klib3_" + ++klib3._child + "_"; 
	this._x       = this._object.offsetLeft || parseInt( this.getStyle( "left" ) ) || 0;
	this._y       = this._object.offsetTop || parseInt( this.getStyle( "top" ) ) || 0;
	this._z       = this.getStyle( "zIndex" );
	this._width   = this._object.offsetWidth || parseInt( this.getStyle( "width" ) ) || 0;
	this._height  = this._object.offsetHeight || parseInt( this.getStyle( "height" ) ) || 0;
	this._visible = this.getStyle( "visibility" ) != "hidden";
	this._fps     = 20;

	window[ ( this._self = this._id + "Object" ) ] = this;
	return this;
};
klib3.dynamic.prototype.getStyle = function( sProperty )
{
	return klib3.style.get( this._object, sProperty );
};
klib3.dynamic.prototype.setStyle = function( sProperty, mValue )
{
	return klib3.style.set( this._object, sProperty, mValue );
};
klib3.dynamic.prototype.attachMethod = function( sMethod, oFunction )
{
	this[ sMethod ] = oFunction;
};
klib3.dynamic.prototype.detachMethod = function( sMethod )
{
	this[ sMethod ] = null;
	delete this[ sMethod ];
};
klib3.dynamic.prototype.attachEvent = function( sEvent, sMethod )
{
	// we will be using addEventListener and attachEvent in the near future.
	eval( "this._object.on" + sEvent.toLowerCase() + "= new Function( \"evt\", \"return " + this._self + "." + sMethod + "( evt );\" )" );
};
klib3.dynamic.prototype.detachEvent = function( sEvent )
{
	this._object[ "on" + sEvent.toLowerCase() ] = null;
};
klib3.dynamic.prototype.timedcall = function( mCall, nMillisecond )
{
	return setTimeout( typeof mCall == "function" ? mCall : this._self + "." + mCall, nMillisecond );
};
klib3.dynamic.prototype.watch = function( sProperty, mMethod )
{
	if ( typeof this.__watch == "undefined" )
		this.__watch = new Object();

	this.__watch[ sProperty ] = {
		value:this[ sProperty ],
		method:mMethod
	};

	if ( typeof this.__monitor == "undefined" )
		this.__monitor = this.timedcall( "monitorWatch()", 1000 / this._fps );
};
klib3.dynamic.prototype.unwatch = function( sProperty )
{
	if ( typeof this.__watch == "object" && typeof this.__watch[ sProperty ] == "object" )
		delete this.__watch[ sProperty ];
};
klib3.dynamic.prototype.monitorWatch = function()
{
	this.stopWatch();
	for ( var p in this.__watch )
		if ( this.__watch[ p ].value != this[ p ] )
		{
			if ( typeof this.__watch[ p ].method == "function" )
				this.__watch[ p ].method( p, this.__watch[ p ].value, this[ p ] );
			else if ( typeof this.__watch[ p ].method == "string" )
				this[ this.__watch[ p ].method ]( p, this.__watch[ p ].value, this[ p ] );
			this.__watch[ p ].value = this[ p ];
		}
	this.startWatch();
};
klib3.dynamic.prototype.stopWatch = function()
{
	if ( typeof this.__monitor != "undefined" )
	{
		clearTimeout( this.__monitor );
		delete this.__monitor;
	}
};
klib3.dynamic.prototype.startWatch = function()
{
	if ( typeof this.__monitor == "undefined" )
		this.__monitor = this.timedcall( "monitorWatch()", 1000 / this._fps );
};
klib3.dynamic.prototype.updateAfterEvent = function()
{
	if ( typeof this.__watch != "undefined" )
	{
		for ( var p in this.__watch )
			if ( this.__watch[ p ].value != this[ p ] )
				this.__watch[ p ].value = this[ p ];
		this.startWatch();
	}
};

klib3.dynamic.prototype.setX = function( nValue )
{
	this._x = parseInt( this.setStyle( "left", nValue + "px" ) );
};
klib3.dynamic.prototype.setY = function( nValue )
{
	this._y = parseInt( this.setStyle( "top", nValue + "px" ) );
};
klib3.dynamic.prototype.setWidth = function( nValue )
{
	this._width = parseInt( this.setStyle( "width", nValue + "px" ) );
};
klib3.dynamic.prototype.setHeight = function( nValue )
{
	this._height = parseInt( this.setStyle( "height", nValue + "px" ) );
};
klib3.dynamic.prototype.setVisible = function( mValue )
{
	this._visible = this.setStyle( "visibility", typeof mValue == "boolean" ? mValue ? "visible" : "hidden" : mValue   ) == "visible";
};

klib3.dynamic.prototype.move = function( nX, nY )
{
	this.stopWatch();
	this.setX( nX );
	this.setY( nY );
	this.updateAfterEvent();
};
klib3.dynamic.prototype.resize = function( nW, nH )
{
	this.stopWatch();
	this.setWidth( nW );
	this.setHeight( nH );
	this.updateAfterEvent();
};
klib3.dynamic.prototype.show = function()
{
	this.stopWatch();
	this.setVisible( true );
	this.updateAfterEvent();
};
klib3.dynamic.prototype.hide = function()
{
	this.stopWatch();
	this.setVisible( false );
	this.updateAfterEvent();
};

