var imagesA;
var currentIndex = 0;
var showcaseanimating = false;
$(document).ready(function(){
	$('.hidden').hide();
	
	showcaseInit();
	 $("#mainMenu").superfish({speed:1,dropShadows:false});
	$('a[href=#top]').click(function(){
		$('html, body').animate({scrollTop:0}, 'slow');
        return false;
    });

 });
 
 $(document).resize(function(){
	showcasePositionning();
 });
 
 
function showcasePositionning(){
	var offset = $('#showcase').offset();
	$('#showcase_over').offset({ top: offset.top, left: (offset.left + 15) });
}

function showcaseInit(){

	imagesA = $('#showcase a');
	
	$('#showcase').after('<a style="text-decoration:none; z-index:100;"><div id="showcase_over" style="position:absolute; "><div id="showcase_overbar" style="position:absolute; line-height:18px; bottom:0px;"><span style="color:white; font-size:14px; position:absolute;  bottom:8px; left:15px; overflow:hidden; white-space:nowrap;"></span><button id="btn_right">&gt;</boutton><button id="btn_left">&lt;</boutton></div></div></a>'); 
	//$('#showcase_over').width($('#showcase img:first').outerWidth()).height($('#showcase img:first').outerHeight())
	
	var offset = $('#showcase').offset();
	var currentImage = $('#showcase img:not(.hidden)');
	$('#showcase_over').width('948px').height('280px')
		.css('background-color','transparent');
	showcasePositionning();
	imagesA.css('position','absolute').offset({left: (offset.left + 15) }).css('z-index','0');
		
	$('#showcase_overbar span').html(currentImage.attr('alt'));
	if(currentImage.parent().attr('href')  == undefined){
		$('#showcase_over').parent().removeAttr('href');
	}else{
		$('#showcase_over').parent().attr('href',currentImage.parent().attr('href'));
	}
	
	$('#showcase_overbar').width('948px').height('35px');
	
	$('#showcase_over button').css('float','right').css('margin','10px 5px 0 0');
	$('#btn_right').click(showcase_next);
	$('#btn_left').click(showcase_previous);
	
	
	if(imagesA.length > 1){
		window.setTimeout(showcase_automatique, 1000);
	}else{
		$('#btn_right,#btn_left').hide();
	}
 }
 
 function showcase_next(){
	var currentImageA = $('#showcase a:not(.hidden)');
	
	if(currentIndex == window.imagesA.size() -1){
		currentIndex = 0;
		var nextImage =  window.imagesA.eq(currentIndex);
	}else{
		currentIndex ++;
		var nextImage =  window.imagesA.eq(currentIndex);
	}
	showcase_swithto(currentImageA, nextImage);
	
	return false;
 }
 function showcase_previous(){
	var currentImageA = $('#showcase a:not(.hidden)');
	
	if(currentIndex == 0){
		currentIndex = window.imagesA.size() -1;
		var nextImage =  window.imagesA.eq(currentIndex);
	}else{
		currentIndex --;
		var nextImage =  window.imagesA.eq(currentIndex);
	}
	showcase_swithto(currentImageA, nextImage);
	
	return false;
 }
 
 function showcase_swithto(currentImageA, nextImage){
	showcaseTimer = 0;
	currentImageA.fadeOut(1000,function(){
		$(this).addClass('hidden');
	});
	nextImage.removeClass('hidden').fadeIn(1500);
	$('#showcase_overbar span').html(nextImage.find('img').attr('alt'));
	if(nextImage.attr('href')  == undefined){
		$('#showcase_over').parent().removeAttr('href');
	}else{
		$('#showcase_over').parent().attr('href',nextImage.attr('href'));
	}
	if(!showcaseanimating){
		showcaseanimating= true; 
		showcase_animate();
	}else{
	
	}
 }
 
 function showcase_animate(){
	//$('#showcase_overbar span').effect( 'slide', { direction: "right", distance:400 }, 700 ).hide("fade", {}, 1000);
	$('#showcase_overbar span').width('90%').css('left','15px');
	var spanWidth = $('#showcase_overbar span').width();
	$('#showcase_overbar span').animate({left: '+=50',opacity: 0,width: "5px"},000)
	.animate({opacity: 0.3, width: spanWidth, left: '-=50'}, 400)
	.animate({opacity: 1}, 200, 'linear', function() {
		showcaseanimating = false;
	});
 }
 
 var showcaseTimer = 0;
 function showcase_automatique(){
	showcaseTimer += 1;
	if(showcaseTimer == 4){
		showcase_next();
		showcaseTimer = 0;
	}
	window.setTimeout(showcase_automatique, 1000);
 }
