﻿var khoa = {}


/**
 * kleine Slideshow der Bilder, die auf jeder Seite links stehen
 * vorausgesetzt, es sind auch mehrere Bilder
 */
 // erstes Bild anzeigen, nach 10 Sekunden das zweite einblenden;
 // anschliessend wird alle 5 Sekunden das nächste Bild eingeblendet
 // nach dem letzten Bild wird wieder beim ersten gestartet
khoa.imageSlideshow = {

    aniLength: 1000,
    theFirstLength: 10000,
    theRestLength: 5000,
    

    init: function() {

        if ($('#images li').size() <= 1) return;
        window.setTimeout(khoa.imageSlideshow.theFirst, khoa.imageSlideshow.theFirstLength);

    },

    // das erste Bild überblenden
    theFirst: function() {
        var el = ($('#images li.visible').next('li').size() > 0) ? $('#images li.visible').next('li') : $('#images li.first');
        $('#images li.visible').fadeOut(khoa.imageSlideshow.aniLength).removeClass('visible');
        $(el).addClass('visible').fadeIn(khoa.imageSlideshow.aniLength, function() {
            window.setTimeout(khoa.imageSlideshow.theRest, khoa.imageSlideshow.theRestLength);
        });
    },

    // die weiteren Bilder überblenden
    theRest: function() {
        var el = ($('#images li.visible').next('li').size() > 0) ? $('#images li.visible').next('li') : $('#images li.first');
        $('#images li.visible').fadeOut(khoa.imageSlideshow.aniLength).removeClass('visible');
        $(el).addClass('visible').fadeIn(khoa.imageSlideshow.aniLength, function() {
            window.setTimeout(khoa.imageSlideshow.theRest, khoa.imageSlideshow.theRestLength);
        });
    }

}



/**
 * eine individuell gestaltete Scrollbar für html-Elemente, die aufgrund ihrer Höhe eine normale Scrollbar darstellen würden
 */
khoa.scrollbar = {

    init: function() {

        if ($('.entry').size() == 0) return;

        $('.entry').jScrollPane({
            'showArrows': true,
            'arrowSize': 11,
            'dragMaxHeight': 11,
            'scrollbarWidth': 11
        });

    }

}



/**
 * Portfolio und Hochzeitsgalerie
 */
