﻿/***************************************************************************************
$Workfile: PrimaryImage.js $
$Author: Syed Ghulam Akbar $
$Revision: 1 $
$Created Date: 07/10/06 19:42 PM $
$Description: Contains the JavaScript utility function for loading and view primary images.
***************************************************************************************/

var imgPrimaryImage = null;     // Contains the primary image object reference
var strPrimaryImagSource = "";  // Contains the primary image source reference
var imgPreviewImage = new Image();     // In-Memory image object to load/store the preview images

// Returns back the primary image object reference
function GetPrimaryImageObj()
{
    // if the primary image object is already loaded, return it back
    if (imgPrimaryImage != null)
        return imgPrimaryImage;
        
    // Loop on all the controls in the base form
    for(var iCounter=0;iCounter<document.images.length;iCounter++)
    {
        var objImage = document.images[iCounter]; // Get the item reference
	    
        // Check if this is the required object
        if (objImage.id.indexOf("imgPrimaryImage") >= 0)
            return objImage
    }
    return null;
}
    
// This method is called, when the user has finished loading the 
function ShowPreviewImage()
{
    var imgObject = GetPrimaryImageObj();
    imgObject.src = imgPreviewImage.src;   // Show the new image loading
}

// Show the given image in the primary image area. This function is used to show
// all the other (non-primary images)
function PreviewImage(imagePath)
{        
    // Get the primary image object reference
    var imgObject = GetPrimaryImageObj();
    
    // Ignore if the loading animation is still running
    if (imgObject.src == "/Images/LoadingAnimationSwap.gif")
        return;
    
    if (strPrimaryImagSource == "")    
        strPrimaryImagSource = imgObject.src ;      // Save the primary image source path
    
    // Load the new image in the memory area
    imgPreviewImage.src = imagePath;    // Load the image in a memory object
    imgPreviewImage.onload = ShowPreviewImage;

    if (imgPreviewImage.src != imgObject.src)
    {
        imgObject.src = "/Images/LoadingAnimationSwap.gif";   // Show the image loading
    }
}

// Show the primary image. This method is called on mouse out event re-load the primary image
function ShowPrimaryImage(evt, srcElement)
{
    if (evt && srcElement)
    {
        // Handle for the nested DIV mouse out i.e. don't hide when mouse is still over the nested DIV
        evt = evt || window.event;    
        var targetElement = evt.relatedTarget || evt.toElement;
        
        while (targetElement && targetElement.nodeName !== 'HTML') 
        {                        
            targetElement = targetElement.parentNode;    
            
            // Don't if it's a child window of our main container window
            if (targetElement.id == srcElement.id)
                return;
        }
    }
        
    // Get the primary image object reference
    var imgObject = GetPrimaryImageObj();
    imgObject.src = strPrimaryImagSource;   // Show the primary image
}

// This function is used to display the place-holder images in case the default
// image is not found on the server.
function NoImage()
{
    // Get the primary image object reference
    var imgObject = GetPrimaryImageObj();
    imgObject.src = "/Images/NoImage.gif"
}
