﻿// Load the thumbs on page load.
var getimages = null;
var autoPlay = null;
var splitchar = ";";
var imageratio = 1.5;

// There is a call right on the page for getImages(true) meaning that the page first loads
getimages = setTimeout("getImages(false);", 500);
//getimages = setTimeout("getImages(false);", 3000);
//getimages = setTimeout("getImages(false);", 5000);
var autoPlay = setTimeout("autoplay();", 6000); // Start playing the slideshow after 6 seconds

// Image index.
var ivar = null;

// Capture the current transition to set back to 
// when random transition mode is checked and play stops.
var currtrans = null;

// Current photo
var currpic = 0;

// Timer used in the slideshow.
var timerRunning = false;
var timerID = null;

// Store the previous thumbs indexes
prevfirstthumb = null;
prevlastthumb = null;

// Store images height and width
imgWidth = null;
imgHeight = null;
// Preview
imgWidthP = null;
imgHeightP = null;

// ******** SET VARIABLE PROPERTIES STARTING HERE ********

    // Preload images ("yes" or "no"). Currently not in use.
    var preloadimg = "yes";

    // Set image border width.
    var imgborderwidth = 0;

    // Index of the first thumbnail
    var firstindex = 0;
    var firstthumb = firstindex; 
    
    // This number is how many thumbnails show up above the photo.
    var lastthumb = 5; //this number MUST BE LARGER than the firstthumb variable!

    // Set the location of the photos
    var piclocation = "images/";
    
    var framelocation = "images/";

    // Width for all photos
    var fixedwidth = "550px";

    // Set the size of the thumbnail and preview.
    // Whichever is larger (width or height) of the image will be resized to this number.
    var previewsize = parseInt(fixedwidth); //px
    var thumbnailsize = 100; //px

    // Set a fixed height to avoid page shifting 
    // (this will show space below photos that have a smaller height)
    var fixedheight = true;

    // How many thumbnails to show on the page 
    // dynamically set with variables from above (i.e. 5-1 = 4 thumbnails).
    var thumbcount = (lastthumb-firstthumb)+1;

    // How many images in the array before showing the next page arrow
    var nextpageindex = thumbcount-lastthumb;
    var nextpagearrowstimeout = 110;

    // Set optional link target to be added to all images with a link
    // such as '_blank' for a new window.
    var optlinktarget = "_blank";
    
    var imgcount = 0;
    var dynimages = null;
    var myImages = null;
    
    var masterpageID = "ctl00_ContentPlaceHolder1_";
    
        //  ~*~*~  v  IMAGES  v  ~*~*~  //

        // Specify IMAGE paths and optional link (set link to "" for no link) and alt text (description):
        // Create a new array to store the images in. Manually creating this array for now.
        // Example: dynimagesthumb[#] = ['src', 'href', 'alt', 'widthpx' or fixedwidth];
