/************************************************************************/
/*															 			*/
/*	Accordent Event Menu v2.00											*/
/*	=========================								 			*/
/*																		*/
/*	Last Edited: 9/15/2008									  			*/
/*	Please send all comments & bug reports to: support@accordent.com	*/
/*																		*/
/************************************************************************/

var topicsVisibility = false; // Don't Show Topics
var rsrcsVisibility = false	// Don't Show Resources Tab
var downldVisibility = false	// Don't show Downloads Section
var linksVisibility = false	// Don't show Links Section
var emailVisibility = false // Don't show Email Tab
var captionsVisibility = false; // Don't show Closed Captions
var chaptersLoaded = false;
var blank = /^[\s]*$/i;

// Checks to see if browser is Firefox and user has Port25 plugin installed.
function hasP25() {
	var P25 = false;
	var plugin = navigator.mimeTypes && navigator.mimeTypes["application/x-ms-wmp"];
	var FFcheck = navigator.userAgent.indexOf("Firefox");
	if (plugin && FFcheck != -1)
		P25 = true;
	return P25;
}

function isWin() {
	var win = false;
	var ua = navigator.userAgent.toLowerCase();
	if (ua.indexOf('win') > -1)
		win = true;
	//if(win){alert('is win');}else{alert('is NOT win');}
	return win;
}

// check to see if user has enabled chaptering functionality
function topicsTabCheck() {
	try{
		var topicsTabTag = 'yes';
		var onDemand = 'visible';
		//var topicsTabTag = 'yes';
		var chkNoTopicsTabTag = topicsTabTag.indexOf("<@");
		if ((topicsTabTag != '' && chkNoTopicsTabTag < 0) && onDemand != 'hidden') {
			topicsVisibility = true;
		}		
	} catch(e) {}
	return topicsVisibility;
}
topicsTabCheck();

// load correct xsl file depending on browser type and plugin
function loadChapters(divLyr){
	if(document.implementation && document.implementation.createDocument){
		// Mozilla
		var xsltProcessor = new XSLTProcessor();
		// load the xslt file
		var myXMLHTTPRequest = new XMLHttpRequest();
		//	IF is WINDOWS MEDIA
		
		if ((isWin() && hasP25()) || isPlayerReal()){
			myXMLHTTPRequest.open("GET", "chapters.xsl", false);
			// otherwise load list of time/topic without link
		} else {
			myXMLHTTPRequest.open("GET", "chapters_text.xsl", false);
		}

		myXMLHTTPRequest.send(null);
		// get the XML document
		xslStylesheet = myXMLHTTPRequest.responseXML;
		xsltProcessor.importStylesheet(xslStylesheet);
		// load the xml file
		myXMLHTTPRequest = new XMLHttpRequest();
		myXMLHTTPRequest.open("GET", "presentation.xml", false);
		myXMLHTTPRequest.send(null);
		var xmlSource = myXMLHTTPRequest.responseXML;
		//transform
		 var resultDocument = xsltProcessor.transformToFragment(xmlSource, document);
		document.getElementById(divLyr).appendChild(resultDocument);
	}else if(window.ActiveXObject){
		// IE
		// Load XML
		xml = new ActiveXObject("MSXML2.DOMDocument");
		xml.async = false
		xml.load("presentation.xml")
		// Load XSL - with linked chapters
		xsl = new ActiveXObject("MSXML2.DOMDocument");
		xsl.async = false
		xsl.load("chapters.xsl")
		// Transform
		document.getElementById(divLyr).innerHTML=xml.transformNode(xsl);
	}else{
		// Browser unknown
		alert("Browser unknown");
	}
	chaptersLoaded = true;
	return chaptersLoaded;
}

