/*
Note
-----
The array 'arrPlaces' contains must contain the ids of all panels to be revealed when their link is clicked.
The ID's of the panel-revealing-links must follow the naming convention: link_[panel ID] (e.g. if the panels ID is 'foo' it's link's ID must be 'link_foo')

TODO
1. Activate close panel links also. Curently Jscript is in page
2. 

*/

// Only show Carousel if javascript is on
function showcarousel(){
	
	// alert("function showcarousel accessed");
	
	// safty check
	if (!document.getElementById("carousel")) return false;
	
	var carouselcontainer = document.getElementById("carousel");
	
	var linkclass = carouselcontainer.getAttribute("class");
	
	// linkclass = linkclass.replace(/hide/i, "show-carousel"); // Not working in IE
		
	carouselcontainer.setAttribute("class", linkclass);
}

// Assign display: none; style onload. (So search bots and those with Jscript disabled can access content. 
function hidepanels(){ 
	
	// safty check
	if (!document.getElementById("processcontent")) return false;
	
	var container= document.getElementById("processcontent");
	var panels = container.getElementsByTagName("div");
	

	
	// Hide the panels
	for (var i=0; i<panels.length; i++) {

		// Change class to hide
		panels[i].className="hidden";	

	}
}

function activatelinks(){

	// alert("function activatelinks accessed");
	
	// Safty check
	if (!document.getElementById("active-links")) return false;

	// 1. get a list of <a>'s in the document.
	var hotSpots =  document.getElementById("active-links");
	
	// Safty check
	if (!hotSpots.getElementsByTagName("a")) return false;
	
	var links = hotSpots.getElementsByTagName("a");
 
	for (var i=0; i<links.length; i++) {
		
		// 1. set class to display links (graceful degradation)
		links[i].className ="question replacement showlink";
		
		// 2. add onclick event.
		links[i].onclick = function() {
			
			// safty check
			if(this.getAttribute("id")){		
								
				// extract id and prep to pass in to show object function
				var myID = this.getAttribute("id");
				var showpanel = myID.replace("link_", "");
				viewObjById(showpanel);
			}
			
			return false; // stop mouse click executing link.
		}
	}
}

function viewObjById(showPanel){
	var arrPlaces= new Array("process_1","process_2","process_3","process_4","process_5","process_6","process_7","process_8","process_9","process_10");
	
	//TO DO
	// 1. dynamically build the array from the list ids on the fly. 
	
	// alert("viewObjById function accessed");
	// alert("viewObjById function accessed showPanel is " + showPanel);	
	
	// for every element in the array (above) 
	for(x in arrPlaces){
		// if array entry is equal to the string passed to the function. 
		if(arrPlaces[x]==showPanel){
			
			// Get the <a> node with the matching ID 
			var strObject=document.getElementById(arrPlaces[x]);
			
			// if the node exists
			if(strObject){
				// set it's CSS class
				document.getElementById(arrPlaces[x]).className="showpanel";
			}
			 
		}else{
			// for all other elements in the array  
			
			// Get the <a> node with the matching ID
			var strObject=document.getElementById(arrPlaces[x]);
			
			// if the node exists
			if(strObject){
				//  set it's CSS class 
				document.getElementById(arrPlaces[x]).className="hidden"; 
			}
		}
	}
}

// queue functions to load on window.onload
function addLoadEvent(func) {

	var oldonload = window.onload;

		if (typeof window.onload != 'function') {

		window.onload = func;

		} else {

		window.onload = function() {

		oldonload();

		func();

		}
	}
}


// queue onload events
addLoadEvent(showcarousel);
// addLoadEvent(hidepanels); // put .hidden class on panels instead to allow one to initially show
addLoadEvent(activatelinks);

// TEST
// addLoadEvent(function(){ viewObjById("process_1"); });


