
window.addEventListener?window.addEventListener("load",so_init,false):window.attachEvent("onload",so_init);

var FlyOffsetX=11,FlyOffsetY=10;
var Speed=1,  pause1=false, pause2=false;


Samol = function(AposX,AposY){
	this.PointsY=AposY;
	this.PointsX=AposX;
	this.curPoint=0;
	this.nxtPoint=1;
	this.dX=1;
	this.dY=1;
	this.A=0;
	this.Rf=0;
	this.LastPosX=0;
	this.LastPosY=0;
	this.CurPosX=0;
	this.CurPosY=0;

	this.flyUp = function ()
	{
		this.dX=1;
		this.dY=1;
		this.A=0;
		this.Rf=0;
		this.LastPosX=0;
		this.LastPosY=0;
		this.CurPosX=0;
		this.CurPosY=0;

		if ((this.PointsX[this.nxtPoint]-this.PointsX[this.curPoint])<0) this.dX=-1;
		if ((this.PointsX[this.nxtPoint]-this.PointsX[this.curPoint])!=0) this.dY=(this.PointsY[this.nxtPoint]-this.PointsY[this.curPoint])/(this.PointsX[this.nxtPoint]-this.PointsX[this.curPoint])*this.dX;
		else {
			this.dX=0;
			if ((this.PointsY[this.nxtPoint]-this.PointsY[this.curPoint])<0) this.dY=-1;
		}

		if (this.dX>0) this.A=Math.atan(this.dY);
		else this.A=Math.PI-Math.atan(this.dY);
		this.Rf=Math.round(Math.sqrt((this.PointsX[this.nxtPoint]-this.PointsX[this.curPoint])*(this.PointsX[this.nxtPoint]-this.PointsX[this.curPoint])+(this.PointsY[this.nxtPoint]-this.PointsY[this.curPoint])*(this.PointsY[this.nxtPoint]-this.PointsY[this.curPoint])));
		this.LastPosX=0;
		this.LastPosY=0;
		this.CurPosX=0;
		this.CurPosY=0;
		this.dY=0;
		this.dX=Speed;

		ctx.restore();
		ctx.save();
		ctx.translate(this.PointsX[this.curPoint],this.PointsY[this.curPoint]);
		ctx.rotate(this.A);
	}

	this.fly = function (){
		this.CurPosX+=this.dX;
		this.CurPosY+=this.dY;
		if (Math.abs(this.CurPosX-this.Rf-FlyOffsetX)<FlyOffsetX) {
			this.clear(this.LastPosX,this.LastPosY);
			this.curPoint=this.nxtPoint;
			this.nxtPoint+=1;
			if (this.nxtPoint>=this.PointsX.length) {
				this.nxtPoint=0;
			}
			this.flyUp();
			clearInterval(pause1);
			pause1=false;
			pause2=setTimeout(PauseFly,3000);
		}
		if (pause1!=false) this.draw(Math.round(this.CurPosX),Math.round(this.CurPosY),this.LastPosX,this.LastPosY);
		this.LastPosX=Math.round(this.CurPosX);
		this.LastPosY=Math.round(this.CurPosY);

	}

	this.draw = function (x,y,lx,ly) {
		ctx.clearRect(lx-FlyOffsetX,ly-FlyOffsetY,2*FlyOffsetX,2*FlyOffsetY);

		/*if (this.Rf>6*FlyOffsetX){

			if (Math.abs(x-this.Rf-FlyOffsetX)<(FlyOffsetX*3)){
				ctx.drawImage(img,x-FlyOffsetX,y-FlyOffsetY,FlyOffsetX+Math.round(Math.abs(this.CurPosX-this.Rf-FlyOffsetX)/3)-1,FlyOffsetY+Math.round(Math.abs(this.CurPosX-this.Rf-FlyOffsetX)/3)-3);
			}
			else if (Math.abs(x-FlyOffsetX)<(FlyOffsetX*3)){
				ctx.drawImage(img,x-FlyOffsetX,y-FlyOffsetY,FlyOffsetX+Math.round(Math.abs(this.CurPosX-FlyOffsetX)/3)-1,FlyOffsetY+Math.round(Math.abs(this.CurPosX-FlyOffsetX)/3)-3);
			}
			else
			ctx.drawImage(img,x-FlyOffsetX,y-FlyOffsetY);
		}else*/
		ctx.drawImage(img,x-FlyOffsetX,y-FlyOffsetY);
	}

	this.clear = function (lx,ly) {
		ctx.clearRect(lx-FlyOffsetX,ly-FlyOffsetY,2*FlyOffsetX,2*FlyOffsetY);
	}
}

function PauseFly(){
	pause1=setInterval(GoFly,80);
}
function GoFly(){
	if (pause1!=false) oceanic.fly();
}
function InitFlyUp(){
	oceanic = new Samol(GlobalX,GlobalY);
	oceanic.flyUp();
	pause1=setInterval(GoFly,80);
}
function so_init() {
	if(!document.getElementById)return;

	ctx = document.getElementById('flycontainer').getContext('2d');
	ctx.translate(0,0);
	ctx.save();

	img = new Image();
	img.onload = InitFlyUp;
	img.src="/pub/design/fly.gif";

}