// check to see if one or more downloadable files has been defined.
downldVisibility = function dwnldDocCheck() {
	var dlArray = new Array();
	var hasdlFile = false;
	// add downloadable file custom tags below. only one by default.
	dlArray[0] = '';
		
	try {
		// loop thru array to see if there are any downloadable files in any position
		for (var i=0;i<=0;i++) {
			if(dlArray[i] != '') {
				hasdlFile = true;
				break;
			}
		}
		//alert(hasdlFile);
		if (!hasdlFile) {
			downldVisibility = false; // no downloadable file has been defined in this array
		} else {
			downldVisibility = true; // at least one downloadable file has been defined in this array
			for (var i=0;i<=0;i++) {
				var dlli = 'dl' + i;
				if (dlArray[i] == '' || dlArray[i].indexOf("@") > -1) {
					document.getElementById(dlli).style.display = 'none';
				}
			}	
		}
	} catch(e) {}
	return downldVisibility;
}();

// check to see if one or more URL links has been defined.
linksVisibility = function linksCheck() {
	var linkArray = new Array();
	var hasLink = false;
	// add URL link custom tags below. only one by default.
	linkArray[0] = '';
	
	try {
		// loop thru array to see if there are any URL links in any position
		for (var i=0;i<=0;i++) {
			if(linkArray[i] != '') {
				hasLink = true;
				break;
			}
		}
		//alert(hasLink);
		if (!hasLink) {
			linksVisibility = false; // no link has been defined in this array
		} else {
			linksVisibility = true; // at least one link has been defined in this array
			for (var i=0;i<=2;i++) {
				var linkli = 'links' + i;
				if (linkArray[i] == '' || linkArray[i].indexOf("@") > -1) {
					document.getElementById(linkli).style.display = 'none';
				}
			}	
		}
	} catch(e) {}
	return linksVisibility;
}();

// if neither downloadable files nor URL links exist, hide Resources tab
if (!downldVisibility && !linksVisibility) {rsrcsVisibility = false;} else {rsrcsVisibility = true;}

function ClosedCaptionsCheck() {
	try{
		var CCtag = '<<@closedcaptionfile@>>';
		var CCtab = '<<@closedcaptiontab@>>';
		//var CCtag = 'somefile';
		var chkNoCCTag = CCtag.indexOf("<@");
		var chkNoCCTab = CCtab.indexOf("<@");
		if ( (CCtab != '') && (chkNoCCTab < 0)  && (CCtag != '') && (chkNoCCTag < 0)  && (isBrowserIeWin() || hasP25()) ) {
			captionsVisibility = true;
		}		
	} catch(e) {}
	return captionsVisibility;
}

// check to see if email address has been defined
emailVisibility = function emailCheck() {
	try {
		var emailTo = '';
		var chkNoTag = emailTo.indexOf("<@");
		if (emailTo != '' && chkNoTag < 0) {
			emailVisibility = true;
		}
	} catch(e) {}
	return emailVisibility;
}();

