var global_domain = "http://dpsg-stpeter.de/";

var trans1 = 0;
var trans2 = 1;
var fade_int = 1;
var event_trans = 0;
var ie6 = false;

var browserName=navigator.appName;
if (browserName=="Microsoft Internet Explorer")
{
    var ie = true;
}

function ajax_request(url,act)
{
    var suche = null;

    if(window.XMLHttpRequest)
        suche = new XMLHttpRequest();
    else if (window.ActiveXObject)
        suche  = new ActiveXObject("Microsoft.XMLHTTP");

    suche.onreadystatechange = function ()
    {
        if(suche.readyState == 4)
        {
            if(suche.status == 200)
            {
                if(suche.responseText == "")
                    alert("Es ist ein Fehler aufgetretenehler");
                else
                    act(suche.responseText);
            }
        }
    }
    suche.open("GET", url, true);
    suche.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    suche.send(null);
}

function change_opacity(id,value)
{
    if(ie)
    {
        document.getElementById(id).style.filter = 'alpha(opacity=' + value*100 + ')';
        if(value==1) document.getElementById(id).style.removeAttribute('filter');
    }
    document.getElementById(id).style.opacity = value;
}

function file_preview(href)
{
    blackout ();
    if(!document.getElementById('img_box'))
    {
        var img_box = document.createElement("div");
        document.getElementById("body").appendChild(img_box);
        img_box.id = "img_box";

        img_box.className = "fullscreen";
    }
    else
        var img_box = document.getElementById('img_box');

    switch(img_box.style.display) {
        case "none" :
            img_box.style.display = "block";
            break;
        case "block" :
            img_box.style.display = "none";
            document.getElementById('blackout').style.display='none';
            break;
        default :
            img_box.style.display = "block";
            break;
    }

    img_box.innerHTML = '<img class="img_max" src="' + href + '" onclick="file_preview(\'' + href + '\')" style="cursor:pointer;" title="Schließen" />';
}

// ****               **** //
// ****   Gallery     **** //
var slideshow = false;
var gallery_link_next = "";
var gallery_link_now = "";
var gallery_link_back = "";
var slideshow_time;
var timeout_time = 3000;

function init_gallery()
{
    blackout ();

    if(!document.getElementById('img_box'))
    {
        var img_box = document.createElement("div");
        document.getElementById("body").appendChild(img_box);
        img_box.id = "img_box";

        img_box.className = "fullscreen";
        img_box.innerHTML = '<div id="fullgallery" class="fullscreen"><div id="gallery_info" class="img_info"><span id="gal_all"><img src="/img/gallery_all.png" width="22" alt="#" title="Zur &Uuml;bersicht" /></span> <span id="gal_back"><img src="/img/gallery_back.png" width="22" alt="#" title="Zurück" /></span> <span id="gal_info"><img src="/static//img/loading2.gif" width="24" alt="..." /></span> <span id="gal_next"><img src="/img/gallery_next.png" width="22" alt="#" title="Vorwärts" /></span> <span id="gallery_slideshow"><img src="/img/gallery_play.png" width="22" alt="#" title="Slideshow starten" /></span> <span id="gal_close"n><img src="/img/gallery_stop.png" width="22" alt="#" title="Schließen" /></span> </div><div id="gallery_main_img" class="fullscreen"></div></div>';

        img_box.style.display = "block";
        change_opacity('gallery_info',0.40);

        document.getElementById('gallery_info').onmouseover = function () {change_opacity('gallery_info',0.80);};
        document.getElementById('gallery_info').onmouseout = function () {change_opacity('gallery_info',0.40);};
    }
    else
    {
        document.getElementById('img_box').style.display = "block";
    }
}

function show_image_s (img)
{
    if(slideshow)
        show_image (img);
}

function show_image (img)
{
    if(!document.getElementById('editor'))
    {
        init_gallery();
        gallery_link_now = img;
        document.getElementById('gal_info').innerHTML = '<img src="/img/loading2.gif" width="16" alt="" />';
        ajax_request(global_domain + "public/ajax.php?type=gallery&id=" + img,place_image);
    }
}

function skip_image (event) {
  if (!event)
    event = window.event;

    switch(event.keyCode) {
        case 27 :
            close_gallery();
            break;
        case 32 :
            if(slideshow)
                slideshow_stop ();
            else
                slideshow_start ();
            break;
        case 37 :
            slideshow_stop();
            show_image(gallery_link_back);
            break;
        case 39 :
            slideshow_stop();
            show_image(gallery_link_next);
            break;
        default:
            break;
    }
}

function place_image (data)
{
    if(data != null)
    {
        var idata = data.split(";;;");

        gallery_link_back = idata[0];
        gallery_link_next = idata[2];

        document.getElementById('gal_all').onclick = function () {window.location = global_domain + idata[6];};
        document.getElementById('gal_back').onclick = function () {show_image(gallery_link_back);slideshow_stop();};
        document.getElementById('gal_info').innerHTML = idata[3] + ' (' + idata[4] + '/' + idata[5] + ')';
        document.getElementById('gal_info').style.cursor = "auto";
        document.getElementById('gal_next').onclick = function () {show_image(gallery_link_next);slideshow_stop();};
        document.getElementById('gal_close').onclick = function () {close_gallery();};

        document.getElementById('gallery_main_img').innerHTML = '<img class="img_max" src="' + idata[1] + '" onclick="show_image(\'' + gallery_link_next + '\');slideshow_stop();" style="cursor:pointer;" />';

        if(slideshow)
            slideshow_time = setTimeout("show_image_s('" + gallery_link_next + "')",timeout_time);
        else
        {
            document.getElementById('gallery_slideshow').onclick = function () {slideshow_start();};
            document.onkeyup = skip_image;
        }
    }
}

function slideshow_start ()
{
    slideshow = true;
    document.getElementById('gallery_slideshow').innerHTML = '<img src="/img/gallery_pause.png" width="22" alt="#" title="Slideshow stoppen" />';
    document.getElementById('gallery_slideshow').onclick = function () {slideshow_stop();};
    slideshow_time = setTimeout("show_image_s('" + gallery_link_next + "')",timeout_time);
}

function slideshow_stop ()
{
    slideshow = false;
    window.clearTimeout(slideshow_time);
    document.getElementById('gallery_slideshow').innerHTML = '<img src="/img/gallery_play.png" width="22" alt="#" title="Slideshow starten" />';
    document.getElementById('gallery_slideshow').onclick = function () {slideshow_start();};
}

function close_gallery ()
{
    slideshow_stop();
    document.getElementById('blackout').style.display = "none";
    document.getElementById('img_box').style.display = "none";
}


