// When Everything's ready
$(document).ready(function() {

    /* in a WEB 2.0 website, there's one thing that never should be missing.
     * An Apple-like "I'm doin' something" animated indicator image.
     */


    $("#sebgalindicator").ajaxStart(function() {
        $(this).show();
    }).ajaxStop(function() {
        $(this).hide();
    });

    $('#steuerung a').addClass('sepp-black');
    $('#seppContent').load('news.html', function() {
        $(this).slideUp(100).fadeIn(500);

    });
    $('li.projekte').hide();

});

/*
 * Everytime new content gets injected by ajax calls,
 * re-initialize the new DOM nodes
 */
function galleryFy() {
    $("a[rel='fancy']").fancybox();
    $(".scrollable").scrollable({
        easing:"swing",
        keyboard: 'static',
        circular: false
    });
}


/*
 * failsave random number generator. Used to get a
 * random header image. Currently this is disabled
 */
function GetRandom(min, max) {
    if (min > max) {
        return( -1 );
    }
    if (min == max) {
        return( min );
    }
    return min + parseInt(Math.random() * ( max - min + 1 ));
}


/*
 * replaces (with effects!) the header image by a randomly chosen image.
 * TODO: remove hardcoded number range of the random generator, this should be determined by a script
 */
function changeHeaderBg() {
    $('#header').animate({ opacity: 'hide' }, "fast", "linear", function() {
        $('#header').css({ backgroundImage: "url(/tinyGallery/images/header_" + GetRandom(1, 5) + ".jpg)" });
        $('#header').animate({ opacity: 'show' }, "fast", "linear");
    });
}


/*
 * Loads gallery markup generated by a php script into the gallery div container.
 * Also, this takes care of hiding and showing all relevant content.
 *
 */
function loadAlbum(name, whichId) {

    // if there's something visible currently, hide it fast!
    $('#seppContent:visible').slideDown(500).hide("slow");

    // first slide the area out, then load markup and slide in again
    $('#SeppowGallery').slideUp().fadeOut(1500,
            function() {
                $('#SeppowGallery').load('list_project_pics.php?album=' + name, function() {
                    $(this).slideDown(1500).fadeIn(1000);
                    setTimeout("galleryFy()", 2550); // wait for effects finishing
                });
            });

    // Really, I know. This is ugly. And yes, I know about .toggle() etc.
    // Ok, it's not only ugly, it's a big WTF. I don't even ..
    // But it works, so I won't change it. Deal with it. I really wanna 
    // wait until it's too late.Feeling ashamed though :-/
    $('#steuerung a').removeClass("sepp-grau");
    $('#steuerung a').removeClass("sepp-black");
    $('#steuerung a').addClass("sepp-black");
    $('#' + whichId).removeClass("sepp-black").addClass('sepp-grau');
    /**
    * append a checkmark to every visited link, BUT:
    * check for an already existing checkmark before do so.
    */
    var linkValue = String($('#' + whichId).text());
    if (linkValue.substring(linkValue.length-1).charCodeAt(0) != 10003){
    	 $('#' + whichId).append(' <span style="font-size:8pt;"> &#x2713;</span>');
    } 
    
   
    
    // Disabled by now:
    // changeHeaderBg();
}

/*
 * Almost the same as loadAlbum, except that this is only for
 * the text oriented sections of this site.
 *
 */
function showText(type, whichId) {
    if (type.length > 0) {
        // Alles auﬂer der Gallery hiden()
        $('#seppContent:visible').fadeOut(500);
        // Gallery auch ausblenden
        $('#SeppowGallery:visible').slideUp(500);

        // den eigentlichen Content anzeigen
        $('#seppContent').hide("slow", function() {
            $('#seppContent').load(type + '.html', function() {
                $(this).slideUp(100).fadeIn(500);

            })
        });

        $('#steuerung a').removeClass("sepp-grau");
        $('#steuerung a').removeClass("sepp-black");
        $('#steuerung a').addClass("sepp-black");
        $('#' + whichId).removeClass("sepp-black").addClass('sepp-grau');
        // Disabled by now:
        // changeHeaderBg();
    }

}