//        var dynimages = new Array();
//        dynimages[0] = ["","","0",fixedwidth];
//        dynimages[1] = ["","","1",fixedwidth];
//        dynimages[2] = ["","","2",fixedwidth];

        function getImages(x)
        {
            // Preload the images
            if (preloadimg=="yes" && x)
            {
                setTimeout("preloadImages(5)",1000);
            }
            
            // Stop the page logo from fading in, just show it
            clearTimeout(pagecheck);
            document.getElementById('header_logo_td').innerHTML = "<img id='header_logo_img' src='images/Logo.jpg' alt='The Pentecostals of Billings' />";
            
            myImages = document.getElementById(masterpageID + 'HiddenFieldSlideshow').value;
            dynimages = myImages.split('|');   
                     
            //number of pictures in the library dynamically set.
            imgcount = dynimages.length;
            
            // Reset the thumbcount and nextpageindex based on how many images were found            
            if(dynimages.length <= lastthumb)
            {
                thumbcount = dynimages.length + 1;
                nextpageindex = dynimages.length;
                lastthumb = dynimages.length;
                setTimeout("document.getElementById('lastthumb').disabled = 'disabled'", nextpagearrowstimeout);
            }
            
            if(dynimages.length > nextpageindex)
            {
                setTimeout("document.getElementById('prevthumb').style.display = ''", nextpagearrowstimeout);
                setTimeout("document.getElementById('nextthumb').style.display = ''", nextpagearrowstimeout);
            }

            setTimeout("pageloadstart(" + x + ")",100);
        }
        
        function pageloadstart(x)
        {
            if(document.getElementById('dynloadarea') != null)
            {
                pageloadthumbs(); 
                pageloadframe(x); // x is true if page is first loading
                setTimeout("modifyimage('dynloadarea', 0)",100);
            }
            document.getElementById('thumbcountnew').value = thumbcount-1;
        }
        // Specify the images to load for the frame.
        function pageloadframe(x)
        {
            document.getElementById('frametop').style.backgroundImage = "url('" + framelocation + "frame/frametoppro.gif')";
            document.getElementById('frametopL').style.backgroundImage = "url('" + framelocation + "frame/frametopLpro.gif')";
            document.getElementById('frametopR').style.backgroundImage = "url('" + framelocation + "frame/frametopRpro.gif')";
            document.getElementById('frameright').style.backgroundImage = "url('" + framelocation + "frame/framerightpro.gif')";
            document.getElementById('framebottom').style.backgroundImage = "url('" + framelocation + "frame/framebottompro.gif')";
            document.getElementById('framebottomL').style.backgroundImage = "url('" + framelocation + "frame/framebottomLpro.gif')";
            document.getElementById('framebottomR').style.backgroundImage = "url('" + framelocation + "frame/framebottomRpro.gif')";
            document.getElementById('frameleft').style.backgroundImage = "url('" + framelocation + "frame/frameleftpro.gif')";
            
            // Starting Image and description.
            //document.getElementById('dynloadarea').innerHTML = "<img id='imgpreview' src='images/slideshow/slideshow.gif' />";
            document.getElementById('description').innerHTML = "";

            if(!x)
            {
                // Have to set width of this area to pic width in order to see transitions
                scaleItPreview(previewsize, 0);
            }
        }

        //  ~*~*~  ^  IMAGES  ^  ~*~*~  //

// ******** END SET VARIABLE PROPERTIES STARTING HERE ********

// ******** NOTHING PAST HERE NEEDS TO BE CHANGED ********

function preloadImages(y)
{
    var divImages = document.getElementById('preloadImages');
    for (x=y; x<dynimages.length; x++)
    {
        var myimage=new Image();
        myimage.src=dynimages[x].split(splitchar)[0];
        myimage.style.width="10px";
        myimage.style.height="10px";
        divImages.appendChild(myimage);
    }
}

function pageloadthumbs()
{
    getthumbs(firstthumb, lastthumb);
}

function getthumbs(firstthumb, lastthumb)
{    
    // Set the height of the thumbnail tr
    document.getElementById('trSlideshowThumbs').style.height = thumbnailsize + 10;
    
    var text = "";
    var p = firstthumb;
    var x = firstthumb;    
    
    var twidth = thumbnailsize + "px";
    var theight = thumbnailsize + "px";
    var size = thumbnailsize;
    // create an array of images
    var tnail = document.getElementsByTagName('img');
    if(document.getElementById('chkthumbsize').checked == true && document.getElementById('txtthumbsize').value != "")
    {
        size = document.getElementById('txtthumbsize').value;
    }
    for (i=0; i < tnail.length; i++) 
    {
        getImgSize(tnail[i].src);
        if(tnail[i].className == 'tnail')
        {
            twidth = parseInt(size) + "px";
            theight = Math.round((parseInt(size))/1.5) + "px";
        }
    }
    
    for (x; x < lastthumb; x++)
    {
        text += "<img id='Img" + x + "' width='" + twidth + "' height='" + theight + "' border='0' class='tnail' alt='' src='' onclick='clearTimeout(autoPlay);clearTimeout(getimages);stopshow();changeimage(this," + x + ");' onmouseover=\"this.style.cursor = 'pointer';\"/>&nbsp;";
    }
    document.getElementById('thumbs').innerHTML = "";
    document.getElementById('thumbs').innerHTML = text;
    for (p; p < lastthumb; p++)
    {
       // document.getElementById('Img' + p).src = dynimages[p][0];
       // document.getElementById('Img' + p).alt = dynimages[p][2];
        document.getElementById('Img' + p).src = dynimages[p].split(splitchar)[0];
        document.getElementById('Img' + p).alt = dynimages[p].split(splitchar)[2];
    }
    
    // Resize the original pictures into thumbnail size with the specified width/height.
    scaleItThumb(thumbnailsize);
    setTimeout("scaleItThumb(thumbnailsize)",100); // Try again in case the browser is slow
    setTimeout("scaleItThumb(thumbnailsize)",500); // Try again in case the browser is slow
    setTimeout("scaleItThumb(thumbnailsize)",1000); // Try again in case the browser is slow
    setTimeout("scaleItThumb(thumbnailsize)",5000); // Try again in case the browser is slow
    
    // Try setting the highlighted photo if in the current set of thumbnails
    try
    {
        // Highlight selected images
        if(document.getElementById('photoid').innerHTML >= firstthumb && document.getElementById('photoid').innerHTML <= lastthumb)
        {            
            document.getElementById('Img' + document.getElementById('photoid').innerHTML).className = "tnailselected";
        }
    }
    catch(e)
    {
        // Do Nothing
    }
}

