/*-----------------------------------------------
'	Company: WORLD NOMADS (www.worldnomads.com)
'	Copyright © 2006, All rights reserved.
'	Date Created: Month 2006
'
'	Last Modified Date: ##th Month, 2006
'	Last Modified By: Benjamin -> ben@worldnomads.com
'
'	DO NOT MODIFY THIS DOCUMENT WITHOUT
'	NOTIFYING THE AUTHOR FIRST
'
------------------------------------------------*/

// External Links
function externalLinks() {
	if (!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors .length; i++) {
			var anchor = anchors[i];
				if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
					anchor.target = "_blank";
					anchor.title = (anchor.title != "") ? anchor.title+" (opens in a new window)" : "opens in a new window";
					anchor.className = (anchor.className != '') ? anchor.className+' external' : 'external';
				}
		}
}


/* This controls the layers */
function swapLayers(id) {
	myLyr = document.getElementById(id);
	if (myLyr.className == "panelShow") {
	/* 	hideLayer(myLyr.id); */
	} else {
		if (cur_lyr != "") hideLayer(cur_lyr);
		showLayer(myLyr.id);
		cur_lyr = myLyr.id;
	}
}

function showLayer(id) {
  var lyr = getElemRefs(id);
  if (lyr && lyr.css) lyr.className = "panelShow";
}

function hideLayer(id) {
  var lyr = getElemRefs(id);
  if (lyr && lyr.css) lyr.className = "panelHidden";
}

/* this controls the tabs */
function selectTab(id) {
	myTab = document.getElementById(id);
	if (myTab.className == "selected") {
	
	} else {
		if (cur_tab != "") hideTab(cur_tab);
		showTab(myTab.id);
		cur_tab = myTab.id;
	}
}

function showTab(id) {
  var lyr = getElemRefs(id);
  if (lyr && lyr.css) lyr.className = "selected";
}

function hideTab(id) {
  var lyr = getElemRefs(id);
  if (lyr && lyr.css) lyr.className = "none";
}


function getElemRefs(id) {
	var el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? document.layers[id]: null;
	if (el) el.css = (el.style)? el.style: el;
	return el;
}

/* Check box checker */
function check(id){
	tickBox = document.getElementById(id);
	if (tickBox.checked)
		tickBox.checked = false;
	else 
		tickBox.checked = true;
}




