if(!window.console)window.console={log:function(){}}; // console.log fallback

$(document).ready(function(){
	
	// === Slideshow ===============================================================================
	
	if(typeof(jsonImages) != 'undefined'){
		var images = $.parseJSON(jsonImages);
		$("div#slideshow").slider(images,8000);
	}
	
	
	// === Autoscrolling and navigation ============================================================
	
	var $subitems = $('#content > .wrapper > .left > ul:eq(1) a');
	var s = $('#section-wrapper');
	var w = $('#section-wrapper > .section').width();
	
	
	// Set initial height
	var minheight = $('#content > .wrapper > .left').height();
	var initheight = s.find('.section:eq(0)').height();

	
	// Bind click action to options
	var slideToSection = function(i,autoAdjustHeight,useMinHeight,autoScroll){
		autoAdjustHeight = (typeof(autoAdjustHeight)==='undefined')?true:autoAdjustHeight;
		useMinHeight = (typeof(useMinHeight)==='undefined')?true:useMinHeight;
		autoScroll = (typeof(autoScroll)==='undefined')?true:autoScroll;
		
		var m = i*w;
		var newheight = (autoAdjustHeight)?s.find('.section:eq('+i+')').height():s.height();
		s.animate({
			'margin-left':-m
		},{
			duration:600,
			queue:false
		});
		s.animate({
			'height':((newheight<minheight&&useMinHeight)?minheight:newheight)
		},{
			duration:600,
			queue:true
		});
		if(!autoScroll) return;
		$.scrollTo(s,{
			duration:600,
			//easing:'easeOutElastic',
			offset: {left:0, top:-70},
			queue:true
		});
	};
	if($('.#content.sec-6').length==0) slideToSection(0,true,true,false);
	
	// Bind click action to options
	$subitems.click(function(e){
		e.preventDefault();
		var i = $(this).parent().index();
		slideToSection(i);
	});
	
	
	
	
	
	
	// ===== Rollover boton portafolio ======================================================
	
	$("#content").not('.sec-6').find('.right .portafolio').hover(function(e){
		$(this).animate({'height':38},{duration:'fast', queue:false});
	},function(e){
		$(this).animate({'height':34},{duration:'fast', queue:false});
	});
	
	
	
	
	
	
	
	// ===== Rollover description thumbs ====================================================
	
	$('.thumbs-page .thumb').hover(function(){
		var title = $(this).find('span');
		title.animate({
			'bottom':0
		},{
			duration:400,
			queue:false
		});
	},function(e){
		var title = $(this).find('span');
		title.animate({
			'bottom':-title.height()-10
		},{
			duration:400,
			queue:false
		});
	});
	
	
	
	
	
	
	// === Movimiento de proyectos =============================================
	
	var current = 0;
	
	$("#section-wrapper .section:eq("+current+")").css("left",0);
				
	var slideLeft = function(n){
		if(current==n) return;
		
		$("#section-wrapper .section:eq("+n+")").css("left",w);
		
		$("#section-wrapper .section:eq("+current+")").animate({
			"left":-w					
		},{
			duration:600,
			//easing:'easeOutElastic',
			queue:true
		});
		$("#section-wrapper .section:eq("+n+")").animate({
			"left":0					
		},{
			duration:600,
			//easing:'easeOutElastic',
			queue:true
		});
		current = n;
		return current;
	}		
						
	var slideRight = function(n){
		if(current==n) return;
		
		$("#section-wrapper .section:eq("+n+")").css("left",-w);
		
		$("#section-wrapper .section:eq("+current+")").animate({
			"left":w					
		},{
			duration:600,
			//easing:'easeOutElastic',
			queue:true
		});
		$("#section-wrapper .section:eq("+n+")").animate({
			"left":0					
		},{
			duration:600,
			//easing:'easeOutElastic',
			queue:true
		});
		current = n;
		return current;
	}		
	
	
	var portfolio = $("#content.sec-6 .right .portafolio");
	var nextSection = $("#content.sec-6 .controls-sections .next");
	var lastSection = $("#content.sec-6 .controls-sections .last");
	var nextPage = $("#content.sec-6 .controls-pages .next");
	var lastPage = $("#content.sec-6 .controls-pages .last");
	
	var $projectThumbs = $('.thumbs-page .thumb a');
	var $pages = $('#thumbs-index');
	var page = 0;
	
	var pageHeight = $('#section-wrapper').height($('.thumbs-page').height()+100).height();
	$('.thumbs-page').height(pageHeight-100);
	
	var thumbsPerPage = $('.thumbs-page:first .thumb').length;
	var numberOfPages = $('.thumbs-page').length;
	var numberOfSections = $('.section').length;
	
	var updateArrows = function(){
		if(current >= numberOfSections-1) nextSection.animate({'opacity':0.5});
		else nextSection.animate({'opacity':1});
		if(current < 1) lastSection.animate({'opacity':0.5});
		else lastSection.animate({'opacity':1});
		
		if(page >= numberOfPages-1) nextPage.animate({'opacity':0.5});
		else nextPage.animate({'opacity':1});
		if(page < 1) lastPage.animate({'opacity':0.5});
		else lastPage.animate({'opacity':1});
	}
		
	
	$projectThumbs.click(function(e){	
		e.preventDefault();
		var p = $(this).parent().parent().index();
		var i = $(this).parent().index();	
		var r = (thumbsPerPage*p+i+1);
		slideLeft(r);
		portfolio.delay(800).animate({'height':34});
		$("#content.sec-6 .controls-sections").css({
			//'right':60,
			'opacity':0
		}).show().delay(1000).animate({
			//'right':10,
			'opacity':1
		});
		$("#content.sec-6 .controls-pages").fadeOut('fast');
		$('#section-wrapper').animate({'height':850});
		updateArrows();
	});
	

	lastSection.click(function(e){
		e.preventDefault();
		var sig = current-1;
		if(sig < 0) return;
		if(sig == 0) portfolio.click();
		slideRight(sig);
		updateArrows();
	});
	
	nextSection.click(function(e){
		e.preventDefault();
		var sig = current+1;
		if(sig >= numberOfSections) return;
		slideLeft(sig);
		updateArrows();
	});

	portfolio.click(function(e){
		e.preventDefault();
		slideRight(0);
		portfolio.animate({'height':2});
		$("#content.sec-6 .controls-pages").css({
			//'right':80,
			'opacity':0
		}).show().delay(1000).animate({
			//'right':20,
			'opacity':1
		});
		$("#content.sec-6 .controls-sections").fadeOut('fast');
		$('#section-wrapper').animate({'height':pageHeight});
		updateArrows();
	});
		
	nextPage.click(function(e){
		e.preventDefault();
		var sig = page+1;
		if(sig >= numberOfPages) return;
		$pages.animate({ 'top':(-pageHeight*sig) });
		page = sig;
		updateArrows();
	});
	
	lastPage.click(function(e){
		e.preventDefault();
		var sig = page-1;
		if(sig < 0) return;
		$pages.animate({ 'top':-pageHeight*sig });
		page = sig;
		updateArrows();
	});
	
	updateArrows();
	
	
	
	
	
	// === Formulario de Contacto ==================================================================
	
	// Parte del nombre
	$(".contenedor #nom").focus(function () { 
     	$("#nom").attr("value","");       
    });
    
    $(".contenedor #nom").focusout(function() {
    	if($(this).attr("value").length==0){
  	 		$("#nom").attr("value","Nombre");     	 		
  	 	}	
    });


	// Parte de la empresa 
    $("#empresa").focus(function () { 
      $(this).attr("value",""); 
    });
    
     $("#empresa").focusout(function() {
    	if($(this).attr("value").length==0){
  	 		 $(this).attr("value","Empresa");
  	 	}	
    });
    
    
    // Parte del puesto
    $("#puesto").focus(function () { 
      $(this).attr("value",""); 
    });
    
    $("#puesto").focusout(function() {
    	if($(this).attr("value").length==0){
  	 		 $(this).attr("value","Puesto");
  	 	}	
    });
    
    
    // Parte del telefono
    $("#tel").focus(function () { 
      $(this).attr("value",""); 
    });
    
    $("#tel").focusout(function() {
    	if($(this).attr("value").length==0){
  	 		 $(this).attr("value","Teléfono");
  	 	}	
    });
    
    
    // Parte del e-mail
    $("#email").focus(function () { 
      $(this).attr("value",""); 
    });
    
    $("#email").focusout(function() {
    	if($(this).attr("value").length==0){
  	 		 $(this).attr("value","Correo Electrónico");
  	 	}	
    });
    
    
    // Parte del mensaje
    $("#mensaje").focus(function () { 
      $(this).attr("value",""); 
    });
    
    $("#mensaje").focusout(function() {
    	if($(this).attr("value").length==0){
  	 		 $(this).attr("value","Mensaje");
  	 	}	
    });
    
    
    
    
    
    
    
    
    // === Validar campos requeridos antes de enviar ======================================
    
    $("#contactForm").submit(function (e) { 
     	if($("#nom").attr("value")=="Nombre"){
  	 		$("#nom").css("border","2px solid red");
  	 		$(".contenedor:eq(0)").css("border","none");
  	 		e.preventDefault();
  	 	}
  	 	
  	 	if($("#email").attr("value")=="Correo Electrónico"){
  	 		$("#email").css("border","2px solid red");
  	 		$(".contenedor:eq(4)").css("border","none");
  	 		e.preventDefault();
  	 	}       
  	 	
  	 	if($("#mensaje").attr("value")=="Mensaje"){
  	 		$("#mensaje").css("border","2px solid red");
  	 		$(".comentario").css("border","none");
  	 		e.preventDefault();
  	 	}   	 	
    });


});	