function scaleItThumb(size) 
{
    // create an array of images
    var tnail = document.getElementsByTagName('img');
    if(document.getElementById('chkthumbsize').checked == true && document.getElementById('txtthumbsize').value != "")
    {
        size = document.getElementById('txtthumbsize').value;
    }
    for (i=0; i < tnail.length; i++) 
    {
        getImgSize(tnail[i].src);
        if(imgWidth >= imgHeight && imgWidth > parseInt(size))
        {
            if(tnail[i].className == 'tnail')
            {
                tnail[i].style.width = parseInt(size) + "px";
                tnail[i].style.height = Math.round((parseInt(size))/(imgWidth/imgHeight)) + "px";
            }
        }
        else if(imgWidth < imgHeight && imgHeight > parseInt(size))
        {
            if(tnail[i].className == 'tnail')
            {
                tnail[i].style.height = parseInt(size) + "px";
                tnail[i].style.width = Math.round((parseInt(size))/(imgHeight/imgWidth)) + "px";
            }
        }
    }
}

function scaleItPreview(size, imgindex) 
{
    imgWidthP = null;
    imgHeightP = null;
    //getImgSizePreview(dynimages[imgindex][0]);
    getImgSizePreview(dynimages[imgindex].split(splitchar)[0]);
    
    if(document.getElementById('chkpreviewsize').checked == true && document.getElementById('txtpreviewsize').value != "")
    {
        size = document.getElementById('txtpreviewsize').value;
    }
    
    var slideshowHeight = document.getElementById('slideshowdiv').style.height;
    
    // Image is wider than it is taller
    if(imgWidthP > imgHeightP)
    {
//            window.status="W:" + imgWidthP + " H:" + imgHeightP;
//        // To avoid page shift, check the width to height ratio
//        // If larger than what is set above resize the image
//        if((imgWidthP / imgHeightP) < imageratio && (imgWidthP / imgHeightP) < 1.33)
//        {
//            imageratio = (imgWidthP / imgHeightP);
//            document.getElementById('slideshowdiv').style.height = (parseInt(size)-(30*imageratio)) + "px";
//        }
//        else
//        {
//            imageratio = (imgWidthP / imgHeightP);
//        }
//                window.status=imageratio;
        
        //document.getElementById('imgpreview').style.width = parseInt(size) + "px";
        document.getElementById('imgpreview').style.height = parseInt(size)/imageratio + "px";
        document.getElementById('imgpreview').style.width = parseInt(document.getElementById('imgpreview').style.height)*(imgWidthP/imgHeightP) + "px";
        //document.getElementById('dynloadarea').style.width = parseInt(size) + "px";
        //document.getElementById('dynloadarea').style.height = parseInt(size)/imageratio + "px";
        document.getElementById('dynloadarea').style.height = parseInt(size)/imageratio + "px";
        document.getElementById('dynloadarea').style.width = parseInt(document.getElementById('imgpreview').style.height)*(imgWidthP/imgHeightP) + "px";
        document.getElementById('slideshowdiv').style.width = (parseInt(size)+40) + "px";
        
//        // Set the height the same as the heighest photo to avoid page shifting.
//        if(document.getElementById('chkfixedsize').checked == true)
//        {
//            document.getElementById('slideshowheight').style.height = "45px"; //45 for Sharepoint.
//        }
    }
    // Image is the same width as it is tall
    else if(imgWidthP == imgHeightP)
    {
//            window.status="W:" + imgWidthP + " H:" + imgHeightP;
//        //imageratio = (imgWidthP / imgHeightP); 
//            window.status=imageratio;
        
        document.getElementById('imgpreview').style.width = parseInt(size)/imageratio + "px";
        document.getElementById('imgpreview').style.height = parseInt(size)/imageratio + "px";
        document.getElementById('dynloadarea').style.width = parseInt(size)/imageratio + "px";
        document.getElementById('dynloadarea').style.height = parseInt(size)/imageratio + "px";
        document.getElementById('slideshowdiv').style.width = (parseInt(size)+40) + "px";
        
//        // Set the height the same as the heighest photo to avoid page shifting.
//        if(document.getElementById('chkfixedsize').checked == true)
//        {
//            document.getElementById('slideshowheight').style.height = "45px"; //45 for Sharepoint.
//        }
    }
    // Image is taller than it is wide
    else
    {
//            window.status="W:" + imgWidthP + " H:" + imgHeightP;
//        // To avoid page shift, check the width to height ratio
//        // If larger than what is set above resize the image
//        if((imgHeightP / imgWidthP) > imageratio)
//        {
//            imageratio = (imgHeightP / imgWidthP);
//                window.status=imageratio; 
//        } 
        
        document.getElementById('imgpreview').style.height = parseInt(size)/imageratio + "px";
        document.getElementById('imgpreview').style.width = (parseInt(size)/imageratio)/imageratio + "px";
        document.getElementById('dynloadarea').style.height = parseInt(size)/imageratio + "px";
        document.getElementById('dynloadarea').style.width = (parseInt(size)/imageratio)/imageratio + "px";
        document.getElementById('slideshowdiv').style.width = (parseInt(size)+40) + "px";
                
//        // Set the height the same as the heighest photo to avoid page shifting.
//        if(document.getElementById('chkfixedsize').checked == true)
//        {
//            document.getElementById('slideshowheight').style.height = "";
//        }
    }
}

