﻿// JScript File for photo gallery.

// -----------------------------------------------------------------------------
// |
// |	Copyright (c) 2006
// |	by James Eby ( james.eby@insightbb.com )
// |
// |	Permission is hereby granted, free of charge, to any person obtaining a copy of
// |	this software and associated documentation files (the "Software"), to deal
// |	in the Software without restriction, including without limitation
// |	the rights to use, copy, modify, merge, publish, distribute, sublicense,
// |	and/or sell copies of the Software, and to permit persons to whom the
// |	Software is furnished to do so, subject to the following conditions:
// |
// |	The above copyright notice and this permission notice shall be included in
// |	all copies or substantial portions of the Software.
// |
// |	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
// |	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
// |	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
// |	IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
// |	CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT 
// |	OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
// |	OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// |
// |	VERSION	BY/DATE			DESCRIPTION
// |	-------	----------------	--------------------------------------
// |	JLE-000	James Eby		Original.
// |		    6/18/2006
// |
// -----------------------------------------------------------------------------

// Full image information arrays.
var imageName = new Array(0);
var imageDescription = new Array(0);
var imageWidth = new Array(0);
var imageHeight  = new Array(0);
var imageTitle   = new Array(0);
var imageSection = new Array(0);

var fullImageItem = new Array("image", "description", "width", "height", "title", "section");

// Other required variables.
var currentImage = -1;
var thumbnailDir = "";
var imageDir = "";
var imagePage = "";
var lastImageSection = "";

// -----------------------------------------------------------------------------
// addImageToGallery
//
//  Adds an image's information to the gallery array, does not attempt to load
//  the image.
//
//  Parms: Array in the order given for fullImageItem with all parameters given.
//
//  Returns the index of the image information in the array or -1 on failure.
//
// -----------------------------------------------------------------------------
function addImageToGallery()
{
var inc=0;
var idx = imageName.length;
var useLastImageSection = false;

	if (arguments.length == fullImageItem.length - 1)
	{
		useLastImageSection = true;
	}
      else if (arguments.length != fullImageItem.length) 
      {
        alert( "Javascript function \"addImageToGallery\" had too many or too few arguments (" + arguments.length + ").");
        return -1;
      }

      imageName[idx] = "";
      imageDescription[idx] = "";
      imageWidth[idx] = "";
      imageHeight[idx]= "";
      imageTitle[idx] = "";
      if(useLastImageSection)
      {
		imageSection[idx] = lastImageSection;
	}
	else
	{
		imageSection[idx] = "";
	}

      for (inc=0; inc < arguments.length; inc++) 
      {
        if (arguments[inc] != null) 
        {
          switch (fullImageItem[inc]){
            case "image":
                imageName[idx] = arguments[inc]; 
                break;
            case "description":
                imageDescription[idx]= arguments[inc]; 
                break;
            case "width":
                imageWidth[idx] = arguments[inc];
                break;
            case "height": 
                imageHeight[idx]= arguments[inc]; 
                break;
            case "title": 
                imageTitle[idx] = arguments[inc]; 
                break;
            case "section": 
                imageSection[idx] = arguments[inc];
                lastImageSection = arguments[inc];
                break;                
          }
        }
      }
      return(idx);
  }
  
// -----------------------------------------------------------------------------
// getImageValueByAttrName
//
//  Returns the specified current image information given by the passed
//  attribute name, if idx is also passed it returns the information of the
//  image at the given array index.
//
//  Parms:  attrName = name of the attribute to retrieve.
//          idx (optional) = Index if retrieving the currently set
//          image information is not the objective.
//
// -----------------------------------------------------------------------------  
function getImageValueByAttrName(attrName)
{
var idx = currentImage;

    if (arguments.length > 1)
        idx = arguments[1];
    
    if(idx == -1)
        return("");
        
    switch (new String(attrName).toLowerCase()) 
    {
    case "image":        
        return imageName[idx]; 
    case "description": 
        return imageDescription[idx]; 
    case "width":       
        return imageWidth[idx]; 
    case "height":      
        return imageHeight[idx];
    case "title":       
        return imageTitle[idx];
    case "section":      
        return imageSection[idx];
    default:
        alert( "Javascript function \"getImageValueByAttrName\" was passed an invalid attribute name (" + attrName + ").");
        break;
    }
}

