var timeOut  		= 40;
var startSpeed		= 4;
var currentSpeed	= startSpeed;

var timeoutIDExpand;
var timeoutIDCollapse;
var tryToExpandID;

var timeoutIDInit;
var currentItem;
var itemMoving = false;

var imgCOn = new Image();
imgCOn.src = "/images/bullets/sphere_c.gif";
var imgCOff = new Image();
imgCOff.src = "/images/bullets/sphere_c_o.gif";

var imgVOn = new Image();
imgVOn.src = "/images/bullets/sphere_v.gif";
var imgVOff = new Image();
imgVOff.src = "/images/bullets/sphere_v_o.gif";

var imgEOn = new Image();
imgEOn.src = "/images/bullets/sphere_e.gif";
var imgEOff = new Image();
imgEOff.src = "/images/bullets/sphere_e_o.gif";

var imgOOn = new Image();
imgOOn.src = "/images/bullets/sphere_o.gif";
var imgOOff = new Image();
imgOOff.src = "/images/bullets/sphere_o_o.gif";


//expands the first item
function expandFirst() {
	clearTimeout(timeoutIDInit);
	timeoutIDInit = null;
	var found = false;
	var courseBlockCollection = document.getElementsByTagName("DIV");
	for (var i = 0; i < courseBlockCollection.length && !found; i++) {
		if (courseBlockCollection[i].className == "courseBlock") {
			var firstBlock = courseBlockCollection[i];
			var contents = firstBlock.getElementsByTagName("DIV")[1];
			expandItem(contents.id.split("_")[2]);
			found = true;
		}
	}
}


//collapses all items
function CollapseAll() {

	var courseBlockCollection = new Array();
	var temp = document.getElementsByTagName("DIV");
	for (var h = 0; h < temp.length; h++) {
		if (temp[h].className == "courseBlock") {
			courseBlockCollection[courseBlockCollection.length] = temp[h];
		}
	}
		
	for (var i = 0; i < courseBlockCollection.length; i++) {
		
		var imgCollection = courseBlockCollection[i].getElementsByTagName("IMG");
		var showHideImg = imgCollection[1];
		showHideImg.src = switchPlusMinus(showHideImg.src)
		
		var divCollection = courseBlockCollection[i].getElementsByTagName("DIV");
		var contents = divCollection[1];
			
		contents.style.display = "none";
		contents.style.overflow = "hidden";
	}
}

//expands an item
function expandItem(contentsNr) {

	var contentsDiv = document.getElementById("item_contents_" + contentsNr);	
	contentsDiv.style.display = "block";
		
	if(!contentsDiv.style.height || contentsDiv.targetHeight == 0) {
		contentsDiv.targetHeight = contentsDiv.offsetHeight; //set target height
		contentsDiv.style.height = "0";
	}
	
	var oldHeight = parseInt(contentsDiv.style.height.split("px")[0]);
	currentSpeed = (currentSpeed * 1.1);
	var newHeight = oldHeight + currentSpeed;
	
	if (newHeight > contentsDiv.targetHeight) {
		newHeight = contentsDiv.targetHeight;
	}

	contentsDiv.style.height = newHeight;
	
	if (newHeight < contentsDiv.targetHeight) {
		itemMoving = true;
		timeoutIDExpand = setTimeout("expandItem('" + contentsNr + "')", timeOut);
		
	} else {
		clearTimeout(timeoutIDExpand);
		timeoutID = null;
		itemMoving = false;
		currentSpeed = startSpeed;
		currentItem = contentsNr; //set active item
			
		//swap icon
		var image = contentsDiv.parentNode.getElementsByTagName("IMG")[1];	
		image.src = switchPlusMinus(image.src)
		
		//done with expanding
		if (inCMS == false) {
			//if we're not in the CMS, show only the fields for the current mode
			hideSwappableContent();
			showSwappableContent(currentItem);
		}
		
		//display the correct colored sphere
		loadSpheres(currentItem);
	}
}