function getImgSize(imgSrc)
{
    var newImg = new Image();
    newImg.src = imgSrc;
    imgHeight = newImg.height;
    imgWidth = newImg.width;
}

function getImgSizePreview(imgSrc)
{
    var newImgP = new Image();
    newImgP.src = imgSrc;
    imgHeightP = newImgP.height;
    imgWidthP = newImgP.width;
}

function returnimgcode(theimg)
{
    var imghtml="";
//    if (theimg[1]!="")
//        imghtml='<a href="'+theimg[1]+'" target="'+optlinktarget+'">';
//        imghtml+='<img id="imgpreview" src="'+theimg[0]+'" border="'+imgborderwidth+'" alt="'+theimg[2]+'" >';
//    if (theimg[1]!="")
//        imghtml+='</a>';
//        return imghtml;
    var widthP = "550px";
    var heightP = "367px";    
    getImgSizePreview(theimg.split(splitchar)[0]);
    if(imgWidthP >= imgHeightP && imgWidthP > parseInt(widthP))
    {
        heightP = Math.round((parseInt(widthP))/(imgWidthP/imgHeightP)) + "px";        
    }
    else if(imgWidthP < imgHeightP && imgHeightP > imgWidthP)
    { 
        widthP = Math.round((parseInt(heightP))/(imgHeightP/imgWidthP)) + "px";       
    }
    else
    {
        widthP = imgWidthP + "px";
        heightP = Math.round((parseInt(widthP))/(imgWidthP/imgHeightP)) + "px";
    }
    
    if (theimg.split(splitchar)[1]!="")
        imghtml='<a href="'+theimg.split(splitchar)[1]+'" target="'+optlinktarget+'">';
        imghtml+='<img width="' + widthP + '" height="' + heightP + '" id="imgpreview" src="'+theimg.split(splitchar)[0]+'" border="'+imgborderwidth+'" alt="'+theimg.split(splitchar)[2]+'" >';
    if (theimg.split(splitchar)[1]!="")
        imghtml+='</a>';
        return imghtml;
}

function clearTnailSelected()
{
    // Deselect all images
    for (x=firstthumb; x<lastthumb; x++)
    {
        document.getElementById('Img' + x).className = "tnail";
    }
}

