﻿//animation class
var animations=[];
function Animation(e,ends,speed,user_functions){
	this.e=e;
	this.ends=ends;
	this.oends=DH_clone(this.ends);
	this.starts={};
	this.ostarts={};
	this.speed=speed||0.3;
	this.user_functions=user_functions||{};
	this.interval=40;
	this.to=null;
	this.active=false;
	this.n=animations.length;
	animations[this.n]=this;
	this.init=function(n){
		var self=animations[n];
		if(!self||!self.e||typeof(self.ends)!="object"){animations[n]=null;return false;}
		for(var k in self.ends){
			if(typeof(self.ends[k])=="undefined"||(typeof(self.e.style[k])=="undefined"&&k!="opacity")){animations[self.n]=null;return false;}
			self.starts[k]=DH__(self.e,k);
			if(k=="width"){self.e.style.overflowX="hidden";}
			if(k=="height"){self.e.style.overflowY="hidden";}
			if((k=="left"||k=="top"||k=="right"||k=="bottom")&&self.e.style.position==""){DH_set_styles(self.e,{position:"relative",top:0,left:0});}
		}
		self.ostarts=DH_clone(self.starts);
		if(self.user_functions&&self.user_functions["init"]){self.user_functions["init"]();}
	};
	this.reset=function(n){
		var self=animations[n];
		if(!self){return false;}
		self.starts=DH_clone(self.ostarts);
		self.ends=DH_clone(self.oends);
	};
	this.begin=function(n){
		var self=animations[n];
		if(!self){return false;}
		for(var i=0;i<animations.length;i++){
			if(animations[i]&&animations[i].active&&animations[i].e==self.e){animations[i].clear(animations[i].n);}
		}
		for(var k in self.ends){self.starts[k]=DH__(self.e,k);}
		self.set(n,self.oends);
		if(self.user_functions&&self.user_functions["begin"]){self.user_functions["begin"]();}
	};
	this.step=function(n){
		var self=animations[n];
		if(!self){return false;}
		clearTimeout(self.to);
		self.to=null;
		self.active=false;
		var o={};
		for(var k in self.ends){
			var d=self.ends[k]-self.starts[k];
			if((k!="opacity"&&Math.abs(d)>.5)||Math.abs(d)>.01){
				self.starts[k]+=d*self.speed;
				o[k]=(k!="opacity"&&(self.e.style[k].indexOf("px")!=-1||k=="height"||k=="width"||k=="left"||k=="top"||k=="right"||k=="bottom"))?self.starts[k]+"px":self.starts[k];
				self.active=true;
			}
		}
		if(self.active){
			DH_set_styles(self.e,o);
			self.to=setTimeout(function(){self.step(n);},self.interval);
			if(self.user_functions&&self.user_functions["step"]){self.user_functions["step"]();}
		}else{
			if(self.user_functions&&self.user_functions["end"]){self.user_functions["end"]();}
		}
	};
	this.abort=function(n){
		var self=animations[n];
		if(!self){return false;}
		self.set(n,self.starts);
		if(self.user_functions&&self.user_functions["abort"]){self.user_functions["abort"]();}
	};
	this.reverse=function(n){
		var self=animations[n];
		if(!self){return false;}
		self.set(n,self.ostarts);
		if(self.user_functions&&self.user_functions["reverse"]){self.user_functions["reverse"]();}
	};
	this.clear=function(n){
		var self=animations[n];
		if(!self){return false;}
		clearTimeout(self.to);
		self.to=null;
		self.active=false;
		if(self.user_functions&&self.user_functions["clear"]){self.user_functions["clear"]();}
	};
	this.set=function(n,ends){
		var self=animations[n];
		if(!self||typeof(ends)!="object"){return false;}
		for(var k in ends){if(typeof(self.ends[k])!="undefined"){self.ends[k]=ends[k];}}
		self.clear(n);
		self.step(n);
		if(self.user_functions&&self.user_functions["set"]){self.user_functions["set"]();}
	};
	this.init(this.n);
}
//drag class
var drags=[];
function Drag(e,bounds,hold,user_functions,end_action,drop_action,drops,drop_vars){
	this.e=e;
	this.end_action=end_action||null;
	this.drop_action=drop_action||null;
	this.user_functions=user_functions||{};
	this.drop_vars=drop_vars||null;
	this.drops=drops||null;
	this.bounds=bounds||null;
	this.hold=hold||false;
	this.placeholder=null;
	this.begin_function=function(){};
	this.track_function=function(){};
	this.end_function=function(){};
	this.ox=this.oy=this.oox=this.ooy=this.ocx=this.ocy=this.oz=0;
	this.oposition="relative";
	this.ocursor="pointer";
	this.n=drags.length;
	drags[this.n]=this;
	this.init=function(n){
		var self=drags[n];
		if(!self||!self.e){drags[n]=null;return false;}
		self.begin_function=function(){self.begin(n);return false;};
		self.track_function=function(){self.track(n);return false;};
		self.end_function=function(){self.end(n);return false;};
		self.e.onmousedown=self.begin_function;
		self.e.ondragstart=function(){return false;};
		self.e.onselectstart=function(){return false;};
		self.e.onmouseout=function(){return false;};
		self.oox=_x(self.e,1);
		self.ooy=_y(self.e,1);
		set_styles(self.e,{cursor:"pointer"});
		if(self.user_functions&&self.user_functions["init"]){self.user_functions["init"]();}
	};
	this.begin=function(n){
		var self=drags[n];
		if(!self){return false;}
		for(var i=0;i<animations.length;i++){
			if(animations[i]&&animations[i].active&&animations[i].e==self.e){animations[i].clear(animations[i].n);}
		}
		self.ocx=cx;
		self.ocy=cy;
		self.ox=_x(self.e,1);
		self.oy=_y(self.e,1);
		self.oz=parseFloat(self.e.style.zIndex)||2;
		self.ocursor=self.e.style.cursor||"pointer";
		self.oposition=self.e.style.position||"relative";
		append_event(document,"mousemove",self.track_function);
		append_event(document,"mouseup",self.end_function);
		if(self.hold){
			if(!self.placeholder){
				self.placeholder=copy_element(self.e,null,{className:self.e.className,id:self.e.id+"-placeholder"},{position:self.e.style.position,top:self.e.style.top,left:self.e.style.left,width:_w(self.e)+"px",height:_h(self.e)+"px"});
				self.e.parentNode.insertBefore(self.placeholder,self.e);
			}
		}
		position_element(self.e,"absolute");
		set_styles(self.e,{zIndex:1000000});
		if(self.user_functions&&self.user_functions["begin"]){self.user_functions["begin"]();}
	};
	this.track=function(n){
		var self=drags[n];
		if(!self){return false;}
		set_styles(self.e,{left:self.boundX(n,self.ox+cx-self.ocx)+"px",top:self.boundY(n,self.oy+cy-self.ocy)+"px"});
		if(self.drops){
			for(var i=0;i<self.drops.length;i++){
				if(self.drops[i].test){self.drops[i].test(self.drops[i].n);}
			}
		}
		if(self.user_functions&&self.user_functions["track"]){self.user_functions["track"]();}
	};
	this.end=function(n){
		var self=drags[n];
		if(!self){return false;}
		remove_event(document,"mousemove",self.track_function);
		remove_event(document,"mouseup",self.end_function);
		set_styles(self.e,{cursor:self.ocursor});
		var pre="end";
		if(self.drops){
			for(var i=0;i<self.drops.length;i++){
				if(self.drops[i].test&&self.drops[i].test(self.drops[i].n)){self.drops[i].unhighlight(self.drops[i].n);self.drops[i].receive(self.drops[i].n,self.drop_vars);pre="drop";}
			}
		}
		if(self[pre+"_action"]=="snap"){
			self.ox=self.oox;
			self.oy=self.ooy;
			var ani=new Animation(self.e,{left:self.oox,top:self.ooy},0.4,{end:function(){if(self.hold){delete_element(self.placeholder);self.placeholder=null;}position_element(self.e,"relative");self.e.style.zIndex=self.oz;if(self.user_functions&&self.user_functions["snap"]){self.user_functions["snap"]();}}});
			ani.begin(ani.n);
		}else if(self[pre+"_action"]=="delete"){
			delete_element(self.e);
			if(self.hold){
				var ani=new Animation(self.placeholder,{height:0},0.6,{end:function(){delete_element(self.placeholder);}});
				ani.begin(ani.n);
			}
		}else if(self[pre+"_action"]=="fade"){
			var ani=new Animation(self.e,{opacity:0},0.4,{end:function(){delete_element(self.e);}});
			ani.begin(ani.n);
			if(self.hold){
				ani=new Animation(self.placeholder,{height:0},0.6,{end:function(){delete_element(self.placeholder);}});
				ani.begin(ani.n);
			}
		}else{
			position_element(self.e,self.oposition);
			self.e.style.zIndex=self.oz;
		}
		if(self.user_functions&&self.user_functions["end"]){self.user_functions["end"]();}
	};
	this.boundX=function(n,x){
		var self=drags[n];
		if(!self){return false;}
		if(!self.bounds){return x;}
		if(typeof(self.bounds.left)=="number"){x=Math.max(self.bounds.left,x);}
		if(typeof(self.bounds.right)=="number"){x=Math.min(self.bounds.right,x);}
		return x;
	};
	this.boundY=function(n,y){
		var self=drags[n];
		if(!self){return false;}
		if(!self.bounds){return y;}
		if(typeof(self.bounds.top)=="number"){y=Math.max(self.bounds.top,y);}
		if(typeof(self.bounds.bottom)=="number"){y=Math.min(self.bounds.bottom,y);}
		return y;
	};
	this.init(this.n);
}
//drop class
var drops=[];
function Drop(e,user_functions){
	this.e=e;
	this.user_functions=user_functions||{};
	this.highlighted=false;
	this.n=drops.length;
	drops[this.n]=this;
	this.init=function(n){
		var self=drops[n];
		if(!self||!self.e){drops[n]=null;return false;}
		if(self.user_functions&&self.user_functions["init"]){self.user_functions["init"]();}
	};
	this.test=function(n){
		var self=drops[n];
		if(!self){return false;}
		var x=_x(self.e);
		var y=_y(self.e);
		if(cx>=x&&cx<=x+_w(self.e)&&cy>=y&&cy<=y+_h(self.e)){if(!self.highlighted){self.highlight(n);}return true;}else{if(self.highlighted){self.unhighlight(n);}return false;}
	};
	this.highlight=function(n){
		var self=drops[n];
		if(!self){return false;}
		self.highlighted=true;
		if(self.user_functions&&self.user_functions["highlight"]){self.user_functions["highlight"]();}
	};
	this.unhighlight=function(n){
		var self=drops[n];
		if(!self){return false;}
		self.highlighted=false;
		if(self.user_functions&&self.user_functions["unhighlight"]){self.user_functions["unhighlight"]();}
	};
	this.receive=function(n,vars){
		var self=drops[n];
		if(!self){return false;}
		if(self.user_functions&&self.user_functions["receive"]){self.user_functions["receive"]();}
	};
	this.init(this.n);
}
//element scaling and moving
/*
var edits=[];
function Edit(e,move,scale){
	this.e=e;
	this.move=move||false;
	this.scale=scale||false;
	this.n=edits.length;
	edits[this.n]=this;
	this.init=function(n){
		var self=edits[n];
		if(!self||!self.e){edits[n]=null;return false;}
		position_element(self.e,"absolute");
		if(self.move){
			//self.drag=new Drag(self.grip,{top:0,right:_x(self.grip,1),bottom:_h(self.bar)-_h(self.grip),left:_x(self.grip,1)},false,{track:self.track_function});
		}
		if(self.scale){
		
		}
	};
	this.scale=function(n){
		var self=edits[n];
		if(!self){return false;}
	};
	this.init(this.n);
}
*/