/* 	File Description & Change Log
	-------------------------------------------------------

	Details
	-------

	Title: 		generalFunctions.js
	Author: 	Andy Smith
	Of:			Tsuko
	Creation:	26/02/2008

	Note:		This is a container for 
				Javascript that is generally 
				used throughout the site.

	Edits
	-----

	Author: 	Andy Smith
	Date:		26/02/2008

	Note:		

	-------------------------------------------------------

*/

/*
 * Title:		mainMenuSwap
 * Author:		Andy Smith
 * Date:		26/02/2008
 * Description:	Looks after the menu swapping function that will 
 * 				swap the previous menu item's background image to 
 * 				ensure that the front vertical line is properly 
 * 				coloured.
 * arguments:	currentID = the ID of the menu item to change
 *				set = whether the menu item is to be set or unset
 * output:		N/a
 *
 */

function mainMenuSwap(currentID, set){
	
	if(currentID > 0){
		var newID = currentID-1;
		
		var on_on = 'background-position:100% 0px; color:#FFFFFF;';
		var on_off = 'background-position:100% -261px; color:#FFFFFF;';
		var off_on = 'background-position:100% -105px;';
		var off_off = 'background-position:100% -188px;';
		
		if (!document.getElementsByTagName) return;
				
		var mainMenuElements = document.getElementById("mainMenu").getElementsByTagName("li");
		if(set == 'set'){
			
			/*
			 * This is the First Area that is used to define the rules for setting menu option classes
			 */
			 
			// if newID-1 is selected set current block on-off, set previous block to on-on, and set the block 2 previous to off-on
			if(mainMenuElements[newID-1] != null && (isSelected(mainMenuElements[newID-1].getElementsByTagName("a")[0].getAttribute("class")) || isSelected(mainMenuElements[newID-1].getElementsByTagName("a")[0].className))){
				
				if(mainMenuElements[newID].getElementsByTagName("a")[0].addEventListener) { //Firefox

					mainMenuElements[newID].getElementsByTagName("a")[0].setAttribute("style", on_off);
					
					if(mainMenuElements[newID-1] != null) mainMenuElements[newID-1].getElementsByTagName("a")[0].setAttribute("style", on_on);
						
					if(mainMenuElements[newID-2] != null) mainMenuElements[newID-2].getElementsByTagName("a")[0].setAttribute("style", off_on);
						
				}else{ // IE
					mainMenuElements[newID].getElementsByTagName("a")[0].style.cssText= on_off;
					
					if(mainMenuElements[newID-1] != null) mainMenuElements[newID-1].getElementsByTagName("a")[0].style.cssText=on_on;
					
					if(mainMenuElements[newID-2] != null) mainMenuElements[newID-2].getElementsByTagName("a")[0].style.cssText=off_on;
				}
				// if newID+1 is selected set current block to on-on, set previous block to off-on
			}else if(mainMenuElements[newID+1] != null && (isSelected(mainMenuElements[newID+1].getElementsByTagName("a")[0].getAttribute("class")) || isSelected(mainMenuElements[newID+1].getElementsByTagName("a")[0].className))){
				
				if(mainMenuElements[newID].getElementsByTagName("a")[0].addEventListener) { //Firefox
				
					mainMenuElements[newID].getElementsByTagName("a")[0].setAttribute("style", on_on);
					
					if(mainMenuElements[newID-1] != null) mainMenuElements[newID-1].getElementsByTagName("a")[0].setAttribute("style", off_on);
						
				}else{ // IE
					
					
					mainMenuElements[newID].getElementsByTagName("a")[0].style.cssText=on_on;
					
					if(mainMenuElements[newID-1] != null) mainMenuElements[newID-1].getElementsByTagName("a")[0].style.cssText=off_on;
				}
				 
			}else{
				
				// else Set current block to on-off and previous block to off-on.
				if(mainMenuElements[newID-1] != null){
					if(mainMenuElements[newID-1].getElementsByTagName("a")[0].addEventListener) {
						mainMenuElements[newID-1].getElementsByTagName("a")[0].setAttribute("style", off_on); //Firefox
					}else{
						mainMenuElements[newID-1].getElementsByTagName("a")[0].style.cssText= off_on; // IE
					}
				}
			}
			
			
			/*
			 * This is the END Area that is used to define the rules for setting menu option classes
			 */
			
			
		}else{
			
			/*
			 * The Cleaner
			 */
			 
			if(mainMenuElements[newID].getElementsByTagName("a")[0].addEventListener) { //Firefox
				//alert("Cleaning this " + mainMenuElements[newID-1].getElementsByTagName("a")[0].getAttribute("class") + " got this " + stripClasses(mainMenuElements[newID-1].getElementsByTagName("a")[0].getAttribute("class")));
				if(mainMenuElements[newID-1] != null) mainMenuElements[newID-1].getElementsByTagName("a")[0].setAttribute("style", '');
				mainMenuElements[newID].getElementsByTagName("a")[0].setAttribute("style", '');
			}else{ // IE
				if(mainMenuElements[newID-1] != null) mainMenuElements[newID-1].getElementsByTagName("a")[0].style.cssText='';
				mainMenuElements[newID].getElementsByTagName("a")[0].style.cssText='';
			} 
		}
	}
}