function modifyimage(loadarea, imgindex)
{
    clearTnailSelected();
    try
    {
        // Highlight selected images
        document.getElementById('Img' + imgindex).className = "tnailselected";
    }
    catch(e)
    {
        // Do Nothing
    }
    
    var transition = document.getElementById('trans').value;
    document.getElementById('tdPhotoIndex').innerHTML = (imgindex + 1) + " of " + dynimages.length;

    var altprop = "";
    if(transition == "GradientWipe"){altprop = " GradientSize=1.0";}
    else {altprop = "";}
    var duration = document.getElementById('dur').value;
    var filterstring = "progid:DXImageTransform.Microsoft." + transition + "(Duration=" + duration + altprop + ")";

    if (document.getElementById)
    {
        var imgobj=document.getElementById(loadarea);
        if (imgobj.filters && window.createPopup)
        {
            imgobj.style.filter=filterstring;
            imgobj.filters[0].Apply();
        }
        imgobj.innerHTML=returnimgcode(dynimages[imgindex]);
        if (imgobj.filters && window.createPopup)
        {
            imgobj.filters[0].Play();
            //document.getElementById("description").innerHTML = dynimages[imgindex][2];
            document.getElementById("description").innerHTML = dynimages[imgindex].split(splitchar)[2];
            scaleItPreview(previewsize, imgindex);
            return false;
        }
    }
}

function changeimage(obj, img)
{
    if(document.getElementById('play').value == '>')
    {
        modifyimage('dynloadarea', img); 
        document.getElementById('photoid').innerHTML = img;            
        obj.style.cursor = 'pointer';
    }
    else
    {
        return;
    }
}

function autoplay()
{    
    document.getElementById("play").value = "||";
    startshow();
}

// Call this function to start the show to 
// resume it where is left off. 
function startshow()
{    
    currtrans = document.getElementById('trans').selectedIndex;
    var currphoto = document.getElementById('photoid').innerHTML;
    
    // First stop the timer
    stopshow();
    
    // Disable buttons
    document.getElementById('prev').disabled = 'disabled';
    document.getElementById('next').disabled = 'disabled';
    
    // Get the next photo to show.
    var nextphoto = parseInt(currphoto) +1;
    if(nextphoto > dynimages.length-1)
    {
        nextphoto = 0;
    }    
    playshow('dynloadarea', nextphoto);
}

function playshow(loadarea, ivar)
{    
    // Clear any startup timeouts 
    clearTimeout(autoPlay);
    clearTimeout(getimages);
    
    // Set the current photo globally
    currpic = ivar;
    
    if(document.getElementById('randomphoto').checked == true)
    {
        // Randomize number for photo index
        var ivarprev = ivar;
        while(parseInt(ivar) == parseInt(ivarprev))
        {
            ivar=Math.floor(Math.random()*dynimages.length);
        }
        if(parseInt(ivar) == parseInt(ivarprev))
        {
            alert('equal' + ivar);
        }
    }
    document.getElementById('tdPhotoIndex').innerHTML = (ivar + 1) + " of " + dynimages.length;

    //Optionally, change 1.0 and 0.7 below to affect Wipe gradient size and duration in seconds in IE5.5+ : GradientWipe(GradientSize=1.0 Duration=0.7)";
    var transition = document.getElementById('trans');
    if(document.getElementById('randomtrans').checked == true)
    {
        transition.selectedIndex = Math.floor(Math.random()*18);
    }
    transition = transition.value;
    //document.getElementById('dynloadarea').style.width = dynimages[ivar][3]; //Causes hickups.!?!
    var altprop = "";
    if(transition == "GradientWipe"){altprop = " GradientSize=1.0";}
    else {altprop = "";}
    var duration = document.getElementById('dur').value;
    var filterstring = "progid:DXImageTransform.Microsoft." + transition + "(Duration=" + duration + altprop + ")";

    if(ivar == imgcount)
    {
        ivar = 0;
    }
    
    if (document.getElementById)
    {
        var imgobj=document.getElementById(loadarea);
        if (imgobj.filters && window.createPopup)
        {
            imgobj.style.filter=filterstring;
            imgobj.filters[0].Apply();
        }
        imgobj.innerHTML=returnimgcode(dynimages[ivar]);
        if (imgobj.filters && window.createPopup)
        {
            imgobj.filters[0].Play(); 
            //document.getElementById("description").innerHTML = dynimages[ivar][2];
            document.getElementById("description").innerHTML = dynimages[ivar].split(splitchar)[2];
            scaleItPreview(previewsize, ivar);
            document.getElementById('photoid').innerHTML = ivar; 
            
            // Clear highlights    
            clearTnailSelected();
            try
            {
                // Highlight selected images
                if(ivar >= lastthumb)
                {
                    nextthumbs();
                }
                if(ivar == 0)
                {
                    firstthumbs();
                }
                document.getElementById('Img' + ivar).className = "tnailselected";
            }
            catch(e)
            {
                // Do Nothing
            }
            
            // Add one to get the next photo
            ivar++;
            if(ivar > dynimages.length-1)
            {
                ivar = 0;
            } 
            // call function recursively immediately (must use setTimeout to avoid overflow)
            timerID = setTimeout("playshow('dynloadarea', " + ivar + ")", document.getElementById('seconds').value * 1000); //setInterval
            // timer is currently running
            timerRunning = true;          
            return false;
        }
    }
}

