var paused=false;

function byId(id)  {
    var wert = document.getElementById(id);
    return wert;
}

function byName(name)  {
    var wert = document.getElementsByName(name);
    return wert;
}

function init() {
    $(document).ready(
        function() {
            if (!$.browser.msie) {
               $('.main_news_box').corner();
               $(".reference_area").corner();
               $(".reference_area h2").corner();
            }
            menuActions();  // in menu.js
            $(".main_news_box_title").click(
                  function () { $(this).next(".main_news_box_content").slideToggle("fast");  });
            $(".main_news_box_entry").click(function() { $(this).children(".main_news_box_static_hint").slideToggle("fast"); }      );
            $(".main_news_box_entry").hover(function() { $(this).children(".entryTitle").css("text-decoration","underline");}, function() { $(this).children(".entryTitle").css("text-decoration","none");}      );
            $(".reference_area").click(function() { $(this).children(".reference_area_hint").slideToggle("fast"); }      );
            $(".table_events_row").hover(function() {$(this).addClass("table_selected");}, function() {$(this).removeClass("table_selected");});
            $(".table_events_row").click(function() {$(this).find(".event_hint").slideToggle("fast");});
            $(".album_action").hover(function() {$(this).addClass("album_hover");}, function() {$(this).removeClass("album_hover");});
            $(".album_element").hover(function() {$(this).children(".album_element_title").css("text-decoration","underline");}, function() { $(this).children(".album_element_title").css("text-decoration","none");}   );
            $(".album_element").click(function() {$(this).children(".album_hint").slideToggle("fast"); } );
            $(".new_guestbook_entry").click(function() { $("#guestbook_box_new").toggle("fast"); });
            $(".message_box").click(function() { $(this).hide();});
            $(".showOverlay").click(function() { $(".main_overlay").fadeIn("fast");});
            $(".main_overlay_background").click(function() { closeDiashow(); });
            $("#diaContainer").click(function() { closeDiashow(); });
            $(".dia_nav_button").hover(function() {$(this).css("padding-top","0px");}, function() { $(this).css("padding-top","3px");}   );
            $("#diaAutoshow").click(function() { if ($(this).checked) paused=true; else paused=false; });
            $("#loginFormTitle").click(function() { $("#loginFormInput").toggle("fast");});

        }); // -- ready
}

function valueFrom(id) {
    var idx= byId(id).selectedIndex;
    var v=byId(id).options[idx].value;
    return v;
}

function addsmilie(targetID,wert)  {
    var elem = byId(targetID);
    elem.value = elem.value + wert;
}

var diaImages;
var currentImage=-1;
var imageNext = new Image();
var imageNextIdx = -1;
var myshow;



function getCurrentImage(image) {
    for (var i=0; i<diaImages.length; i++)
        if (diaImages[i]==image) {
            currentImage=i;
            break;
        }
}

function setImage(idx) {
    if (idx<0 || idx>=diaImages.length) idx=0;
    $("#diaContainer").fadeOut("fast", function() { setImage2(idx);});
}

function setImage2(idx) {
    var img = byId('diaimage');
    if (imageNextIdx==idx) {
        img.src = imageNext.src;
    } else {
        img.src = diaImages[idx];
    }
    $("#imageInfo").html("Bild "+ (idx+1) +" von " + (diaImages.length-1));
    $("#diaContainer").fadeIn("slow");
    imageNextIdx = idx+1;
    imageNext.src = diaImages[imageNextIdx];
    currentImage=idx;
}

function nextImage() {
    if (currentImage<diaImages.length-1)
        setImage(currentImage+1);
        else {
           alert ("Ende - Start von vorn.");
           setImage(0);
        }

    if (byId('diaAutoshow').checked==false)
        window.clearInterval(myshow);


}

function prevImage() {
    if (currentImage>0)
        setImage(currentImage-1);
    else {
        alert ("Anfang");
        setImage(0);
    }
}

function closeDiashow() {
    currentImage=0;
    window.clearInterval(myshow);
    imageNextIdx=-1;
    byId('diaAutoshow').checked=false;
    /* playDia=false; */
    $(".main_overlay").fadeOut("fast");
}


function myAutoshow(status) {
        myshow = window.setInterval("nextImage()", parseInt(byId('diaTime').options[byId('diaTime').selectedIndex].value));
}

function showImages(file,autoshow,searchDir) {
    $("#diaContainer").hide();
    $(".main_overlay").fadeIn("fast");
    if (autoshow=='1') {
        $(".dia_navigation").show();
        byId('diaAutoshow').checked=true;
    }
    var i=byId('diaimage');
    if (searchDir!=0)
    // get all Images in Directory
    $.get('getImages.php?path='+file,
           function(data) {
               diaImages = data.split(";");
               getCurrentImage(file);
               setImage(currentImage);
           }
    ); else {
        diaImages=new Array(file);
        currentImage=0;
        setImage(currentImage);
    }
    if (autoshow=='1') myAutoshow(true);
}

function loginClear() {

}

function loginSend() {
  document.forms["loginForm"].submit();
}
