/*
    Portfolio Scripts
            Grafikart, www.grafikart.fr
*/

$(function(){
    var portfolioCourant = null;
    $('.detail').hide();
    if ($.browser.msie){
            $(".work .cache").remove();
    }
    else{
            $(".work .cache").css("opacity",0);
            $(".work").mouseover(function(){
                     $(this).find(".cache").stop().fadeTo(500,1);
            });
            $(".work").mouseout(function(){
                     $(this).find(".cache").stop().fadeTo(500,0);
            });
    }
    $(".work").click(function(){ 
        portfolio.show($(this));
    });
    $(".pagination span 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);}
        $(".pagination span").removeClass('current'); 
        $(this).parent().addClass('current');
        return false;
     });
});


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

    show : function(clicked){
            portfolio.clicked = clicked;
            if(this.courant!=null && this.courant.text() == clicked.next(".detail").text()){
            	this.courant.slideUp(1000,function(){
                    portfolio.courant.remove();
                    clicked.parent().removeClass("gris_fonce");
                    clicked.parent().addClass("gris");
                    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
           // alert(clicked.parent().nextAll(".workSpace:first").attr("class") );
            clicked.parent().next(".detail").find(".visus span").each(function(){
                $(this).replaceWith('<img src="'+$(this).text()+'"/>');
            });
            clicked.parent().nextAll(".workSpace:first").append('<div class="detail">'+clicked.next(".detail").html()+'</div>');
            var clone = clicked.parent().nextAll(".workSpace:first").find(".detail");
            //passage au gris + sombre
            clicked.parent().removeClass("gris");
            clicked.parent().addClass("gris_fonce");
            this.courant=clone;
            clone.css("display","block").hide().slideDown(1000,function(){
            });
            carrousel.init(clone);
            clone.find(".mosaic").click(portfolio.hide);
    },

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

};

var carrousel = {

    slide : 0,
    elem : null,
    timer : null,
    courant : null,

    init : function(elem){
        if(elem.find(".visus img").length<=1){
            return false;
        }
		this.slide=0;
        if(this.timer!=null){window.clearInterval(carrousel.timer);}
        this.elem = elem;
        elem.find(".visus img").hide();
        elem.find(".visus .navigation li:first").addClass("active");
        elem.find(".visus img:first").show();
        this.courant = elem.find(".visus img:first");
        this.timer = window.setInterval("carrousel.next()",4000);
        this.elem.find(".visus .navigation li").click(function(){
            window.clearInterval(carrousel.timer);
            carrousel.show(carrousel.elem.find(".visus .navigation li").index(this));
            carrousel.slide=carrousel.elem.find(".visus .navigation li").index(this);
            carrousel.timer = window.setInterval("carrousel.next()",4000);

        });
    },

    next : function(){
        this.slide++;
        if(this.slide>this.elem.find(".visus img").length-1){
            this.slide = 0;
        }
        this.show(this.slide);
    },

    prev : function(){
        this.slide--;
        if(this.slide<0){
            this.slide=this.elem.find(".visus img").length-1;
        }
        this.show(this.slide);
    },

    show : function(num){
        this.elem.find(".visus img").css("z-index",1).hide();
        this.elem.find(".visus .navigation li.active").removeClass("active");
        this.courant.show();
        this.elem.find(".visus .navigation li:eq("+num+")").addClass("active");
        this.elem.find(".visus img:eq("+num+")").css("z-index",2).fadeIn(1000);
        this.courant = this.elem.find(".visus img:eq("+num+")");
    }
};
