/****************************************************************************
 File: core.js
 Author: Richard Coleman
 Date: 25/08/2006
 
 © Red Kite Business Systems, 2006
 
 ****************************************************************************
 
 Description:
 ------------
 
 Provides the core functions to be used on most, if not all, of the pages
 within the application.
 ****************************************************************************/

/*
 * Locates and returns a reference to the document object by the given ID.
 *
 * Parameters
 * ==========
 *	id	the string name of the id to locate
 */
function findObject(id){
	var ele;
	
	if (typeof(id) != "string"){
	    return id;
	}
	
	if (document.getElementById){
		ele = document.getElementById(id);
	} else {
		ele = document.all.images[id];
	}

	return ele;
}

/*
 * Swaps the existing source image for the new specified image, and 
 * stored the previous in another reference, ready for restoring.
 *
 * Parameters
 * ==========
 *	imgId		the string id of the image to locate and swap
 *	newImage	the string path and filename of the rollover image
 */
function swapImage(imgId, newImage) {
    var ele = findObject(imgId);
    if (ele){
        if (ele.altSrc)
        {
            newImage = ele.altSrc;
        }
        ele.oldSrc = ele.src;
        ele.src = newImage;
    }
}

/*
 * Restores an image's original source image from its held reference.
 *
 * Parameters
 * ==========
 *	imgId		the string id of the image to locate and restore
 */
function restoreImage(imgId){
    var ele = findObject(imgId);
    if (ele){
        ele.src = ele.oldSrc;
    }
}

/*
 * Preloads the array of images into the browser cache to make the 
 * rollovers appear smoother.
 *
 * Parameters
 * ==========
 *  Array of strings defining the location of the images to load.
 */
function preloadImages(){
	if (document.images){
		for (i = 0 ; i < arguments.length ; i++){
			var image = new Image();
			image.src = arguments[i];
		}
	}
}




var popupMenu;
var max_opacity = 90;
var min_opacity = 0;
var fader_direction;
var fader_steps;
var fader_msecs_in;
var fader_msecs_out;



/*
 * blockId : string name of block to fade
 * maxOpacity : the number between 0 and 100 representing the maximum opacity level
 * minOpacity : the number between 0 and 100 representing the minimum opacity level
 * fStep : the number of steps in the fade
 * fmSecsIn : the number of milli-seconds to operate the fade in
 * fmSecsOut : the number of milli-seconds to operate the fade out
 */
function activateBlock(blockId, maxOpacity, fSteps, fmSecsIn, fmSecsOut) {
	popupMenu = findObject(blockId);
	max_opacity = maxOpacity;
	fader_steps = fSteps;
	fader_msecs_in = fmSecsIn;
	fader_msecs_out = fmSecsOut;
	
	if (popupMenu)
	{
		var dispVal = popupMenu.style.display;
		
		if (!dispVal || dispVal == "none")
		{
			popupMenu.style.display = "block";
			fader_direction = 1;
			fader();
		}
		else
		{
			fader_direction = -1;
			fader();
		}
	}
}


function fader()
{
	var step_size = (max_opacity/fader_steps)*fader_direction;
	var curr_opacity = (isNaN(popupMenu.style.opacity) ? 0 : popupMenu.style.opacity) * 100;
	var new_opacity = curr_opacity + step_size;
	
	var refreshTime = (fader_direction > 0 ? fader_msecs_in : fader_msecs_out)/fader_steps;
	
	var complete = true;
	
	if (new_opacity < 0)
	{
		new_opacity = 0;
		popupMenu.style.display = "none";
	}
	else if (new_opacity > max_opacity)
	{
		new_opacity = max_opacity;
	}
	else
	{
		complete = false;
	}
	
	popupMenu.style.opacity = new_opacity/100;
	popupMenu.style.filter = "alpha(opacity=" + new_opacity + ")";
	
	if (complete)
		return;
	
	setTimeout('fader()', refreshTime);
}

function adjustTextSize(direction, amount)
{
	var ele = findObject("container");
	if (!ele.style.fontSize)
		ele.style.fontSize = "100%";
	
	if (direction == 1)
		increaseTextSize(ele, amount);
	else
		decreaseTextSize(ele, amount);
}

function increaseTextSize(ele, amount) 
{
	var retVal = extractFigure(ele.style.fontSize);
	ele.style.fontSize = (parseInt(retVal) + parseInt(amount)) + "%";
}

function decreaseTextSize(ele, amount) 
{
	var retVal = extractFigure(ele.style.fontSize);
	ele.style.fontSize = (parseInt(retVal) - parseInt(amount)) + "%";
}

function extractFigure(val)
{
	if (isNaN(val) && val.lastIndexOf("%") > 0)
		return val.substr(0, val.lastIndexOf("%"));
	else
		return val;
}