function isSelected(classGroup){
	
	if(classGroup == null) return false;
	
	if(classGroup.indexOf('in-area') != -1){
		return true;
	}else{
		return false;
	}
}

function isPreSelected(classGroup){
	
	if(classGroup == null) return false;
	
	if(classGroup.indexOf('pre-highlight-area') != -1){
		return true;
	}else{
		return false;
	}
}

/*
 * Title:		menuReset
 * Author:		Andy Smith
 * Date:		26/02/2008
 * Description:	Looks for the main-menu then makes property 
 *				changes to the menu to improve accessibility 
 *				and validity.
 * arguments:	N/a
 * output:		N/a
 *
 * Paused in the interests of better using the time
 *
 */
function menuReset(){

	var x = document.getElementById('mainMenu');
	if (!x) return;
	
	var y = x.getElementsByTagName('a');
	for (var i=0;i<y.length;i++){
 		
		/* Get this to work when we have time.
		
		y[i].onmouseover = 'mainMenuSwap(i, \'set\')';
		y[i].onfocus = 'mainMenuSwap(i, \'set\')';
		y[i].onmouseout = 'mainMenuSwap(i, \'unset\')';
		y[i].onblur = 'mainMenuSwap(i, \'unset\')';
		*/
		
		if(i == 0) y[i].className = y[i].className + ' first-child';
		if(i == 3) y[i].className = y[i].className + ' iphone-last-child';
		if(i == 4) y[i].className = y[i].className + ' iphone-first-child';
		if(i == (y.length -1)) y[i].className = y[i].className + ' last-child';
		
	}
}


/*
 * Title:		externalLinks
 * Author:		Andy Smith
 * Date:		26/02/2008
 * Description:	Looks for the main-menu then makes property 
 *				changes to the menu to improve accessibility 
 *				and validity.
 * arguments:	N/a
 * output:		N/a
 *
 * 
 


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";
			if(anchor.addEventListener) { //Firefox
				anchor.setAttribute("class", 'external');
			}else{ // IE
				anchor.className='external';
			} 
		}
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "pdf"){
			anchor.target = "_blank";
			if(anchor.addEventListener) { //Firefox
				anchor.setAttribute("class", 'pdf');
			}else{ // IE
				anchor.className='pdf';
			}
		}
	}
}
*/
function externalLinks()  {

     if (!document.getElementsByTagName) return;
	 var journey  = document.getElementById("journeyPlannerForm")
	 if(journey){
	    document.getElementById("journeyPlannerForm").target = "_blank";
	 }
}
/*
 * Title:		externalLinks
 * Author:		Andy Smith
 * Date:		26/02/2008
 * Description:	Looks for the main-menu then makes property 
 *				changes to the menu to improve accessibility 
 *				and validity.
 * arguments:	N/a
 * output:		N/a
 *
 * 
 */
 
