// define ufo manager
var UfoMgr = {};

// define ufo class
var Ufo = Class.create();

Ufo.prototype = {

	initialize: function(idLink, optionsForLink, idImage, imageSource, executeClickMethod, speed) {
		this.idLinkUfo ='Ufo_'+idLink;
		this.optionsForLinkUfo = optionsForLink;
		this.idImageUfo = idImage;
		this.executeClick = executeClickMethod;

		this.x1;
		this.y1;
		this.x2;
		this.y2;
		this.direction;
		this.yAxis;
		this.xc;
		this.yc;
		this.speed = speed || 4.5;
		this.periodical;
		this.ufo;
		this.ufoImg;
		this.maxWidth;
		this.maxHeight;

		new Insertion.Top(document.body, '<div id="'+this.idLinkUfo+'" style="cursor:pointer;position:absolute; z-index: 1000;" onclick="UfoMgr[\''+this.idLinkUfo+'\'].catchUfo(); return false;" onmousedown="return false;" onmousemove="return false;"><img style="position:absolute; z-index: 1000;" src="'+imageSource+'" id="'+this.idImageUfo+'"/></div>');
		this.ufo = $(this.idLinkUfo);
		this.ufoImg = $(this.idImageUfo);

		this.maxWidth = function() { return ( typeof(window.innerWidth) == 'undefined' ? document.documentElement.clientWidth : window.innerWidth) - this.ufoImg.width; }
		this.maxHeight = function() {return ( typeof(window.innerHeight) == 'undefined' ? document.documentElement.clientHeight : window.innerHeight) - this.ufoImg.height; }

		this.initPosition();

		UfoMgr[this.idLinkUfo] = this;

		this.ufo.show();
		this.ufoImg.show();
		this.periodical = new PeriodicalExecuter( this.move.bind(this), 0.005);

	},

	initPosition: function() {
		this.x1 = this.xObject();
		this.y1 = this.yObject();

		this.createXObject();
		this.createYObject();

		this.direction = (this.y2 - this.y1) / (this.x2 - this.x1);
		this.yAxis = this.y1 - this.direction * this.x1;

		this.xc = this.x1;
		this.yc = this.y1;
		this.ufo.style.left = this.xc + 'px';
		this.ufo.style.top = this.yc + 'px';
	},

	stop: function() {
		for (idLinkUfo in UfoMgr) {
			UfoMgr[idLinkUfo].periodical.stop();
			UfoMgr[idLinkUfo].ufo.hide();
		};
	},

	xObject: function() {
		var res = Math.floor(Math.random() * this.maxWidth());
		return res;
	},

	yObject:function () {
		var res = Math.floor(Math.random() * this.maxHeight());
		return res;
	},

	createXObject:function () {
		do {
			this.x2 = this.xObject();
		} while(Math.abs(this.x2 - this.x1) < 50);
	},

	createYObject: function() {
		do {
			this.y2 = this.yObject();
		} while(Math.abs(this.y2 - this.y1) < 50);
	},

	catchUfo: function() {
		if(this.ufo.display != 'none') {
			this.stop();
			this.executeClick();
		}
	},

	move: function(pe) {

		try{
			if(this.xc == this.x2 && this.yc == this.y2) {

			this.x1 = this.x2;
			this.y1 = this.y2;

				this.createXObject();
				this.createYObject();

				this.direction = (this.y2 - this.y1) / (this.x2 - this.x1);
				this.yAxis = this.y1 - this.direction * this.x1;

				this.xc = this.x1;
				this.yc = this.y1;
			}

			if(this.x2 > this.x1) {
				this.xc += this.speed;
				if(this.xc > this.x2) {
					this.xc = this.x2;
				}

			} else {
				this.xc -= this.speed;
				if(this.xc < this.x2) {
					this.xc = this.x2;
				}
			}

			this.yc = Math.round(this.yAxis + this.direction * this.xc);

			/*if(this.xc > this.maxWidth()) {
				this.xc = this.maxWidth();
			}

			if(this.yc > this.maxHeight()) {
				this.yc = this.maxHeight();
			}*/

			this.ufo.style.left = this.xc + 'px';
			this.ufo.style.top = this.yc + 'px';
		} catch(e) {
			alert(e);
		}
	},

	remove: function() {
		delete UfoMgr[this.idLinkUfo];
	}
};

var UfoGiftAjax = Class.create();
UfoGiftAjax.prototype = {
	id: 'ufoBox',
	page: projectUrl +'/member/ufo/catch',
	ufo : null,
	
	construct: function(arguments) {
		this.ufo = arguments.ufo;
	},
	
	parameters: function() {
		return "ufo=" + this.ufo;
	},
	
	execute: function(object) {
		this.content();
		showBox('ufoBoxContent');
	}
}
Object.extend(UfoGiftAjax.prototype, CommonAjax);

function initUfo(idLink, optionsForLink, idImage, imageSource, speed) {
	
	var page = typeof(optionsForLink.page) == 'undefined' ? '/member/ufo/catch' : optionsForLink.page;
	
	new Ufo(idLink, optionsForLink, idImage, imageSource, function() {
			Ajax.JSON(new UfoGiftAjax({"ufo" : optionsForLink.id, "page": page}));
		}, speed
	)
	
}
