/* dynlite fx module
 * peter assenov- aip solutions ltd' 2001-2011
 * @version: 1.01 - 2011-02-12
 */
/* dl.fx class */
/** !!! For IE the semi-transparent elements MUST have hasLayout=true **/
dl.fx={fps:25}
dl.fx.el=function(id){
	var fx=this;
	var el=dl.el(id);
		el.alpha=new this.alpha(el);
		el.layout=new this.layout(el);
		el.opacity=function(val){
			return el.alpha.opacity(val);
		}
		el.fade=function(end, dur){
			return el.alpha.fade(end, dur);
		}
		el.pos=function(left,top){
			return el.layout.pos(left,top);
		}
		el.size=function(width,height){
			return el.layout.size(width,height);
		}
		el.resize=function(endw, endh, dur){
			return el.layout.resize(endw, endh, dur);
		}

	return el;
}
dl.fx.fire=function(evt,par){
	if(this[evt])
		this[evt](par);
}
/* transperancy handling class */
dl.fx.alpha=function(el){
	if(!el) return null;
	//dom object init
	this.el=(typeof(el)=='object')? el : dl.el(el);
	//getting pointer to alpha modifier	
	if(this.el.style.opacity!==undefined) //dom compatible
		 this.ptr=this.el.style;
	else if(this.el.filters){
		if(!this.el.filters.alpha)
			this.el.style.filter=(dl.dom.css(this.el,'display')=='none')? "alpha(opacity=0)":"alpha(opacity=100)"; /* IE sucks */
		this.ptr=this.el.filters.alpha;
	} else
		this.ptr=null;
	//get current opacity value
	this.op=this.opacity();
}
dl.fx.Alpha=dl.fx.alpha.prototype;
dl.fx.Alpha.fire=dl.fx.fire;
dl.fx.Alpha.opacity=function(val){
	if(val!=undefined) {
		this.op=val;	
		this.ptr.opacity=(this.el.filters)?  this.op*100 : this.op;
	} 
	if(this.op==undefined)
		this.op = dl.dom.css(this.el,'opacity');
	if(this.op==undefined)
		this.op = (dl.dom.css(this.el,'display')=='none')? 0 : 1;
		this.op = Math.round(this.op*100)/100; //rounded to the second digit
	return this.op;
}
dl.fx.Alpha.fade=function(end,dur){
	if(!this.el || !this.ptr) return null;
	if(this.timer)   	//stops any previous transitions
		this.stop();
	//initializes parameters
	this.min=0;								//minimal opacity
	this.max=1;								//maxinum opacity
	this.end=end||0;						//target opacity
	this.fps=dl.fx.fps||25;					//transition frame-rate	
	this.dur=dur||0.5;						//transition sureation in seconds
	this.time=parseInt(1000/this.fps); 		//time betweeb iterations
	this.steps=parseInt(this.dur*this.fps);//number of steps
	this.inc=Math.round((this.max-this.min)/this.steps*100)/100;//value increment per step
	if(this.end < this.op)
		this.inc = -this.inc;
	this.fire('onstart');
	if(this.op == end) 	//no transition required
		this.stop();
	else
		this.step();
return this;
}
dl.fx.Alpha.step=function(){
	var _this=this;
	if(!this.inc) 
		return;
	if(this.inc>0 && this.op>=this.end)
		return this.stop(this.end);
	if(this.inc<0 && this.op<=this.end)
		return this.stop(this.end);
	this.opacity(this.op+this.inc);
	this.timer=setTimeout(function(){_this.step()},this.time)
	this.fire('onstep')
return this;
}
dl.fx.Alpha.stop=function(val){
	clearTimeout(this.timer);
	if(val)
		this.opacity(val);
	this.fire('onend')
return this;
}
/* Alpha events * /
dl.fx.Alpha.onstart=function(par){
//	trace('start: '+this.min+'<'+this.op+'<'+this.max+'='+this.end+' [1000/'+this.fps+'='+this.time+'] '+this.dur+'*'+this.fps+'='+this.steps+'x'+this.inc);
}
dl.fx.Alpha.onstep=function(par){
//	trace('step: '+this.min+'<'+this.op+'<'+this.max+'='+this.end);
}
dl.fx.Alpha.onend=function(par){
	//trace('end: '+this.min+'<'+this.op+'<'+this.max+'='+this.end);
}
/* position and size handling class */
dl.fx.layout=function(el){
	if(!el) return null;
	//dom object init
	this.el=(typeof(el)=='object')? el : dl.el(el);
	this.css=this.el.style;
	this.p=this.pos();
	this.s=this.size();
}
dl.fx.Layout=dl.fx.layout.prototype;
dl.fx.Layout.fire=dl.fx.fire;
dl.fx.Layout.pos=function(left,top){
	if(left||left==0) {
		this.p.l=left;
		this.css.left=this.p.l+'px';
	}
	if(top||top==0) {
		this.p.t=top;
		this.css.top=this.p.t+'px';
	}
	return{l:this.el.offsetLeft,t:this.el.offsetTop}
}
dl.fx.Layout.size=function(width,height){
	if(width||width==0){
		this.s.w=width;
		this.css.width=this.s.w+'px';
	}
	if(height||height==0){
		this.s.h=height;
		this.css.height=this.s.h+'px';
	}
	return{w:this.el.offsetWidth,h:this.el.offsetHeight}
}
dl.fx.Layout.resize=function(endw,endh,dur){
	if(!this.el || !this.css) return null;
	if(this.timer)   	//stops any previous transitions
		this.stop();
	//initializes parameters
	this.min={w:0,h:0}
	this.max={w:this.el.scrollWidth,h:this.el.scrollHeight}
	this.end={w:(endw||endw==0)? endw : this.max.w, h:(endh||endh==0)? endh : this.max.h}
	this.fps=dl.fx.fps||25;					//transition frame-rate	
	this.dur=dur||0.5;						//transition sureation in seconds
	this.time=parseInt(1000/this.fps); 		//time betweeb iterations
	this.steps=parseInt(this.dur*this.fps);//number of steps
	this.inc={w:Math.round((this.max.w-this.min.w)/this.steps), h:Math.round((this.max.h-this.min.h)/this.steps)};//value increment per step
	if(this.end.w < this.s.w)
		this.inc.w = -this.inc.w;
	if(this.end.h < this.s.h)
		this.inc.h = -this.inc.h;
	this.fire('onstart');
	if(this.s.w == this.end.w && this.s.h == this.end.h) 	//no transition required
		this.stop();
	else
		this.step();
return this;
}
dl.fx.Layout.step=function(){
	var _this=this;
	if(this.inc.w>0 && this.s.w>=this.end.w)
		this.inc.w=0;
	if(this.inc.h>0 && this.s.h>=this.end.h)
		this.inc.h=0;
	if(this.inc.w<0 && this.s.w<=this.end.w)
		this.inc.w=0;
	if(this.inc.h<0 && this.s.h<=this.end.h)
		this.inc.h=0;
	if(!this.inc.w && !this.inc.h) {
		this.stop(this.end.w, this.end.h);
		return this;
	}
	this.size(this.s.w+this.inc.w, this.s.h+this.inc.h);
	this.timer=setTimeout(function(){_this.step()},this.time)
	this.fire('onstep')
return this;
}
dl.fx.Layout.stop=function(w,h){
	clearTimeout(this.timer);
	if(w||h)
		this.size(w,h);
	this.fire('onend')
}
/* Layout events * /
dl.fx.Layout.onstart=function(par){
	trace('start w: '+this.min.w+'<'+this.s.w+'<'+this.max.w+'='+this.end.w+'x'+this.inc.w);
	trace('start h: '+this.min.h+'<'+this.s.h+'<'+this.max.h+'='+this.end.h+'x'+this.inc.h);
	trace('1000/'+this.fps+'='+this.time+' '+this.dur+'*'+this.fps+'='+this.steps);
}
dl.fx.Layout.onstep=function(par){
	trace('start w: '+this.min.w+'<'+this.s.w+'<'+this.max.w+'='+this.end.w+'x'+this.inc.w);
	trace('start h: '+this.min.h+'<'+this.s.h+'<'+this.max.h+'='+this.end.h+'x'+this.inc.h);
}
dl.fx.Layout.onend=function(par){
	trace('start w: '+this.min.w+'<'+this.s.w+'<'+this.max.w+'='+this.end.w+'x'+this.inc.w);
	trace('start h: '+this.min.h+'<'+this.s.h+'<'+this.max.h+'='+this.end.h+'x'+this.inc.h);
	trace('1000/'+this.fps+'='+this.time+' '+this.dur+'*'+this.fps+'='+this.steps);
}
/* end of code enjoy */