khoa.gallery = {

    max_photos: 0, // Anzahl der Fotos
    max_top: 0, // maximale Top-Position der Thumbnails


    init: function() {

        if ($('#gallery').size() == 0) return;

        // Anzahl der Fotos auslesen und maximale Top-Position der Thumbnails berechnen
        khoa.max_photos = $('#thumbnails li').size();
        khoa.max_top = Math.ceil((khoa.max_photos - 12) / 2) * -70;

        // bilder nachladen
        window.setTimeout("khoa.gallery.loadImages(13)", 1000);

        // zeige ein Photo
        $('#thumbnails a').click(function(event) {
            event.preventDefault();

            // thumbnails aktualisieren
            $('#thumbnails a.visible').removeClass('visible');
            $(this).addClass('visible');

            // wenn nötig, thumbnailsliste verschieben
            if (($(this).parents('li').position().top + $('#thumbnails').position().top) > 350) $('#page_down').click();

            // photos aktualisieren
            $('#photos li.visible').fadeOut('normal').removeClass('visible');
            $('#' + $(this).attr('rel')).fadeIn('normal').addClass('visible');
        });

        // eine Reihe hoch scrollen
        $('#row_up').click(function(event) {
            event.preventDefault();
            var pos = $('#thumbnails').position();
            if (pos.top < 0) {
                var topPos = pos.top + 70;
                $('#thumbnails').animate({ top: topPos + "px" });
            }
        });

        // eine Seite hoch scrollen
        $('#page_up').click(function(event) {
            event.preventDefault();
            var pos = $('#thumbnails').position();
            if (pos.top < 0) {
                var topPos = pos.top + 420;
                if (topPos > 0) topPos = 0;
                $('#thumbnails').animate({ top: topPos + "px" });
            }
        });

        // eine Reihe runter scrollen
        $('#row_down').click(function(event) {
            event.preventDefault();
            var pos = $('#thumbnails').position();
            if (pos.top > khoa.max_top) {
                var topPos = pos.top - 70;
                $('#thumbnails').animate({ top: topPos + "px" });
            }
        });

        // eine Seite runter scrollen
        $('#page_down').click(function(event) {
            event.preventDefault();
            var pos = $('#thumbnails').position();
            if (pos.top > khoa.max_top) {
                var topPos = pos.top - 420;
                if (topPos < khoa.max_top) topPos = khoa.max_top;
                $('#thumbnails').animate({ top: topPos + "px" });
            }
        });

        // animation abspielen
        $("#animation").click(function(event) {
            event.preventDefault();
            if ($(this).hasClass('paused')) {
                $(this).removeClass('paused').addClass('playing').everyTime(5000, function() {
                    if ($('#thumbnails a.visible').parents('li').next('li').size() > 0) {
                        $('#thumbnails a.visible').parents('li').next('li').children('div').children('a').click();
                    } else {
                        $('#thumbnails').animate({ top: "0px" });
                        $('#thumbnails a').eq(0).click();
                    }
                });
            } else {
                $(this).removeClass('playing').addClass('paused').stopTime();
            }
        });

        // animation und sound starten
        $('#animation').click();
        khoa.sound.init();

    },
    // end method 'khoa.gallery.init'



    // Ein Versuch, das Laden der Bilder zu optimieren:
    // - Beim Start werden nur die ersten 12 Fotos und Thumbnails direkt in den HTML-Code eingetragen.
    // - Anschließend werden alle 2 Sekunden jeweils die nächsten 12 Bilder als img-Elemente in das HTML eingebaut.
    loadImages: function(idx) {

        for (j=(idx-1); j<(parseInt(idx)+11); j++) {
            if ( j > khoa.max_photos || !$('#thumbnails a')[j] || !$('#photos div.shadow')[j] ) return;

            var t = $('#thumbnails a')[j];
            var p = $('#photos div.shadow')[j];

            if ( $(t).size() == 0 || $(p).size() == 0 || $(t) == null || $(p) == null ) return;

            // noch nicht geladene Thumbnails als Bild einbauen und laden
            if ($(t).children('img').size() == 0) {
                var src = $(t).children('span').html();
                var alt = $(t).children('em').html();
                $(t).html('<img src="'+src+'" alt="'+alt+'">');
            }

            // noch nicht geladene Fotos als Bild einbauen und laden
            if ($(p).children('img').size() == 0) {
                var src = $(p).children('span').html();
                var alt = $(p).children('em').html();
                $(p).html('<img src="'+src+'" alt="'+alt+'"><div class="shadow_bottom_left"></div><div class="shadow_top_right"></div>');
            }
        }
        nextidx = parseInt(idx)+12;
        if (nextidx <= khoa.max_photos) window.setTimeout("khoa.gallery.loadImages(nextidx)", 2000);
    }

}
// end object 'khoa.gallery'




/**
 * Hintergrundmusik für die Galerie
 */
khoa.sound = {

    init : function() {

        if ($('#jquery_jplayer').size() == 0) return;

        // Flash laden und Musik abspielen
        $("#jquery_jplayer").jPlayer({
            ready: function() {
                // Bei einer Hochzeitsgalerie wird die Musik sofort abgespielt ...
                if ( $('.hochzeitsgalerie').size() > 0 ) {
                    $("#jquery_jplayer").changeAndPlay('/assets/sound/bgsound.mp3');
                    $("#animation").addClass('playing');
                    $("#jquery_jplayer").jPlayerId("volumeMin", "volume_min");
                    $("#jquery_jplayer").jPlayerId("volumeBarValue", "volume_value");
                    $("#jquery_jplayer").jPlayerId("volumeBar", "volume");

                // beim Portfolio erst nach einem Klick auf den Lautsprecher-Button
                } else if ( $('.portfolio').size() > 0 ) {
                    $('#volume_min').click(function(event){
                        event.preventDefault();
                        $('#volume_min').unbind('click');
                        $("#jquery_jplayer").changeAndPlay('/assets/sound/bgsound.mp3');
                        $("#jquery_jplayer").jPlayerId("volumeMin", "volume_min");
                        $("#jquery_jplayer").volumeMax();
                    });
                    $("#jquery_jplayer").jPlayerId("volumeBarValue", "volume_value");
                    $("#jquery_jplayer").jPlayerId("volumeBar", "volume");
                    $("#jquery_jplayer").volumeMin();
                }
            },
            swfPath: 'assets/flash'
        });

        // nach dem Abspielen, wieder von Vorne starten
        $("#jquery_jplayer").onSoundComplete( function() { $("#jquery_jplayer").play(); } );

    }

}



$(function() {
    khoa.gallery.init();
    khoa.imageSlideshow.init();
    khoa.scrollbar.init();
});
