﻿// JScript File

var interval = 2000;
var ptrShow;
var currentPicIndex = 0;

function getGridRowIndex(intInd)
{
    var rowIndex = intInd + "";
    if(rowIndex.length == 1)
        return "0" + intInd;
    else
        return rowIndex;
}

function resetSlideShow()
{
    clearInterval(ptrShow);
    ptrShow = null;
    currentPicIndex = 0;
    document.getElementById(playButtonId).value = 'Play';
    document.getElementById(playButtonId).onclick = function(){return startShow();};
    document.getElementById(pnlImageGallery).style.display = "";
    showCurrentImage();
    //setTimeout("showCurrentImage()", 2);
}

function HideImageGallery()
{
    document.getElementById(pnlImageGallery).style.display = "none";
}

function preparePicList()
{
    var intLoop = 0;
    var imgPic;
  
    for(intLoop = 2; intLoop < dlPicCnt + 2; intLoop++)
    {
        imgPic = document.getElementById(dlPics + '_ctl' + getGridRowIndex(intLoop) + '_ImgPath');
        
        if(imgPic != null)
        {
            picList[intLoop - 2] = imgPic.innerHTML.replace('_s', '_l').replace('_t', '_l');
        }
    }
  
  
   showCurrentImage();
   document.getElementById(playButtonId).value = 'Stop';
   document.getElementById(playButtonId).onclick = function(){stopShow();return false;};
   document.getElementById(prevButtonId).onclick = function(){stopShow();showPrevPic();return false;};
   document.getElementById(nextButtonId).onclick = function(){stopShow();showNextPic();return false;};
   stopShow();
    if(dlPicCnt <= 1)
    {
        document.getElementById(playButtonId).style.display='none';
        document.getElementById(nextButtonId).style.display='none';
        document.getElementById(prevButtonId).style.display='none';
        if(dlPicCnt == 0)
        {
            document.getElementById(imgLargePic).style.display='none';
        }
        
    }
}

function startShow()
{
    if(picList[0] != null)
    {
        document.getElementById('imgNextPic').src = picList[0];
    }
    if(picList[currentPicIndex] != null)
    {
        //document.getElementById(imgLargePic).src = picList[currentPicIndex];
        
        preLoadPrevNextPics();
        
        if(ptrShow == null)
            ptrShow = setInterval('showNextPic()', interval);
        
        if(currentPicIndex < dlPicCnt - 1)
            currentPicIndex = currentPicIndex + 1;
        else
            currentPicIndex = 0;
            
        document.getElementById(playButtonId).value =  'Stop';
        document.getElementById(playButtonId).onclick = function(){return stopShow();};
    }
    
    return false;
}

function preLoadPrevNextPics()
{
    if(picList[currentPicIndex - 1] != null)
        document.getElementById('imgPrevPic').src = picList[currentPicIndex - 1];
    else
        document.getElementById('imgPrevPic').src = picList[picList.length - 1];
        
    if(picList[currentPicIndex + 1] != null)
        document.getElementById('imgNextPic').src = picList[currentPicIndex + 1];
    else 
        document.getElementById('imgNextPic').src = picList[0];
}

function stopShow()
{
    clearInterval(ptrShow);
    ptrShow = null;
    document.getElementById(playButtonId).value = 'Play';
    document.getElementById(playButtonId).onclick = function(){return startShow();};
    return false;
}


function showNextPic()
{
    
    //stopShow();
    
    if(currentPicIndex < dlPicCnt - 1)
    {
        currentPicIndex = parseInt(currentPicIndex) + 1;
    }
    else
    {
        currentPicIndex = 0;
    }
     
    showCurrentImage();
    return false;
}

function showPrevPic()
{
    //stopShow();
    
    if(currentPicIndex > 0)
        currentPicIndex = currentPicIndex - 1;
    else
        currentPicIndex = dlPicCnt - 1;
        
    showCurrentImage();
    return false;
}

function showCurrentImage()
{
    if(picList[currentPicIndex] != null)
    {
        document.getElementById(imgLargePic).parentNode.style.height = "";
        document.getElementById(imgLargePic).parentNode.style.width = "";
        document.getElementById(imgLargePic).style.height = "";
        document.getElementById(imgLargePic).style.width = "";
        document.getElementById(imgLargePic).src = picList[currentPicIndex];
        
        var height = document.getElementById(imgLargePic).parentNode.offsetHeight;
        var width = document.getElementById(imgLargePic).parentNode.offsetWidth;
        
        if(height/480 > width/640)
        {
            document.getElementById(imgLargePic).style.height = "480px";
        }
        else
        {
            document.getElementById(imgLargePic).style.width = "640px";
        }
        
        preLoadPrevNextPics();
    }
}