//collapse an item
function collapseItem(contentsNr) {
	
	var contentsDiv = document.getElementById("item_contents_" + contentsNr);
	contentsDiv.style.display = "block";
	contentsDiv.targetHeight = 0;
	var oldHeight = parseInt(contentsDiv.style.height.split("px")[0]);
	currentSpeed = (currentSpeed * 1.15);
	var newHeight = oldHeight - currentSpeed;

	//newHeight may never become less than zero! -Andy
	if(newHeight <= 1) {
		newHeight = 0;
	}
		
	contentsDiv.style.height = newHeight;
	
	if (newHeight > contentsDiv.targetHeight) {
		itemMoving = true;
		timeoutIDCollapse = setTimeout("collapseItem('" + contentsNr + "')", timeOut);
	} else {
		clearTimeout(timeoutIDCollapse);
		timeoutIDCollapse = null;
		contentsDiv.style.display = "none";
		contentsDiv.style.height = "";
		itemMoving = false;
		currentSpeed = startSpeed;
			
		//swap icon
		var image = contentsDiv.parentNode.getElementsByTagName("IMG")[1];
		image.src = switchPlusMinus(image.src)
	}
}


function contentItemHover(div) {

	if(div.style.backgroundColor == '#EEF3F8' || div.style.backgroundColor == '') {
		div.style.backgroundColor = '#E0E9F3';
		/*
		if(navigator.appVersion.indexOf("MSIE") != -1) {
			div.style.cursor = 'hand';
		} else {
			div.style.cursor = 'pointer';
		}
		*/
	} else {
		div.style.backgroundColor = ''
		//div.style.cursor = ''
	}
}

function courseTypeHover(div) {
	if(navigator.appVersion.indexOf("MSIE") != -1) {
		div.style.cursor = 'hand';
	} else {
		div.style.cursor = 'pointer';
	}
}

function togglecontentsView(aTag) {

	if (inCMS) return; //menu items are only foldable outside of the CMS
	
		var courseBlock = aTag.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
		var contents = courseBlock.getElementsByTagName("DIV")[1];
		var contentsNr = contents.id.split("_")[2];
		var currentHeight = parseInt(contents.style.height.split("px")[0]);
			
	if(!itemMoving) {
		if (currentHeight) {
			collapseItem(contentsNr);
		} else {
				
			if (getItemStatus(currentItem) == "block") {
				/*
				option 1: collapse the current item and expand the other simultaneously
				collapseItem(currentItem);
				expandItem(contentsNr);
				
				option 2: collapse the current item without animation
				collapseAtOnce(currentItem);
				expandItem(contentsNr);
				
				option 3: collapse 1 first, then expand the other
				collapseAndExpand(currentItem, contentsNr)
				*/
				collapseAndExpand(currentItem, contentsNr);
			} else {
				expandItem(contentsNr);
			}
		}
	}
	
}

function collapseAtOnce(currentItem) {
	var contentsDiv = document.getElementById("item_contents_" + currentItem);
	contentsDiv.style.display = "none";
	contentsDiv.style.height = "0px";
}

function courseTypeToggle(div) {
	var aTag = div.parentNode.getElementsByTagName("DIV")[0].getElementsByTagName("A")[0];
	togglecontentsView(aTag);
}

function getItemStatus(contentsNr) {
	var contentsDiv = document.getElementById("item_contents_" + contentsNr);	
	return contentsDiv.style.display;
}


//starts the collapse of the first item, the initiates the 
//tryToExpand function
function collapseAndExpand(collapseNr, expandNr) {
	var currentDiv = document.getElementById("item_contents_" + collapseNr);
	collapseItem(collapseNr);
	tryToExpand(collapseNr, expandNr);
}

//checks periodically if the expanding item can be expanded
function tryToExpand(collapseNr, expandNr) {
	var currentDiv = document.getElementById("item_contents_" + collapseNr);
	if (currentDiv.style.height) {
		tryToExpandID = setTimeout("tryToExpand(" + collapseNr + "," + expandNr + ")", 100);
	} else {
		clearTimeout(tryToExpandID);
		expandItem(expandNr);
	}
}


