var image_index = 0;
var images = new Array();
var gallery_plugin_dir = "gallery/";
var idle_time = 12;
var autoSlideAt = 12;
var autoslide = false;
var inA = false;
var atFadeIn = false;
var atFadeOut = false;

function catchObject(obj_id)
{
    return document.getElementById(obj_id);
}

function loadXMLDoc(strPath)
{
    var xmlDoc;
    if (window.XMLHttpRequest)
    {
        xmlDoc = new window.XMLHttpRequest();
        xmlDoc.open("GET", strPath, false);
        xmlDoc.send("");
        return xmlDoc.responseXML;
    }
    else if (ActiveXObject("Microsoft.XMLDOM"))
    {   // For IE 5 and IE 6
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.load(strPath);
        return xmlDoc;
    }

    alert("Error loading document: " + strPath);
    return null;
}

function loadGallery(gal_url)
{
    var xmlImageList = loadXMLDoc(gallery_plugin_dir+"images.php?dir="+gal_url);
    images = xmlImageList.getElementsByTagName("image");

    image_index = 0;
    loadImage(image_index);

    setTimeout(countIdleTime, '1000');
}

function loadGalleryJQ(gal_url)
{
    var xmlImageList = loadXMLDoc(gallery_plugin_dir+"images.php?dir="+gal_url);
    images = xmlImageList.getElementsByTagName("image");

    image_index = 0;
    loadImageJQ(image_index);

    //setTimeout(countIdleTime, '1000');
}

function countIdleTime()
{
    idle_time++;
    if(idle_time >= autoSlideAt && !autoslide)
    {
        setTimeout(autoSlide, '3000');
        autoslide = true;
    }
    setTimeout(countIdleTime, '1000');
}

function autoSlide()
{
    if(idle_time >= autoSlideAt)
    {
        callNext();
        setTimeout(autoSlide, '3000');
    }
}

function refreshThumbs()
{
    var t_box = catchObject('mmGallery_thumbBox');
    var output = "<table width='100%' cellpadding='0' cellspacing='0' border='0'><tr>";

        for(var n=image_index-2; n<=image_index+2; n++)
        {
            if(n>=0 && n<images.length)
            {
                var thumb_id;
                if(n==image_index)
                {
                    thumb_id = " id='mmGallery_thumbOfImage' ";
                }
                else
                {
                    thumb_id = "";
                }
                output += "<td style='width:85px;' align='center' valign='center'><a href='javascript:galleryPick("+n+");'><img class='mmGallery_thumb' "+thumb_id+"src='" + gallery_plugin_dir + images[n].attributes[1].value + "' /></td>";
            }
            else
            {
                output += "<td style='width:85px;'>&nbsp;</td>";
            }
        }

    output += "</tr></table>";
    t_box.innerHTML = output;
}

function galleryPrev()
{
    image_index--;
    if(image_index < 0)
    {
        image_index = images.length-1;
    }
    loadImage(image_index);
    idle_time = 0;
    autoslide = false;
}

function galleryNext()
{
    callNext();
    idle_time = 0;
    autoslide = false;
}

function callNext()
{
    image_index++;
    if(image_index >= images.length)
    {
        image_index = 0;
    }
    loadImage(image_index);
}

function galleryPick(index)
{
    image_index = index;
    idle_time = 0;
    autoslide = false;
    loadImage(index);
}

function fadeIn()
{
    
}

function loadImage(index)
{
    refreshThumbs();
    if(!atFadeIn && !atFadeOut)
    {
        if(images[index] != undefined)
        {
            // ausgeben
            var img_a = catchObject('mmGallery_image_a');
            var img_b = catchObject('mmGallery_image_b');

            if(!inA)
            {
                fadeIn('mmGallery_image_a', 0);
                fadeOut('mmGallery_image_b', 100);
                atFadeIn = true;
                atFadeOut = true;
                img_a.src = gallery_plugin_dir + images[index].attributes[1].value;
                inA = true;
            }
            else
            {
                fadeOut('mmGallery_image_a', 100);
                fadeIn('mmGallery_image_b', 0);
                atFadeIn = true;
                atFadeOut = true;
                img_b.src = gallery_plugin_dir + images[index].attributes[1].value;
                inA = false;
            }
            //catchObject('mmGallery_imageBox').innerHTML = '<a href="javascript:galleryNext();"><img id="mmGallery_image_a" class="mmGallery_image" src="' + gallery_plugin_dir + images[index].attributes[1].value + '" /></a>';
        }
    }
    else
    {
        setTimeout('loadImage('+image_index+')', 100);
    }
}

function loadImageJQ(index)
{
    refreshThumbs();
    if(images[index] != undefined)
    {
        // ausgeben
        $("#mmGallery_image_a").src = gallery_plugin_dir + images[index].attributes[1].value;
    }
}

function fadeIn(element_id, start)
{
    var el = catchObject(element_id);
    el.style.zIndex = 0;
    if(start <= 100)
    {
        el.style.opacity = ((100-start)/100);
        setTimeout("fadeIn('"+element_id+"','"+(start+2)+"')", 10);
    }
    else
        {
            el.style.opacity = '1.0';
            atFadeIn = false;
        }
}

function fadeOut(element_id, current)
{
    var el = catchObject(element_id);
    el.style.zIndex = 1;
    if(current > 2)
    {
        el.style.opacity = (current/100);
        setTimeout("fadeOut('"+element_id+"','"+(current-2)+"')", 10);
    }
    else
        {
            el.style.opacity = '0.0';
            atFadeOut = false;
        }
}
