/************************************************************************/
/*															 			*/
/*	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;
}

function isBrowserIeWin() {
	var iewin = false;
	var ua = navigator.userAgent.toLowerCase();
	if ((ua.indexOf('ie') > -1) & (ua.indexOf('win') > -1))
		iewin = true;
	return iewin;
}

// check to see if user has enabled chaptering functionality
function checkChapters(){
	var chap = '<a href="command:seek(00:00:01)" target="_player">Chapter 1</a><br><a href="command:seek(00:10:30)" target="_player">Chapter 8</a><br><a href="command:seek(00:14:41)" target="_player">Chapter 9</a><br><a href="command:seek(00:15:22)" target="_player">Chapter 10</a><br><a href="command:seek(00:15:38)" target="_player">Chapter 11</a><br><a href="command:seek(00:16:13)" target="_player">Chapter 12</a><br><a href="command:seek(00:16:35)" target="_player">Chapter 13</a><br><a href="command:seek(00:16:54)" target="_player">Chapter 14</a><br><a href="command:seek(00:18:36)" target="_player">Chapter 15</a><br><a href="command:seek(00:19:42)" target="_player">Chapter 16</a><br><a href="command:seek(00:20:04)" target="_player">Chapter 17</a><br><a href="command:seek(00:20:19)" target="_player">Chapter 18</a><br><a href="command:seek(00:20:39)" target="_player">Chapter 19</a><br><a href="command:seek(00:20:50)" target="_player">Chapter 20</a><br><a href="command:seek(00:21:02)" target="_player">Chapter 21</a><br><a href="command:seek(00:21:15)" target="_player">Chapter 22</a><br><a href="command:seek(00:21:33)" target="_player">Chapter 23</a><br><a href="command:seek(00:21:54)" target="_player">Chapter 24</a><br><a href="command:seek(00:22:07)" target="_player">Chapter 25</a><br><a href="command:seek(00:22:21)" target="_player">Chapter 26</a><br><a href="command:seek(00:22:51)" target="_player">Chapter 27</a><br><a href="command:seek(00:23:00)" target="_player">Chapter 28</a><br><a href="command:seek(00:23:28)" target="_player">Chapter 29</a><br><a href="command:seek(00:24:23)" target="_player">Chapter 30</a><br><a href="command:seek(00:25:06)" target="_player">Chapter 32</a><br>';
	if(chap=='' || (chap.indexOf("<@")>0) ){
		// chapters not loaded
		return false;
	}
	return true;
}
function topicsTabCheck() {
	try{
		var topicsTabTag = 'yes';
		//var topicsTabTag = 'yes';
		var chkNoTopicsTabTag = topicsTabTag.indexOf("<@");
		if ((topicsTabTag != '') && (chkNoTopicsTabTag < 0)) {
			if(checkChapters()) topicsVisibility = true;
		}		
	} catch(e) {}
	return topicsVisibility;
}
topicsTabCheck();

// load correct xsl file depending on browser type and plugin
function loadChapters(divLyr){
	if(!topicsVisibility){ return true; }
	if(document.implementation && document.implementation.createDocument){
		// Mozilla
		var xsltProcessor = new XSLTProcessor();
		// load the xslt file
		var myXMLHTTPRequest = new XMLHttpRequest();
		//	IF is WINDOWS MEDIA
		if (isBrowserIeWin()|| hasP25()){
			myXMLHTTPRequest.open("GET", "chapters.xsl", false);
			// otherwise load list of time/topic without link
		} else {
			myXMLHTTPRequest.open("GET", "chapters_f.xsl", false);
		}
		myXMLHTTPRequest.send(null);
		// get the XML document
		xslStylesheet = myXMLHTTPRequest.responseXML;
		xsltProcessor.importStylesheet(xslStylesheet);
		// load the xml file
		myXMLHTTPRequest = null;
		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);
		// cleanup variables
		delete xsltProcessor;
		delete myXMLHTTPRequest;
		delete xslStylesheet;
		delete xmlSource;
		delete 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);
		// cleanup variables
		delete xml;
		delete 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] != '' && dlArray[i].indexOf("@>") < 0) {
				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] != '' && linkArray[i].indexOf("@>") < 0) {
				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;}

// check to see if Captions are used
function ClosedCaptionsCheck() {
	try{
		var CCtag = 'http://accordent.powerstream.net/008/00191/Session3/Session3.smi';
		var CCtab = '';
		//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;
}
captionsVisibility = ClosedCaptionsCheck();

// check to see if email address has been defined
emailVisibility = function emailCheck() {
	try {
		var emailTo = '';
		var chkNoTag = emailTo.indexOf("<@");
		if (emailTo == '' || chkNoTag > -1) {
			linksVisibility = false;
		}
	} catch(e) {}
	return linksVisibility;
}();

/* ########################################################################### */

// define all possible region2 content tabs
// tabs not implemented may be remarked-out, or set with 'visibility':false
var tabDefs = new Array();
	tabDefs[0] = { 'visibility':true,               'title':'Event Info',      'tab':'Info',      'panel':'eventInfoBox'      };
	tabDefs[1] = { 'visibility':topicsVisibility,   'title':'Topics',          'tab':'Topics',    'panel':'chaptersBox'       };
	tabDefs[2] = { 'visibility':rsrcsVisibility,    'title':'Resources',       'tab':'Resources', 'panel':'resourcesBox'      };
