//Tiny lib, image scrolling animation (c)Alexander Melanchenko, http://alexnd.com

//obj, objtarg, anims, type, step, delay
function anim_imgscroll()
{
	this.obj = null;
	this.objtarg = null;
	this.anims = new Array();
	this.aimg = new Array();
	this._namepf='subdiv';
	this._inamepf='subimg';
	this._started=false;
	this._load=false;
	this._initialized=false;
	this._pi=-1;
	this.id=0;
	this.w=100;
	this.x=0;
	this.y=0;
	this.dx=0;
	this.dy=0;
	this.type='L';
	this.mvx=0;
	this.mvy=0;
	this.step=1;
	this.delay=900;
	this.ai=0;
	this.fixw=0;
	
	if(undefined!=arguments[0]){
		if('string'==typeof arguments[0]) this.obj=$$(arguments[0]);
		else if($isobj(arguments[0])) this.obj=arguments[0];
	}
	if(undefined!=arguments[1]){
		if('string'==typeof arguments[1]) this.objtarg=$$(arguments[1]);
		else if($isobj(arguments[1])) this.objtarg=arguments[1];
	}
	if(undefined!=arguments[2]){ this.anims=arguments[2]; }
	if(undefined!=arguments[3]){ this.type=arguments[3]; }
	if(undefined!=arguments[4]){ this.step=arguments[4]; }
	if(undefined!=arguments[5]){ this.delay=arguments[5]; }
	
	this.preload = function(){
		if(undefined==document._anim_preloaders) document._anim_preloaders = new Array();
		this.id=document._anim_preloaders.length;
		document._anim_preloaders[this.id]=this;
		document._anim_preloaders[this.id]._preload(this.id);
	}
	
	this.set_target = function(ob){
		if('string'==typeof ob)this.objtarg=$$(ob);
		else if($isobj(ob))this.objtarg=ob;
	}
	
	this.fixpos = function(){
		var p=$$pos('scrollarea');
		$$setpos('divroll',p.x,p.y);
		this.dx=0;
	}
	
	this.on_load = function(){
		for(var i=0; i<this.aimg.length; i++){
			$$(this._inamepf+this.id+'_'+i).src=this.aimg[i].src;
		}
		this._load=true;
		this.fixpos();
		this.animate(this.id);
	}
	
	this._preload = function(n){
		if(undefined==document._anim_preloaders[n])return;
		var _=document._anim_preloaders[n];
		if('object'!=typeof _)return;
		if(_._pi < 0){
			_._pi++;
		}else if(_._pi < _.anims.length){
			if(_.aimg.length<1)_.preloadnext();
			else if(_.aimg[_._pi].complete){_._pi++;if(_._pi<_.anims.length)_.preloadnext()}
		}else{
			_.on_load();return;
		}
		setTimeout("document._anim_preloaders["+n+"]._preload("+n+")", 100);
	}
	
	this.preloadnext = function(){
		if(this._pi<1)this.obj.innerHTML="";
		var inm=this._inamepf+this.id+'_'+this._pi;
		this.obj.innerHTML+="<div id='"+this._namepf+this.id+'_'+this._pi+"' style='display:none'><img id='"+inm+"' src='/img/t.gif' border='0'></div>";
		this.aimg[this._pi]=new Image();
		this.aimg[this._pi].src=this.anims[this._pi];
		var _=this;
		this.aimg[this._pi].onerror=function(){
			if(_._pi < _.anims.length)_.preloadnext();
		}
	}
	
	this._init = function(){
		this.ai=0;
		var s=$size(this.obj,1);
		var si=$size(this.aimg[this.ai],1);
		var p=$pos(this.obj);
		this.x=p.x;
		this.y=p.y;
		this.w=s.w;
		if(si.w>s.w)this.sw=si.w-s.w+this.fixw;
		else this.sw=0;
		if(this.type=='L'){
			this.dx=-this.sw;
			this.mvx=this.step;
			this.mvy=0;
		}else if(this.type=='R'){
			this.dx=0;
			this.mvx=-this.step;
			this.mvy=0;
		}else if(this.type=='T'){
			this.x=0;
			this.mvx=0;
			this.mvy=-this.step;
		}else if(this.type=='B'){
			this.x=0;
			this.mvx=0;
			this.mvy=0;
		}
		$$setdisplay(this._namepf+this.id+'_'+this.ai, 1);
		$move(this.obj, this.dx, this.dy);
		this._initialized=true;
	}
	
	this.animate = function(n){
		if(undefined==document._anim_preloaders[n])return;
		var _=document._anim_preloaders[n];
		if('object'!=typeof _)return;
		if(!_._initialized)_._init();
		var dx=_.step*_.mvx,dy=_.step*_.mvy;
		_.dx+=dx;
		if(_.mvx<0){
			if(_.dx<-_.sw){
				_.mvx=1;
				_.swapnext();
			}
		}else if(_.mvx>0){
			if(_.dx>0){
				_.mvx=-1;
				_.swapnext();
			}
		}
		$move(_.obj, dx, dy);
		$setclip(_.obj, -_.dx, 'auto', _.w+1+(-_.dx), 'auto');
		setTimeout("document._anim_preloaders["+n+"].animate("+n+")", _.delay);
	}
	
	this.swapnext = function(){
		$$setdisplay(this._namepf+this.id+'_'+this.ai,0);
		this.ai++;
		if(this.ai>=this.aimg.length)this.ai=0;
		$$setdisplay(this._namepf+this.id+'_'+this.ai,1);
	}
	
	this.start = function(){
		this._started=true;
		if(this._load)this.animate(this.id);
	}
}