function stopshow() 
{
    // if the timer is currently running
    if (timerRunning)
    {
        // clear the current timeout (stop the timer)
        clearTimeout(timerID);

        // assign false to global variable because timer is not running 
        timerRunning = false;
        
        // Enable buttons
        document.getElementById('prev').disabled = '';
        document.getElementById('next').disabled = '';

	// Set the transition back to what it was
	//document.getElementById('trans').selectedIndex = currtrans;
	document.getElementById('trans').selectedIndex = 0;

	// Change the play button to stop
	document.getElementById('play').value = '>';
    }
}

function prevphoto()
{            
    var currphoto = parseInt(document.getElementById('photoid').innerHTML) -1;
    if(currphoto < 0)
    {
        currphoto = dynimages.length -1;
    }
    
    try
    {
        if(currphoto < firstthumb)
        {
            prevthumbs();
        }
        if(currphoto > lastthumb)
        {
            lastthumbs();
        }
    }
    catch(e)
    {
        // Do Nothing
    }
    
    modifyimage('dynloadarea', currphoto);
    document.getElementById('photoid').innerHTML = currphoto;
}

function nextphoto()
{
    var currphoto = parseInt(document.getElementById('photoid').innerHTML) +1;
    if(currphoto == dynimages.length)
    {        
        currphoto = 0;
    }    
    
    try
    {
        if(currphoto >= lastthumb)
        {
            nextthumbs();
        }
        if(currphoto < firstthumb)
        {
            firstthumbs();
        }
    }
    catch(e)
    {
        // Do Nothing
    }
    
    modifyimage('dynloadarea', currphoto);
    document.getElementById('photoid').innerHTML = currphoto;
} 

function prevthumbs()
{
    if(firstthumb-thumbcount <= 0)
    {
	    firstthumb = firstindex;
	    lastthumb = thumbcount-1;
        document.getElementById('prevthumb').disabled = "disabled";
        document.getElementById('firstthumb').disabled = "disabled";
        document.getElementById('nextthumb').disabled = "";
        document.getElementById('lastthumb').disabled = "";
    }
    else
    {
	    firstthumb = (firstthumb - thumbcount) + 1;
	    lastthumb = (firstthumb + thumbcount)-1;
        prevfirstthumb = firstthumb - thumbcount;
        prevlastthumb = firstthumb + thumbcount;
        document.getElementById('nextthumb').disabled = "";
        document.getElementById('lastthumb').disabled = "";
        window.status="else"
    }   
    getthumbs(firstthumb, lastthumb);
}

function nextthumbs()
{   
    // Clear any startup timeouts 
    clearTimeout(autoPlay);
    clearTimeout(getimages);
    
    prevfirstthumb = firstthumb;
    prevlastthumb = lastthumb;

    if(lastthumb <= dynimages.length - thumbcount)
    {
        firstthumb = firstthumb + thumbcount-1;
        lastthumb = lastthumb + thumbcount-1;
        document.getElementById('prevthumb').disabled = "";
        document.getElementById('firstthumb').disabled = "";
            //window.status="if";
    }
    else
    {
        firstthumb = firstthumb + thumbcount-1;
        lastthumb = lastthumb + (dynimages.length - lastthumb);
        document.getElementById('nextthumb').disabled = "disabled";
        document.getElementById('lastthumb').disabled = "disabled";
        document.getElementById('prevthumb').disabled = "";
        document.getElementById('firstthumb').disabled = "";
            //window.status="else";
    }
    getthumbs(firstthumb, lastthumb);
}

