﻿// JScript File
var FirstActiveImage = true; 
var CurrentlyLoading = false; 

function PrevImage() 
{ 
    try 
    { 
        var oldIndex = CurrentIndex; 
        CurrentIndex--; 
        
        if (CurrentIndex < 0) 
            CurrentIndex = CurrentIndex + ShowImages.length; 
            
        SetThumbIndex(oldIndex, CurrentIndex); 
        $('SSPreload').alt = ShowText[CurrentIndex]; 
        $('SSPreload').src = ShowImages[CurrentIndex];
    } 
    catch(e) { alert(e.message); } 
};

function NextImage() 
{
    try 
    { 
        var oldIndex = CurrentIndex; 
        CurrentIndex++; 
        
        if (CurrentIndex >= ShowImages.length) 
            CurrentIndex = CurrentIndex - ShowImages.length; 
        
        SetThumbIndex(oldIndex, CurrentIndex);                 
        $('SSPreload').alt = ShowText[CurrentIndex]; 
        $('SSPreload').src = ShowImages[CurrentIndex]; 
    } 
    catch(e) { alert(e.message); } 
};

function SetActiveImage(index, image) 
{ 
    try 
    { 
        if (FirstActiveImage == true)
        {
            FirstActiveImage = false;
            ConfigSlideShowAjax(); 
        }
        
        if (index == -1) 
        { 
            $('SSImage').parentNode.style.paddingTop = ((MainMinHeight - ShowHeights[CurrentIndex]) / 2) + "px"; 
            $('SSImage').alt = image.alt; 
            $('SSImage').src = image.src; 
        } 
        else 
        { 
            SetThumbIndex(CurrentIndex, index); 
            CurrentIndex = index; 
            $('SSPreload').CurrentIndex = CurrentIndex; 
            $('SSPreload').alt = ShowText[CurrentIndex];
            $('SSPreload').src = ShowImages[CurrentIndex]; 
            image.parentNode.className = "SSThumbSel"; 
        } 
    } 
    catch(e) { alert(e.message); } 
};

function ssiSwap(image, file, over) 
{ 
    try 
    { 
        var path = image.src.substring(0, image.src.lastIndexOf('/') + 1) + file; 
        if (over) 
            path = path + '-over'; 
        
        path = path + '.gif'; 
        image.src = path; 
    } 
    catch(e) { alert(e.message); } 
};


function SetThumbIndex(oldIndex, newIndex) 
{ 
    try 
    { 
        $('thumb_' + oldIndex).className = 'SSThumb'; 
        $('thumb_' + newIndex).className = 'SSThumbSel'; 
        ShowThumbsPageForIndex(newIndex); 
    } 
    catch(e) { alert(e.message); } 
};

function PrevThumbsPage() 
{ 
    try 
    { 
        if (CurrentThumbsPage > 0) 
        { 
            CurrentThumbsPage--; 
            ShouldLoadNextPage(CurrentThumbsPage * ThumbsPerPage);
            $('ThumbsPageScroll').style.marginLeft = (-CurrentThumbsPage * ThumbsPageSize) + "px"; 
        } 
        
        SetThumbPageButtons(false, true); 
    } 
    catch(e) { alert(e.message); } 
};

function NextThumbsPage() 
{ 
    try 
    { 
        if (CurrentThumbsPage < ThumbsPages) 
        {
            CurrentThumbsPage++;             
            ShouldLoadNextPage(CurrentThumbsPage * ThumbsPerPage);
            $('ThumbsPageScroll').style.marginLeft = (-CurrentThumbsPage * ThumbsPageSize) + "px"; 
        } 
        
        SetThumbPageButtons(true, false); 
    } 
    catch(e) { alert(e.message); } 
};

function SetThumbPageButtons(forcePrev, forceNext) 
{ 
    try 
    { 
        if (forceNext || CurrentThumbsPage >= ThumbsPages) 
        { 
            ssiSwapT($(NextThumbID), 'ThumbsNextPage', false, true); 
        } 
        
        if (forcePrev || CurrentThumbsPage <= 0) 
        { 
            ssiSwapT($(PrevThumbID), 'ThumbsPrevPage', false, false); 
        } 
    } 
    catch(e) { alert(e.message); } 
};

