// (c) 2006-2008 by Saga-Games -- http://sagagames.de \\

var sl_position = new Array(); //which pic?
var sl_timer = new Array(); //milliseconds counter for fading
var sl_images = new Array(); //filenames (small pics)
var sl_links = new Array(); //filenames (large pics)
var sl_cookie = new Array(); //cookie name

// amount of pictures that should be precached
var sl_cache_count = 3;
var sl_cache = new Array();

// current pictures
var sl_img = new Array(2);

var IE = (navigator.appName.indexOf("Microsoft") !=-1);

// navigation icons should be precached
var precacheIcons = new Array();
precache_navigation("start");
precache_navigation("prev");
precache_navigation("next");
precache_navigation("end");

function slideshow_load(reihe) {
    if(!document.getElementById || !document.getElementById("slideshow-" + reihe)) return;
    if(!sl_position[reihe]) sl_position[reihe] = 0;

    for(var i = 0; i < 2; i++) {
        sl_img[i] = document.createElement("div");
        with(sl_img[i]){
            id = "slideimg" + reihe + "-" + i;
            className = "slidelayer"
            style.position = "absolute";
            style.backgroundColor = "transparent";
            style.backgroundImage = "url(" + sl_images[reihe][sl_position[reihe]] + ")";
            try {style.cursor = "pointer";} catch(e) {style.cursor = "hand";}
            sl_img[i].onclick = function() {slideshow_openimage(reihe);};
        }

        document.getElementById("slideshow-" + reihe).style.backgroundImage="url(bilder/slide/load.gif)";
        document.getElementById("slideshow-" + reihe).appendChild(sl_img[i]);
    }
    
    document.getElementById("slideshow-" + reihe).removeChild(document.getElementById("slideshow-" + reihe).firstChild);

    sl_img[0].style.display = "block";
    sl_img[1].style.display = "none";
    sl_timer[reihe] = 0;
    slideshow_setbuttons(reihe);
    
    // image cache
    sl_cache[reihe] = new Array(sl_cache_count)
    for (var i = 0; i < sl_cache_count; i++) {
        sl_cache[reihe][i] = new Image();
    }
    slideshow_cache_fill(reihe);
}

function slideshow_openimage(reihe) {
    self.location = sl_links[reihe][sl_position[reihe]];
}

function slideshow_start(reihe) {
    if(sl_position[reihe] > 0)
        slideshow_goto(reihe, 0);
}

function slideshow_prev(reihe) {
    if(sl_position[reihe] > 0)
        slideshow_goto(reihe, sl_position[reihe] - 1);
}

function slideshow_next(reihe) {
    if(sl_position[reihe] < sl_images[reihe].length - 1)
        slideshow_goto(reihe, sl_position[reihe] + 1);
}

function slideshow_end(reihe) {
    if(sl_position[reihe] < sl_images[reihe].length - 1)
        slideshow_goto(reihe, sl_images[reihe].length - 1);
}

function slideshow_goto(reihe, imgID) {
    if(sl_timer[reihe] > 0) return;
    
    sl_position[reihe] = imgID;

    sl_img[1].style.backgroundImage = "url(" + sl_images[reihe][sl_position[reihe]] + ")";

    sl_img[0].style.opacity="1";
    sl_img[1].style.opacity="0";
    if(IE) {
        sl_img[0].style.filter="Alpha(opacity=100)";
        sl_img[1].style.filter="Alpha(opacity=0)";
    }
    sl_img[1].style.display="block";

    var dt = new Date();
    sl_timer[reihe] = dt.getTime();

    slideshow_setbuttons(reihe);
    slideshow_save(reihe);
    slideshow_progress(reihe);
    slideshow_cache_fill(reihe);
}

function slideshow_progress(reihe) {
    var dt = new Date();
    var alpha = (dt.getTime() - sl_timer[reihe]) / 5;
    if(alpha > 100) alpha = 100;

    sl_img[0].style.opacity=(1 - alpha / 100);
    sl_img[1].style.opacity=(alpha / 100);
    if(IE) {
        sl_img[0].style.filter="Alpha(opacity=" + (100 - alpha) + ")";
        sl_img[1].style.filter="Alpha(opacity=" + alpha + ")";
    }
    
    if(alpha < 100) window.setTimeout("slideshow_progress(" + reihe + ")", 5);
    else {
        sl_timer[reihe] = 0;
        sl_img[0].style.backgroundImage = "url(" + sl_images[reihe][sl_position[reihe]] + ")";
        sl_img[0].style.opacity="1";
        if(IE) sl_img[0].style.filter="Alpha(opacity=100)";
        sl_img[1].style.display="none";
    }
}

function slideshow_save(reihe) {
    var expire = new Date();
    expire.setTime(expire.getTime() + (30 * 60 * 1000));
    document.cookie = "slideshow-" + sl_cookie[reihe] + "=" + sl_position[reihe] + "; expires=" + expire.toGMTString();
}

function slideshow_setbuttons(reihe) {
    document.getElementById("slideprev-" + reihe).className = "slideprev" + (sl_position[reihe] == 0 ? 'imp' : '');
    document.getElementById("slidenext-" + reihe).className = "slidenext" + (sl_position[reihe] == sl_images[reihe].length - 1 ? 'imp' : '');
    document.getElementById("slidestart-" + reihe).className = "slidestart" + (sl_position[reihe] == 0 ? 'imp' : '');
    document.getElementById("slideend-" + reihe).className = "slideend" + (sl_position[reihe] == sl_images[reihe].length - 1 ? 'imp' : '');
    document.getElementById("slideinfo-" + reihe).innerHTML = (sl_position[reihe] + 1) + " von " + (sl_images[reihe].length);
}

function slideshow_cache_fill(reihe) {
    for (var i = 0; i < sl_cache_count; i++) {
        if(sl_position[reihe] + i + 1 < sl_images[reihe].length) 
            sl_cache[reihe][i].src = sl_images[reihe][sl_position[reihe] + i + 1];
    }
}

function slideshow_picture(reihe, imgID) {
    if (imgID >= 1 && imgID <= sl_images[reihe].length) {
        if(imgID != sl_position[reihe] + 1) slideshow_goto(reihe, imgID - 1);
        if(document.getElementById("slideshow-" + reihe).scrollIntoView)
            document.getElementById("slideshow-" + reihe).scrollIntoView();
    } 
}

function precache_navigation(buttonName) {
    var picName, id;
    for(var i = 0; i <= 3; i++) {
        id=precacheIcons.length;
        precacheIcons[id] = new Image();
        if(i == 0) picName = "";
        if(i == 1) picName = "_active";
        if(i == 2) picName = "_hover";
        if(i == 3) picName = "_imp";
        precacheIcons[id].src = "bilder/slide/nav_" + buttonName + picName + ".png";
    }
}

