
$(function(){
    var portfolioCourant = null;
    $('.detail').hide();
    $(".work").click(function(){ 
        portfolio.show($(this));
    });
    $(".filter a").click(function(){
        mClass = $(this).attr("class");
        if(mClass=="dev"){$(".work.dev").fadeTo(500,1); $(".work.graph").fadeTo(500,0.2);}
        else if(mClass=="graph"){ $(".work.dev").fadeTo(500,0.2);$(".work.graph").fadeTo(500,1);}
        else{$(".work").fadeTo(500,1);}
	$(".filter a").removeClass('active');
        $(this).addClass('active');
        return false;
     });
});


var portfolio = {
    courant : null,
    clicked : null,

    show : function(clicked){
            portfolio.clicked = clicked;
            if(this.courant!=null && this.courant.attr('id') == ('w' + clicked.next(".detail").attr('id'))){
                this.courant.slideUp(1000,function(){
                    portfolio.courant.remove();
                    portfolio.courant=null;
                });
                return false;
            }
            if(this.courant!=null){
                this.courant.slideUp(1000,function(){
                    portfolio.courant.remove();
                    portfolio.courant=null;
                    portfolio.show(portfolio.clicked);
                    return false;
                });
                return false;
            }
            // On remplace les span par des images
            clicked.next(".detail").find(".visus span").each(function(){
                $(this).replaceWith('<img src="'+$(this).text()+'"/>');
            });
            clicked.nextAll(".workSpace:first").append('<div class="detail">'+clicked.next(".detail").html()+'</div>');
            var clone = clicked.nextAll(".workSpace:first").find(".detail");
	    clone.attr('id','w' + clicked.next('.detail').attr('id'));
            this.courant=clone;
            clone.css("display","block").hide().slideDown(1000,function(){
                   scrollTo(clone);
            });
            clone.find('.visus').wrapInner('<div id="slider">').find('#slider').nivoSlider({
		effect : 'fold',
		directionNav : false
	});
    },

    hide : function(){
        portfolio.courant.slideUp(1000,function(){
            portfolio.courant.remove();
            portfolio.courant=null;
            scrollTo($(".work:first"));
        });
    }

};