/* red24 news feed ticker used in safety pages.
** Built upon prototype.js and scriptaculous libraries.
** Written by Ben Gillies -> ben.gillies at worldnomads.com
*/
function feedTicker(instanceName) {
	this.debug = false;  // set true for error alerts when debugging.
	this.initialised = false;  // initiate with false.  startFeedTimer() method evalutes whether this object instance is initiated.
	this.instanceName = instanceName;  // instance's variable name in string format.  Required for iteration management.
	this.feedItemCount = 1;  // initialise the first item index.
	this.feedItemTimer = null;  // initialise the timer property for controlling the iteration management.
	this.maxNumFeedItems = 10;  // maximum number of feed items in total. Default is 10.
	this.timerDelay = 10000;  // delay in between each iteration.
	this.numGroupItems = 1; // how many items in the group for each iteration.
	this.delayBetweenGroupItems = 1000;  // if using multiple items in a group, this is the delay between each item in the group.
	this.wrapperId = null;  // required to be set by the setItemsAndContainer method.
	this.feedItemIdPrefix = "feedItem_";  // prefix of the id attribute for each item.  item's id value must be feedItemIdPrefix followed by the item's index number in order. ie. "feedItem_1" for the first item.
	
	this.setDebug = function(isDebug) {
			this.debug = isDebug;
		}
	this.setItemsAndContainer = function(wrapperId, itemPrefix) {
		// required to be set (if default values will not work) before the instance's startFeedTimer method is called, so the items can be identified.
			this.wrapperId = wrapperId;
			this.feedItemIdPrefix = itemPrefix;
			// get number of children feed items..
			myWrapperObj = document.getElementById(wrapperId);
			if (myWrapperObj != null && typeof(myWrapperObj) != "undefined") {
				this.maxNumFeedItems = 0;
				for (i=0; i < myWrapperObj.childNodes.length; i++) {
					// for each child node, see if it begins with the item prefix.
					try {
						if (myWrapperObj.childNodes[i].id.substr(0, itemPrefix.length) == itemPrefix) {
							this.maxNumFeedItems++;
						}
					} catch (exception) {
						// ignore exceptions..
					}
				}
			} else {
				if (this.debug) alert("FeedTimer error: Container element does not exist.");
				return false;
			}
			// confirm that feed items have been found..
			if (this.maxNumFeedItems == 0) {
				// no feed items found..
				if (this.debug) alert("FeedTimer error: Can not find feed items using " + itemPrefix + " as the feed item's id prefix.");
				return false;
			}
		}
	this.setIterationTimeout = function(timeoutDelay) {
			this.timerDelay = timeoutDelay;
		}
	this.nextFeedItem = function() {
			this.cancelFeedTimer();
			this.toggleFeedItems();
		}
	this.previousFeedItem = function() {
			this.cancelFeedTimer();
			if (this.feedItemCount == 1) {
				previousItem = this.maxNumFeedItems;
			} else {
				previousItem = this.feedItemCount - 1;
			}
			$(this.feedItemIdPrefix + this.feedItemCount).hide();
			new Effect.Parallel([
				//new Effect.Fade($(feedItemIdPrefix + feedItemCount), { sync: true, from: 1, to: 0, duration: 0 }), 
				new Effect.Appear($(this.feedItemIdPrefix + previousItem), { sync: true, duration: 1 }) 
			], { 
			  duration: 1,
			  delay: 0
			});
			this.startFeedTimer();
			if (this.feedItemCount == 1) {
				this.feedItemCount = this.maxNumFeedItems;
			} else {
				this.feedItemCount = this.feedItemCount - 1;
			}
		}
	this.cancelFeedTimer = function() {
			clearTimeout(this.feedItemTimer);
		}
	this.startFeedTimer = function() {
			if (!this.initialised) {
				if (this.wrapperId == null) {
					// test whether the default settings will be correct enough for auto running the feed timer.. by checking to see if the last item exists.
					lastItem = document.getElementById(this.feedItemIdPrefix + this.maxNumFeedItems);
					if (lastItem == null || typeof(lastItem) == "undefined") {
						// last item does not exist so can not run with default values..
						if (this.debug) alert("FeedTimer error: can not start feed timer, please set " + this.instanceName + ".setItemsAndContainer() method.");
						return false;
					} else {
						// last item must exist so assume okay to run the feedItemTimer on the default values..
						this.initialised = true;
					}
				} else {
					// assume wrapperId, feed itemIdPrefix and maximum number of feed items are already configured from setItemsAndContainer() method..
					this.initialised = true;
				}
			}
			this.feedItemTimer = setTimeout("window['" + this.instanceName + "'].toggleFeedItems();", this.timerDelay);  // call this instance's method to toggleFeedTimer.
		}
	this.toggleFeedItems = function() {
			if (this.feedItemCount > this.maxNumFeedItems - 1) {
				var currentItem = this.maxNumFeedItems;
				var nextItem = 1;
				this.feedItemCount = 0; // reset.
			} else {
				var currentItem = this.feedItemCount;
				var nextItem = this.feedItemCount + 1;
			}
			$(this.feedItemIdPrefix + currentItem).hide();
			new Effect.Parallel([
			  // new Effect.Fade($(feedItemIdPrefix + currentItem), { sync: true, from: 1, to: 0, duration: 1 }), 
			  new Effect.Appear($(this.feedItemIdPrefix + nextItem), { sync: true, duration: 1 }) 
			], { 
			  duration: 1,
			  delay: 0
			});
			this.startFeedTimer();
			this.feedItemCount++;
		}
}  // end feedTicker object.