/**
 *
 *    NAME: api.js
 *    AUTHOR: Arturo Escartin
 *    PURPOSE: Global JavaScript variables / functions.
 *
 */

//---------------------------------------------------------
//	GLOBAL VARIABLES
//----------------------------------------------------------

var NS4 = (document.layers) ? 1 : 0;
var NS6 = (!document.all && document.getElementById) ? true : false;
var IE = (document.all) ? 1 : 0;

/* This array holds the cached image wrappers. */
var imageArray = new Array();

/*
 * These are the Regular Expressions that, when applyed to
 * the image src will signify the image as lit (on) or unlit (off).
*/
var onRE = /(\_on\_)/;
var offRE = /(\_off\_)/;

function GetBr()
{
	var agt = navigator.userAgent.toLowerCase();
	var browser = navigator.appName.toLowerCase();
	var isNav = (browser=="netscape");

	if (agt.indexOf("msie") !=-1)
	{ 
		br = 'ie';
	}

	var isMac = (agt.indexOf("mac") != -1);

	if (isMac)
	{
		platform = 'mac';
	}
	else
	{
		platform = 'pc';
	}

	if (isNav && agt.indexOf("netscape6") != -1)
	{
		ver=agt.substr(agt.indexOf("netscape6")).split("/")[1];
		br='nn6';
	}
	else if (isNav)
	{
		ver=(isNav)?parseFloat(navigator.appVersion):agt.split(";")[1].substr(6) ;
		 br='nn4';
	}

	return br;
    return platform;
}

//---------------------------------------------------------
//	FUNCTIONS
//----------------------------------------------------------

function SwitchImg(imageName,state)
{
	if (document.images)
	{
		imgSource = document[imageName].src;


		if (state == "on")
		{
			onState = imgSource.replace(/-off/i,"-on");
  			//onState = imageName + "_on.gif";
			document[imageName].src = onState;
		}
		else //while (currentMenu != null)
		{
			offState = imgSource.replace(/-on/i,"-off");
			//offState = imageName + "_off.gif"
			document[imageName].src = offState;
		}
	}
}

function SwitchInputImg(inputName,state) {
	imgSource = inputName.src
	if (state == "on") {
		onState = imgSource.replace(/_off/i,"_on");
  		//onState = imageName + "_on.gif";
		inputName.src = onState;
	} else //while (currentMenu != null)
	{
		offState = imgSource.replace(/_on/i,"_off");
		//offState = imageName + "_off.gif"
		inputName.src = offState;
	}
}

function SwitchLogoImg(imageName, switchImageName)
{
	if (document.images)
	{
		document[imageName].src = switchImageName.src;
	}
}

function netscaperesizeFix()
{
	if (pageWidth != window.innerWidth || pageHeight != window.innerHeight)
	{
		document.location.reload();
	}
}

//----------------------------------------------------------------|

/**
 * Handles mouse overs for cached images.
 */
function doCachedMouseOver(imgObj)
{
	var theWrapper = imageArray[imgObj.name];

   	if(theWrapper)
   	{
    	if(imgObj.src != theWrapper.onImage.src)
      	{
        	imgObj.src = theWrapper.onImage.src;
      	}
   	}
}

//----------------------------------------------------------------|

/**
 * Handles mouse outs for cached images.
 */
function doCachedMouseOut(imgObj)
{
	var theWrapper = imageArray[imgObj.name];
   	if(theWrapper)
   	{
    	if(theWrapper.initial == 0)
      	{
        	imgObj.src = theWrapper.offImage.src;
      	}
   	}
}

//----------------------------------------------------------------------------------------------

/**
 * This is a simple JavaScript object that stores information
 * about each navigation bar image.
 *
 * onImage  - The lit (ON) image src.
 * offImage - The unlit (OFF) image src.
 * initial  - Either 1 or 0.  1 means that the image was initially ON,
 *            0 means it was initially off.
 */
function ImageWrapper(m_sOnImage, m_sOffImage, m_sInitial)
{
	this.onImage = new Image();
   	this.onImage.src = m_sOnImage;
   	this.offImage = new Image();
   	this.offImage.src = m_sOffImage;
   	this.initial = m_sInitial;

   return this;
}

//----------------------------------------------------------------------------------------------

/**
 * This is the initialization function for the header.
 * It loads up all the image objects into the ImageWrappers
 * which implicitly caches them.
 */
function initHeader()
{
	imageArray['logo'] = createWrapper(document.images.logo.src);
   	imageArray['whats'] = createWrapper(document.images.whats.src);
   	imageArray['whos'] = createWrapper(document.images.whos.src);
   	imageArray['research'] = createWrapper(document.images.research.src);
   	imageArray['portfolio'] = createWrapper(document.images.portfolio.src);
   	imageArray['products'] = createWrapper(document.images.products.src);
}

//----------------------------------------------------------------|

//----------------------------------------------------------------|

function createCachedImage(m_sImgName, m_sImgSrc)
{
	imageArray[m_sImgName] = createWrapper(m_sImgSrc);
}

//----------------------------------------------------------------|

/**
 * This is a factored method that takes the initial image,
 * figures out whether it's lit or unlit, then
 * creates an ImageWrapper with the correct INITIAL value.
 */
function createWrapper(m_sImgSrc)
{
	var wrapper;
	var initial = 0;
   	var offSrc, onSrc;

	if(isLit(m_sImgSrc))
   	{
    	initial = 1;
      	onSrc = m_sImgSrc;
      	offSrc = m_sImgSrc.replace(onRE,"_off");
      	wrapper = new ImageWrapper(onSrc,offSrc,initial);
	}
   	else
   	{
		initial = 0;
      	offSrc = m_sImgSrc;
      	onSrc = m_sImgSrc.replace(offRE,"_on");
      	wrapper = new ImageWrapper(onSrc,offSrc,initial);
	}
   return wrapper;
}

//----------------------------------------------------------------|

/**
 * Uses the Regular Expressions to see if an image
 * is lit (ON) or unlit (OFF).
 */
function isLit(anImgSrc)
{
	// If has the string '_on_' in it's name, it's lit.
	var RE = /\_on\_/;
	
	if(RE.test(anImgSrc))
	{ 
		return true;
	}
	else
	{
		return false;
	}
}

//----------------------------------------------------------------|

//----------------------------------------------------------------|

/**
 * Admin function
 * m_sUrl must contain the all variables needed by the recieving page.
 * 
 */
function checkDelete(m_sItemName, m_sUrl)
{
	m_blnDelete = confirm("Are you sure you want to delete item " + m_sItemName + "?");

	if (m_blnDelete)
	{
		self.location = m_sUrl;
	}
}

//----------------------------------------------------------------|