$(function() {
    // project links
    $(".project-link a").mouseover(function(){
        $(this).css("background-position", "left bottom");
    });

    $(".project-link a").mouseout(function(){
        $(this).css("background-position", "left top");
    });

    // jcarousel init
    if ($("#gallery-thumbs").children().length) {
        $("#gallery-thumbs").show();
        $("#gallery-thumbs").jcarousel({
            initCallback : carousel_initCallback,
            scroll: 7
        });
    }

    // jcarousel init callback
    function carousel_initCallback(carousel) {
        // use pointer
        $(".gallery-image").css("cursor", "pointer");
        $(".gallery-thumb").css("cursor", "pointer");

        // init images
        $(".gallery-image").click(function(){
            // image id
            var imageId = this.id.substr(6);
            if (imageId == $(".gallery-image").size()) {
                imageId = 1;
                carousel.scroll(1);
            } else {
                imageId++;
                carousel.scroll(imageId);
            }

            // thumb opacity
            $(".gallery-thumb").removeClass("selected");
            $(".gallery-thumb").css("opacity", "0.5");
            $("#thumb-" + imageId).addClass("selected");
            $("#thumb-" + imageId).css("opacity", "1");

            // image display
            $(".gallery-image").hide();
            $("#image-" + imageId).show();
        });

        // init thumbs
        $(".gallery-thumb").mouseover(function(){
            $(this).css("opacity", "1");
        });

        $(".gallery-thumb").mouseout(function(){
            if (!$(this).hasClass("selected")) {
                $(this).css("opacity", "0.5");
            }
        });

        $(".gallery-thumb").click(function() {
            // image id
            var imageId = this.id.substr(6);

            // thumb opacity
            $(".gallery-thumb").removeClass("selected");
            $(".gallery-thumb").css("opacity", "0.5");
            $(this).addClass("selected");
            $(this).css("opacity", "1");

            // image display
            $(".gallery-image").hide();
            $("#image-" + imageId).show();
        });

        // go
        $("#thumb-1").click();
    };
});

