// Main Function: create the object
function obj(name)
{
	// Internal Attributes
	this.name = name;
	this.interval = new Object();
	this.interval.mileseconds;
	this.interval.iteraction;
	this.interval.iteractions;
	this.active = false; // Status of execution

	// Declarations
	this.obj = document.getElementById(this.name);

	// Attibutes that can have default value modified
	this.x = strToNumber(this.obj.style.left);
	this.y = strToNumber(this.obj.style.top);
	this.width = strToNumber(this.obj.style.width);
	this.height = strToNumber(this.obj.style.height);
	this.leftIdeal = this.x;
	this.topIdeal = this.y;
	this.widthIdeal = this.width;
	this.heightIdeal = this.height;
	this.type = "smoothOut";
	this.speedy = 0.5; // seconds
	this.precision = 50; // precision of the moviments and scalations (mileseconds)	
	
	// Methods
	this.execFunction = objExecFunction;
	this.stopFunction = objStopFunction;
	this.calcParameters = objCalcParameters;
	this.go = objGo;
}
var objStak = Array();


function objCalcParameters(refMotion)
{
	this.interval.iteractions = (1000/this.precision)*this.speedy;
}


// Exec function in the intervalSpeedy velocity
function objExecFunction(functionObj, refMotion)
{
	if(!this.interval.id)
	{
		this.interval.iteraction = 1;
		this.calcParameters();
		refMotion.setValues(this, refMotion);
		
		//this.interval.id = setInterval(functionObj, this.precision, this, refMotion);
		objStak[this.name] = new Object();
		objStak[this.name].object = this;
		objStak[this.name].functionObj = functionObj;
		objStak[this.name].refMotion = refMotion;
		this.interval.id = eval("setInterval(\"objExec('"+this.name+"')\", "+this.precision+");");
		this.active = true;
	}
}

function objExec(name)
{
	objStak[name].functionObj(objStak[name].object, objStak[name].refMotion);
}


// Stop execution of the itnerval and increment intervalIteraction
function objStopFunction()
{
	if(this.interval.iteractions == this.interval.iteraction)
	{
		clearInterval(this.interval.id);
		this.interval.id = undefined;
		this.active = false;
	}
	this.interval.iteraction++;
}


// Apply modifications
function objGo()
{
	// If obj was changed
	if(this.x != strToNumber(this.obj.style.left) || this.y != strToNumber(this.obj.style.top) || this.width != strToNumber(this.obj.style.width) || this.height != strToNumber(this.obj.style.height))
	{
		if (window.isLoadingMenu) {
			if (!isNaN(this.x)) this.obj.style.left = this.x + "px";
			if (!isNaN(this.y)) this.obj.style.top = this.y + "px";
			if (!isNaN(this.width)) this.obj.style.width = this.width + "px";
			if (!isNaN(this.height)) this.obj.style.height = this.height + "px";
			this.active = false;
			window.isLoadingMenu = undefined;
		}
		else {
			var refMotion = new motion();
			refMotion.obj = this.obj;
			refMotion.type = this.type;
			refMotion.exec = refMotion.transform;
			this.execFunction(refMotion.go, refMotion);
		}
	}
}
