/****************************************************************************
 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;
    }
}


/*
 * Wraps the swapImage() function to specify the rollover/hover image
 * based on the rule that there is an xxx_a.xxx version and an xxx_b.xxx
 * version.  This function works out the xxx_b.xxx name and passes it to
 * the swapImage() function for operation.
 *
 * Parameters
 * ==========
 *	imgId		the string id of the image to locate and swap
 */
function swapHoverImage(imgId)
{
	var img = findObject(imgId);
	if (img)
	{
		var currSrc = img.src;
		swapImage(imgId, currSrc.replace("_a","_b"));
	}
}


/*
 * 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;
    }
}

/*
 * Loads a series of images into the cache for quicker response time
 * when user's trigger rollovers.
 */
var imgArr = new Array();
function preloadImages() {
	for (i=0;i<preloadImages.arguments.length;i++) {
		imgArr[i]=new Image()
		imgArr[i].src=preloadImages.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 setTextSize()
{
	var ele = findObject("container");
	var storedSize = readCookie("semta.fontSize");
	if (storedSize)
	{
		ele.style.fontSize = storedSize;
	}
}

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);
	
	createCookie("semta.fontSize", ele.style.fontSize, 365);
}

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;
}



function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


/*
 * Looks for the given id, then hunts for all anchor tags that are children
 * to this element.  Any tag that starts with 'http://' or 'https://' has its
 * target set to _blank in order that the user is not taken totally away
 * from the website.
 */
function eventTargetBlank()
{
	if (document.getElementById("eventDetailsDiv1"))
	{
		var allDivs = document.getElementsByTagName("div");
		for ( x = 0 ; x < allDivs.length ; x++ )
		{
			if (allDivs[x].id.indexOf("eventDetailsDiv") > -1)
			{
				var detailDiv = document.getElementById(allDivs[x].id);

				var anchors = detailDiv.getElementsByTagName("a");
				for (i = 0 ; i < anchors.length ; i++)
				{
					var url = anchors[i].href;
					if ( ((url.indexOf("http://") > -1 && url.indexOf("http://") < 1) || 
						 (url.indexOf("https://") > -1 && url.indexOf("https://") < 1)) &&
						 url.indexOf("10.1.0.146") < 0 && url.indexOf("www.semta.org.uk") < 0 && url.indexOf("www.semtagroup.com") < 0 )
					{
						anchors[i].target = "_blank";
					}
				}
			}
		}
	}
}