function checkSharedContent() {
	//check for content present in shared content fields
	if(!classroomHasContent('topics')) {renderNotIds("swap_class_topics")}; 		//topics
	if(!classroomHasContent('target_audience')) {renderNotIds("target_audience")}; 	//prerequisites
	if(!classroomHasContent('prerequisites')) {renderNotIds("prerequisites")}; 		//prerequisites
	if(!classroomHasContent('followons')) {renderNotIds("followons")}; 				//followons
	if(!classroomHasContent('certifications')) {renderNotIds("certifications")}; 	//certifications
	
	showFoldAllButton();
}

function hideSwappableContent() {
	var allDivs = document.getElementsByTagName("DIV");
	var swapDivs = new Array();
	
	//get all the swap divs
	for (var i = 0; i < allDivs.length; i++) {
		var id = allDivs[i].id;
		if (id.indexOf("swap") != -1) {
			swapDivs[swapDivs.length] = allDivs[i];
		}
	}
	for (var j = 0; j < swapDivs.length; j++) {
		swapDivs[j].style.display = "none";
	}
}


function showSwappableContent(itemNr) {

	//at the start of this function, ALL the swappable divs will be invisible
	//this function makes the correct ones visible

	var allDivs = document.getElementsByTagName("DIV");
	var swapDivs = new Array();
	
	var fieldA;
	var fieldB;
	var contentDivA;
	var contentDivB;
	
	//gather all the swap divs
	for (var i = 0; i < allDivs.length; i++) {
		var id = allDivs[i].id;
		if (id.indexOf("swap") != -1) {
			swapDivs[swapDivs.length] = allDivs[i];
		}
	}

	switch (parseInt(itemNr)) {
		case 1:
			//Classroom Learning		
			//display all the swap divs
			for (var j = 0; j < swapDivs.length; j++) {
				if (swapDivs[j].id.indexOf("swap_class") != -1) {
					swapDivs[j].style.display = "block";
				}
			}
			
			//hide the divs that have no content
			if(!classroomHasContent('course_summary')) {
				renderNotIds("swap_class_summary");
			}
			if(!classroomHasContent('detailed_outline')) {
				renderNotIds("swap_class_outline");
			}
			if(!classroomHasContent('topics')) {
				renderNotIds("swap_class_topics");
			}
			if(!classroomHasContent('value_adds')) {
				renderNotIds("swap_class_value_add");
			}
		
		checkSharedContent();
		break;

		case 2:
			//virtual learning		
			for (var j = 0; j < swapDivs.length; j++) {
				if (swapDivs[j].id.indexOf("swap_virtual") != -1) {
					swapDivs[j].style.display = "block";
				}
			}
			
			if (inCMS == false) {	
				
				/* 1) Course summary -------------------------------------- */
				fieldA = document.getElementById("swap_virtual_summary");
				fieldB = document.getElementById("swap_class_summary");
				
				if ((fieldA.innerHTML == fieldB.innerHTML) || (!hasContent(fieldA.innerHTML))) {
					//check op classroom
					if (classroomHasContent('course_summary')) {
						//Classroom has content for this field, show the classroom content
						fieldA.style.display = "none"; fieldB.style.display = "block";
					} else {
						//Classroom does not have content for this field, hide both fields
						fieldA.style.display = "none"; fieldB.style.display = "none";
					}
				}
				/* ------------------------------------------------------- */
				
				
				/* 2) Detailed /course outline (foldout) -------------------------------------- */
				fieldA = document.getElementById("swap_virtual_outline");
				fieldB = document.getElementById("swap_class_outline");
				
				var contentDivA = fieldA.getElementsByTagName("DIV")[0];
				var contentDivB = fieldB.getElementsByTagName("DIV")[0];
								
				if ((contentDivA.innerHTML == contentDivB.innerHTML) || (!hasContent(contentDivA.innerHTML))) {
					//fields are alike, or virtual outline has no content
					if (classroomHasContent('detailed_outline')) {
						//Classroom has content for this field, show the classroom content
						fieldA.style.display = "none"; fieldB.style.display = "block";
					} else {
						//Classroom does not have content for this field, hide both fields
						fieldA.style.display = "none"; fieldB.style.display = "none";
					}
				}
				
					
				/* 3) Value adds -------------------------------------- */
				fieldA = document.getElementById("swap_virtual_value_add");
				fieldB = document.getElementById("swap_class_value_add");
				
				var contentDivA = fieldA.getElementsByTagName("DIV")[1];
				var contentDivB = fieldB.getElementsByTagName("DIV")[1];
							
				if ((contentDivA.innerHTML == contentDivB.innerHTML) || (!hasContent(contentDivA.innerHTML))) {
					if (classroomHasContent('value_adds')) {
						//Classroom has content for this field, show the classroom content
						fieldA.style.display = "none"; fieldB.style.display = "block";
					} else {
						//Classroom does not have content for this field, hide both fields
						fieldA.style.display = "none"; fieldB.style.display = "none";
					}
				}
				/* ------------------------------------------------------- */
				
				
				/* 3) Value adds -------------------------------------- */
				if(!classroomHasContent('topics')) {
					renderNotIds("swap_class_topics")
				} else {
					renderIds("swap_class_topics");
				}
				/* --------------------------------------------------- */
				
			}
		checkSharedContent();
		
		break;
	
		case 3: 
			//SPeL (self-paced E-learning)	
			for (var j = 0; j < swapDivs.length; j++) {
				if (swapDivs[j].id.indexOf("swap_spel") != -1) {
					swapDivs[j].style.display = "block";
				}
			}
			
			if (inCMS == false) {
			
				/* 1) Titles -------------------------------------- */
				/* ------------------------------------------------------- */
				
				
				/* 2) Course summary -------------------------------------- */
				
				fieldA = document.getElementById("swap_spel_summary");
				fieldB = document.getElementById("swap_class_summary");
						
				if ((fieldA.innerHTML == fieldB.innerHTML) || (!hasContent(fieldA.innerHTML))) {
					if (classroomHasContent('course_summary')) {
						//Classroom has content for this field, show the classroom content
						fieldA.style.display = "none"; fieldB.style.display = "block";
					} else {
						//Classroom does not have content for this field, hide both fields
						fieldA.style.display = "none"; fieldB.style.display = "none";
					}
				}
				
				/* ------------------------------------------------------- */
				
				
				/* 3) Detailed /course outline (foldout) -------------------------------------- */
				fieldA = document.getElementById("swap_spel_outline");
				fieldB = document.getElementById("swap_class_outline");
				
				var contentDivA = fieldA.getElementsByTagName("DIV")[0];
				var contentDivB = fieldB.getElementsByTagName("DIV")[0];
				
				if ((contentDivA.innerHTML == contentDivB.innerHTML) || (!hasContent(contentDivA.innerHTML))) {
					if (classroomHasContent('detailed_outline')) {
						//Classroom has content for this field, show the classroom content
						fieldA.style.display = "none"; fieldB.style.display = "block";
					} else {
						//Classroom does not have content for this field, hide both fields
						fieldA.style.display = "none"; fieldB.style.display = "none";
					}
				}	
				/* ------------------------------------------------------- */
				
				
				/* 4) Topics (what you'll learn) -------------------------------------- */
				fieldA = document.getElementById("swap_spel_topics");
				fieldB = document.getElementById("swap_class_topics");
				
				var contentDivA = fieldA.getElementsByTagName("DIV")[0];
				var contentDivB = fieldB.getElementsByTagName("DIV")[0];
				
				if ((fieldA.innerHTML == fieldB.innerHTML) || (!hasContent(contentDivA.innerHTML))) {
					if (classroomHasContent('topics')) {
						//Classroom has content for this field, show the classroom content
						fieldA.style.display = "none"; fieldB.style.display = "block";
					} else {
						//Classroom does not have content for this field, hide both fields
						fieldA.style.display = "none"; fieldB.style.display = "none";
					}
				}
				/* ------------------------------------------------------- */	

				
				/* 5) Value adds -------------------------------------- */
				fieldA = document.getElementById("swap_spel_value_add");
				fieldB = document.getElementById("swap_class_value_add");
				
				var contentDivA = fieldA.getElementsByTagName("DIV")[1];
				var contentDivB = fieldB.getElementsByTagName("DIV")[1];
				
				if ((contentDivA.innerHTML == contentDivB.innerHTML) || (!hasContent(contentDivA.innerHTML))) {
					if (classroomHasContent('value_adds')) {
						//Classroom has content for this field, show the classroom content
						fieldA.style.display = "none"; fieldB.style.display = "block";
					} else {
						//Classroom does not have content for this field, hide both fields
						fieldA.style.display = "none"; fieldB.style.display = "none";
					}
				}
				/* ------------------------------------------------------- */
			}
			
		checkSharedContent();
		break;
		
		case 4:
			//On-site learning
			//Treat this like Classroom learning

			for (var j = 0; j < swapDivs.length; j++) {
				if (swapDivs[j].id.indexOf("swap_class") != -1) {
					swapDivs[j].style.display = "block";
				}
			}
			
			//hide the divs that have no content
			if(!classroomHasContent('course_summary')) {
				renderNotIds("swap_class_summary");
			}
			if(!classroomHasContent('detailed_outline')) {
				renderNotIds("swap_class_outline");
			}
			if(!classroomHasContent('topics')) {
				renderNotIds("swap_class_topics");
			}
			if(!classroomHasContent('value_adds')) {
				renderNotIds("swap_class_value_add");
			}
			
			checkSharedContent();
		break;
	}		
}

