﻿// JScript File for dictionary pages.

// -----------------------------------------------------------------------------
// |
// |	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 termName = new Array(0);
var termDescription = new Array(0);
var termAudio = new Array(0);
var termVideo = new Array(0);

var fullDictionaryItem = new Array("term", "definition", "audio", "video");

// Other required variables.
var currentTerm = -1;
var audioDir = "";
var videoDir = "";
var dictionaryPage = "";

// -----------------------------------------------------------------------------
// addDefinition
//
//  Adds an term's definition for the array.
//
//  Parms: Array in the order given for fullDictionaryItem with all parameters given.
//
//  Returns the index of the image information in the array or -1 on failure.
//
// -----------------------------------------------------------------------------
function addDefinition()
{
var inc=0;
var idx = termName.length;

      if (arguments.length != fullDictionaryItem.length) 
      {
        alert( "Javascript function \"addDefinition\" had too many or too few arguments (" + arguments.length + ").");
        return -1;
      }

      termName[idx] = "";
      termDescription[idx] = "";
      termAudio[idx] = "";
      termVideo[idx]= "";
      
      for (inc=0; inc < arguments.length; inc++) 
      {
        if (arguments[inc] != null) 
        {
          switch (fullDictionaryItem[inc]){
            case "term":
                termName[idx] = arguments[inc]; 
                break;
            case "definition":
                termDescription[idx]= arguments[inc]; 
                break;
            case "audio":
                termAudio[idx] = arguments[inc];
                break;
            case "video": 
                termVideo[idx]= arguments[inc]; 
                break;               
          }
        }
      }
      return(idx);
  }
  
// -----------------------------------------------------------------------------
// getTermValueByAttrName
//
//  Returns the specified current term 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 getTermValueByAttrName(attrName)
{
var idx = currentTerm;

    if (arguments.length > 1)
        idx = arguments[1];
    
    if(idx == -1)
        return("");
        
    switch (new String(attrName).toLowerCase()) 
    {
    case "term":        
        return termName[idx]; 
    case "definition": 
        return termDescription[idx]; 
    case "audio":       
        return termAudio[idx]; 
    case "video":      
        return termVideo[idx];
    default:
        alert( "Javascript function \"getTermValueByAttrName\" was passed an invalid attribute name (" + attrName + ").");
        break;
    }
}

/* file names and locations functions */
function setAudioDir(path)
{ 
    audioDir = path; 
}

function setVideoDir(path)
{
    videoDir = path;
}

function setDictionaryPage(page)
{
    dictionaryPage=page;
}

/* Information Retrieval Functions */
function getTermCount()
{
    return(termName.length)
}

// -----------------------------------------------------------------------------
// writeTerminologyListPage
//
//  Writes out the HTML for the terminiology list page. Run this 
//  function after you have defined all the terms that will be added to the 
//  dictionary.
//
// -----------------------------------------------------------------------------
function writeTerminologyListPage() 
{
var startIdx = 0;
var stopIdx = termName.length;
var idx;
var fullInnerHTML = "";
var firstTime = true;

    fullInnerHTML = "<div class=\"terms\">";

    for (idx = startIdx; idx < stopIdx; idx++) 
    {
        fullInnerHTML += "<a href=\"" + dictionaryPage + "?term=" + idx + "\" target=\"dictionaryDest\" target=\"_top\">" + termName[idx] + "</a>";
        fullInnerHTML += "<div class=\"line\"><span></span></div>";
	}
	
	fullInnerHTML += "</div>";
	document.getElementById("terminiologyDest").innerHTML = fullInnerHTML;
}

// -----------------------------------------------------------------------------
// writeDictionaryPage
//
//  Writes out the HTML for the dictionary page using the index passed in the page 
//  URL or index 0. Run this function after you have defined all the terms that 
//  will be added to the dictionary.
// -----------------------------------------------------------------------------
function writeDictionaryPage() 
{
var idx = currentImage = privGetURLIdx();

    document.getElementById("term").innerHTML = termName[idx];
    document.getElementById("definition").innerHTML = termDescription[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,4)=="term")
    {
        // strip "term=" from URL
        tempString = new String( tempString.substr( 6, tempString.length-6 ));
        if (tempString.length < 1)
        {
            tempString= "0";
        }
        else if (isNaN(tempString))
        {
            alert( "Javascript function \"privGetURLIdx\" was passed an invalid term index " + tempString + ".");
        }
        else
        {
            idx = parseInt(tempString);
        }
        if (idx >= termName.length)
        {
            idx = (termName.length - 1);
        }
    }
    return(idx);
}