/**
* Class for animations, sizing and moving
*/
just.sizeEffect = function(obj) {
	this.obj = obj;
	this.att = {};
	this.obj.onComplete = function() {};
	this.endSizing = function() {
		for (var attribute in this.att) {
			if (this.att[attribute].intervalId !== null)
				return;
		}
		this.obj.onComplete();
	};	
/**
* Clear all executing intervals of the object
*/
	this.clearAll = function() {
		for (var attribute in this.att) {			
			try {clearInterval(this.att[attribute].intervalId)} catch(e) {}
		}
	}

/**
* Sets the attributes for the animation. Attributes must be in object style.
*/
	this.setEffectsAttributes = function(attributes) {
		this.style.overflow = "hidden";
		this._justObj.clearAll();
		this._justObj.att = {};		
		for (var attribute in attributes) {			
			eval("this._justObj.att."+attribute+" = {};");			
			for (var a in attributes[attribute]) {
				eval("this._justObj.att."+attribute+"."+a+" = attributes['"+attribute+"']['"+a+"'];")
			}			
			this._justObj.att[attribute].intervalId = null;
			this._justObj.att[attribute].begin = null;
			this._justObj.att[attribute].time = 0;
			this._justObj.att[attribute].method = this._justObj.att[attribute].method || just.easing.easeNone;
			this._justObj.att[attribute].duration = this._justObj.att[attribute].duration || 100;
		}
	};
	
	/* executes from obj */	
	this.start = function() {		
		this._justObj.clearAll();
		var hasChange = false;
		for (var attribute in this._justObj.att) {
			if (just.util.getStyle(this,attribute) - this._justObj.att[attribute].value !== 0) {
				hasChange = true;
				//alert(just.util.getStyle(this,attribute) + attribute);
				//alert(this._justObj.att[attribute].value + attribute);
			}
		}	
		if (!hasChange) return false;
		this.style.display = "";
		for (var attribute in this._justObj.att) {
			this._justObj.att[attribute].time = 0;			
			if (this._justObj.att[attribute].toContent) {
				this._justObj.att[attribute].value = this._justObj.getContentSizes(attribute);
			}
			this._justObj.att[attribute].begin = parseInt(just.util.getStyle(this,attribute),10);
			if (this._justObj.att[attribute].durationFactor)
				this._justObj.att[attribute].duration = this._justObj.getDurationByFactor(attribute);
			this._justObj.att[attribute].isResized = true; // used for toogle function			
			this._justObj.att[attribute].intervalId = setInterval('just.util.getObj("'+this.id+'")._justObj.resize("'+attribute+'",'+this._justObj.att[attribute].begin+','+this._justObj.att[attribute].value+','+this._justObj.att[attribute].duration+')', 1);
		}		
	};
	
	this.getDurationByFactor = function(attribute) {
		var intDur = parseInt((this.att[attribute].value-this.att[attribute].begin) * this.att[attribute].durationFactor,10)
		if (intDur < 0) intDur = intDur * -1;
		if (intDur == 0) intDur = 1;
		return intDur;
	}

	/* executes from obj */
	this.toogle = function() {
		this.style.overflow = "hidden";
		this.style.display = "";
		var newBegin;
		this._justObj.clearAll();
		for (var attribute in this._justObj.att) {
			this._justObj.att[attribute].time = 0;			
			var currentStyle = parseFloat(just.util.getStyle(this,attribute),10);

			if (this._justObj.att[attribute].begin == null)
				this._justObj.att[attribute].begin = currentStyle;
			var change = this._justObj.att[attribute].value - this._justObj.att[attribute].begin;
			duration = this._justObj.att[attribute].duration;

			if (this._justObj.att[attribute].isResized == true) {				
				// if it already is in the right position, change would be 0, div by 0 raises an error, so...
				if (change == 0) change = 1;
				newBegin = this._justObj.att[attribute].begin;				
				this._justObj.att[attribute].begin = this._justObj.att[attribute].value;
				this._justObj.att[attribute].value = newBegin;
				// third rule, calculate duration in case of the animation is still running
				duration =  parseInt((this._justObj.att[attribute].value - currentStyle) * this._justObj.att[attribute].duration / change,10);
				if (duration == 0) duration = 1;
			} else {
				if (this._justObj.att[attribute].toContent) {				
					this._justObj.att[attribute].value = this._justObj.getContentSizes(attribute);
				}
			}
			this._justObj.att[attribute].isResized = true;
			this._justObj.att[attribute].intervalId = setInterval('just.util.getObj("'+this.id+'")._justObj.resize("'+attribute+'",'+ currentStyle +','+this._justObj.att[attribute].value+','+ Math.abs(duration) +')', 1);
		}
	};
	
	this.getContentSizes = function(att) {
		var size = 0;
		var tempObj = document.createElement("div");
		document.body.appendChild(tempObj);
		tempObj.id = "sizeEffectDiv";
		tempObj.style.position = "absolute";
		tempObj.style.top = "0px";
		tempObj.style.left = "0px";
		var attTextInvert = (att == "height")?"Width":"Height";		
		if (this.att[att]) {			
			if (this.att[att].value == undefined) 
				tempObj.style[att] = eval("this.obj.client"+attTextInvert+"+'px'");
			else
				tempObj.style[att] = this.att[att].value;
		} else {
			tempObj.style[att] = eval("this.obj.client"+attTextInvert+"+'px'");
		}

		tempObj.style[att] = "auto";
	
		tempObj.className = this.obj.className;
		tempObj.style.overflow = "visible";		
		tempObj.innerHTML = this.obj.innerHTML;
		tempObj.style.display = "";
		var size = (att == "height")?parseInt(tempObj.clientHeight,10):parseInt(tempObj.clientWidth,10);
		tempObj.style.display = "none";
		if (this.obj.innerHTML == "") newSize = 0;				
		document.body.removeChild(tempObj);	
		return size;
	}	

	this.resize = function(att,begin,end,dur) {
		var size = this.att[att].method(this.att[att].time, begin, end - begin,dur);
		size = parseFloat(size,10);
		var sufix = "";
		if (att == "width" || att == "height" || att == "top" || att == "left") {
			sufix = "px";
			if (size < 0) size = 0;
			size = parseInt(size,10);
		}
		if (att == "opacity") {
			size = formatNum(size,2);
		}		
		just.util.setStyle(this.obj,att,size+sufix);
		this.att[att].time += 1;
		if (this.att[att].time > dur || (this.att[att].time >= dur && (end == just.util.getStyle(this.obj,att)||end == size))) {
			try { clearInterval(this.att[att].intervalId);this.att[att].intervalId = null} catch(e) {}
			if (size < 1 & (att == "width" || att == "height"))  {				
				size = 0;
				just.util.setStyle(this.obj,"display","none");
			}
			just.util.setStyle(this.obj,att,size+sufix);
			this.endSizing();
		}

	};

	this.obj._justObj = this;
	// methods
	this.obj.setEffectsAttributes = this.setEffectsAttributes;
	this.obj.start = this.start;
	this.obj.toogle = this.toogle;
};