// JavaScript Document


var projetsResize = function() {
	var projectsWidth=0;
	$('#projets img').each(function(){ 
		projectsWidth += $(this).outerWidth(true); 
	});
	$('#projets').css({width: projectsWidth + 30});
}

var pageResize = function() {
	var menuWidth    = $('#nav').outerWidth(true);
	var contentWidth = $('#content').outerWidth(true);
  var content_size = menuWidth + contentWidth + 30;
	
	var presizeWidth;
	if($('.resize img').closest('.wrap').length > 0) {
		presizeWidth = $('.resize img').closest('.wrap').first().outerWidth(true) * Math.ceil($('.resize img').length / 2);
	} else {
		presizeWidth = $('.resize img').first().outerWidth(true);
	} 
	
	console.log(presizeWidth);

	var resize_size = $(".texts").outerWidth(true) + presizeWidth;
  
	$('.resize').css({width: resize_size});
}

var scrollTo = function(e) {
	e.preventDefault();
	var move = "+=0";
	if($(this).hasClass('next')){
		current = $('#projets').find('img.current');
		if($(current).next("img").length > 0){
			current.removeClass("current");
			move = "-=" + current.outerWidth(true);
			current.next("img").addClass("current");		
		}
	}
	if($(this).hasClass('previous')){
		current = $('#projets').find('img.current');
		if(current.prev("img").length > 0){
			current.removeClass("current");
			move = "+=" + current.prev("img").outerWidth(true);
			current.prev("img").addClass("current");		
		}
	}
	$('#projets').animate({left: move}), pageResize();
	if($(this).hasClass('start')){
		current = $('#projets').find('img.current');
		current.removeClass("current");
		$('#projets img').first().addClass("current");	
		$('#projets').animate({left: 0}), pageResize();
	}
}

$(document).ready(function() {
	$("#proj_nav").hide();
	$("#proj_nav").fadeIn(2000);
	
													 
	// init slideshow					   	
  $('.slideshow').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		speed : 1500,
		timeout : 500
	});
	
	$("#nav a.next, a.previous, a.start").bind("click", scrollTo);
	
	// resize page
	projetsResize();
	pageResize();
});

function checkKey(e){
     switch (e.keyCode) {
        case 40:
						e.preventDefault();
						current = $('#projets').find('img.current');
						current.removeClass("current");
						$('#projets img').first().addClass("current");	
						$('#projets').animate({left: 0}), pageResize();
            break;
        case 37:
						e.preventDefault();
						var move = "+=0";
							current = $('#projets').find('img.current');
							if(current.prev("img").length > 0){
								current.removeClass("current");
								move = "+=" + current.prev("img").outerWidth(true);
								current.prev("img").addClass("current");		
							}
						$('#projets').animate({left: move}), pageResize();
            break;
        case 39:
						e.preventDefault();
						var move = "+=0";
							current = $('#projets').find('img.current');
							if($(current).next("img").length > 0){
								current.removeClass("current");
								move = "-=" + current.outerWidth(true);
								current.next("img").addClass("current");		
							}
						$('#projets').animate({left: move}), pageResize();
            break;
            }      
}

if ($.browser.mozilla) {
    $(document).keypress (checkKey);
} else {
    $(document).keydown (checkKey);
}


