/*
Funcionalidad para mover el ovni

18/09/2011 0:12:53
// áéúíó
*/



(function($){

var fps=18;
var frame=0;
var timeoutID;
var sprite = new Array();
var debug;
var finish=false;
var border_left=-50.0;
var border_right=1800.0;
var border_left_actual=border_left;
var border_right_actual=border_right;
var border_top=0.0;
var border_bottom=198.0;
var border_top_actual=border_top;
var border_bottom_actual=border_bottom;
var zoom_mode='x';
var gb_debug=false;

$(document).ready(function(){
	debug=$('div#debug');
	
	sprite[0] = new Array();
	sprite[0][0]='ovni';
	sprite[0][1]=-70.0; // x
	sprite[0][2]=0.0;   // y
	sprite[0][3]=2.1;   // avance X
	sprite[0][4]=0.2;   // avance y
	sprite[0][5]=0.1;   // incremento del avance X
	sprite[0][6]=0.1;   // incremento del avance y
	sprite[0][7]=1.0;   // scale
	sprite[0][8]=0.0;   // rotation
	sprite[0][9]=1.0;   // avance rotation
	sprite[0][10]=1.0;   // máximo avance actual x
	sprite[0][11]=1.0;   // máximo avance actual y
	sprite[0][12]=106;   // z-index
	
	sprite[0][0]=$('div#ovni');
	sprite[0][13]=document.getElementById('ovni');
	timeoutID = window.setTimeout($.f_run, (1000 / fps));
});


$.f_run = function() {
	$.f_path();
	
	frame++;
	
	// Es necesario poner el show() al final para que funcione correctamente en webkit
	
	sprite[0][0].css('left', Math.round(sprite[0][1]).toString()+'px')
	            .css('top',  Math.round(sprite[0][2]).toString()+'px').show();
	            
	sprite[0][0].css('z-index',  sprite[0][12].toString()).show();
	
	sprite[0][0].scale(sprite[0][7].toString())
	            .rotate(sprite[0][8].toString()+'deg')
	            .show();
	
	if ($.gb_debug) debug.html(
		'frame='+frame+'<br/>x='+sprite[0][1].toString()+' ['+Math.round(sprite[0][1]).toString()+']<br/>y='+sprite[0][2].toString()+' ['+Math.round(sprite[0][2]).toString()+']'+
		'<br/>av_x='+sprite[0][3]+'<br/>av_y='+sprite[0][4]+
		'<br/>av_av_x='+sprite[0][5]+'<br/>av_av_y='+sprite[0][6]+
		'<br/>scale='+sprite[0][7]+'<br/>rotation='+sprite[0][8]+'<br/>av rotation='+sprite[0][9]+
		'<br/>max_av_actual x='+sprite[0][10]+'<br/>max_av_actual y='+sprite[0][11]+
		'<br/>z-index='+sprite[0][12]+
		'<br/>border_left_actual='+border_left_actual+'<br/>border_right_actual='+border_right_actual+
		'<br/>border_top_actual='+border_top_actual+'<br/>border_bottom_actual='+border_bottom_actual+
		'<br/>zoom_mode='+zoom_mode
	);
	
	if (!finish) {
		timeoutID = window.setTimeout($.f_run, (1000 / fps));
	}
}


$.f_reset = function() {
	frame=0;
	finish=false;
}


// Aquí vamos calculando el movimiento del ovni
$.f_path = function() {
	// Recoge los valores para calcularlos más cómodamente
	x= sprite[0][1]; y= sprite[0][2]; av_x = sprite[0][3]; av_y = sprite[0][4];
	av_av_x = sprite[0][5]; av_av_y = sprite[0][6];
	scale=sprite[0][7];
	rotation=sprite[0][8];
	av_rotation=sprite[0][9];
	av_x_max = sprite[0][10]; av_y_max = sprite[0][11];
	zindex = sprite[0][12];
	
	
	
	// Gestiona los avances
	av_x += av_av_x;
	if (Math.abs(av_x) >= av_x_max) {
		av_x_max = 12.0 + Math.floor(Math.random()*3);
		av_av_x = -av_av_x;
		av_x += av_av_x;
	} else if (Math.abs(av_x) <= 0.1) {
		av_av_x = -av_av_x;
		av_x += av_av_x;
	};
	
	av_y += av_av_y;
	if (Math.abs(av_y) >= av_y_max) {
		av_y_max = 4.0 + Math.floor(Math.random()*2);
		av_av_y = -av_av_y;
		av_y += av_av_y;
	} else if (
		Math.abs(av_y) <= 0.1) {av_av_y = -av_av_y;
		av_y += av_av_y;
	};
	
	
	
	// Gestiona el scale
	if (zoom_mode=='x') scale= Math.abs(av_x) / 4; else scale= Math.abs(av_y) / 4;
	if (scale < 0.4) scale=0.4;
	if (scale > 1) scale=1;

	if (scale < 0.7) zindex=104; else zindex=106;

	// Gestiona el rotate
	rotation+=av_rotation;
	if (rotation > 25) av_rotation = -av_rotation;
	if (rotation < -25) av_rotation = -av_rotation;
	
	
	// Calcula la nueva posición
	x+=av_x;
	y+=av_y;
	
	
	// Controla los bordes
	if (x > border_right_actual) {
		x=border_right_actual;
		border_left_actual = border_left - (Math.floor(Math.random()*800) - 400);
		av_x = -av_x;
		// Recalcula el zoom mode
		if (Math.floor(Math.random()*2)==1) zoom_mode='x'; else zoom_mode='y';
	};
	if (x < border_left_actual) {
		x=border_left_actual;
		border_right_actual = border_right + (Math.floor(Math.random()*400) - 200);
		av_x = -av_x;
	};
	if (y < border_top_actual) {
		y=border_top_actual;
		border_bottom_actual = border_bottom - (Math.floor(Math.random()*50));
		av_y=-av_y;
	};
	if (y > border_bottom_actual) {
		y=border_bottom_actual;
		border_top_actual = border_top + (Math.floor(Math.random()*50));
		av_y=-av_y
	};
		
	
	// Vuelve a ponerlos en su sitio
	sprite[0][1] = x; sprite[0][2] = y; sprite[0][3] = av_x; sprite[0][4] = av_y;
	sprite[0][5] = av_av_x; sprite[0][6] = av_av_y;
	sprite[0][7] = scale;
	sprite[0][8] = rotation;
	sprite[0][9] = av_rotation;
	sprite[0][10] = av_x_max; sprite[0][11] = av_y_max;
	sprite[0][12] = zindex;
}



})(jQuery);


