var inst_timeout;
var fancy_is_open = false;


$(document).ready(function () {
    timeout_btw_imgs = 10000;
    direction = "fwd"; //oder bwd = rückwärts

    if ( $("#slideshow").length > 0 ) {
        inst_timeout = window.setTimeout("nextImage(direction)", timeout_btw_imgs);
    }

    if ( $("#slideshowNews").length > 0 ) {
        inst_timeout = window.setTimeout("nextImageNews(direction)", timeout_btw_imgs);
    }


    $('#slideshow').width($('#slideshow').width());
    $('#slideshow').height($('#slideshow').height());

    $('#slideshowNews').width($('#slideshowNews').width());
    $('#slideshowNews').height($('#slideshowNews').height());

    $('#more_infos').hide();

    $('#show_more_infos').click(function() {
        $('#more_infos').show();
        $('#show_more_infos').hide();
        return false;
    });

    $('.switch_slideshow_images').click(function() {
        if ($(this).attr("id") == "next_img")
            nextImage("fwd");
        else
            nextImage("bwd");
        return false;
    });

    $('.switch_slideshow_images_news').click(function() {
        if ($(this).attr("id") == "next_img")
            nextImageNews("fwd");
        else
            nextImageNews("bwd");
        return false;
    });

    if (1 == slideshow_images.length) {
        $('#next_img').hide();
        $('#prev_img').hide();
    }

    $(".zoom_image").fancybox({
        onStart: function () {fancy_is_open = true; },
        onClosed: function () {fancy_is_open = false;},
        onComplete: function() {
            if ($("#fancybox-content #bild").width() > $("#fancybox-content").width()){
                $("#fancybox-content #bild").width($("#fancybox-content").width());
                $("#fancybox-content #bild").parent().width($("#fancybox-content").width());
                $("#fancybox-content #big_image_description").width($("#fancybox-content").width());
            }
        }
    });

    $('#image_text').hide();

  $('#mainImage img').hover(
    function () {
      $(this).next().show();
    },
    function () {
      $(this).next().hide();
    }
  );

  $('#image_text').hover(
    function () {
      $(this).show();
    },
    function () {
      $(this).hide();
    }
  );


});

var index = 0;
var new_dir;


function nextImage(dir) {

    if (fancy_is_open == false) {

        window.clearTimeout(inst_timeout);
        new_dir = dir;

        $('.zoom_image').fadeOut(20, function(){

            if (new_dir == "fwd")
                index++;
            else if (new_dir == "bwd")
                index--;

            if (index >= slideshow_images.length)
                index = 0;
            else if (index < 0)
                index = slideshow_images.length;

            if (index == slideshow_images.length && dir == "bwd")
                index = slideshow_images.length - 1;

            $('.zoom_image').html("<img src='layout/images/ajax_loader2.gif' id='img_ajax_loader' />");

            $('.zoom_image').fadeIn(1, function() {

                $("#img_ajax_loader").css("margin-left", Math.floor($('#slideshow').width() / 2) - 16);
                $("#img_ajax_loader").css("margin-top", Math.floor($('#slideshow').height() / 2) - 16);

                $('#subtitle').fadeOut("fast");

                next_image_id = slideshow_images[index];

                $.ajax({
                  url: 'ajax_getSlideshowImage.php',
                  type: "GET",
                  dataType: "json",
                  data: ({id: next_image_id, preview: preview_id, node_id: node_nr}),
                  success: function(data) {
                    $.loadImages(data.file_name, function(){
                        $('.zoom_image').fadeOut("fast", function(){
//                              $('.zoom_image').fadeIn("slow");

                            $('.zoom_image').html("<img src='" + data.file_name + "' />").fadeIn(2500);

//                              $('#slideshow').animate({"width" : data.width, "height" : data.height }, "slow");
//                              $('#outer_slideshow').animate({"width" : data.width}, "slow");

                            $('#subtitle').html(data.subtitle);
                            $('#subtitle').fadeIn()

                            $('.zoom_image').attr("href", "big_image.php?image_id=" + next_image_id + "&node_id=" + node_nr);

                            if ( $("#slideshow").length > 0 ) {
                                inst_timeout = window.setTimeout("nextImage(new_dir)", timeout_btw_imgs);
                            }
                        }); //$('.zoom_image').fadeOut(20, function(){
                    });//$.loadImages
                  } //success
                }); //$.ajax
            });//$('.zoom_image').fadeIn
        }); //$('.zoom_image').fadeOut
    } //if
    else
        inst_timeout = window.setTimeout("nextImage(new_dir)", timeout_btw_imgs);
}

function nextImageNews(dir) {

    if (fancy_is_open == false) {

        window.clearTimeout(inst_timeout);
        new_dir = dir;

        $('.zoom_image').fadeOut(20, function(){

            if (new_dir == "fwd")
                index++;
            else if (new_dir == "bwd")
                index--;

            if (index >= slideshow_images.length)
                index = 0;
            else if (index < 0)
                index = slideshow_images.length;

            if (index == slideshow_images.length && dir == "bwd")
                index = slideshow_images.length - 1;

            $('.zoom_image').html("<img src='layout/images/ajax_loader2.gif' id='img_ajax_loader' />");

            $('.zoom_image').fadeIn(1, function() {

                $("#img_ajax_loader").css("margin-left", Math.floor($('#slideshowNews').width() / 2) - 16);
                $("#img_ajax_loader").css("margin-top", Math.floor($('#slideshowNews').height() / 2) - 16);

                $('#subtitle').fadeOut("fast");

                next_image_id = slideshow_images[index];

                $.ajax({
                  url: 'ajax_getSlideshowImageNews.php',
                  type: "GET",
                  dataType: "json",
                  data: ({id: next_image_id, preview: preview_id, news_id: news_nr}),
                  success: function(data) {
                    $.loadImages(data.file_name, function(){
                        $('.zoom_image').fadeOut("fast", function(){

                            $('.zoom_image').html("<img src='" + data.file_name + "' />").fadeIn(2500);

                            $('#subtitle').html(data.subtitle);
                            $('#subtitle').fadeIn()

                            $('.zoom_image').attr("href", "big_image_news.php?image_id=" + next_image_id + "&news_id=" + news_nr);

                            if ( $("#slideshowNews").length > 0 ) {
                                inst_timeout = window.setTimeout("nextImageNews(new_dir)", timeout_btw_imgs);
                            }
                        }); //$('.zoom_image').fadeOut(20, function(){
                    });//$.loadImages
                  } //success
                }); //$.ajax
            });//$('.zoom_image').fadeIn
        }); //$('.zoom_image').fadeOut
    } //if
    else
        inst_timeout = window.setTimeout("nextImageNews(new_dir)", timeout_btw_imgs);
}

//eof
