////////////////////////////////////
/// jQuery script by Andre Collin
////////////////////////////////////


////////////////////////////////////
// open / close navigation 
////////////////////////////////////

var menu_height = $('header').height();
var mailboxheight = $('.mailbox').height()+50;

function close_nav() {
	$('header .close').hide();
	//$('.thumbs').stop().animate({ top:"-99px"}, 200 );
	$('.previous').stop().animate({ left:"-100px"}, 1 );
	$('.next').stop().animate({ right:"-100px"}, 1 );
	$('header').stop().animate({ top:"-="+menu_height}, 200 , function(){
		$('.open').show();
		$('header h1').css('line-height','1em');
		$('nav').hide(1,function(){
			$('header').animate({ top:"-25px"}, 200);
			});
		});
	$('.mailbox').animate({bottom:"-"+mailboxheight},200);
	 $(document).unbind('mousemove');
	 $('.previous').unbind('click');
	 $('.next, .background2').unbind('click');
	 $('.background2').css('cursor','default');
	}

function open_nav() {
	var open_menu_height = $('header').height();
	$('.open').hide();
	$('header').stop().animate({ top:"-="+menu_height}, 200 , function(){
		$('header .close').show();
		$('header h1').css('line-height','1.7em');
		$('nav').show(1,function(){
			$('header').animate({ top:"0"}, 200);
			});
		});
	}
	

////////////////////////////////////
//  lanch_gallery() function
////////////////////////////////////

var photostream = '';
var currentdisplay = -1;
var currentgallery='';

function lanch_gallery(galleryID) {
	close_nav();
	// affiche preloader
	//$('.preloader').stop().fadeIn(200);
	// on va rechercher les photos de la gallerie
	$.ajax({
	   type: "POST",
	   url: "/inc/php/get_pix_list.php",
       dataType: "json",
	   data: "album="+galleryID,
	   success: function(msg){
	   		$('.background').css('background','url('+msg[0]+') center center no-repeat');
			$('.background2').css('cursor','pointer');
			// charge le compteur et les boutons
			//$('.thumbs span.x').html('1');
			//$('.thumbs span.y').html(msg.length);	
			//$('.previous').fadeTo(1, 0.2).css('cursor','default');
			$('.previous').fadeTo(1, 1.0);
			$('.next').fadeTo(1, 1.0);
			$('.previous').delay(500).animate({left:"-100px"},1);
			$('.next').delay(500).animate({right:"-100px"},1,function(){
			//$.loadImages(msg, function(){$('.preloader').stop().fadeOut(200);});});
			$.loadImages(msg)});//, function(){$('.preloader').stop().fadeOut(200);});});
			  $(document).mousemove(function(e){
				  var limitegauche = 150;
				  var limitedroite = Math.ceil($(document).width()-150);
				 if(e.pageX<limitegauche)
					$('.previous').stop().animate({left:"0"},1);
				else
					$('.previous').stop().animate({left:"-100px"},1);
				if (e.pageX>limitedroite)
					$('.next').stop().animate({right:"0"},1);
				else
					$('.next').stop().animate({right:"-100px"},1);
				
			   }); 
			photostream = msg;
			currentdisplay = 0;
			currentgallery = galleryID;
			
			
			// bind NEXT button event
	$('.next, .background2').bind('click', function(){
		$(this).css('cursor','pointer');
		if (currentdisplay<(photostream.length-1))
			{
			currentdisplay ++;
			}
		else {
			currentdisplay = 0
			}; 
		$('.background').stop().animate({left:"-1200", opacity:0},500, function(){
			$(this).animate({left:"0", opacity:1},1,function(){
				$(this).css('background','url('+photostream[currentdisplay]+') center center no-repeat');})
			;});
		$('.background2').stop().animate({left:"1200", opacity:0},1, function(){
			$(this).css('background','url('+photostream[currentdisplay]+') center center no-repeat').animate({left:"0", opacity:1},500, function(){
				$(this).animate({background:"none"},100);;
				});
			})
		});

	// bind PREVIOUS button event
	$('.previous').bind('click', function(){
		$(this).css('cursor','pointer');
		if (currentdisplay>0)
			{
			currentdisplay --;
			}
		else {
			currentdisplay=(photostream.length-1);
			} 
		$('.background').stop().animate({left:"1200", opacity:0},500, function(){
			$(this).animate({left:"0", opacity:1},1,function(){
				$(this).css('background','url('+photostream[currentdisplay]+') center center no-repeat');})
			;});
		$('.background2').stop().animate({left:"-1200", opacity:0},1, function(){
			$(this).css('background','url('+photostream[currentdisplay]+') center center no-repeat').animate({left:"0", opacity:1},500, function(){
				$(this).animate({background:"none"},100);;
				});
			})
		});
			
			
			
			
	   }
	});
	}

////////////////////////////////////
//  onload events
////////////////////////////////////
$(document).ready(function(){
	
	// menu close / open 
	$('header .close').bind('click', function(){close_nav();});
	$('.open').bind('click', function(){open_nav();});
	$('.previous').animate({left:"-99px"},1);
	$('.next').animate({right:"-99px"},1);
	});


//////////////////////////////////////////////
///  AJAX contact form 
/////////////////////////////////////////////

$(document).ready(function(){
$('.mailbox').animate({bottom:"-"+mailboxheight},1,function(){$(this).show()});
$('.mailbox .close').css('cursor','pointer').bind('click', function(){
	$('.mailbox').animate({bottom:"-"+mailboxheight},200, function(){ open_nav();});
	

	});
// apparition du formulaire
$('.mailme').css('cursor','pointer').bind('click',function(){
	 close_nav();
	 $('.mailbox').delay(300).animate({bottom:"0"},200);
	});

// soumission du formulaire
$("#contactform").submit(function(){
    $('input[type=submit]', this).attr('disabled', 'disabled');
	$(".loading").fadeIn(200);
	$(".notification_error").fadeOut(200);
	$("#contactform").addClass("wait");
	var str = $(this).serialize();
	$.ajax({
	   type: "POST",
	   url: "/inc/php/contact.php",
	   data: str,
	   success: function(msg){
	   	$(".loading").fadeOut(200,function(){
		$(this).hide(1)});
		$("#note").ajaxComplete(function(event, request, settings){
			if(msg == 'OK') 
				{
				$("#contactform").hide();
				$(".mailbox h2").hide();
				result = '<div class="notification_ok"><h3>Your message has been sent. </h3><h2>Thank you!</h2></div>';
				$('.mailbox').delay(2000).animate({bottom:"-"+mailboxheight},200);
				}
			else
				{
   				$('input[type=submit]').removeAttr('disabled');
				result = msg;
				$(".notification_error").show(200);
				$("#contactform").removeClass('wait');
				}	
			$(this).html(result);
			});
		}
 });
return false;
});
});
