

function LoadXLgeImage(lgeName) {
    var img = new Image();
    img.src = lgeName;
    return (img);
}

function ShowImgPreview(event, picName, index) {

    var pvw = document.getElementById("imgPvwPanel_" + index);
    pvw.style.display = "block";
    var imgpvw = document.getElementById("imgPreview_" + index);
    imgpvw.src = picName.src;
    pvw.style.width = (picName.width + 20) + "px";
    pvw.style.height = (picName.height + 40) + "px";
    var winWidth = GetWindowSize(true);
    var winHeight = GetWindowSize(false);
    var scrollTop = GetScrollTop();
    var posx = (winWidth / 2) - (picName.width / 2);
    var posy = (winHeight / 2) - (picName.height / 2);
    pvw.style.top = scrollTop + posy + "px";
    pvw.style.left = posx + "px";
}

function HideImgPreview(index) {
    var pvw = document.getElementById("imgPvwPanel_" + index);
    pvw.style.display = "none";
}

function fadeInit(index, startDelay) {
    if (document.getElementById) {
        eval("slides_" + index + " = new Array");
        var node = eval("gallery_" + index + ".firstChild");
        while (node) {
            if (node.nodeType == 1) {
                eval("slides_" + index + ".push(node)");
            }
            node = node.nextSibling;

        }
        var thisSlides = eval("slides_" + index);
        for (i = 0; i < thisSlides.length; i++) {
            /* loop through all these child nodes and set up their styles */
            thisSlides[i].style.position = 'absolute';
            thisSlides[i].style.top = 0;
            thisSlides[i].style.zIndex = 0;
            /* set their opacity to transparent */
            SetOpacity(i, 0, thisSlides);
        }

        var firstImgObj = thisSlides[0].firstChild;
        if (firstImgObj.tagName != "IMG") {
            firstImgObj = firstImgObj.firstChild;
        }

        if (firstImgObj != null) {
            var ctls = document.getElementById("slidectrls_" + index);
            if (ctls != null) {
                ctls.style.width = firstImgObj.width + "px";
            }
            var slideno = document.getElementById("slideno_" + index);
            if (slideno != null) {
                slideno.style.display = (firstImgObj.width >= 200) ? "block" : "none";
            }
            var fullWidth = eval("outerWidth_" + index);
            var fullHeight = eval("outerHeight_" + index);
            var ctlStart = eval("ctlStart_" + index);
            //alert(fullHeight + ", " + firstImgObj.height);
            //alert(fullWidth + ", " + firstImgObj.width);
            var ss = document.getElementById("slideshow_" + index);
            if (firstImgObj.width < fullWidth) {
                var left = parseInt((fullWidth - firstImgObj.width) / 2);
                ss.style.marginLeft = left + "px";
                if (ctls != null) {
                    ctls.style.marginLeft = left + "px";
                }
            }

            if (firstImgObj.height < fullHeight) {
                //var diff = fullHeight - firstImgObj.height - 38;
                //ss.style.marginTop = diff + "px";
            }
        }


        /* make the list visible again */
        eval("gallery_" + index + ".style.visibility = 'visible';");

        /* initialise a few parameters to get the cycle going */
        eval("currentImage_" + index + "=0;");
        eval("previousImage_" + index + "=thisSlides.length-1;");
        eval("opacity_" + index + "=100;");
        eval("SetOpacity(currentImage_" + index + ", 100, thisSlides);");
        eval("thisSlides[currentImage_" + index + "].style.zIndex = 1;");
        //Start the crossfade
        if (startDelay == 0) {
            PrepareNextSlide(false, index);
        }
        else {
            window.setTimeout("PrepareNextSlide(true, " + index + ")", startDelay * 1000);
        }
    }
}

function SetOpacity(imageNumber, opacity, thisSlides) {
    /* helper function to deal specifically with images and the cross-browser differences in opacity handling */
    var obj = thisSlides[imageNumber];
    if (obj.style) {
        if (obj.style.MozOpacity != null) {
            /* Mozilla's pre-CSS3 proprietary rule */
            obj.style.MozOpacity = (opacity / 100) - .001;
        }
        else if (obj.style.opacity != null) {
            /* CSS3 compatible */
            obj.style.opacity = (opacity / 100) - .001;
        }
        else if (obj.style.filter != null) {
            /* IE's proprietary filter */
            obj.style.filter = "alpha(opacity=" + opacity + ")";
        }
    }
}

