// UFO MANAGEMENT
var ufoManager = {

	elem: null,
	elemTab: null,

	dataTab: null,

	x1: null,
	y1: null,
	x2: null,
	y2: null,
	xc: null,
	yc: null,

	direction: null,
	ordonnee: null,
	speed: null,

	type: null,
	paramAjax: null,

	NORMAL: 0,
	DOUDOU: 1,

	/* MARKETING */
	SPRING_2009: 10,
	SIGNUP_GIFT: 11,
	MANY_OBJECT: 12,


	init: function(image, speed, type, paramAjax, decalY) {

		if(type == undefined) {
			this.type = this.NORMAL;
		}
		else {
			this.type = type;
		}

		if(paramAjax == undefined) {
			this.paramAjax = '';
		}
		else {
			this.paramAjax = paramAjax;
		}


		if(this.type === this.SIGNUP_GIFT) {

			// Init tabs
			if(this.elemTab === null) {
				this.elemTab = new Array();
				this.dataTab = new Array();
			}

			// Set each ufo informations
			var id = this.elemTab.length;

			this.elemTab[id] = $('ufo_'+id);
			this.elemTab[id].innerHTML = image;
			this.elemTab[id].show();

			this.dataTab[id] = {};
			this.dataTab[id]['x1'] = Math.floor(Math.random() * 900) - 100;
			this.dataTab[id]['y1'] = 530 + Math.random() * 90;
			this.dataTab[id]['speed'] = speed;
			this.dataTab[id]['decalY'] = decalY;

			this.elemTab[id].style.left = this.dataTab[id]['x1'] + 'px';
			this.elemTab[id].style.top = this.dataTab[id]['y1'] + 'px';

			// When the last ufo is in tabs
			if(id === 14) {
				setInterval('ufoManager.moveVerticalAllObject()', 10);
			}
		}
		else if (this.type === this.MANY_OBJECT) {

			// Init tabs
			if(this.elemTab === null) {
				this.elemTab = new Array();
				this.dataTab = new Array();
			}

			// Set each ufo informations
			var id = this.elemTab.length;

			this.elemTab[id] = $('ufo_'+id);
			this.elemTab[id].innerHTML = image;
			this.elemTab[id].show();

			this.dataTab[id] = {};
			this.dataTab[id]['x1'] = this.xObject();;
			this.dataTab[id]['y1'] = this.yObject();
			this.dataTab[id]['x2'] = this.createXObject(this.dataTab[id]['x1']);
			this.dataTab[id]['y2'] = this.createYObject(this.dataTab[id]['y1']);

			this.dataTab[id]['direction'] = (this.dataTab[id]['y2'] - this.dataTab[id]['y1']) / (this.dataTab[id]['x2'] - this.dataTab[id]['x1']);
			this.dataTab[id]['ordonnee'] = this.dataTab[id]['y1'] - this.dataTab[id]['direction'] * this.dataTab[id]['x1'];

			this.dataTab[id]['xc'] = this.dataTab[id]['x1'];
			this.dataTab[id]['yc'] = this.dataTab[id]['y1'];

			this.dataTab[id]['speed'] = speed;

			// When the last ufo is in tabs
			setInterval('ufoManager.moveAllObject()', 10);

		}
		else {
			if(this.type === this.DOUDOU) {
				this.elem = $('ufoDoudou');
			}
			else {
				this.elem = $('ufo');
			}
			this.elem.innerHTML = image;
			this.elem.show();

			this.x1 = this.xObject();
			this.y1 = this.yObject();

			this.x2 = this.createXObject(this.x1);
			this.y2 = this.createYObject(this.y1);

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

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

			this.speed = speed;

			setInterval('ufoManager.moveObject()', 10);
		}

	},

	redirect: function() {

		if(this.elem !== null && this.elem.display !== 'none') {
			this.elem.hide();

			if(this.type === this.DOUDOU) {
				Ajax.JSON(new GetDoudouFoundAjax({params: this.paramAjax}));
			}
			else if (this.type === this.SPRING_2009){
				Ajax.JSON(new GetMarketingUfoAjax({}));
			}
			else {
				Ajax.JSON(new GetProductUfoAjax({}));
			}
		}
		else if((this.type === this.SIGNUP_GIFT || this.MANY_OBJECT) && 
			this.elemTab !== null && 
			this.elemTab[0].display !== 'none') {

			for(var cUfo = 0; cUfo < this.elemTab.length; cUfo++) {
				this.elemTab[cUfo].hide();
			}
			Ajax.JSON(new GetMarketingUfoAjax({}));
		}

	},

	xObject: function() {
		return Math.floor(Math.random() * 900);
	},

	yObject: function() {
		return Math.floor(Math.random() * 500);
	},

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

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

	moveObject: function() {

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

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

			this.x2 = this.createXObject(this.x1);
			this.y2 = this.createYObject(this.y1);

			this.direction = (this.y2 - this.y1) / (this.x2 - this.x1);
			this.ordonnee = 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.ordonnee + this.direction * this.xc);

		this.elem.style.left = this.xc + 'px';
		this.elem.style.top = this.yc + 'px';
	},

	moveVerticalAllObject: function() {

		for(var cUfo = 0; cUfo < this.elemTab.length; cUfo++) {

			// Update x
			var xRand = Math.floor(Math.random() * 6);
			if(Math.random() < 0.5) {
				xRand = -xRand;
			}
			this.dataTab[cUfo]['x1'] = this.dataTab[cUfo]['x1'] + xRand;

			// Update y
			this.dataTab[cUfo]['y1'] = this.dataTab[cUfo]['y1'] - this.dataTab[cUfo]['speed'];
			if(this.dataTab[cUfo]['y1'] <= this.dataTab[cUfo]['decalY']) {
				this.dataTab[cUfo]['y1'] = this.dataTab[cUfo]['decalY'];
			}

			// Update style
			this.elemTab[cUfo].style.left = this.dataTab[cUfo]['x1'] + 'px';
			this.elemTab[cUfo].style.top = this.dataTab[cUfo]['y1'] + 'px';
		}

	},
	moveAllObject: function() {

		for(var id = 0; id < this.elemTab.length; id++) {

			if(this.dataTab[id]['xc'] == this.dataTab[id]['x2'] && this.dataTab[id]['yc'] == this.dataTab[id]['y2']) {

				this.dataTab[id]['x1'] = this.dataTab[id]['x2'];
				this.dataTab[id]['y1'] = this.dataTab[id]['y2'];

				this.dataTab[id]['x2'] = this.createXObject(this.dataTab[id]['x1']);
				this.dataTab[id]['y2'] = this.createYObject(this.dataTab[id]['y1']);

				this.dataTab[id]['direction'] = (this.dataTab[id]['y2'] - this.dataTab[id]['y1']) / (this.dataTab[id]['x2'] - this.dataTab[id]['x1']);
				this.dataTab[id]['ordonnee'] = this.dataTab[id]['y1'] - this.dataTab[id]['direction'] * this.dataTab[id]['x1'];

				this.dataTab[id]['xc'] = this.dataTab[id]['x1'];
				this.dataTab[id]['yc'] = this.dataTab[id]['y1'];

			}

			if(this.dataTab[id]['x2'] > this.dataTab[id]['x1']) {

				this.dataTab[id]['xc'] += this.dataTab[id]['speed'];

				if(this.dataTab[id]['xc'] > this.dataTab[id]['x2']) {
					this.dataTab[id]['xc'] = this.dataTab[id]['x2'];
				}

			} else {

				this.dataTab[id]['xc'] -= this.dataTab[id]['speed'];

				if(this.dataTab[id]['xc'] < this.dataTab[id]['x2']) {
					this.dataTab[id]['xc'] = this.dataTab[id]['x2'];
				}

			}

			this.dataTab[id]['yc'] = Math.round(this.dataTab[id]['ordonnee'] + this.dataTab[id]['direction'] * this.dataTab[id]['xc']);

			this.elemTab[id].style.left = this.dataTab[id]['xc'] + 'px';
			this.elemTab[id].style.top = this.dataTab[id]['yc'] + 'px';
		}

	}
};



var GetProductUfoAjax = Class.create();
GetProductUfoAjax.prototype = {

	id: 'productBox',
	page: projectUrl +'/member/product/prepare/getUfo',

	execute: function(object) {
		this.content();
		showBox('productBoxContent');
	}

}
Object.extend(GetProductUfoAjax.prototype, CommonAjax);


var GetMarketingUfoAjax = Class.create();
GetMarketingUfoAjax.prototype = {

	id: 'productBox',
	page: projectUrl +'/member/marketing/ufo/getDetail',

	execute: function(object) {
		this.content();
		showBox('productBoxContent');
	}

}
Object.extend(GetMarketingUfoAjax.prototype, CommonAjax);
