var diaporama_carousel;

jQuery(document).ready(function () {
    jQuery("#diaporama .image-list").jcarousel({
        scroll: 1,
        initCallback: on_init_callback
    });

    jQuery("#diaporama-view-btn").bind("click", function () {
        $lightbox.openPopup({
            rel: '#popup',
            closeBtnText: 'fermer',
            themeClass: 'DiaparomaPopup',
            callback: function () {
                diaporama_carousel.reload();
            }
        });

        return false;
    });
	
	jQuery("#diaporama-view-img").bind("click", function () {
        $lightbox.openPopup({
            rel: '#popup',
            closeBtnText: 'fermer',
            themeClass: 'DiaparomaPopup',
            callback: function () {
                diaporama_carousel.reload();
            }
        });

        return false;
    });
    
});

function on_init_callback (carousel, item) {
    if ( diaporama_carousel == undefined ) {
        diaporama_carousel = carousel;
    }

    var active_item = carousel.list.find("li#diaporama-image-active > a");
    active_item.parent().addClass("active-item");
    var active_image_infos = active_item.parent().find(".image-infos").eq(0).clone(true);
    jQuery("#diaporama-image-infos").empty().append(active_image_infos);

    var imgObj = new Image();
    jQuery("#diaporama-image-viewer").empty();

    jQuery(imgObj).bind("load", function () {
        jQuery("#diaporama-image-viewer").html(imgObj);
    });

    imgObj.src = active_item.attr("href");
    imgObj.width = "438";
    imgObj.height = "292";
    imgObj.alt = active_item.attr("title");
    imgObj.title = active_item.attr("title");

    carousel.list.find("li > a").each(function (index) {
        var el = jQuery(this);

        el.bind("click", function () {
            var image_infos = el.parent().find(".image-infos").eq(0).clone(true);
            jQuery("#diaporama-image-infos").empty().append(image_infos);

            if ( active_item != undefined ) {
                active_item.parent().removeClass("active-item");
                active_item = el;
            }
            el.parent().addClass("active-item");

            var imgURL = el.attr("href");
            var imgTitle = el.attr("title");

            var imgObj = new Image();
            jQuery("#diaporama-image-viewer").children().fadeOut("fast", function () {
                jQuery("#diaporama-image-viewer").empty();

                jQuery(imgObj).bind("load", function () {
                    jQuery("#diaporama-image-viewer").html(this);
                    jQuery(this).hide();
                    jQuery(this).fadeIn("fast", function (callback) {
                        if ( callback != undefined ) {
                            callback();
                        }
                    });
                });

                imgObj.src = imgURL;
                imgObj.width = "438";
                imgObj.height = "292";
                imgObj.alt = imgTitle;
                imgObj.title = imgTitle;
            });

            return false;
        });
    });
}