$(document).ready(function(){
	var w = 0;
	var h = 0;
	var objw = 0;
	var objh = 0;
	var posx = 0;
	var posy = 0;
	var hoy;
	var hora, minutos, segundos;
	crearAscii();
	resizeWindow();
	
	var resizeTimer = null;
	$(window).bind('resize', function(){
		if (resizeTimer){
			clearTimeout(resizeTimer);
		}
		resizeTimer = setTimeout(resizeWindow, 100);
	});
	var clockTimer = setInterval(setClock, 1000);
	
});
function resizeWindow(){
	w = $(document).width();
	h = $(document).height();
	objw = $('.app').innerWidth();
	objh = $('.app').height();
	posx = (w / 2) - (objw / 2);
	posy = (h / 2) - (objh / 2);
	$('.app').css({'position':'absolute', 'top':posy + 'px', 'left':posx + 'px'});
}

function setClock(){
	hoy = new Date();
	hora = hoy.getHours();
	minutos = hoy.getMinutes();
	segundos = hoy.getSeconds();
	if(segundos < 10){
		segundos = '0' + segundos;	
	}
	if(minutos < 10){
		minutos = '0' + minutos;	
	}
	$('.actual span').empty().append(hora + ':' + minutos + ':' + segundos);
}
var pos = new Array("mbt.jpg", "mbr.jpg", "mbb.jpg", "mbl.jpg");
function crearAscii(){
	var cols = 20;
	var rows = 20;
	for(i = 0; i < cols; i++){
		for(j = 0; j < rows; j++){
			var id = i + j * cols;
			$('#ascii').append('<input class="btn" type="image" src="images/' + pos[0] + '" value=""/>');
		}	
	}
	$('.btn').click(function(e){
		e.preventDefault();	
	});
	$('.btn').hover(
		function(){
			$(this).attr("src", "images/" + pos[getCurrentMB(this)]);	
		},
		function(){
			
		}
	);
}
function getCurrentMB(obj){
	var src = $(obj).attr("src").split("/");
	var id = pos.indexOf(src[1]);
	id++;
	if(id >= pos.length){
		return 0;
	}else{
		return id++;	
	}
}