 $(document).ready(function(){

	change_menu_and_footer_position();
	
	$(window).bind('resize', function() {
		change_menu_and_footer_position();
	});
	
	id = $(".project").length;
	$("div.project").eq(id-1).addClass("last_item");
	
	Cufon.replace('#identity .tag', { fontFamily: 'Deck Black' });
	Cufon.replace('#identity #company', { fontFamily: 'Deck Black' });
	Cufon.replace('#identity #engineer', { fontFamily: 'Deck Regular' });
	Cufon.replace('#identity #adress p', { fontFamily: 'Deck Regular' });
	Cufon.now();
	
	var url = window.location;
	if(url == 'http://www.ergopix.com/#usermessagea'){
		$('#identity').css('display','none');
		$('#contact_form').css('display','block');
	}
	
	nb_block_line =  parseInt(width/310)-1;

	nb_total = (nb_block_line*3);
	$('.project').css("display","none");
	if(nb_total == 0){
		length_project = $('.project').length;
		for (i=0; i<=length_project; i++) {
 			$('.project').eq(i).css("display","block");
 			$('.project').eq(i).addClass("displayed");
 			$('#footer').css("display","none")
		}
	}else{
		for (i=0; i<nb_total; i++) {
 			$('.project').eq(i).css("display","block");
 			$('.project').eq(i).addClass("displayed");
		}
	}
	
	$('.project').each(function(){
   		url = $(this).children('p').eq(0).children('a').attr('href');
		if(url == null){
   			$(this).children('p').children('a').remove();
		}else{
			$(this).children('.url_project').html(url);
   			$(this).children('.url_project').attr('href',url);
   			html = $(this).children('p').children('a').html();
   			$(this).children('p').children('a').remove();
   			$(this).children('p').html(html);
		}
 	});
 	
 	$('.list_page').each(function(){
   		url = $(this).children('p').children('a').attr('href');
   		$(this).children('.url_project').html(url);
   		$(this).children('.url_project').attr('href',url);
   		html = $(this).children('p').children('a').html();
   		$(this).children('p').children('a').remove();
   		$(this).children('p').html(html);
 	});
 	
 	 $('.list_page_team').each(function(){
   		url = $(this).children('p').children('a').attr('href');
   		if(url != null){
   			$(this).children('.url_project').html("Linked in");
   		}
   		$(this).children('.url_project').attr('href',url);
   		/*html = $(this).children('p').children('a').html();
   		$(this).children('p').children('a').remove();
   		$(this).children('p').html(html);*/
 	});
		
	style = 'easeOutQuart';

	// if the mouse hover the image
	$('.project').hover(
		function() {
			element = $(this);
			element.children('div:first').stop();
			//display heading and caption
			//setTimeout(function(){
				element.children('div:first').stop(false,true).animate({top:0},{duration:1000, easing: style});
			//},500);
		},
		function() {
			//hide heading and caption
			$(this).children('div:first').stop();
			$(this).children('div:first').stop(false,true).animate({top:-127},{duration:1000, easing: style});
		}
	);
	
	$('.list_page_team').hover(
		function() {
			//display heading and caption
			$(this).children('div:first').stop(false,true).animate({top:0},{duration:1000, easing: style});
		},
		function() {
			//hide heading and caption
			$(this).children('div:first').stop(false,true).animate({top:-127},{duration:1000, easing: style});
		}
	);
	
		// if the mouse hover the image
	$('.list_page').hover(
		function() {
			//display heading and caption
			$(this).children('div:first').stop(false,true).animate({top:0},{duration:1000, easing: style});
		},
		function() {
			//hide heading and caption
			$(this).children('div:first').stop(false,true).animate({top:-127},{duration:1000, easing: style});
		}
	);
	
	$('#close_form').click(function() {
  		$('#contact_form').fadeOut(1000);
  		$('#identity').fadeIn(1000);
	});
	
		$("#footer").click(function(){
	
		more_project(nb_block_line, $(".project").length);
	
	});
	
	$('.project').each(function(){
   		$(this).children("p").children("img").eq(1).css("display","none");
 	});
	
});

function change_menu_and_footer_position(){
	width = $(window).width();
	if((width >= 0 && width < 620)){
		$('#menu').css("margin-left","0px")
	}
	if((width >= 621 && width < 930)){
		$('#menu').css("margin-left","310px")
	}
	if((width >= 931 && width < 1240)){
		$('#menu').css("margin-left","620px")
	}
	if((width >= 1241 && width < 1550)){
		$('#menu').css("margin-left","930px")
	}
	if((width >= 1551 && width < 1860)){
		$('#menu').css("margin-left","1240px")
	}
	if((width >= 1861 && width < 2170)){
		$('#menu').css("margin-left","1550px")
	}
	if(width >= 2171){
		$('#footer').css("margin-left","0px")
	}
	if((width >= 0 && width < 620)){
		$('#footer').css("margin-left","10px")
	}
	if((width >= 621 && width < 930)){
		$('#footer').css("margin-left","320px")
	}
	if((width >= 931 && width < 1240)){
		$('#footer').css("margin-left","630px")
	}
	if((width >= 1241 && width < 1550)){
		$('#footer').css("margin-left","940px")
	}
	if((width >= 1551 && width < 1860)){
		$('#footer').css("margin-left","1250px")
	}
	if((width >= 1861 && width < 2170)){
		$('#footer').css("margin-left","1560px")
	}
	if(width >= 2171){
		$('#footer').css("margin-left","10px")
	}
}

function more_project(nb_block_line, length_project){
	nb_total = ((nb_block_line + 1)*3);
	length_displayed = $('.displayed').length;
	limit = length_displayed + nb_total;
	for (i=length_displayed; i<limit; i++) {
 		$('.project').eq(i).fadeIn(1000);
 		$('.project').eq(i).addClass("displayed");
	}
	$('#contact_form').fadeOut(1000);
	$('#identity').fadeIn(1000);
	if(limit >= length_project){
		$('#footer').css("display","none")
	}
}

function get_form_contact(){
	$('#contact_form').fadeIn(1000);
}