var slideshow_Timer;

$(document).ready(function() {

    //Execute the slideShow, set 4 seconds for each images
    var items = $("div.slideshow");
    $(items).each(function() {
        var parts = $(this).attr("id").split("_");
        var gruppe = parts[1];
        var size = parts[2];
        if (typeof size != "undefined") {
            sizeStr = ":size="+size;
        } else {
            sizeStr = "";
        }

        $(this).load("/_slideshow:gruppe="+gruppe+sizeStr,function(){
            //if(len == idx) {
            slideShow($(this).attr("id"),8000);
        //}
        });
    });


});

var sl_direction="next";

function next_slide(id) {
    sl_direction = "next";
    gallery(id);
}
function prev_slide(id) {
    sl_direction = "prev";
    gallery(id);
}
function slideShow(id,speed) {

    //append a LI item to the UL list for displaying caption
    $('#'+id+' ul.slideshow').append('<li id="slideshow-caption_'+id+'" class="caption"><table class="caption_table"><tr><td onClick="prev_slide(\''+id+'\')" class="button slideshow-caption-prev">&lt;</td><td class="slideshow-caption-container"><h3></h3><p></p></td><td onClick="next_slide(\''+id+'\')" class="button slideshow-caption-next">&gt;</td></tr></table></li>');
    //$('#'+id+' ul.slideshow').append('<li id="slideshow-caption_'+id+'" class="caption"><div class="slideshow-caption-container"><h3></h3><p></p></div></li>');

    //Set the opacity of all images to 0
    $('#'+id+' ul.slideshow li').css({
        opacity: 0.0
    });
    /*
	//Get the first image and display it (set it to full opacity)
	current = $('#'+id+' ul.slideshow li:first');
        height = $(current).find('img').height();
        width =  $(current).find('img').width();
        title = $(current).find('img').attr('title');
        alt = $(current).find('img').attr('alt');
        $(current).css({opacity: 1.0});
	$('#slideshow-caption_'+id+' h3').html(title);
	$('#slideshow-caption_'+id+' p').html(alt);
        caption_height = $('#'+id+' .slideshow-caption-container').height();
        alert($('#'+id+' .slideshow-caption-container').height()+"   "+$('#'+id+' .slideshow-caption-container').width()+"  "+$('#'+id+' .slideshow-caption-container').offset().top);
        $('#slideshow-caption_'+id+' .slideshow-caption-next').css("line-height",caption_height+"px");
        $('#slideshow-caption_'+id+' .slideshow-caption-prev').css("line-height",caption_height+"px");

        $('#'+id+' ul.slideshow').height((height+caption_height)+"px");
        $('#slideshow-caption_'+id).width((width)+"px");

	//Display the caption
	$('#slideshow-caption_'+id).css({opacity: 0.8, bottom:0});
        var height_overall = $('#'+id+' ul.slideshow').height();
        slideshow_position(width,height_overall);
        */
    gallery(id);
    //Call the gallery function to run the slideshow
    slideshow_Timer = setInterval('gallery("'+id+'")',speed);

    //pause the slideshow on mouse over
    $('#'+id+' ul.slideshow').hover(
        function () {
            clearInterval(slideshow_Timer);
        },
        function () {
            slideshow_Timer = setInterval('gallery("'+id+'")',speed);
        }
        );

}

function gallery(id) {

    //if no IMGs have the show class, grab the first image

    var next;

    if ($('#'+id+' ul.slideshow li.show')) {
        var current = $('#'+id+' ul.slideshow li.show');
        //Get next image, if it reached the end of the slideshow, rotate it back to the first image
        if (sl_direction=="next") {
            next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption_'+id)? $('#'+id+' ul.slideshow li:first') :current.next()) : $('#'+id+' ul.slideshow li:first'));
        } else {
            next = ((current.prev().length) ? (current.prev()) : $('#'+id+' ul.slideshow li:last').prev());
        }
    } else {
        next = $('#ul.slideshow li:first');
    }


    //Get next image caption
    var title = next.find('img').attr('title');
    var desc = next.find('img').attr('alt');
    var width = next.find('img').width();
    var height = next.find('img').height();
    //Set the fade in effect for the next image, show class has higher z-index
    next.css({
        opacity: 0.0
    }).addClass('show').animate({
        opacity: 1.0
    }, 300);
    $('#slideshow-caption_'+id).css({
        opacity: 0.0
    });
    $('#slideshow-caption_'+id+' h3').html(title);
    $('#slideshow-caption_'+id+' p').html(desc);
    $('#slideshow-caption_'+id).width((width)+"px");
    var caption_height = $('#slideshow-caption_'+id).height();
    $('#slideshow-caption_'+id+' .slideshow-caption-next').css("line-height",caption_height+"px");
    $('#slideshow-caption_'+id+' .slideshow-caption-prev').css("line-height",caption_height+"px");
    $('#'+id+' ul.slideshow').height((height+caption_height)+"px");
    $('#slideshow-caption_'+id).css({
        opacity: 1.0
    });
    current.animate({
        opacity: 0.0
    }, 1000).removeClass('show');
    slideshow_position(width,$('#'+id+' ul.slideshow').height());

}

var tb_pathToImage = "/media/resources/loadinganimation.gif";
var imgLoader;

/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/

//on page load call tb_init
jQuery(document).ready(function(){   
    slideshow_init('a.slideshowButton, area.slideshowButton');//pass where to apply thickbox
    imgLoader = new Image();// preload image
    imgLoader.src = tb_pathToImage;
});

//add slideshow to href & area elements that have a class of .slideshowButton
function slideshow_init(domChunk){
    jQuery(domChunk).click(function(){
        slideshow_show(this.href);
        this.blur();
        return false;
    });
}

function slideshow_show(url) {
    $("body").append('<div id="slideshow_overlay"></div>');
    $("body").append('<div id="slidehow_window"></div>');

    $("#slideshow_overlay").css({
        opacity: 0.0
    }).show().animate({
        opacity: 0.75
    }, 300);
    $("#slideshow_overlay").click(slideshow_remove);
    $("body").append("<div id='slideshow_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
    $("#slideshow_load").show();//show loader

    var parts = url.split("_");
    var gruppe = parts[1];
    var size = parts[2];
    if (typeof size != "undefined" && typeof size != "NaN" && size != "NaN") {
        sizeStr = ":size="+parseInt(size);
    } else {
        sizeStr = "";
    }
    
    $("#slidehow_window").load("/_slideshow:gruppe="+gruppe+sizeStr,function(){
        $("#slideshow_load").hide();
        $(this).show();
        $(this).append("<div onclick=\"slideshow_remove()\" title=\"Slideshow schliessen\" class=\"button close_button\"></div>");
        slideShow($(this).attr("id"),8000);
    });
}

function slideshow_remove() {
    clearInterval(slideshow_Timer);
    jQuery("#slidehow_window").slideUp("fast",function(){
        jQuery('#slidehow_window,#slideshow_overlay').trigger("unload").unbind().remove();
    });
    jQuery("#slideshow_load").remove();
}


function slideshow_position(width,height) {
    jQuery("#slidehow_window").css({
        marginLeft: '-' + parseInt((width / 2),10) + 'px',
        width: width + 'px'
    });
    if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
        jQuery("#slidehow_window").css({
            marginTop: '-' + parseInt((height / 2),10) + 'px',
            height: height+'px'
        });
    }
}
