//News related variables
var textArray = new Array;
var iNews=0;
var iMax=0;
var xmlDoc;
//Menu related variables
var menuLink;
var hURL = stripUrl();

$(document).ready(function(){
	enableMenu();
	externalLinks();
	stripeTables();
	//loadNewsXml("/opencms/opencms/tnf/endurancechallenge/content/scripts/xml/news.xml");
	loadNewsXml("/endurancechallenge/content/scripts/xml/news.xml");
});
function enableMenu(){
	//Hide all submenus
	$("ul.submenu").hide();

	//Show hide state for all menu arrows
	$("div.menuLink").find("div.menuoff").show();
	$("div.menuLink").find("div.menuon").hide();
	
	//user cursor for menu buttons
	$("div.menuLink").css("cursor","pointer");	
	
	//onclick buttons
	$("div.menuLink").click(function() {
		var $nextDiv = $(this).next();
		var $visibleSiblings = $('ul.submenu:visible').not($(this).next());
		
		if($visibleSiblings.length){
			$("div.menuLink").find("div.menuoff").show();
			$("div.menuLink").find("div.menuon").hide();
			$visibleSiblings.toggle('fast', function() {
				window.location = menuLink;
				//$nextDiv.toggle('fast', function(){	
				//});
				//$nextDiv.parent().find("div.menuoff").hide();
				//$nextDiv.parent().find("div.menuon").show();
		
			});
		}else{
			setTimeout("window.location = menuLink",500);
			//window.location = menuLink;
			//$nextDiv.toggle('normal', function(){
			//});
			//$nextDiv.parent().find("div.menuoff").toggle();
			//$nextDiv.parent().find("div.menuon").toggle();
		}
	});
	
	//Find which link in list matches  code of current page
	$("ul.submenu a[@href*="+hURL+"]").addClass("smenuCur").parent().parent().toggle().prev().each(function(i) {
		$(this).find("div.menuoff").toggle();
		$(this).find("div.menuon").toggle();	
	});
}
function setLink(newURL){
	if(hURL != false){
		if(newURL.substring(0,3) == hURL.substring(0,3)){
			window.location = newURL;
		}else{
			menuLink = newURL;
		}
	}else{
		menuLink = newURL;
	}
}
//Split the URL String into something workable
function stripUrl(){
	if(location.href.lastIndexOf(".html") != -1){
		return location.href.substring(location.href.lastIndexOf("/")+1,location.href.length);
	}else{
		return false;
	}
}
//select all links that need to be external for strict compliance
function externalLinks(){
	$("a[@rel$=external]").click(function(){
		this.target = "_blank";
	});
	//convert forms
	var forms = document.getElementsByTagName("form");
	for(var i = 0; i < forms.length; i++)
	{
		var form = forms[i];
		if(form.getAttribute("action").substring(0, 4) == "http") {
			form.target = "signup";
		}
	}
}
//function to stripe tables
function stripeTables(){
	$("table.striped tr:nth-child(even)").addClass("stripe");
}	
//function to load XML based news
function loadNewsXml(source) {
	$.ajax({
		url: source,
		type: 'GET',
		dataType: 'xml',
		timeout: 1000,
		error: function(){
			$("#newstext").html("<strong>&nbsp;</strong>")
		},
		success: function(xml){
			$(xml).find('newsitem').each(function(i){
				textArray[i] = $(this).text();
			});
			insertTicker();
		}
	});
}

//function to insert text for news
function insertTicker(){
	if(textArray.length > 1){
		$("#newstext").fadeOut("fast", function(){
			$(this).html(textArray[iNews]).fadeIn("fast");
			if(iNews<textArray.length-1){
				iNews++;
			}else{
				iNews=0;
			}
			window.setTimeout("insertTicker()",10000);
		});
	}else{
		$("#newstext").html(textArray[iNews]);
	}
}

//Load Image Gallery
function LoadGallery(pictureName,imageFile,titleCaption,captionText)
{
  document.getElementById(pictureName).src = imageFile;
  document.getElementById(titleCaption).innerHTML=captionText;
}