// display tabs as needed in region 2
function changeMenu(current){
	var newList = new String()
	if(current == 5){ // EMAIL TAB SELECTED
		newList =  "<li><a href='#' onClick='changeMenu();' title='Event Info'>Event Info</a></li>";
		if (topicsVisibility) {newList += "<li><a href='#' onClick='changeMenu(2);' title='Topics'>Topics</a></li>"};
		if (rsrcsVisibility) {newList += "<li><a href='#' onClick='changeMenu(3);' title='Resources'>Resources</a></li>"};
		if (captionsVisibility) {newList += "<li><a href='#' onClick='changeMenu(4);' title='Closed Captions'>Captions</a></li>"};
		if (emailVisibility) {newList += "<li id='current'>Email</li>"};
		document.getElementById('listMenu').innerHTML = newList
		document.getElementById('eventInfoBox').style.display = "none";
		document.getElementById('chaptersBox').style.display = "none";
		document.getElementById('resourcesBox').style.display = "none";
		document.getElementById('emailBox').style.display = "block";
		// document.getElementById('closedCaptionsBox').style.display = "block";
	}else if (current == 4){ // CAPTION TAB SELECTED
		newList =  "<li><a href='#' onClick='changeMenu();' title='Event Info'>Event Info</a></li>";
		if (topicsVisibility) {newList += "<li><a href='#' onClick='changeMenu(2);' title='Topics'>Topics</a></li>"};
		if (rsrcsVisibility) {newList += "<li><a href='#' onClick='changeMenu(3);' title='Resources'>Resources</a></li>"};
		if (captionsVisibility) {newList += "<li id='current'>Captions</li>"};
		if (emailVisibility) {newList += "<li><a href='#' onClick='changeMenu(5);' title='Email'>Email</a></li>"};
		document.getElementById('listMenu').innerHTML = newList
		document.getElementById('eventInfoBox').style.display = "none";
		document.getElementById('chaptersBox').style.display = "none";
		document.getElementById('resourcesBox').style.display = "none";
		document.getElementById('emailBox').style.display = "none";
		// document.getElementById('closedCaptionsBox').style.display = "block";
	}else if (current == 3){ // RESOURCES TAB SELECTED
		newList =  "<li><a href='#' onClick='changeMenu();' title='Event Info'>Event Info</a></li>";
		if (topicsVisibility) {newList += "<li><a href='#' onClick='changeMenu(2);' title='Topics'>Topics</a></li>"};
		newList += "<li id='current'>Resources</li>";
		if (captionsVisibility) {newList += "<li><a href='#' onClick='changeMenu(4);' title='Closed Captions'>Captions</a></li>"};
		if (emailVisibility) {newList += "<li><a href='#' onClick='changeMenu(5);' title='Email'>Email</a></li>"};
		document.getElementById('listMenu').innerHTML = newList
		if (topicsVisibility) {document.getElementById('chaptersBox').style.display = "none";};
		document.getElementById('eventInfoBox').style.display = "none";
		document.getElementById('resourcesBox').style.display = "block";
		if(!downldVisibility){document.getElementById('dlGroup').style.display = "none";} // hide downloads section
		if(!linksVisibility){document.getElementById('linksGroup').style.display = "none";} // hide links section
		document.getElementById('emailBox').style.display = "none";
		// document.getElementById('closedCaptionsBox').style.display = "none";
	}else if(current == 2){ // CHAPTERS TAB SELECTED
		if(!chaptersLoaded){loadChapters('chaptersBox');}
		newList =  "<li><a href='#' onClick='changeMenu();' title='Event Info'>Event Info</a></li>";
		newList += "<li id='current'>Topics</li>";
		if (rsrcsVisibility) {newList += "<li><a href='#' onClick='changeMenu(3);' title='Resources'>Resources</a></li>"};
		if (captionsVisibility) {newList += "<li><a href='#' onClick='changeMenu(4);' title='Closed Captions'>Captions</a></li>"};
		if (emailVisibility) {newList += "<li><a href='#' onClick='changeMenu(5);' title='Email'>Email</a></li>"};
		document.getElementById('listMenu').innerHTML = newList
		document.getElementById('eventInfoBox').style.display = "none";
		document.getElementById('chaptersBox').style.display = "block";
		if (rsrcsVisibility) {document.getElementById('resourcesBox').style.display = "none";};
		document.getElementById('emailBox').style.display = "none";
		// document.getElementById('closedCaptionsBox').style.display = "none";
	}else{ // EVENT INFO TAB SELECTED	
		newList =  "<li id='current'>Event Info</li>"
		if (topicsVisibility) {newList += "<li><a href='#' onClick='changeMenu(2);' title='Topics'>Topics</a></li>"};
		if (rsrcsVisibility) {newList += "<li><a href='#' onClick='changeMenu(3);' title='Resources'>Resources</a></li>"};
		if (captionsVisibility) {newList += "<li><a href='#' onClick='changeMenu(4);' title='Closed Captions'>Captions</a></li>"};
		if (emailVisibility) {newList += "<li><a href='#' onClick='changeMenu(5);' title='Email'>Email</a></li>"};
		document.getElementById('listMenu').innerHTML = newList
		document.getElementById('eventInfoBox').style.display = "block";
		document.getElementById('resourcesBox').style.display = "none";
		document.getElementById('chaptersBox').style.display = "none";
		document.getElementById('emailBox').style.display = "none";
		// document.getElementById('closedCaptionsBox').style.display = "none";
	}
}