prepPDF = function() {
  var pageHasPDFs = false;
  var mainContent = document.getElementById("centralBlock");
  var links = mainContent.getElementsByTagName("a");
  for (i=0; i<links.length; i++) {
    var currentLink = links[i];
    var images = currentLink.getElementsByTagName("img");
    /*Check if the link is an image (we don't want icons next to images) and that ".pdf" appears in the link href.*/
    if (images.length == 0 && currentLink.href.indexOf('.pdf') != -1) {
      var curClass = currentLink.getAttribute("class");
      if (curClass != null) { currentLink.className = curClass + " pdf"; }
      else { currentLink.className = "pdf"; }
      pageHasPDFs = true;
    }
	
	if (currentLink.getAttribute("href") && currentLink.getAttribute("rel") == "external"){
		currentLink.target = "_blank";
		if(currentLink.addEventListener) { //Firefox
			currentLink.setAttribute("class", 'external');
		}else{ // IE
			currentLink.className='external';
		} 
	}
	
	if (currentLink.getAttribute("class") == "print-this"){
		if(currentLink.addEventListener) { //Firefox
			currentLink.setAttribute("href", 'javascript:printPage();');
		}else{ // IE
			currentLink.href='javascript:printPage();';
		} 
	}
	
  }

}

/*
 * Title:		printPage
 * Author:		Andy Smith
 * Date:		10/04/2008
 * Description:	Looks for the main-menu then makes property 
 *				changes to the menu to improve accessibility 
 *				and validity.
 * arguments:	N/a
 * output:		N/a
 *
 * 
 */


function printPage() {
	window.print()
}


/*
 * Title:		Window.onLoad
 * Author:		Andy Smith
 * Date:		11/04/2008
 * Description:	Looks for the main-menu then makes property 
 *				changes to the menu to improve accessibility 
 *				and validity.
 * arguments:	N/a
 * output:		N/a
 *
 * 
 */


/*
 *  20081111 - new icon link code
 */

function combineLinks1(a, b){
	var hoverOverLink =  document.getElementById('leftHandColumn').getElementsByTagName('A');
		for (var i=0; i<hoverOverLink.length; i++) {
			if(hoverOverLink[i].className== a+' deselected'){
					hoverOverLink[i].className=a +' deselected3';
			}
			else if(hoverOverLink[i].className== a +' deselected3'){
					hoverOverLink[i].className=a +' deselected';
			}
			else if(hoverOverLink[i].className== 'hoverOverLinkNormal'){
					hoverOverLink[i].className= b;
			}
			else if(hoverOverLink[i].className== b){
					hoverOverLink[i].className='hoverOverLinkNormal';
			}
		}
	}
function combineLinks(c, d){
	var hoverOverLink =  document.getElementById('leftHandColumn').getElementsByTagName('A');
		for (var i=0; i<hoverOverLink.length; i++) {
			if(hoverOverLink[i].className== c){
				hoverOverLink[i].className=c +' deselected2';
			}
			else if(hoverOverLink[i].className== c+' deselected2'){
				hoverOverLink[i].className=c;
			}
			else if(hoverOverLink[i].className== d){
				hoverOverLink[i].className= 'hoverOverLink';
			}
			else if(hoverOverLink[i].className== 'hoverOverLink'){
				hoverOverLink[i].className=d;
			}
		}
	}
	
/*
 *   new icon link code end
 */

window.onload = function () {
	menuReset();
	externalLinks();
	prepPDF();
	
	var tempDiv = document.createElement("DIV");
	tempDiv.setAttribute("id", "targetForIframe");
	last_child = document.getElementById('pagetop').lastChild;
	document.getElementById('pagetop').insertBefore(tempDiv, last_child.nextSibling);

	document.getElementById('targetForIframe').innerHTML="<iframe style='height:0px;width:0px;visibility:hidden' src='about:blank'>this frame prevents back forward cache</iframe>";
}