function crossfade(opacity, index) {
    var thisSlides = eval("slides_" + index);
    /* make sure the current image is on top of the previous one */
    if (opacity == 0) {
        //eval("alert(previousImage_" + index + " + ', ' + currentImage_" + index + ");");
        eval("thisSlides[previousImage_" + index + "].style.zIndex = 0;");
        eval("thisSlides[currentImage_" + index + "].style.zIndex = 1;");
        var curImg = eval("currentImage_" + index);
        var prevImg = eval("previousImage_" + index);
        document.getElementById("slidedesc_" + index + "_" + prevImg).className = "slidedescunsel";
        document.getElementById("slidedesc_" + index + "_" + curImg).className = "slidedesc";

        if (typeof PreparingSlideCallback == 'function') {
            PreparingSlideCallback(curImg);
        } 

    }

    if (opacity < 100) {
        //Alter opacity
        eval("SetOpacity(currentImage_" + index + ", opacity, thisSlides);");
        //SetOpacity(previousImage,100-opacity);

        opacity += 5;
        eval("timer_" + index + " = window.setTimeout('crossfade(' + opacity + ', " + index + ")', 25);");
        eval("fadeInProgress_" + index + " = true;");
    }
    else {
        var playing = eval("playing_" + index);
        if (playing) {
            PrepareNextSlide(false, index);
        }
        else {
            eval("clearTimeout(timer_" + index + ");");
        }
    }
}



function PrepareNextSlide(fadeIn, index) {
    var thisSlides = eval("slides_" + index);

    /* make the previous image - which is now covered by the current one fully - transparent */
    eval("SetOpacity(previousImage_" + index + ", 0, thisSlides);");

    var curImg = eval("currentImage_" + index);
    var prevImg = eval("previousImage_" + index);
    if (!fadeIn) {
        thisSlides[prevImg].style.zIndex = 0;
        thisSlides[curImg].style.zIndex = 1;
        SetOpacity(curImg, 100, thisSlides);
        //alert("here");
        //PrepareNextSlide(true);
    }

    //Change the description
    //alert (prevImg + ", " + curImg);
    document.getElementById("slidedesc_" + index + "_" + prevImg).className = "slidedescunsel";
    document.getElementById("slidedesc_" + index + "_" + curImg).className = "slidedesc";
    //eval("slidedesc_" + index + "_" + prevImg + ".className = 'slidedescunsel'");
    //eval("slidedesc_" + index + "_" + curImg + ".className = 'slidedesc'");
    //eval("slidedesc_" + index + ".innerHTML = arrDescs_" + index + "[curImg];");
    var slideno = document.getElementById("slideno_" + index);
    if (slideno != null) {
        eval("slideno_" + index + ".innerHTML = 'Slide ' + (curImg + 1) + ' of ' + thisSlides.length;");
    }
    //slidedesc.innerHTML = currentImage;
    if (typeof SlideChangeCallback == 'function') {
        SlideChangeCallback(curImg);
    } 
    
    /* current image is now previous image, as we advance in the list of images */
    eval("previousImage_" + index + " = currentImage_" + index + ";");
    eval("currentImage_" + index + "+=1;");
    var curImg = eval("currentImage_" + index);
    if (curImg >= thisSlides.length) {
        /* start over from first image if we cycled through all images in the list */
        eval("currentImage_" + index + "=0;");
    }

    //alert(slides[currentImage].id);
    /* start the crossfade */
    eval("opacity_" + index + " = 0;");
    eval("fadeInProgress_" + index + " = false;");
    var playing = eval("playing_" + index);
    if (playing) {
        eval("timer_" + index + " = window.setTimeout('crossfade(' + opacity_" + index + " + ', " + index + ")', showImgFor_" + index + ");");
    }
}



function MoveControls(show, index) {
    var ctls = document.getElementById("slidectrls_" + index);
    var ctlTop = eval("ctlTop_" + index);
    var ctlPos = eval("ctlPos_" + index);
    if (show) {

        if (ctlPos > ctlTop) {
            eval("showInProgress_" + index + " = true;");
            eval("hideInProgress_" + index + " = false;");
            eval("ctlPos_" + index + " -= 2;");
            eval("ctltimer_" + index + " = window.setTimeout('MoveControls(true, " + index + ")', 10);");
        }
        else {
            eval("showInProgress_" + index + " = false;");
            eval("clearTimeout(ctltimer_" + index + ");");
        }
    }
    else {
        if (ctlPos < (ctlTop + 38)) {
            eval("showInProgress_" + index + " = false;");
            eval("hideInProgress_" + index + " = true;");
            eval("ctlPos_" + index + " += 2;");
            eval("ctltimer_" + index + " = window.setTimeout('MoveControls(false, " + index + ")', 10);");
        }
        else {
            eval("hideInProgress_" + index + " = false;");
            eval("clearTimeout(ctltimer_" + index + ");");
        }
    }

    var ctlPos = eval("ctlPos_" + index);
    if (ctls != null) {
        ctls.style.marginTop = ctlPos + "px";
    }

}




