// JavaScript Document
var totalLessonPage;
var index = 1;
function XmlContent(){
	var lessonNumber;
	var xLoader_obj;
	var display_obj;
	var note_obj;
	var scorm_obj;
	var xLoader_ob;
	var display_ob;
	var note_ob;
	var scorm_ob;
	var chapTitle;
	var chapIndex							= 0;
	var pgIndex									= -1;
	var pageTotals;     //array of page totals for each chapter
	var pageTotal;
	var doc 												= document;
	var chapNumber;
	var xmlFeed;
	var _pageDiv;
	var pages											= new Array();
	var sections								= new Array(); //first page of each section in chapter
	var _this											= this;
	var _fNoteCnt        = new Number();
	//----
 this.init = function(){
		 pageTotals  = window.pageTotals;
			scorm_obj   = window.scorm ? window.scorm_obj : null;
			display_obj = window.display_obj;
			note_obj    = window.note_obj;
			xLoader_obj = window.xLoader_obj;
		
		 //---- ADD AUDIO PLAYER
			var fTag = new FlashTag('swf/audioPlayer.swf', 200, 20, 'ffffff');
			fTag.setFlashvars('lcId='+uid);
			$('fap_container').innerHTML = fTag.toString();
			//----
			var initChap = scorm_obj ? parseInt(scorm_obj.getXmlChapIndex()) : 'welcome';
		//	xLoader_obj.initParser('fam','chapter',false);
			_this.initChapter(initChap,0);
	}
	

	this.initChapter = function(chap, page){
		 lessonNumber=0;
										 chapNumber =0;
			var nodes;
			pgIndex   = -1;
			if(chapIndex != chap){
				
					chapIndex 		= chap;
					xLoader_obj = new load_XML('xml/'+projType+'_'+projCode+'_chap_'+chapIndex+'.xml');
                                         var nodes = xLoader_obj.tagNameElement("fam:chapter", nodes);
                                          chapTitle = nodes[0].getAttribute('title');
                                          //alert (chapTitle);
                                          pages = xLoader_obj.tagNameElement('fam:page',nodes[0]);
                                           //alert (pages.length);
  if(scorm_obj)scorm_obj.onInitXML();
					buildDropDown(pages);
					display_obj.displayChapNav(chap);  // show intra chapter nav unless welcome page
					_this.loadPage(page);
			} else {
				xLoader_obj = new load_XML('xml/'+projType+'_'+projCode+'_chap_'+chapIndex+'.xml');
                                         var nodes = xLoader_obj.tagNameElement("fam:chapter", nodes);
                                          chapTitle = nodes[0].getAttribute('title');
										  
                                          //alert (chapTitle);
                                          pages = xLoader_obj.tagNameElement('fam:page',nodes[0]);
										  buildDropDown(pages);
					display_obj.displayChapNav(chap); 
				 _this.loadPage(page);
			}
	}
	
	this.lessonChapter = function(chap, page){
		 
		   	var nodes;
			pgIndex   = -1;
			var tempt = chap.split('_');
			chapNumber = tempt[1];
			lessonNumber = tempt[3];
			xLoader_obj1 = new load_XML('xml/'+ projType + '_' + projCode + '_' + chap + '.xml');
                                         var nodes = xLoader_obj.tagNameElement("fam:chapter", nodes);
                                          chapTitle = nodes[0].getAttribute('title');
                                          pages = xLoader_obj.tagNameElement('fam:page',nodes[0]);
										  totalLessonPage = pages.length;
                    if(scorm_obj)scorm_obj.onInitXML();                    
					buildDropDown(pages);
				   _this.loadPage(page);		
	}

	this.loadPage = function(direct, infer){
	
		 var ok  											  = getNewPageIndex(direct, infer);
		 if(ok){
			  
			       
					var page 										= pages[pgIndex].childNodes;
					_pageDiv 								  = ce('div');
					_fNoteCnt											= 0;
					note_obj.resetNotes();
					for(var i=0; i<page.length; i++){
							var xmlNode = page[i];
							if(xmlNode.nodeName != '#text'){
								 var htmlNode = ce('div');
									var htmlResult;
									htmlNode.className = removePrefix(xmlNode.nodeName);
									
									switch (xmlNode.nodeName){
										 case 'fam:title':
											htmlResult = parseTitleNode(xmlNode);
											htmlNode.appendChild(htmlResult);
											break;
											case 'fam:subtitle':
											htmlResult = parseTitleNode(xmlNode);
											htmlNode.appendChild(htmlResult);
											break;
											case 'fam:subhead':
											htmlResult = parseTitleNode(xmlNode);
											htmlNode.appendChild(htmlResult);
											break;
											case 'fam:paragraph':
											htmlResult = parseParagraphNode(xmlNode);
											htmlNode.appendChild(htmlResult.img);
											htmlNode.appendChild(htmlResult.pg);
											break;
											case 'fam:flash':
											htmlResult = parseFlashNode(xmlNode);
											htmlNode.appendChild(htmlResult);
											break;
											case 'fam:exchange':
											htmlResult = parseExchangeNode(xmlNode);
											htmlNode.appendChild(htmlResult.header);
											htmlNode.appendChild(htmlResult.main);
											break;
											case 'fam:table':
											htmlResult = parseTableNode(xmlNode);
											htmlNode.appendChild(htmlResult);
											break;
											case 'fam:ul':
											htmlResult = parseBulletNode(xmlNode);
											htmlNode.appendChild(htmlResult);
											break;
											case 'fam:blockquote':
											htmlResult = parseBlockquoteNode(xmlNode);
											htmlNode.appendChild(htmlResult);
											break;
											
									}
									
									//alert('xml node: '+xmlNode.nodeName+'\nhtml node: '+htmlNode);
							_pageDiv.appendChild(htmlNode);
							}
					   
					}
					//---- ADD PROXY CONTAINER
					var pc = ce('div');
					pc.setAttribute('id','proxy_container');
					_pageDiv.appendChild(pc);
					//----
					
					display_obj.display(_pageDiv, pgIndex);
					
			//---- PREVIOUS CHAPTER
			} else if(infer == -1 && chapIndex > 0) {
				   if( pgIndex == 0) { 
				         _this.initChapter(chapIndex-1, pageTotals[chapIndex-1]-1); }
				   else
				   		 _this.initChapter(chapIndex-1, pageTotals[chapIndex-2]);
			//---- NEXT CHAPTER
			} else if(infer == 1 && chapIndex < pageTotals.length){
				
				 _this.initChapter(chapIndex+1, 0);
				
			}
	}
	
	this.getPageIndex = function(){
		 return pgIndex;
	}
	this.getLessonNumber= function(){
		 return lessonNumber;
	}
	
	this.getChapterNumber = function(){
		 return chapNumber;
	}
	this.getChapIndex = function(){
		 return chapIndex;
	}
	this.getSections = function(){
		 return sections;
	}
	this.getChapTitle = function(){
		 return chapTitle;
	}
	//----
	function parseTitleNode(xmlNode){
			var hPart = parseInlineNodes(xmlNode);
			if(xmlNode.getAttribute('no_clear')){
					hPart.className = 'no_clear';
			}else{
					hPart.className = 'clear';
			}
			return hPart;
	}
	function parseParagraphNode(xmlNode){
		
			var pgObj = new Object();
			//---- ADD IMAGE
			var ip  = xmlNode.getAttribute('imgPath');
			var ia  = xmlNode.getAttribute('imgAlign');
			var alt = xmlNode.getAttribute('alt') ? xmlNode.getAttribute('alt') : '';
			var ioc = xmlNode.getAttribute('imgOnClick') ? xmlNode.getAttribute('imgOnClick') : null;
			var ipExt = ip.substr(ip.length-4);
			var imgHold = ce('div');
			if(ipExt == '.jpg' | ipExt == '.gif' | ipExt == '.png'){
						var imgEl      = ce('img');
						imgEl.setAttribute('src',ip);
						imgEl.setAttribute('alt',alt);
						imgEl.setAttribute('title',alt);
						if(ioc){
								imgEl.onclick = function(){eval(ioc)};
								imgEl.className = 'button';
						}
						
						switch(ia){
								case 'center':
										imgHold.className = 'floatNone';
										break;
								case 'left':
										imgHold.className = 'floatLeft';
										break;
								default:
										imgHold.className = 'floatRight';
										break;
							}
							imgHold.appendChild(imgEl);
							pgObj.img = imgHold;
				}
				//---- PARSE PARAGRAPH NODES
				
				var hPart = parseInlineNodes(xmlNode);
				pgObj = {img:imgHold, pg:hPart};
				
				return pgObj;					
	}
	function parseFlashNode(xmlNode){
			//!!!!DMO
			_pageDiv.setAttribute('flash_fps', xmlNode.getAttribute('fps') ? xmlNode.getAttribute('fps') : null);
			_pageDiv.setAttribute('flash_preload', xmlNode.getAttribute('preload') ? null : true);
			//!!!!DMO
			_pageDiv.setAttribute('flash_path', xmlNode.getAttribute('src'));
			_pageDiv.setAttribute('flash_width', xmlNode.getAttribute('width') ? xmlNode.getAttribute('width') : null);
			_pageDiv.setAttribute('flash_height', xmlNode.getAttribute('height') ? xmlNode.getAttribute('height') : null);
			_pageDiv.setAttribute('flash_bgcolor', xmlNode.getAttribute('bgcolor') ? xmlNode.getAttribute('bgcolor') : null);
			_pageDiv.setAttribute('flashXML_path', xmlNode.getAttribute('xmlPath') ? xmlNode.getAttribute('xmlPath') : null);
			_pageDiv.setAttribute('certificate', xmlNode.getAttribute('certificate') ? xmlNode.getAttribute('certificate') : null);

			var fc       = ce('div');
			var hPart    = parseInlineNodes(xmlNode);
			//get float attribute
			var float    = xmlNode.getAttribute('float'); 
			//if float exists make first letter upper case (ie. 'left' --> 'Left') otherwise set float to null
			float 		     = float ? float.substr(0,1).toUpperCase()+float.substr(1) : null; 
			/*
			if float has a value set flash container class name to 'float' + whatever the value is, 
			otherwise set to 'floatNone' (see css for how this affects div positioning)
			*/
			fc.className = float ? 'float'+float : 'floatNone'; 
			fc.id 			    = 'f_container';
			fc.appendChild(hPart);
			
			return fc;
	}
	function parseExchangeNode(xmlNode){
									var exchObj = new Object();
									var eNo = new Number();
									var header = ce('div');
									var main = ce('div');
									header.className = 'exchange_header';
									main.className = 'exchange_main';
							  for(var j=0; j<xmlNode.childNodes.length; j++){
								   var xPart = xmlNode.childNodes[j];
											if(xPart.nodeName == '#text' && xPart.data != ''){
											  var _split = xPart.data.split(':');
													var hPart  = (j==0) ? ce('span') : ce('div');
													var hPart1 = ce('span');
													var hPart2 = ce('span');
													hPart1.appendChild(ctn(_split[0]));
             hPart.appendChild(hPart1);
													if(_split[1]){
													  hPart1.appendChild(ctn(': '));
															//node 0="Exchange: n", node 2="Soldier:", node 6="Local:", node 10="Soldier:"
														 if(j==0 | j==2 | j==6 | j==10 | j==14)hPart1.className = 'strong';
															hPart2.appendChild(ctn(_split[1]));
															hPart.appendChild(hPart2);
													}
													if(j==0){
														 //hPart1.firstChild.insertData(0,"Exchange ");
															eNo = _split[0].substr(9);
															header.appendChild(hPart);
													} else {
														 main.appendChild(hPart);
													}
											} 
									}
									var fap_nest = ce('div');
									fap_nest.className = 'fap_nest';
									main.appendChild(fap_nest);
									//---- BUILD PLAY BUTTON
									var anc_play = ce('a');
									if ( (parseInt(chapNumber) > 0) && (parseInt(lessonNumber) >0)) {
										anc_play.className = 'chap_exchange_play';
									     var numEX =  'chap' + chapNumber +'_'+ 'lesson' + lessonNumber +'_'+ doubleDigitize(eNo);
										
									}
									else {
										anc_play.className = 'chap_exchange_play';
									     var numEX = doubleDigitize(eNo);
									}
										 
									anc_play.setAttribute('id',numEX);
							  var img_play = ce('img');
									img_play.setAttribute('src','images/play.gif');
									anc_play.appendChild(img_play);
         header.insertBefore(anc_play, header.childNodes[0]);

									xchObj  = {header:header, main:main}
									return xchObj;
	}
	
		function parseBlockquoteNode (xmlNode){
			var htmlT = ce('blockquote');
			htmlT.className = 'quote';
			htmlT.appendChild(parseInlineNodes(xmlNode));
			return htmlT;
	}
	
function parseTableNode(xmlNode){
			var htmlT = ce('table');
			var tBorder  = parseInt(xmlNode.getAttribute('border'));
			var tWidth  = parseInt(xmlNode.getAttribute('width'));
			var tHeight  = parseInt(xmlNode.getAttribute('height'));
			var tAlign  = xmlNode.getAttribute('align');
			 if(tBorder >= 0)
			     htmlT.setAttribute('border', tBorder);
			 if(tWidth > 0)
			     htmlT.setAttribute('width', tWidth);
			 if(tHeight > 0)
			     htmlT.setAttribute('height', tHeight);
			 if(tAlign && tAlign != 'null')
			     htmlT.setAttribute('align', tAlign);
			
			htmlT.setAttribute('cellspacing', '0');
			htmlT.className = 'generic';
			//var rowNodes = xLoader_obj.tagNameElement('tr', xmlNode);
					for(var j=0; j<xmlNode.childNodes.length; j++){ 
					var xmlTr  = xmlNode.childNodes[j];
					var htmlTr = htmlT.insertRow(j);
					  var i =0;
					for(var k=0; k<xmlTr.childNodes.length; k++){
								var xmlTd  = xmlTr.childNodes[k];
								if ( xmlTd.nodeName == 'td') {
										var htmlTd = htmlT.rows[j].insertCell(i);
										htmlTd.appendChild(parseInlineNodes(xmlTd));
										i++;
								}
										
								
				}
		 }
		 
			return htmlT;
	}
	
	
	
function parseBulletNode(xmlNode){
		var temp = getBrowserName();
		if((temp == "safari") || (temp == "firefox") ) {
				   var htmlUL = ce('ul');
				   htmlUL.className = 'bullet';
				  	for(var j=1; j<xmlNode.childNodes.length-1; j++){ 
                  			var xml  = xmlNode.childNodes[j];
				  			var htmlLI = ce('li');
				     		 	htmlUL.appendChild(htmlLI);
		   		      			htmlLI.appendChild(parseInlineNodes(xml));
              		} 
              	} else {
							var htmlUL = ce('ul');
							htmlUL.className = 'bullet';
                        for(var j=0; j<xmlNode.childNodes.length; j++){ 
		     				var xml  = xmlNode.childNodes[j];
							var htmlLI = ce('li');
							htmlUL.appendChild(htmlLI);
		   					htmlLI.appendChild(parseInlineNodes(xml));
			     		}
				}
                               
		return htmlUL;
	} 
	

 function parseInlineNodes(xmlNode){
		 var inlPart = ce('span');
			for(var i=0; i<xmlNode.childNodes.length; i++){
				var hPart;
				var xPart = xmlNode.childNodes[i];
				//alert('node name: '+xPart.nodeName+'\nnode value: '+xPart.data);
					if(xPart.nodeName == '#text'){
							hPart = ce('span');
							hPart.appendChild(ctn(xPart.data));
				
					} else if(xPart.nodeName == 'fam:footnote'){
						 //---- LOAD DATA INTO NOTE_OBJ
							var fn = xPart.firstChild;
							note_obj.loadNote(fn.data);
						 //---- BUILD FOOTNOTE REFERENCE LINKS
							_fNoteCnt++;
							var prevNode1 = inlPart.childNodes[Math.max(0,i-1)];
							var prevNode2 = inlPart.childNodes[Math.max(0,i-2)];
							if(prevNode1.firstChild.data == ',' && prevNode2.className == 'footnote'){//if last node was ',' and 2nd to last has 'footnote' class name 
							prevNode1.className = removePrefix(xPart.nodeName); //set comma's node class to 'footnote';
							}
							hPart   = ce('a');
							hPart.appendChild(ctn(_fNoteCnt));
							hPart.setAttribute('href',"javascript:note_obj.displayNote("+_fNoteCnt+")");
							hPart.className = removePrefix(xPart.nodeName);
							
					} else if(xPart.nodeName == 'a'){
						 hPart = ce('a');
							var href = xPart.getAttribute('href');
							var targ = xPart.getAttribute('target');
							var idLesson= xPart.getAttribute('id');
							if (idLesson) { 
							        hPart.className = 'lesson_click';
									hPart.setAttribute('id', idLesson);
									if(href) {
									    hPart.setAttribute('href',href);
										hPart.setAttribute('target','_blank');
									}
									else
									    hPart.setAttribute('href','#') 
							} else {
				            if(href)hPart.setAttribute('href',href);
							//if(targ)hPart.setAttribute('target',targ);
							hPart.setAttribute('target','_blank'); }
							hPart.appendChild(ctn(xPart.firstChild.data)); 
					} else {
							hPart = ce(xPart.nodeName); //html tags (b, u, i etc.)
							if(xPart.firstChild)
									hPart.appendChild(ctn(xPart.firstChild.data));
					}
					inlPart.appendChild(hPart);
			}
			return inlPart;;
	}
 function getNewPageIndex(direct, infer){
	 
	  var nInd = (typeof(direct)=='number') ? direct : pgIndex + infer;
			nInd = (nInd <= 0) ? 0 : (nInd >= pages.length-1) ? pages.length-1 : nInd;
			if(nInd == pgIndex){
				
			  return false;
			} else {
				
				 pgIndex = nInd;
				 return true;
			}
	}
	function $(a){
	  e = doc.getElementById(a);
	  return e;
 }
	function ce(name){
		var dn = doc.createElement(name);
		return dn;
	}
	function ctn(from){
		 var tn = doc.createTextNode(from);
			return tn;
	}
	function removePrefix(nodeName){ //removes "fam:" namespace prefix from xml node name
		 var str    = nodeName.toString();
			var newStr = str.substr(4);
			return newStr;
	}
	function buildDropDown(pgs){
		 var ddDiv = ce('select');
			sections=[];
			for(var i=0; i<pgs.length; i++){
				 var pg = pgs[i];
					var s = pg.getAttribute('section');
					if((s && s != '-------') && s != "null"){
					  var option = ce('option');
							sections.push(i);
							if(sections.length == 1)
							  option.setAttribute('defaultSelected', true);
							option.appendChild(ctn(s));
							ddDiv.appendChild(option);
					}
			}
			display_obj.displayDropDown(ddDiv);
	}
		function doubleDigitize(n){
			
				if(n.length > 1){
					
					 return n;
				} 
				var dd = '0'+n;
				return dd;
		}
	
}
function getBrowserName() {
	var browserName = "";
	var name = navigator.userAgent.toLowerCase();
	if (name.indexOf("opera") != -1) 
		browserName = "opera";
	else if (name.indexOf("msie") != -1) 
		browserName = "msie";
	else if (name.indexOf("safari") != -1) 
		browserName = "safari";
	else if (name.indexOf("mozilla") != -1) { 
			if (name.indexOf("firefox") != -1)
				browserName = "firefox";
			else {
				browserName = "mozilla"; 
			}
			
	}
	return browserName;
}
			
		
//----

