/**
 * @author bruno DA SILVA
 */

var Config = Class.create({
	nbWeuk : 20
	, GameWidth : 800
	, GameHeight : 600
	//interval entre les call de repos
	, restInterval: 1000
	, restMinInterval: 1000
	, getWeukSrc: function(id) {
		return './img/Weuk-'+(parseInt(id) % 3 + 1)+'.gif'
	}
	, getRestInterval: function() {
	 	return parseInt(Math.random() * this.restInterval + this.restMinInterval);
	}
	//interval entre les call de vol
	, FlyMinInterval: 1000
	, FlyInterval: 3000
	, getFlyInterval: function() {
	 	return parseInt(Math.random() * this.FlyInterval + this.FlyMinInterval);
	}
	//durree des animations de vol
	, flySpeed: 4
	, flyMinSpeed: 0.1
	, getFlySpeed: function() {
	 	return parseInt(Math.random() * this.flySpeed + this.flyMinSpeed);
	}
	//groupe du vol
	, scopeAmount : 4
	, getFlyScope: function() {
		return parseInt(Math.random() * this.scopeAmount);
	}
	//init modif des param en fonction de la page
	, init: function() {
		var reg = new RegExp("synd=", "g");
		var url = window.location;
		if (reg.test(url.toString())) {
			//alors on est sur igoogle, on reduit la taille de l interface :
			this.GameWidth = 365
			this.GameHeight = 540
			//code sale : pas d'affichage dans une config :'(, mais pas le tmeps, je doit aller me coucher...
			weukHunt.reduce()
		}
	}
	 
});

var Weuck = Class.create({
	init: function(id) {
		this.id = id;
		//elmt n'est pas pointé car pointer un element DOM en JS peut créer 
		//des redondancs cycliques, en lieu et place, on utilisera l'id
		var elmt = new Element('img', { 
			'class': 'weuk '+this.id
			, 'id': 'weuk_'+this.id
			, src: Conf.getWeukSrc(this.id) 
		});		
		$("gameContent").appendChild(elmt);
		this.x = 0;
		this.y = 0;
		
		//listener
		elmt.observe('mousedown', function(event) {
			var element = event.element();
			element.remove();
			weukHunt.addPoint(10);
			Event.stop(event)
			weukHunt.removeWeuk(this.id);
		});
		
		this.rest();
	}, 
	fly: function() {
		var id = this.id
		this.caclNextpos();
		new Effect.Move($('weuk_'+this.id), { 
			x: this.x
			, y: this.y
			, mode: 'absolute'
			, duration: Conf.getFlySpeed()
			, queue: { 
				position: 'end'
				, scope: 'weuk'+Conf.getFlyScope()
			} 
			, afterFinishInternal: function() { //callbackFunction
			setTimeout('weukHunt.getWeuk('+id+').rest()', Conf.getFlyInterval());
		}
		});
	},
	rest: function() {
		setTimeout('weukHunt.getWeuk('+this.id+').fly()', Conf.getRestInterval())
	}, 
	caclAbspos: function(refSize, actualPos, posDecalage, negDecalage) {
		var pos;
		if (actualPos < (refSize / 2)) {
			pos = refSize + posDecalage;
		} else {
			pos = - negDecalage;
		}
		return pos;
	},
	caclRelpos: function(refSize) {
		return parseInt(Math.random() * refSize);
	},
	caclNextpos: function() {
		if (parseInt(Math.random() *2) == 1) {
			this.x = this.caclAbspos(Conf.GameWidth, this.x, 0, 125);
			this.y = this.caclRelpos(Conf.GameHeight, this.y, 0, 0);
		} else {
			this.x = this.caclRelpos(Conf.GameWidth, this.x, 0, 0);
			this.y = this.caclAbspos(Conf.GameHeight, this.y, 0, 125);
		}
		
	}
});

var Game = Class.create({		
	init: function() {
		this.point = 0;
		this.addPoint(0)
		this.affiche();
		this.nbWeuk = Conf.nbWeuk;
		this.tabWeuk = new Array();
		$("gameContent").update();
		for (var i = 0; i < this.nbWeuk; i++) {
			this.tabWeuk[i] = new Weuck(); 			
			this.tabWeuk[i].init(i);
		}
		$("gameContent").observe('mousedown', function(event) {
			weukHunt.addPoint(-10);
		});
		this.pe = new PeriodicalExecuter(function(pe) {
			weukHunt.addPoint(-0.25);
		}, 0.1);
		$('nbWeuk').update(this.nbWeuk);

	},
	addPoint: function (points) {
		this.point = this.point + points;
		$('score').update(parseInt(this.point));
	},
	makeRollement: function(message) {
		return this.name + ': ' + message;
	}, 
	getWeuk: function(id) {
		return this.tabWeuk[id];
	},
	removeWeuk: function(id) {
		this.tabWeuk[id] = null;
		this.nbWeuk--;
		if (this.nbWeuk <= 0) {
			this.pe.stop();
			$('diag').update('Finished!!!');
		}
		$('nbWeuk').update(this.nbWeuk);
	},
	affiche : function() {
		new Effect.Morph('gameContent', {
				style: 'background:#7BC4F4; width: '+Conf.GameWidth+'px; height: '+Conf.GameHeight+'px'
				, duration: 3 
		});
	},
	reduce : function() {
		Effect.SlideUp('cd');
		$('header').update('score :<span id="score"></span><span id="score"></span> - (<span id="nbWeuk">0</span> weuks restants) - game by<a target="_blank" href="http://les-da-silva.fr/cv"> Bruno DA SILVA</a>');
		new Effect.Morph('coeur', {
				style: 'width: '+Conf.GameWidth+'px; height: '+Conf.GameHeight+'px; margin:0px; padding:0px;'
				, duration: 3 
		});
		new Effect.Morph('page', {
				style: 'width: '+Conf.GameWidth+'px; height: '+Conf.GameHeight+'px; margin:0px; padding:0px;'
				, duration: 3 
		});
		new Effect.Morph('coeur', {
				style: 'width: '+Conf.GameWidth+'px; height: '+Conf.GameHeight+'px; margin:0px; padding:0px;'
				, duration: 3 
		});
		new Effect.Morph('gameContent', {
				style: 'margin:0px; padding:0px;'
				, duration: 3 
		});
	}
});
var Conf = new Config();
var weukHunt = new Game();

Event.observe(window, 'load', function() {
	Conf.init();
	weukHunt.init();
});