/* file names and locations functions */
function setThumbnailDir(path)
{ 
    thumbnailDir = path; 
}

function setImageDir(path)
{
    imageDir = path;
}

function setImagePage(page)
{
    imagePage=page;
}

/* Information Retrieval Functions */
function getThumbnailCount()
{
    return(imageName.length)
}

// -----------------------------------------------------------------------------
// writeThumbNailPage
//
//  Writes out the HTML for the thumbnail page in the thumbnail page. Run this 
//  function after you have defined all the images that will be added to the 
//  gallery.
//
// -----------------------------------------------------------------------------
function writeThumbNailPage() 
{
var startIdx = 0;
var stopIdx = imageName.length;
var idx;
var fullInnerHTML = "";
var lastSection = "";
var firstTime = true;

    fullInnerHTML = "<div class=\"thumbnails\">";

    for (idx = startIdx; idx < stopIdx; idx++) 
    {
        if(lastSection != imageSection[idx])
        {
            if(!firstTime)
            {
                fullInnerHTML += "<br><br>";
            }
            else
            {
                firstTime = false;
            }
            fullInnerHTML += "<div class=\"thumbnailSection\">" + imageSection[idx] + "</div>";
            lastSection = imageSection[idx]
            
        }
        fullInnerHTML += "<a href=\"" + imagePage + "?img=" + idx + "\" target=\"galleryDest\" target=\"_top\" />";
        if(imageHeight[idx] == 0)
            fullInnerHTML += "<img src=\"" + thumbnailDir + "/" + imageName[idx] + "\" />"
		else
		    fullInnerHTML += "<img src=\"" + thumbnailDir + "/" + imageName[idx] + "\" height=" + imageHeight[idx] + " width = " + imageWidth[idx] + " />"
		
		fullInnerHTML += "</a><div class=\"thumbnailTitle\">" + imageTitle[idx] + "</div>";
	}
	
	fullInnerHTML += "</div>";
	document.getElementById("thumbnailDest").innerHTML = fullInnerHTML;
}

// -----------------------------------------------------------------------------
// writeGalleryPage
//
//  Writes out the HTML for the gallery page using the index passed in the page 
//  URL or index 0. Run this function after you have defined all the images that 
//  will be added to the gallery.
// -----------------------------------------------------------------------------
function writeGalleryPage() 
{
var idx = currentImage = privGetURLIdx();

    document.getElementById("title").innerHTML = imageTitle[idx];
    document.getElementById("image").innerHTML = "<img src=\"" + imageDir + "/" + imageName[idx] + "\" />";
    document.getElementById("description").innerHTML = imageDescription[idx];
}

// -----------------------------------------------------------------------------
// privGetURLIdx
//
//  Looks at URL parameters, accepts either "img=???", where ??? is 
//  index of image within this gallery or "file=???", where ??? is image filename.
// -----------------------------------------------------------------------------
function privGetURLIdx() 
{
var tempString = new String(window.location.search).toLowerCase();
var idx = 0;
  
    if (tempString.substr(1,3)=="img")
    {
        // strip "img=" from URL
        tempString = new String( tempString.substr( 5, tempString.length-5 ));
        if (tempString.length < 1)
        {
            tempString= "0";
        }
        else if (isNaN(tempString))
        {
            alert( "Javascript function \"privGetURLIdx\" was passed an invalid image index " + tempString + ".");
        }
        else
        {
            idx = parseInt(tempString);
        }
        if (idx >= imageName.length)
        {
            idx = (imageName.length - 1);
        }
    }
    return(idx);
}