//	tabDefs[3] = { 'visibility':captionsVisibility, 'title':'Closed Captions', 'tab':'Captions',  'panel':'closedCaptionsBox' };
	tabDefs[4] = { 'visibility':emailVisibility,    'title':'Email',           'tab':'Email',     'panel':'emailBox'          };

function pageInit(){
	chaptersLoaded = loadChapters('chaptersBox');
    captioned = captionsVisibility;
	objPlacements();
}

// display tabs as needed in region 2
function changeMenu(current){
	var panel, newList = '';
	var tabGroup = document.getElementById('listMenu');
    if(!arguments.length){ current = 0; }
	if(!chaptersLoaded){ pageInit(); }
	for(i=0; i<tabDefs.length; i++){
		if(tabDefs[i]!=null && tabDefs[i]['visibility']!=false){
			var panel = document.getElementById(tabDefs[i]['panel']);
			if(current==i){ 
				// display tab with css-highlighted id, and no hyperlink
				newList += '<li id="current">'+tabDefs[i]['tab']+'</li>';
				panel.style.display = 'block';
			}else{
				// display standard tab, and hide the content panel
				newList += '<li><a href="#" onClick="changeMenu('+i+');"';
				newList += ' title="'+tabDefs[i]['title']+'">'+tabDefs[i]['tab']+'</a></li>';
				panel.style.display = 'none'; } } }
	tabGroup.innerHTML = newList;
	return true;
}

/* DHTML PAGE LAYOUT */
var swapped = false;
var captioned;

function panelHgt(shortened){
	for(i=0; i<tabDefs.length; i++){
		if(tabDefs[i]!=null && tabDefs[i]['visibility']!=false){
			var panel = document.getElementById(tabDefs[i]['panel']);
			if(shortened){
				// truncate each panel below, to preserve the balance of the page layout
				panel.className = 'region2content r2short';
			}else{
				panel.className = 'region2content r2normal'; } } }
}

function objPlacements(){
	var i, tag, V, R, MP, SM, C, arrElements;
    // rearrange top-menu links, by re-parenting
	var Lb1Pt, Lb1, Lb2Pt, Lb2, Rb1Pt, Rb1, Rb2Pt, Rb2, tog;
	Lb1Pt = document.getElementById('vidGroup');	// left  button 1 parent
	Lb1	  = document.getElementById('zoomVid');		// left  button 1
	Lb2Pt = document.getElementById('captioning');	// left  button 2 parent
	Lb2	  = document.getElementById('captions');	// left  button 2
	Rb1Pt = document.getElementById('slideGroup');	// right button 1 parent
	Rb1	  = document.getElementById('zoomSlide');	// right button 1
	Rb2Pt = Rb1Pt;									// right button 2 parent (same)
	Rb2	  = document.getElementById('thumbs');		// right button 2
	if(swapped && (Rb1.parentNode!=Lb1Pt)){
		Lb1Pt.appendChild(Rb1, true);	Rb1Pt.appendChild(Lb2, true);
		Lb2Pt.appendChild(Rb2, true);	Rb2Pt.appendChild(Lb1, true);
	}else if(!swapped && (Rb1.parentNode==Lb1Pt)){
		Lb1Pt.appendChild(Lb1, true);	Rb1Pt.appendChild(Rb2, true);
		Lb2Pt.appendChild(Lb2, true);	Rb2Pt.appendChild(Rb1, true); }
    // position remaining elements by DHTML, and by CSS class assignments
	V = frames['video'].window;
	R = frames['region1'].window;
	arrElements = new Array();
	arrElements[0]  = document.getElementById('region1');
	arrElements[1]  = R.document.getElementById('slideimage');
	arrElements[2]  = document.getElementById('video');
	arrElements[3]  = document.getElementById('video_bg');
	arrElements[4]  = SM = V.document.getElementById('sourcemedia');
	arrElements[5]  = MP = V.document.getElementById('MediaPlayer');
	arrElements[6]  = V.document.getElementById('captionbox');
	arrElements[7]  = V.document.getElementById('mscc');
	arrElements[8]  = C = V.document.getElementById('controls');
	var suffix = '';
	if(swapped)   suffix += '_swapped';
	if(captioned) suffix += '_captioned';
	for(i=(arrElements.length-1); i>=0; i--){ 
		// counting down, collapsing array as we go
		if(arrElements[i]==null) continue;
		tag = arrElements[i].id;
		if(tag==null)  tag = arrElements[i].name;
		if(tag!=null){
			// element exists and has a name or ID that we can match in the CSS
			if(tag=='MediaPlayer' && C != null) tag = 'RealPlayer';
			if(tag=='sourcemedia' && C != null) tag = 'sourcereal';
        	arrElements[i].className = tag + suffix;
		} // end if(got tag)
		delete arrElements[i];
		arrElements.length = i;
    } // end for(arrElements)
	// set panelheight for each tabbed panel
	if(captioned && (! swapped)){ 
		panelHgt(true); 
	}else{ 
		panelHgt(false); }
	// cleanup object references
	delete V;  delete R;  delete MP;  delete SM;  delete C;
}

function swapVidAndSlide() {
    swapped = ((swapped)? false : true);
	objPlacements();
}

function togCCdisplay(){
    if(!captionsVisibility) return false;
    captioned = ((captioned)? false : true);
	objPlacements();
}
