/*
	====================================================================
	CHAPTERLIST.JS
	v0.0 
	4/19/2006
	====================================================================
		
	DESCRIPTION:

		Dynamically generates table of video cuepoints with XML data.
		
		Add the following to pages in video section whose video uses cuepoints 
		from external xml file:

			[1] 
				<link rel="stylesheet" type="text/css" href="chapterlist.css" />
				<script src="chapterlist.js" language="javascript" type="text/javascript"></script>

			[2] 
				<body onload="loader('filename.xml');">
				
				-- ideally.. otherwise, put at end of <body> as kludge
			
			[3]
				<h5 id="chapterlistlink">	
					&bull; <a href="javascript:showChapterList();">Show Chapter List</a>	
				</h5>
				
				<h5 id="chapterlist">
				</h5>
				
*/


function loader(printChapterList) {
	try 		{ loadXML(printChapterList, printCuePointList); }
	catch (e) 	{ /* do nothing*/ }
}

// This function loads the XML document from the specified URL, and when
// it is fully loaded, passes that document and the url to the specified
// handler function.  This function works with any XML document. Source: O'Reilly JS 4E
function loadXML(url, handler) {
    // Use the standard DOM Level 2 technique, if it is supported
    if (document.implementation && document.implementation.createDocument) {
        // Create a new Document object
        var xmldoc = document.implementation.createDocument("", "", null);
        // Specify what should happen when it finishes loading
        xmldoc.onload = function() { 
			handler(xmldoc, url); 
		}
        // And tell it what URL to load
        xmldoc.load(url);
    }
    // Otherwise use Microsoft's proprietary API for Internet Explorer
    else if (window.ActiveXObject) { 
        var xmldoc = new ActiveXObject("Microsoft.XMLDOM");   // Create doc.
        xmldoc.onreadystatechange = function() {              // Specify onload
            if (xmldoc.readyState == 4) handler(xmldoc, url);
        }
        xmldoc.load(url);                                     // Start loading!
    }
}

function printCuePointList(xmldoc, url) {
	
    var chapters = xmldoc.getElementsByTagName("chapter");
	if (chapters.length<1) { return; }

	// things look good at this point, so make display link visible
	document.getElementById("chapterlistlink").style.display = "block"; 


    var table = document.createElement("table");
    table.setAttribute("width", "100%");
    table.setAttribute("border", "0");
    table.setAttribute("cellpadding", "0");
    table.setAttribute("cellspacing", "0");
	
    document.getElementById("chapterlist").appendChild(table);

    var header = table.createTHead();
    var headerrow = header.insertRow(0);
    headerrow.insertCell(0).appendChild(document.createTextNode("Num."));
    headerrow.insertCell(1).appendChild(document.createTextNode("Title"));
    headerrow.insertCell(2).appendChild(document.createTextNode("Time"));
    
	var c1;
    for (var i = 0; i < chapters.length; i++) {

        var e = chapters[i];
        var txtTitle = e.getAttribute("title");
        var txtTime = e.getAttribute("time");

        var row = table.insertRow(i+1);
        row.insertCell(0).appendChild(document.createTextNode(i+1));
        c1 = row.insertCell(1).appendChild(document.createTextNode(txtTitle));
        row.insertCell(2).appendChild(document.createTextNode(txtTime));
    }
	
	// assign styles to table cells
	var r = table.getElementsByTagName("tr");
	// headerrow
	r[0].childNodes[0].setAttribute("style", "font-weight:bold;");
	r[0].childNodes[1].setAttribute("style", "font-weight:bold;");
	r[0].childNodes[2].setAttribute("style", "font-weight:bold;");
	// rest of table
	for (var i = 1; i < r.length; i++) { 
		r[i].childNodes[0].className = "cnum";
		r[i].childNodes[1].className = "ctitle";
		r[i].childNodes[2].className = "ctime";
	}
	
}

function showChapterList() {
	var o = document.getElementById("chapterlist");
	if (o.style.display.length<1 || o.style.display == "none")	{ o.style.display = "block"; }
	else 														{ o.style.display = "none";  }
}
