//Cufon.set('fontFamily', 'euros').replace('#header ul li a', {"hover": true}).replace("h1, h2, h3, h4, h5").replace(".annonce", {"hover": true});

Cufon.replace('#nav li.selected a', {'hover': true, fontFamily: 'hThinCondensed'} )
.replace('#nav li:not(.selected) a', {'hover': true, fontFamily: 'hCondensed'} )
.replace('#subnav li a', {'hover': true, fontFamily: 'hCondensed'} )
.replace('#navbottom li a', {'hover': true, fontFamily: 'hCondensed'} )
.replace('.sidenav h3', {fontFamily: 'hCondensed'} );



//document.body.oncontextmenu = function(){return false;}



function getDetailsSrc(psrc) {
	url = psrc.substr(0, psrc.lastIndexOf('/'));
	
	//format imagename
	psrc = psrc.substr(psrc.lastIndexOf('/')+1);
	psrc = psrc.substr(0, psrc.lastIndexOf('.'));
	psrc = psrc + '_details.png';
	
	return url+'/'+psrc;
}

// Slideshow =================================================
function setCurrent(objNext, objCurrent) {
	if ($(objNext).length > 0) {
		// ajouter class current sur current 
		$(objNext).addClass('current');
		// enlever class current sur previous
		$(objCurrent).removeClass('current');
		
		//if ($("img", objNext).hasClass('hasdetails')) {
			$('#details').fadeOut(200);
		//}
			
		// fade current to 70%
		$(objCurrent).animate({
			opacity: 0.7
		}, 600);
		
		// fade next to 100%
		$(objNext).animate({
			opacity: 1
		}, 600);
		
		// translate
		var gauche = $(objNext).position().left;
		

		
		gauche = Math.round(gauche/486)*(486+3);
		///alert(gauche);
		$('#slideshow').stop().animate({
			left: -gauche
		}, 500, function(){
			if ($("img", objNext).hasClass('hasdetails')) {
				$('#details').fadeIn(200);
			}
		});
	}
}
	
	
$(function(){
	
	$("a[rel=external]").click(function(){ $(this).attr('target', '_blank'); });
	
	
	
	// Slideshow =================================================
	if ($('#slideshow').length > 0) {
		$('#slideshow div:last').addClass('last');
		if ($('#overlay').length == 0) {
			$('.content.project').append('<div id="overlay"><img src="images/plus.png" alt=""></img></div>');
		}
		$(".content.project").append('<a id="prev" href="#"></a><a id="next" href="#"></a><a id="details" href="#">View details</a>');
		
		
		if ($('#slideshow div.current').length == 0) {
			$('#slideshow div:first').addClass('current');
			$('#slideshow div:first').animate({opacity: 1}, 250);
		}
		
		// btn view details
		if ($('#slideshow div.current img').hasClass('hasdetails')) {
			url = getDetailsSrc($('#slideshow div.current img').attr('src'));			
			$("#overlay img").attr('src', url);
		}
		
		
		if (!$('#slideshow div.current img').hasClass('hasdetails')) {
			$('#details').fadeOut(1);
			
		}
				
		$('#details').click(function(e){
			e.preventDefault();
			if (!$(this).hasClass('shown')) {
				$("#overlay").stop().animate({opacity: 1}, 500, 'easeInQuad'); 
				$(this).addClass('shown'); 
			} else { 
				$("#overlay").stop().animate({opacity: 0}, 300, 'easeOutQuad'); 
				$(this).removeClass('shown'); 
			}
		});
		
		// Init navigation 
		$("#next").click(function(e){
			e.preventDefault();
			$('#details').removeClass('shown');
			setCurrent($('#slideshow div.current').next(), $('#slideshow div.current'));
			
			$("#overlay").animate({opacity: 0}, 1, function(){
				// btn view details
				if ($('#slideshow div.current img').hasClass('hasdetails')) {
					url = getDetailsSrc($('#slideshow div.current img').attr('src'));			
					$("#overlay img").attr('src', url);
				}
				if ($('#slideshow div.current').next().length == 0) { $('#next').fadeOut(300); }
				if ($('#slideshow div.current').prev().length >= 0) { $('#prev').fadeIn(300); }
			});
		});
		$("#prev").click(function(e){
			e.preventDefault();
			$('#details').removeClass('shown');
			
			setCurrent($('#slideshow div.current').prev(), $('#slideshow div.current'))
			
			$("#overlay").fadeTo(1, 0, function(){	
				// btn view details
				if ($('#slideshow div.current img').hasClass('hasdetails')) {
					url = getDetailsSrc($('#slideshow div.current img').attr('src'));			
					$("#overlay img").attr('src', url);
				}		
				if ($('#slideshow div.current').prev().length == 0) { $('#prev').fadeOut(300); }
				if ($('#slideshow div.current').next().length >= 0) { $('#next').fadeIn(300); }
			});
		});

		if ($('#slideshow div.current').prev().length == 0) { $('#prev').hide(); }
		if ($('#slideshow div.current').next().length == 0) { $('#next').hide(); }
	}
	// Slideshow =================================================



	// Form inscription ==========================================
	$("form#inscription").submit(function(){
		if ($('#response').length == 0) {
			$("form#inscription").after('<div id="response"></div>');
		}
		
		// disparait le form
		$("form#inscription").fadeTo(200, 0);		
		// apparait le wait/reponse
		$("#response").fadeTo(1, 0)
				.css('width', $('.box').width() )
				.css('height', $('.box').height() )
				.html("<table><tr><td><img src='images/loader.gif' alt=''/></td></tr></table>")
				.animate({opacity: 1}, 200);
		
		$.ajax({
			url: 'addnewsletter.php',
			data: {name: $('form#inscription #name').val(), email: $('form#inscription #email').val()},
			success: function(data){
				if (data == 'true') {
					msg = "<table><tr><td>Your name and email have been added to our newsletter. Thank you.</td></tr></table>";
				} else {
					msg = "<table><tr><td>There has been an error while adding your name and email to our newsletter, please check your information and <a id='retry' href='#'>try again.</a></td></tr></table>";
				}
				
				$('#response img').fadeTo(200, 0, function(){
					$('#response').fadeTo(1, 0, function(){
						$('#response').html(msg).fadeTo(1000, 1);
						$('#retry').click(function(e){
							e.preventDefault();
							$('#response').fadeTo(200, 0, function(){ 
								$('#response').remove();
								$('form#inscription').fadeTo(200, 1);
							});
						});
					});
				});
				
			},
			dataType: 'text'
		});
		return false;
	});
	$("form#inscription #submit").click(function(e){ 
		e.preventDefault(); 
		$("form#inscription").submit(); 
	})
	
	$("form#inscription :input").bind('focus', function(){
		if (this.value == this.title) this.value = '';
	});
	$("form#inscription :input").bind('blur', function(){
		if (this.value == '') this.value = this.title;
	});
	
		
	// Form inscription ==========================================


	/* Anim sous-menu */
	$("#subnav.fade").hide();
	$("#subnav.fade").fadeIn(800);

});
