

var i = 0;
var intervalo;

function rompe(id){
	var rnd = Math.round(Math.random()*10000);
	$('#glitch_'+id).glitch(rnd);
	if(i>15){
		$('#glitch_barras').show();
		clearInterval(intervalo);
		abreContenido(id);
	}
	i ++;
}

function abreContenido(id){
	$('#contenido').hide().load('ajax.php?id='+id, function(){
															
			$('#contenido').show();
			$('body').removeClass('pensando');
	});
	$('body').removeClass('body_b1 body_b2 body_b3 body_b4').addClass('body_'+id);
}

$(document).ready(function(){
						   
	$('#mas').click(function(){
		$('#cuad_contacto').animate({ width: 300, height: 300 }, 500, function(){
				$('#cuad_contacto').css('overflow', 'visible');
			});
		$('#todo').animate({ top: -200, left: -200 }, 500);
	});
	
	$('#menos').click(function(){
		$('#cuad_contacto').css('overflow', 'hidden');
		$('#cuad_contacto').animate({ width: 0, height: 0 }, 500);
		$('#todo').animate({ top: 0, left: 0 }, 500);
	});
	
	
	$('#menu a').click(function(){ //event
		//event.preventDefault();
		$('#menu a').removeClass('active inicial');
		$(this).addClass('active');
		var id = $(this).attr('id');
		i = 0;
		$('.glitch').hide();
		$('#contenido').hide();
		$('body').addClass('oscuro');
		$('#glitch_'+id).show().attr('src', 'imgs/glitch_'+id+'.png');
		intervalo = setInterval('rompe(\''+id+'\')', 50);
		$('body').addClass('pensando');
	});
	
	
	$('form input, form textarea').focus(function(){
		var nombre = $(this).attr('title');
		var valor = $(this).val();
		if(valor == nombre){
			$(this).val('');
		}
	});
	$('form input, form textarea').focusout(function(){
		var nombre = $(this).attr('title');
		var valor = $(this).val();
		if(valor == ''){
			$(this).val(nombre);
		}
	});
	
	$('#boton_enviar').click(function(){
								   
		var camp_eml = $('#form_eml').val();
		if(camp_eml.length < 3 || camp_eml.indexOf('@') < 1 || camp_eml.indexOf('.') < 1){
			$('#divres').html('El e-mail ingresado es incorrecto.');
			$('#form_eml').focus();
		}else{
			$('#divres').html('Enviando mensaje...');
			$('body').addClass('pensando');
			var datastring = $('#form_form').serialize();
			$.ajax({
				type: "POST",
				url: "exemail.php",
				data: datastring,
				dataType: "json",
				success: function(data){
					$('#divres').html(data.msg);
					$('body').removeClass('pensando');
					if(data.res == 'ok'){
						$('#form_form input, #form_form textarea').each(function(){
							$(this).val('').focusout();
						});
					}
				}
			});
		}
	});
	
	
	
	var jash = window.location.hash;
	var jash = jash.substring(1);
	if(jash == 'contacto'){
		$('#mas').click();
	}else if(jash == 'web' || jash == 'ilustracion' || jash == 'animacion' || jash == 'identidad' ){
		$('#menu a.hash_'+jash).click();
	}
	
	
	
  	Shadowbox.init({
        animate: false,
		displayCounter: false,
		overlayOpacity: 0.7,
		overlayColor: '#423B2B',
		initialHeight: 22,
		viewportPadding: 40
	});
	
	/*
		Shadowbox.init({
			language: 'en',
			players:  ['img',  'swf']
		});
	*/
	
});
	
$("#sb-nav-previous").live('click', function(){
	$(".trabajos").data("scrollable").prev();
});

$("#sb-nav-next").live('click', function(){
	$(".trabajos").data("scrollable").next();
});

