﻿if (window.loadingImgSrc != null)
{
    var loadingImg = new Image();
    loadingImg.src = loadingImgSrc;
}

function NavigatorDisplayImage(navigatorid, imageindex)
{
    // Get elements
	var lst = document.getElementById(navigatorid + "_lstImages");
	var lnk = document.getElementById(navigatorid + "_lnkImage");
    var imgElem = document.getElementById(navigatorid + "_imgImage");
	
	// Set loading image
	if (window.loadingimgElemSrc != null)
	{
	    imgElem.style.height = 'auto';
	    imgElem.style.width = 'auto';
	    imgElem.src = loadingImgSrc;
	}
	else
	{
	    imgElem.style.filter = 'alpha(opacity=20)';
	}
	lnk.href = '';
	
	if ( imageindex<0 ) imageindex = 0;
	if ( imageindex>=lst.options.length ) imageindex = lst.options.length-1;
	
	imageurl = lst.options[imageindex].value.replace('&amp;', '&') 

    // Setup the links
	navlnk = document.getElementById(navigatorid + "_navlnk" + lst.selectedIndex);
	if ( navlnk != null ) navlnk.className = "ImageNavigationLink";
	lst.selectedIndex = imageindex;
	navlnk = document.getElementById(navigatorid + "_navlnk" + lst.selectedIndex);
	if ( navlnk != null ) navlnk.className = "ImageNavigationLinkDisabled";
	
	prevlink = document.getElementById(navigatorid + "_navprev");
	if ( prevlink != null )
	{
	    if ( imageindex <= 0 ) prevlink.className = "ImageNavigationLinkDisabled";
	    else prevlink.className = "ImageNavigationLink";
	}
	nextlink = document.getElementById(navigatorid + "_navnext");
	if ( nextlink != null )
	{
	    if ( imageindex >= lst.options.length-1 ) nextlink.className = "ImageNavigationLinkDisabled";
	    else nextlink.className = "ImageNavigationLink";
	}
	
	// Load the image
	newimage = new Image();
    newimage.onload = new Function("setTimeout('OnLoadImage(\"" + navigatorid + "\");', 0)");
	newimage.src = imageurl;
}

function NavigatorPreviousImage(navigatorid)
{
	lst = document.getElementById(navigatorid + "_lstImages");
	return NavigatorDisplayImage(navigatorid, lst.selectedIndex-1);
}

function NavigatorNextImage(navigatorid)
{
	lst = document.getElementById(navigatorid + "_lstImages");
	return NavigatorDisplayImage(navigatorid, lst.selectedIndex+1);
}

function OnLoadImage(navigatorid)
{    
    // Get elements
    var lst = document.getElementById(navigatorid + "_lstImages");
	var lnk = document.getElementById(navigatorid + "_lnkImage");
	var imgElem = document.getElementById(navigatorid + "_imgImage");

    if (imgElem != null)
    {
        // Get URLs
	    imageurl = lst.options[lst.selectedIndex].value.replace('&amp;', '&') 
	    linkurl = lst.options[lst.selectedIndex].innerHTML.replace('&amp;', '&');	
    
        // Set URLs
	    lnk.href = linkurl;
	
	    imgElem.src = imageurl;
	    imgElem.style.filter = 'auto';

        imgElem.style.width = 'auto';
        imgElem.style.height = 'auto';

	    // Fit image
        NavigatorFitImage(navigatorid);
    }
}

function NavigatorFitImage(navigatorid)
{
    var imgElem = document.getElementById(navigatorid + "_imgImage");
    if (imgElem != null)
    {        
        maxwidth = GetMaxImageWidth();
        maxheight = GetMaxImageHeight();

        if ( (maxheight>0) && (imgElem.height > maxheight) )
        {
            if (imgElem.style.height != maxheight + 'px')
            {
                imgElem.style.height = maxheight + 'px';
            }
            if (imgElem.style.width != 'auto')
            {
                imgElem.style.width = 'auto';
            }
        }        
        if ( (maxwidth>0) && (imgElem.width > maxwidth) )
        {
            if (imgElem.style.width != maxwidth + 'px')
            {
                imgElem.style.width = maxwidth + 'px';
            }
            if (imgElem.style.height != 'auto')
            {
                imgElem.style.height = 'auto';
            }
        }
        if ( (maxheight>0) && (imgElem.height > maxheight) )
        {
            if (imgElem.style.height != maxheight + 'px')
            {
                imgElem.style.height = maxheight + 'px';
            }
            if (imgElem.style.width != 'auto')
            {
                imgElem.style.width = 'auto';
            }
        }        
    }
}