function firstthumbs()
{
    firstthumb = firstindex;
    lastthumb = firstthumb + (thumbcount-1);
    getthumbs(firstthumb, lastthumb);
    document.getElementById('firstthumb').disabled = "disabled";
    document.getElementById('prevthumb').disabled = "disabled";
    document.getElementById('lastthumb').disabled = "";
    document.getElementById('nextthumb').disabled = "";
}

function lastthumbs()
{
    // Clear any startup timeouts 
    clearTimeout(autoPlay);
    clearTimeout(getimages);
    
    if((dynimages.length) % (thumbcount-1) != 0)
    {
        firstthumb = (dynimages.length - ((dynimages.length) % (thumbcount-1)));
    }
    else
    {
        firstthumb = dynimages.length - (thumbcount-1);
    }
    lastthumb = dynimages.length;

    getthumbs(firstthumb, lastthumb);
    document.getElementById('firstthumb').disabled = "";
    document.getElementById('prevthumb').disabled = "";
    document.getElementById('lastthumb').disabled = "disabled";
    document.getElementById('nextthumb').disabled = "disabled";
}

function adjustthumbs()
{
    var endthumb = parseInt(document.getElementById('thumbcountnew').value);
    if(dynimages.length < endthumb)
    {
        return;
    }
    if(endthumb <= dynimages.length+1 && endthumb > 2 && endthumb < 7)
    {
        firstthumb = 0;
        lastthumb = endthumb;
        thumbcount = lastthumb - firstthumb + 1;
        pageloadthumbs();
        document.getElementById('firstthumb').disabled = "disabled";
        document.getElementById('prevthumb').disabled = "disabled";
        if(document.getElementById('lastthumb'))
        {
            document.getElementById('lastthumb').disabled = "";
        }
        document.getElementById('nextthumb').disabled = "";
    }
    else
    {
        alert('Invalid Number [ >2 & <7 ]');
    }
    if(endthumb == dynimages.length)
    {
        document.getElementById('nextthumb').disabled = "disabled";
        document.getElementById('lastthumb').disabled = "disabled";
    }
    else
    {
        document.getElementById('nextthumb').disabled = "";
        document.getElementById('lastthumb').disabled = "";        
    }
}

function chkShow(obj, chk)
{
    if(obj.checked == true)
    {
        document.getElementById(chk).style.display = "";
    }
    else
    {
        document.getElementById(chk).style.display = "none";
    }
}

function showhideoptions(obj, checks)
{
    if(obj.innerHTML == "+")
    {
        document.getElementById(checks).style.display = "";
        obj.innerHTML = "-";
    }
    else
    {
        document.getElementById(checks).style.display = "none";
        obj.innerHTML = "+";
    }
}

function fixImgs(objid, maxW) 
{
    var pix=document.getElementById(objid).getElementsByTagName('img');
    document.getElementById(objid).style.display = "";
    for (i=0; i<pix.length; i++)
    {
        w=pix[i].width;
        h=pix[i].height;
        if (w > maxW)
        {
            f=1-((w - maxW) / w);
            if((w * f) < (h * f))
            {
                pix[i].width=w * f;
                pix[i].height=maxW;
            }
            else
            {
                pix[i].width=maxW;
                pix[i].height=h * f;
            }
        }
    }
}

function addRow(r)
{
    var root = r.parentNode;//the root
    var allRows = root.getElementsByTagName('tr');//the rows' collection
    var cRow = allRows[0].cloneNode(true)//the clone of the 1st row
    var cInp = cRow.getElementsByTagName('input');//the inputs' collection of the 1st row
    for(var i=0;i<cInp.length;i++)
    {   //changes the inputs' names (indexes the names)
        cInp[i].setAttribute('name',cInp[0].getAttribute('name')+'_'+(allRows.length+1))
    }
    var cSel = cRow.getElementsByTagName('select')[0];
    cSel.setAttribute('name',cSel.getAttribute('name')+'_'+(allRows.length+1));//change the selecet's name
    root.appendChild(cRow);//appends the cloned row as a new row
    //<td width="286"><input name="button" type="button" value="+" onclick="addRow(this.parentNode.parentNode)"></td>
}