function ShowControls(index) {
    var showInProgress = eval("showInProgress_" + index);
    if (!showInProgress) {
        eval("clearTimeout(ctltimer_" + index + ");");
        MoveControls(true, index);
    }
}



function HideControls(index) {
    var hideInProgress = eval("hideInProgress_" + index);
    if (!hideInProgress) {
        eval("clearTimeout(ctltimer_" + index + ");");
        MoveControls(false, index);
    }
}



function NextSlide(index) {
    eval("window.clearTimeout(timer_" + index + ");");
//    var curImg = eval("currentImage_" + index);
//    var prevImg = eval("previousImage_" + index);
    PrepareNextSlide(false, index);
//    document.getElementById("slidedesc_" + index + "_" + prevImg).className = "slidedescunsel";
//    document.getElementById("slidedesc_" + index + "_" + curImg).className = "slidedesc";
//    //slidedesc.innerHTML = "cur = " + currentImage + ", prev = " + previousImage;
}



function ShowSlide(index, slideNo) {
    var fadeInProgress = eval("fadeInProgress_" + index);
    if (fadeInProgress == true) 
    {
        return;
    }
    eval("window.clearTimeout(timer_" + index + ");");
    eval("currentImage_" + index + " = " + slideNo + ";");
    PrepareNextSlide(false, index);
    //    document.getElementById("slidedesc_" + index + "_" + prevImg).className = "slidedescunsel";
    //    document.getElementById("slidedesc_" + index + "_" + curImg).className = "slidedesc";
    //    //slidedesc.innerHTML = "cur = " + currentImage + ", prev = " + previousImage;
}



function PrevSlide(index) {
    var fadeInProgress = eval("fadeInProgress_" + index);
    if (fadeInProgress == true) {
        return;
    }
    eval("window.clearTimeout(timer_" + index + ");");
    var thisSlides = eval("slides_" + index);
    if (fadeInProgress == true) {
        eval("fadeInProgress_" + index + " = false;");
        eval("SetOpacity(currentImage_" + index + ", 0, thisSlides);");
    }

    eval("currentImage_" + index + "-=2;");
    var currentImage = eval("currentImage_" + index);
    if (currentImage == -1) {
        eval("currentImage_" + index + " = thisSlides.length - 1;");
    }
    else if (currentImage < -1) {
        eval("currentImage_" + index + " = thisSlides.length - 2;");
    }
    eval("SetOpacity(previousImage_" + index + ", 0, thisSlides);");
    PrepareNextSlide(false, index);
    //slidedesc.innerHTML = "cur = " + currentImage + ", prev = " + previousImage;
}




function StartStop(index) {
    //slidedesc.innerHTML = "cur = " + currentImage + ", prev = " + previousImage;
    eval("playing_" + index + "= !playing_" + index + ";");
    var link = document.getElementById("startstop_" + index);
    var img = document.getElementById("playpause_" + index);

    //link.innerHTML = playing ? "Pause" : "Play";
    var playing = eval("playing_" + index);
    eval("img.src = playing ? pauseImage_" + index + ".src : playImage_" + index + ".src;");

    if (playing) {
        eval("timer_" + index + " = window.setTimeout('crossfade(0, " + index + ")', showImgFor_" + index + ");");
    }
    else {

        var fadeInProgress = eval("fadeInProgress_" + index);
        if (!fadeInProgress) {
            eval("window.clearTimeout(timer_" + index + ");");
        }
    }
}


function ShowVideo(id) 
{
    var videowidth = 560;
    var videoheight = 340;
    var div = document.getElementById("video_" + id);
    var winWidth = GetWindowSize(true);
    var winHeight = GetWindowSize(false);
    //posx = (winWidth / 2);
    //posy = GetScrollTop() + (winHeight / 2) - 85; //100-15
    div.style.display = "block";

    var posx = (winWidth / 2) - (videowidth / 2);
    //var posy = (winHeight / 2) - (videoheight / 2);
    var posy = GetScrollTop() + (winHeight / 2) - (videoheight / 2); //100-15
    div.style.top = posy + "px";
    div.style.left = posx + "px";
    //alert(posx + ", " + posy);

    var screen = document.getElementById("screen");
    var height = FindHeight(screen);
    var bg = document.getElementById("videobg");
    bg.style.display = "block";
    bg.style.height = height + "px";
}

function HideVideo(id) 
{
    var div = document.getElementById("video_" + id);
    div.style.display = "none";
    var bg = document.getElementById("videobg");
    bg.style.display = "none";
}