function ShowThumbsPageForIndex(index) 
{ 
    try 
    { 
        var page = (index / ThumbsPerPage) | 0; 
       
        ShouldLoadNextPage(page * ThumbsPerPage);
        
        CurrentThumbsPage = page; 
        $('ThumbsPageScroll').style.marginLeft = (-CurrentThumbsPage * ThumbsPageSize) + "px"; 
        SetThumbPageButtons(true, true); 
    } 
    catch(e) { alert(e.message); } 
};

function ShouldLoadNextPage(index)
{        
    if ($("thumb_" + index) == null)
    {
        //alert("Load from " + index); 
        TryGetSlideShowData(index); 
    }
    else if (index < TotalNumberOfThumbs) 
    {
        var endIndex = index; 
        for (var i = 0; i < 16; i++)
        {
            endIndex = endIndex + ThumbsPerPage; 
            
            if (endIndex < TotalNumberOfThumbs) 
            {
                if ($("thumb_" + endIndex) == null)
                {
                    TryGetSlideShowData(endIndex);                      
                    return; 
                }
            }
        }
    }
}; 

function TryGetSlideShowData(index)
{
    if (!CurrentlyLoading)
    {
        CurrentlyLoading = true; 
        GetSlideShowData(index); 
    }       
};

function ssiSwapT(image, file, over, next) 
{ 
    try 
    { 
        var src = image.src; 
        var path = src.substring(0, src.lastIndexOf('/') + 1) + file; 
        
        if ((next && CurrentThumbsPage < ThumbsPages) || (!next && CurrentThumbsPage > 0)) 
        { 
            if (over) path = path + '-over'; 
        } 
        else 
        { 
            path = path + '-none'; 
        } 
        
        path = path + '.gif'; 
        image.src = path; 
    } 
    catch(e) { alert(e.message); } 
};


function ConfigSlideShowAjax()
{
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_pageLoaded(SlideShowLoadChunk_Callback);
    
    TryGetSlideShowData(ShowImages.length); 
};

function SlideShowLoadChunk_Callback(sender, args)
{
    
    var value = ""; 
    
    if(document.all)
        value = $(ArraysPlaceholderID).innerText;     
    else 
        value = $(ArraysPlaceholderID).textContent;     

    if (value != null && value != "")
    {        
        eval(value); 
                
        CurrentlyLoading = false;
        
        if (IndexLoadedFrom_Next == IndexLoadedTo_Next) 
            return; 
        
        var lastThumbObj = $("thumb_" + (IndexLoadedFrom_Next - 1)); 
        
        var arrayIndex = 0; 
        
        for (var index = IndexLoadedFrom_Next; index < IndexLoadedTo_Next; index++)
        {
            var altText = ShowText_Next[arrayIndex]; 
            var mainImage = ShowImages_Next[arrayIndex]; 
            var mainHeight = ShowHeights_Next[arrayIndex]; 
            var thumbUrl = ThumbImages_Next[arrayIndex]; 
            
            var thumbObj = $("thumb_" + index); 
            
            if (thumbObj == null) 
            {                 
                var newThumb = document.createElement("DIV");
                newThumb.id = "thumb_" + index; 
                newThumb.className = "SSThumb"; 
                //newThumb.style.display = "none"; 
                
                newThumb.innerHTML = "<img style=\"display:none\" src=\"" + thumbUrl + "\" alt=\"" + altText + "\" onclick=\"return SetActiveImage(" + index + ", this);\"  onload=\"this.style.display = 'block';\" />"; 
                
                if (lastThumbObj.nextSibling != null) 
                    lastThumbObj.parentNode.insertBefore(newThumb, lastThumbObj.nextSibling);
                else 
                    lastThumbObj.parentNode.appendChild(newThumb);
                                    
                lastThumbObj = newThumb;           
            }
            
            if (ShowText.length <= index) 
                ShowText.push(altText); 
            else 
                ShowText[index] = altText;
            
            if (ShowHeights.length <= index) 
                ShowHeights.push(mainHeight); 
            else 
                ShowHeights[index] = mainHeight;
                             
            if (ShowImages.length <= index) 
                ShowImages.push(mainImage); 
            else 
                ShowImages[index] = mainImage;
                                                        
            arrayIndex++; 
        }
    }
    else 
        CurrentlyLoading = false;
};             
