



function Entity(id,x,y,z,dist,imgs) {
	this.id=id;
	this.x=x;
	this.y=y;
	this.z=z;
	this.images=imgs;

	this.currentimg=0;
	
	this.dist=dist;
	this.adjust();
}
Entity.prototype.adjustdist = function() {
	setinput("f_"+this.id+"_d",this.dist);
	this.adjustx();
}

Entity.prototype.adjustx = function() {
	setinput("f_"+this.id+"_x",this.x);
	if (this.dist>0) {
		setleft("d_"+this.id+"_l",this.x-this.dist);
		setleft("d_"+this.id+"_r",this.x+this.dist);
		return;
	}
	setleft("d_"+this.id,this.x);
}
Entity.prototype.adjusty = function() {
	setinput("f_"+this.id+"_y",this.y);
	if (this.dist>0) {
		settop("d_"+this.id+"_l",this.y);
		settop("d_"+this.id+"_r",this.y);
		return;
	}
	settop("d_"+this.id,this.y);
}

Entity.prototype.adjustimg = function() {
	setinput("f_"+this.id+"_i",this.currentimg);
	if (this.dist>0) {
		//setz(this.id+"_l",this.z);
		//setz(this.id+"_r",this.z);
		//setimage("i_"+this.id+"_l","data/"+this.id+"_l"+"/"+"000"+this.currentimg+".png");
		//setimage("i_"+this.id+"_r","data/"+this.id+"_r"+"/"+"000"+this.currentimg+".png");
		setimage("i_"+this.id+"_l","data/"+this.id+"_l"+"/"+padnull(this.currentimg,4)+".png");
		setimage("i_"+this.id+"_r","data/"+this.id+"_r"+"/"+padnull(this.currentimg,4)+".png");
		return;
	}
	//setimage("i_"+this.id,"data/"+this.id+"/"+"000"+this.currentimg+".png");
	setimage("i_"+this.id,"data/"+this.id+"/"+padnull(this.currentimg,4)+".png");
}


Entity.prototype.adjust = function() {
	this.adjustx();
	this.adjusty();
	this.adjustimg();
	this.adjustdist();
	if (this.dist>0) {
		//setz(this.id+"_l",this.z);
		//setz(this.id+"_r",this.z);
		return;
	}
	//setz(this.id,this.z);
}



Entity.prototype.change = function() { 
	var obj;
	obj=getelement("f_"+this.id+"_x");
	if (obj) { this.x=parseInt(obj.value); }
	obj=getelement("f_"+this.id+"_y");
	if (obj) { this.y=parseInt(obj.value); }
	obj=getelement("f_"+this.id+"_i");
	if (obj) { this.currentimg=parseInt(obj.value); }
	obj=getelement("f_"+this.id+"_d");
	if (obj) { this.dist=parseInt(obj.value); }
	this.adjust();
}

Entity.prototype.movex = function(c) { this.x+=c; this.adjustx(); }
Entity.prototype.movey = function(c) { this.y+=c; this.adjusty(); }
Entity.prototype.movedist = function(c) { this.dist+=c; this.adjustdist(); }

Entity.prototype.imgup = function() {
	this.currentimg++;
	if (this.currentimg>=this.images) this.currentimg=0;
	this.adjustimg();
}
Entity.prototype.imgdown = function() {
	this.currentimg--;
	if (this.currentimg<0) this.currentimg=this.images-1;
	this.adjustimg();
}
Entity.prototype.tostring = function() {
	var str="";
	str+=this.x+"_";
	str+=this.y+"_";
	//str+=this.z+"_";
	str+=this.dist+"_";
	str+=this.currentimg+"_";
	return str;
}
Entity.prototype.fromstring = function(str) {
	var a;
	a=str.split("_");
	this.x=parseInt(a[0]);
	this.y=parseInt(a[1]);
	this.dist=parseInt(a[2]);
	this.currentimg=parseInt(a[3]);
}

function imgdown(num) { var ent; ent=entities[num]; ent.imgdown(); }
function imgup(num) { var ent; ent=entities[num]; ent.imgup(); }

function xdown(num) { var ent; ent=entities[num]; ent.movex(-1); }
function xup(num) { var ent; ent=entities[num]; ent.movex(1); }

function ydown(num) { var ent; ent=entities[num]; ent.movey(-1); }
function yup(num) { var ent; ent=entities[num]; ent.movey(1); }

function distdown(num) { var ent; ent=entities[num]; ent.movedist(-1); }
function distup(num) { var ent; ent=entities[num]; ent.movedist(1); }

function change(num) { var ent; ent=entities[num]; ent.change(); }

function imglist(num) {
	//iframe frame fuellen mit liste der bilder
	var ent; ent=entities[num];
	var id=ent.id;
	if (ent.dist>0) id+="_l";
	frames[0].display_list(id,num,ent.images);
}
function select_img(entid,imgid) {
	var ent; ent=entities[entid];
	ent.currentimg=imgid;
	ent.adjustimg();
}

//fixme: checks
function save() {
	var str="";
	for (var i=0;i<entities.length;i++) {
		str+=entities[i].tostring();
		str+=",";
	}
	var obj=getelement("save");
	obj.value=str;
}

function load() {
	var obj=getelement("save");
	var str=obj.value;
	var a=str.split(",");
	
	for (var i=0;i<entities.length;i++) {
		entities[i].fromstring(a[i]);
		entities[i].adjust();
	}
}


var entities=[];
var ent;

ent=new Entity("hair",0,0,0,0,6);
entities.push(ent);

ent=new Entity("eyebrow",0,-20,0,40,4);
entities.push(ent);

ent=new Entity("eye",0,-5,0,40,8);
entities.push(ent);

ent=new Entity("glasses",0,0,0,0,3);
entities.push(ent);

ent=new Entity("ear",0,-5,0,90,1);
entities.push(ent);

ent=new Entity("nose",0,35,0,0,6);
entities.push(ent);
ent=new Entity("beard",0,40,0,0,3);
entities.push(ent);

ent=new Entity("mouth",0,80,0,0,6);
entities.push(ent);
ent=new Entity("head",0,0,0,0,7);
entities.push(ent);
ent=new Entity("hat",0,-150,0,0,3);
entities.push(ent);



