var duration = 5000; // Geschwindigkeit
var ico_anz  = 10;   // Anzahl Vorschaubilder
var ico_w    = 0;    // Breite Vorschaubilder
var ico_h    = 100;  // Höhe Vorschaubilder
var img_w    = 0;    // Breite Vollbild
var img_h    = 400;  // Höhe Vollbild

jQuery.fx.interval = 100;

function bildwechsel() {
    var $alt = $('#galerie > img:eq(0)');
    var $neu = $('#galerie > img:eq(1)');
    $alt.css('z-index', '1');
    $neu.css('z-index', '2').delay(5000).fadeIn(3000, function() {
        $alt.css('display', 'none').insertAfter($('#galerie > img:last'));
        bildwechsel();
    });
}

function ImgSlide() {
    var bild = $(".img:last");

    //die Breite wieder auf den Standardwert zurück setzen
    bild.css("width", "auto");

    //Breite bestimmen
    var mrgLeft = parseInt(bild.width());

    //letztes Bild entfernen und an erster Position wieder einfügen
    bild.remove();
    bild.css('marginLeft', "-"+mrgLeft+"px");
    bild.insertBefore(".img:first");

    //Animation in Abhängigkeit der Bildbreite steuern
    var dur = (mrgLeft*duration)/100;

    //erstes Bild von links nach rechts schieben
    bild.animate( {marginLeft:"0"}, {duration:dur, easing:"linear", complete: ImgSlide});
}

$(document).ready(function() {
    //Bildwechsel starten
    $('#galerie > img:gt(0)').css('display', 'none');
    bildwechsel();

    //Bilderleiste starten
    $.get('image.php', {anz: ico_anz}, function(data) {
        var bilderliste = '';
        var bnr = 0;
        $.each(data, function(key, img) {
            $("#film").append('<img src="image.php?img='+img+'&w='+ico_w+'&h='+ico_h+'" class="img" alt="'+bnr+'">');
            
            var bname = img.substr(0, img.indexOf('.'));
            bilderliste = bilderliste+",{'href':'image.php?img="+img+"&w="+img_w+"&h="+img_h+"','title':'"+bname+"'}";
            bnr++;
        });
        bilderliste = '[' + bilderliste.substr(1) + ']';

        //Verschiebung bei Mouseover anhalten und fortsetzen
        $("#film").hover(
            function() {
                $(".img").stop();
            },
            function() {
                var bild = $(".img:first");
                var mrgLeft = 0-parseInt(bild.css('marginLeft'));
                var dur = (mrgLeft*duration)/100;
                bild.animate( {marginLeft:"0"}, {duration:dur, easing:"linear", complete:ImgSlide});
            }
        );

        //Bildverschiebung starten
        $(".img:last").load(function() {
            ImgSlide();
        });

        //Click-Event für Bilder
        $('.img').live('click', function() {
            var idx = parseInt($(this).attr('alt'));
            $.fancybox(eval(bilderliste), {
                'index'         : idx,
                'cyclic'        : 'true',
                'type'          : 'image',
                'transitionIn'  : 'none',
                'transitionOut'	: 'none'
            });
            
        });
        
        //Fancybox für normale Bilder
        $("a#example4").fancybox();

        //Fancybox in Galerie
        $('a[rel=group1]').fancybox({
            'transitionIn' : 'none',
            'transitionOut' : 'none'
        });
    });
});

