function getUrlVars() {
	var vars = [], hash;
	var hashes = window.location.href.slice(
			window.location.href.indexOf('?') + 1).split('&');
	for ( var i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}
function bienvenida() {
	if (getUrlVars()['intro'] != 'no') {
		$("#app").hide();
		var duracionAnimacion = 6000;
		var o_retraso = 400;
		time = new Date().getTime();

		$("#ramoglobos").animate({
			top : '-40%'
		}, duracionAnimacion);

		$("#img_cargando").animate({
			opacity : 1
		}, 500, function() {
			$("#oi_1").animate({
				opacity : 1
			}, o_retraso, function() {
				$("#oi_2").animate({
					opacity : 1
				}, o_retraso, function() {
					$("#oi_3").animate({
						opacity : 1
					}, o_retraso, function() {
						$("#oi_4").animate({
							opacity : 1
						}, o_retraso, function() {
							$("#oi_5").animate({
								opacity : 1
							}, o_retraso, function() {
								$("#oi_6").animate({
									opacity : 1
								}, o_retraso, function() {
									$("#oi_7").animate({
										opacity : 1
									}, o_retraso, function() {
										$("#oi_8").animate({
											opacity : 1
										}, o_retraso);
									});
								});
							});
						});
					});
				});
			});
			$("#od_1").animate({
				opacity : 1
			}, o_retraso, function() {
				$("#od_2").animate({
					opacity : 1
				}, o_retraso, function() {
					$("#od_3").animate({
						opacity : 1
					}, o_retraso, function() {
						$("#od_4").animate({
							opacity : 1
						}, o_retraso, function() {
							$("#od_5").animate({
								opacity : 1
							}, o_retraso, function() {
								$("#od_6").animate({
									opacity : 1
								}, o_retraso, function() {
									$("#od_7").animate({
										opacity : 1
									}, o_retraso, function() {
										$("#od_8").animate({
											opacity : 1
										}, o_retraso);
									});
								});
							});
						});
					});
				});
			});
		});
	}
}
function colisionEntreGlobos(globo1, globo2) {
	if ($(globo1).parent().attr('id') == $(globo2).parent().attr('id')) {

		p1 = globo1.offset();
		p2 = globo2.offset();
		var x1 = p1.left + globo1.width() / 2;
		var y1 = p1.top + globo1.height() / 2;
		var x2 = p2.left + globo2.width() / 2;
		var y2 = p2.top + globo2.height() / 2;
		var dist = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
		var orientacionX = x1 < x2;
		var orientacionY = y1 < y2;
		if (dist < globo1.radio + globo2.radio) {
			// alert('chocan: ' + $(globo1).id + " con " + $(globo2).id);
			if (orientacionX) {
				globo1.velocidadX = -Math.abs(globo2.velocidadX);
				globo2.velocidadX = Math.abs(globo1.velocidadX);
			} else {
				globo1.velocidadX = Math.abs(globo2.velocidadX);
				globo2.velocidadX = -Math.abs(globo1.velocidadX);
			}
			if (orientacionY) {
				globo1.velocidadY = -Math.abs(globo2.velocidadY)-1;
				globo2.velocidadY = Math.abs(globo1.velocidadY);
			} else {
				globo1.velocidadY = Math.abs(globo2.velocidadY);
				globo2.velocidadY = -Math.abs(globo1.velocidadY)-1;
			}
		}
	}

}
function helio(globo) {
	if (globo.position().top > 10) {
		globo.velocidadY = globo.velocidadY - 2;
	}
}
function colisionConBorde(globo, contenedor) {
	p = globo.position();
	if (p.left <= 0) {
		globo.velocidadX = Math.abs(globo.velocidadX);
		return 2;
	}
	if (p.left + globo.width() > contenedor.width()) {
		globo.velocidadX = -Math.abs(globo.velocidadX);
		return -2;
	}
	if (p.top < 10) {
		globo.velocidadY = Math.abs(globo.velocidadY) + 3;
		return 1;
	}
	if (p.top + globo.height() > contenedor.height()) {
		globo.velocidadY = -Math.abs(globo.velocidadY);
		return -1;
	}
	if (p.top > contenedor.height() - 20) {
		globo.velocidadY = 0;
		return 1;
	}
	return 0;
}
function disiparMovimiento(globo) {
	if (Math.abs(globo.velocidadY) > 0) {
		globo.velocidadY = Math.floor(0.5 * globo.velocidadY);
	} else {
		globo.velocidadY = Math.ceil(0.5 * globo.velocidadY);
	}

}
function aleatorio(globo) {
	if (Math.random() > 0.95) {
		if (Math.random() > 0.5) {
			globo.velocidadY = globo.velocidadY + 1;
		} else {
			globo.velocidadY = globo.velocidadY - 1;
		}
	}
}
function mover(globo) {
	$(globo).stop(true, true);
	p = globo.position();
	var x = parseInt(p.left) + globo.velocidadX;
	var y = parseInt(p.top) + globo.velocidadY;
	if (x < 0) {
		x = 0;
	}
	if (y < 0) {
		y = 0;
	}
	var newPos = {
		'left' : x,
		'top' : y
	};
	globo.animate(newPos, 120, 'linear');
	colisionConBorde(globo, $(globo).parent());
	helio(globo);
	aleatorio(globo);
}
function mueveGlobos() {
	if ($('#app').is(":visible")) {
		mover(globoCelebraciones);
		mover(globoRegalos);
		mover(globoEventos);
		colisionEntreGlobos(globoCelebraciones, globoRegalos);
		colisionEntreGlobos(globoEventos, globoRegalos);
		colisionEntreGlobos(globoCelebraciones, globoEventos);
		contadorDisipacion++;
		if (contadorDisipacion > 5) {
			disiparMovimiento(globoCelebraciones);
			disiparMovimiento(globoEventos);
			disiparMovimiento(globoRegalos);
			contadorDisipacion = 0;
		}
	}
}
function volumenGlobos() {
	globoCelebraciones.radio = ($('#celebraciones img').width() + $(
			'#celebraciones img').height()) / 4;
	globoRegalos.radio = ($('#regalos img').width() + $('#regalos img')
			.height()) / 4;
	globoEventos.radio = ($('#eventos img').width() + $('#eventos img')
			.height()) / 4;
}
function iniciarMovimientoGlobos() {
	clearInterval(interval1);
	clearInterval(interval2);
	contadorDisipacion = 0;
	globoCelebraciones = $('#celebraciones');
	globoRegalos = $('#regalos');
	globoEventos = $('#eventos');
	globoCelebraciones.velocidadX = 2;
	globoCelebraciones.velocidadY = -3;
	globoCelebraciones.radio = (globoCelebraciones.width() + globoCelebraciones
			.height()) / 3;
	globoRegalos.velocidadX = 2;
	globoRegalos.velocidadY = -1;
	globoRegalos.radio = (globoRegalos.width() + globoRegalos.height()) / 3;
	globoEventos.velocidadX = -2;
	globoEventos.velocidadY = -3;
	globoEventos.radio = (globoEventos.width() + globoEventos.height()) / 3;
	volumenGlobos();
	interval1 = setInterval("volumenGlobos()", 100);
	interval2 = setInterval("mueveGlobos()", 150);
}

function recalculaVolumen(globo) {
	var parent = $(globo).parent();
	if (parent.id == "globitos") {
		alert('globitos ' + parent.id);
	}
}
function getAltura(spanText) {
	 spanText = $(spanText);

//	 var h = 0;
//	 spanText.each(function() {
//	 h = h + spanText.height();
//	 });
//	 return h;

	if (spanText.length == 0){
		return 0;
	}
	var yMin = Number.MAX_VALUE;
	var yMax = Number.MIN_VALUE;
	spanText.each(function() {
		var top = $(this).offset().top;
		if (top < yMin) {
			yMin = top;
		}
		if (top + $(this).height() > yMax) {
			yMax = top + $(this).height();
		}
	});
	return yMax - yMin;

}
function pintaTextoHTML(divContenedor, spanText) {

	var temp = new Date().getTime();
	var h = getAltura(spanText);
	if (h==0){
		return;
	}

	while (h < divContenedor.height()) {
		spanText.each(function() {
			var fontSize = $(this).css('fontSize');
			$(this).css('fontSize', Math.ceil(parseFloat(fontSize, 10) + 1));
		});

		if (new Date().getTime() - temp > 10000) {
			$(spanText).css('fontSize', '0.95em');
			break;
		}
		h = getAltura(spanText);
	}
	while (h > divContenedor.height()) {
		$(spanText).each(function() {
			var fontSize = $(this).css('fontSize');
			$(this).css('fontSize', Math.floor(parseFloat(fontSize, 10) - 1));
		});

		if (new Date().getTime() - temp > 10000) {
			$(spanText).css('fontSize', '0.95em');
			break;
		}
		h = getAltura(spanText);
	}
	divContenedor.animate({
		opacity : 1,
		filter : 'alpha(opacity = 100)'
	}, 500);
}
function pintaTexto(globo) {
	var divContenedor = $('#descr' + $(globo).attr('id'));
	var spanText = $('#descr' + $(globo).attr('id') + ' span');
	pintaTextoHTML(divContenedor, spanText);
}
function iniciaApp() {
	$('.globito').css({
		top : '70%'
	});
	var retraso = esperaCargando - time + new Date().getTime();
	$("#app").delay(retraso).fadeIn(800);
	$("#cargando").delay(retraso).fadeOut(100);
	iniciarMovimientoGlobos();
	intervalReinicio = setInterval('iniciarMovimientoGlobos()', 60000);

}
var interval1, interval2, intervalReinicio;
var esperaCargando = 3000;
var globoCelebraciones;
var globoRegalos;
var globoEventos;
var contadorDisipacion = 0;
var time;