function hasContent(field) {
	field = field.replace(/<BR>/i, "");
	field = field.replace(/<\/BR>/i, "");
	field = field.replace(/<DIV>/i, "");
	field = field.replace(/<\/DIV>/i, "");
	field = field.replace(/&nbsp;/i, "");
	
	if (trim(field) == "") {
		return false;
	} else {
		return true;
	}
}


//checks whether classroom field learning has content
function classroomHasContent(field) {
	var tmp;
	if (field == 'course_summary') {
		tmp = document.getElementById("swap_class_summary").innerHTML;
	} else if (field == 'detailed_outline') {
		tmp = document.getElementById("swap_class_outline").getElementsByTagName("DIV")[0].innerHTML;
	} else if (field == 'topics') {
		tmp = document.getElementById("swap_class_topics").getElementsByTagName("DIV")[0].innerHTML;
	} else if (field == 'followons') {
		tmp = document.getElementById("followons").getElementsByTagName("DIV")[0].innerHTML;
	} else if (field == 'prerequisites') {
		tmp = document.getElementById("prerequisites").getElementsByTagName("DIV")[0].innerHTML;
	} else if (field == 'certifications') {
		tmp = document.getElementById("certifications").getElementsByTagName("DIV")[0].innerHTML;
	} else if (field == 'target_audience') {
		tmp = document.getElementById("target_audience").getElementsByTagName("DIV")[0].innerHTML;
	} else if (field == 'value_adds') {
		tmp = document.getElementById("swap_class_value_add").getElementsByTagName("DIV")[1].innerHTML;
	}
	if (hasContent(tmp)) {return true;} else {return false;}
}


function trim(str){return str.replace(/^\s+|\s+$/g,'')} 

function loadSpheres(itemNr) {
	var sphere_c = document.getElementById("sphere_c");
	var sphere_v = document.getElementById("sphere_v");
	var sphere_e = document.getElementById("sphere_e");
	var sphere_o = document.getElementById("sphere_o");
	
	if (itemNr == 1) {	
		sphere_c.src = imgCOn.src; sphere_v.src = imgVOff.src; sphere_e.src = imgEOff.src; sphere_o.src = imgOOff.src;
	}
	if (itemNr == 2) {		
		sphere_c.src = imgCOff.src; sphere_v.src = imgVOn.src; sphere_e.src = imgEOff.src; sphere_o.src = imgOOff.src;
	}
	if (itemNr == 3) {		
		sphere_c.src = imgCOff.src; sphere_v.src = imgVOff.src; sphere_e.src = imgEOn.src; sphere_o.src = imgOOff.src;
	}
	if (itemNr == 4) {
		sphere_c.src = imgCOff.src;	sphere_v.src = imgVOff.src; sphere_e.src = imgEOff.src; sphere_o.src = imgOOn.src;
	}
}