// this function adds he editor functions.

freevolution.prototype.hasmoved=new Array();
freevolution.prototype.shouldmove=new Array();
freevolution.prototype.tilehelperthread=false;
freevolution.prototype.dragObj=new Object;

freevolution.prototype.extend(
	{
		starttilehelperthread:function(){
			if(this.tilehelperthread==false && this.shouldmove.length!=0){
				this.tilehelperthread=true;
				while(this.shouldmove.length>0){
					tile = this.shouldmove.pop();
					if(!this.hasmoved.hasValue(tile)){
						tile = tile.split('-');
						this.matchneighbours(parseInt(tile[0]),parseInt(tile[1]));
					}
					// do something with tile
				}
				this.tilehelperthread=false;
			}
			return true;
		},
		matchneighbours:function(x,y){
			var tile=this.tiles[ x ][ y ];
			var moved=new Array(false,false,false,false);
			// test all 4 neighbours.
			var previousnorth=tile[3][0];
			if(this.hasmoved.hasValue(x+'-'+(y+1)) ){ // check the tile to the bottom right
				if(this.tiles[x][(y+1)][3][0] + this.tiles[x][(y+1)][4] != tile[3][1] + tile[4]){
					tile[3][1]=(this.tiles[x][(y+1)][4] - tile[4]) + this.tiles[x][(y+1)][3][0];
					moved[1]=true;
				}
				if(this.tiles[x][(y+1)][3][3] + this.tiles[x][(y+1)][4] != tile[3][2] + tile[4]){
					tile[3][2]=(this.tiles[x][(y+1)][4] - tile[4]) + this.tiles[x][(y+1)][3][1];
					moved[2]=true;
				}
			}
			if(this.hasmoved.hasValue((x-1)+'-'+y) ){ // check the tile to the top right
				if(this.tiles[(x-1)][y][3][3] + this.tiles[(x-1)][y][4] != tile[3][0] + tile[4]){
					tile[3][0]=(this.tiles[(x-1)][y][4] - tile[4]) + this.tiles[(x-1)][y][3][3];
					moved[0]=true;
				}
				if(this.tiles[(x-1)][y][3][2] + this.tiles[(x-1)][y][4] != tile[3][1] + tile[4]){
					tile[3][1]=(this.tiles[(x-1)][y][4] - tile[4]) + this.tiles[(x-1)][y][3][2];
					moved[1]=true;
				}
			}
			if(this.hasmoved.hasValue((x+1)+'-'+y) ){ // check the tile to the bottom left
				if(this.tiles[(x+1)][y][3][0] + this.tiles[(x+1)][y][4] != tile[3][3] + tile[4]){
					tile[3][3]=(this.tiles[(x+1)][y][4] - tile[4]) + this.tiles[(x+1)][y][3][0];
					moved[3]=true;
				}
				if(this.tiles[(x+1)][y][3][1] + this.tiles[(x+1)][y][4] != tile[3][2] + tile[4]){
					tile[3][2]=(this.tiles[(x+1)][y][4] - tile[4]) + this.tiles[(x+1)][y][3][1];
					moved[2]=true;
				}
			}
			if(this.hasmoved.hasValue(x+'-'+(y-1)) ){ // check the tile to the top left
				if(this.tiles[x][(y-1)][3][1] + this.tiles[x][(y-1)][4]  != tile[3][0] + tile[4]){
					tile[3][0]=(this.tiles[x][(y-1)][4] - tile[4]) + this.tiles[x][(y-1)][3][1];
					moved[0]=true;
				}
				if(this.tiles[x][(y-1)][3][2] + this.tiles[x][(y-1)][4] != tile[3][3] + tile[4]){
					tile[3][3]=(this.tiles[x][(y-1)][4] - tile[4])+this.tiles[x][(y-1)][3][2];
					moved[3]=true;
				}
			}
			if(previousnorth!=tile[3][0]){ // check for north move
				var northdelta=previousnorth-tile[3][0];
				tile[2].style.top=parseInt(tile[2].style.top)+(northdelta*8)+'px';
			}
			tile[2].src=this.backgroundpath+tile[5]+'/'+tile[3][0]+'-'+tile[3][1]+'-'+tile[3][2]+'-'+tile[3][3]+'.png';
			//tile[2].style.border='1px solid #f00';
			this.hasmoved.pushUnique(x+'-'+y);
			//return false;
			if(moved[0]==true){
				this.shouldmove.pushUnique((x-1)+'-'+y);
				this.shouldmove.pushUnique(x+'-'+(y-1));
			}
			if(moved[1]==true){
				this.shouldmove.pushUnique((x+1)+'-'+y);
				this.shouldmove.pushUnique(x+'-'+(y-1));
			}
			if(moved[2]==true){
				this.shouldmove.pushUnique((x+1)+'-'+y);
				this.shouldmove.pushUnique(x+'-'+(y+1));
			}
			if(moved[3]==true){
				this.shouldmove.pushUnique((x-1)+'-'+y);
				this.shouldmove.pushUnique(x+'-'+(y+1));
			}
			this.starttilehelperthread();
		},
		movetile:function(z){
			// x,y,z
			//var z=1;
			// this function moves the selected tile by z.
			
			var x=parseInt(this.cursorover['x']);
			var y=parseInt(this.cursorover['y']);
			
			var tile=this.tiles[ x ][ y ];
			var tileimg=tile[2];
			var moved=new Array(false,false,false,false);
			var tileup=false;	

			if(z==0){
				return true;
			} else if(z>0){
				for(var zstep=0;zstep<z;zstep++){
					// increase by 1
					var lowest=tile[3].min();
					for(var i=0;i<4;i++){
						if(tile[3][i] <= lowest ){
							moved[i]=true;
						}
					}
					// did north move?
					if(tile[3][0]==lowest){
						this.cursor.style.top=tileimg.style.top=parseInt(tileimg.style.top)-8+'px';
					}
					for(var i=0;i<4;i++){
						if( tile[3][i]!=lowest ){
							// decrease all other corners
							tile[3][i]--;
						}
					}
					tile[4]++;
				}
			} else if(z<0) {
				for(var zstep=0;zstep>z;zstep--){
					// decrease by 1
					var heighest=tile[3].max();	
					for(var i=0;i<4;i++){
						if(tile[3][i] <= heighest ){
							moved[i]=true;
						}
					}
					
					// did north move?
					if(tile[3][0]==heighest && !(tile[3][1]>tile[3][0]+2) && !(tile[3][3]>tile[3][0]+2)){
						this.cursor.style.top=tileimg.style.top=parseInt(tileimg.style.top)+8+'px';
					}
					for(var i=0;i<4;i++){
						if( tile[3][i]!=heighest ){
							// increase all other corners
							tile[3][i]++;
						}
					}
				}
			}

			//var path=tileimg.src.split('/');
			//var type=path[ path.length-2 ];
			tileimg.src=this.backgroundpath+tile[5]+'/'+tile[3][0]+'-'+tile[3][1]+'-'+tile[3][2]+'-'+tile[3][3]+'.png';
			this.cursor.src=this.backgroundpath+'cursor/'+tile[3][0]+'-'+tile[3][1]+'-'+tile[3][2]+'-'+tile[3][3]+'.png';
			//this.cursor.style.zIndex=this.cursor.style.zIndex+1;
			//tileimg.style.zIndex=tileimg.style.zIndex+1;
			
			// process neighbours
			this.hasmoved.pushUnique(x+'-'+y);
			if(moved[0]==true){
				this.shouldmove.pushUnique((x-1)+'-'+y);
				this.shouldmove.pushUnique(x+'-'+(y-1));
			}
			if(moved[1]==true){
				this.shouldmove.pushUnique((x+1)+'-'+y);
				this.shouldmove.pushUnique(x+'-'+(y-1));
			}
			if(moved[2]==true){
				this.shouldmove.pushUnique((x+1)+'-'+y);
				this.shouldmove.pushUnique(x+'-'+(y+1));
			}
			if(moved[3]==true){
				this.shouldmove.pushUnique((x-1)+'-'+y);
				this.shouldmove.pushUnique(x+'-'+(y+1));
			}
			this.starttilehelperthread();
			return true;
		},
		
		move_vertex:function (x,y,changes){
			if(!this.tiles[x] && !this.tiles[x][y]){
				return false;
			}
			var tile=this.tiles[x][y];
			var tileimg=tile[2];
			
			var moved=new Array(false,false,false,false);
			for(i=0;i<4;i++){
				if(changes[i]!=null){
					moved[i]=true;
					tile[3][i]=tile[3][i]+changes[i];
				}
			}
			var lowest=tile[3].min();
			if(lowest<0){
				for(i=0;i<4;i++){
					tile[3][i]=tile[3][i]-lowest;
				}
				lowest=0;
				// increase tile height in array somewhere
			}
			
			var tiletop=parseInt(tileimg.style.top);
			if(moved[0]!=false){
				if(changes[0]!=-1){
					tileimg.style.top=tiletop-(8*changes[0])+'px';
				} else {
					if(tile[3][1]<=(tile[3][0]+2) && tile[3][3]<=(tile[3][0]+2)){
						tileimg.style.top=tiletop-(8*changes[0])+'px';
					}
				}
			}
			if(tile[3][1]>(tile[3][0]+2) && moved[1]==true){
				tileimg.style.top=tiletop-(8*changes[1])+'px';
			}
			if(tile[3][3]>(tile[3][0]+2) && moved[3]==true){
				tileimg.style.top=tiletop-(8*changes[3])+'px';
			}
			var path=tileimg.src.split('/');
			var type=path[ path.length-2 ];
			tileimg.src=this.backgroundpath+type+'/'+tile[3][0]+'-'+tile[3][1]+'-'+tile[3][2]+'-'+tile[3][3]+'.png';
		},
		
		dragGo:function(event) {
			if(this.drag==true){
				this.cursor.style.top=parseInt(this.cursor.style.top)-(this.dragObj.clickY-event.layerY)+'px';
				var tile=this.tiles[ this.cursorover['x'] ][this.cursorover['y'] ][2];
				var diff=Math.floor((parseInt(tile.style.top)-parseInt(this.cursor.style.top))/8);	
				if(diff>0){
					for(var i=0;i<diff;i++){
						this.drag=false;
						this.movetile(1);
						this.drag=true;
					}
				} else {
					for(var i=0;i>diff;i--){
						this.drag=false;
						this.movetile(-1);
						this.drag=true;
					}
				}
				event.preventDefault();
			}
		},
	
		dragStop:function(event) {
			// Stop capturing mousemove and mouseup events.	
			this.drag=false;
			this.cursor.removeEventListener("mousemove",   this.dragGo, true);
			this.cursor.removeEventListener("mouseup",   this.dragStop, true);
		}
	}
);
