// JavaScript Document
    var req = null;
    var arr = new Array();
    
    function processText() {
      //alert('readystate= ' + req.readyState);
      if (req.readyState == 4 ) {
        var content = req.responseText;
        var lines = content.split(/\r\n|\r|\n/);
        for (var i = 0; i < lines.length; i++) {
          arr[i] = lines[i];
        }
        fillSelect();
      }
    }

    function loadText(url) {
      if(window.XMLHttpRequest) {
        try { req = new XMLHttpRequest();
        } catch(e) { req = false; }
      } else if(window.ActiveXObject) {
        try { req = new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
          try { req = new ActiveXObject('Microsoft.XMLHTTP');
          } catch(e) { req = false; }
        } 
      }      
      if(req) {
        
        req.onreadystatechange = processText;
        req.open('GET', url, true);    //true=asynchronous, false=synchronous
        req.send('');
      } else {
        alert('no req object');
      }
    }

    function fillSelect() {
      // takes a comma delimited string value then text displayed
      //arr[0] = "1,JS Applications and Games";    
      box = document.forms[0].content;
       // clears any existing items
      box.options.length = 0;
      //box.options.length++;
      //box.options[0].value = 'a';
      //box.options[0].text = 'jump list';
       // loops through the array adding item text/value
      for(var i=0;i < arr.length;i++) {
        box.options.length++;
        box.options[i].value = arr[i].split(",")[0];
        box.options[i].text = arr[i].split(",")[1];
      }
    }
    
    var currentSlide = 0;
    var currentIncr = 0;
    var currentId = '';
    var autoAdvance = false;
    
    function windowLoad(){    
      //var url = window.location.toString();
      //url = url.replace( /antipat3_fixed.html/, 'antipat3_content.html' );
      var slides = "slides1.txt";
      loadText(slides);
      //fillSelect();
      //req = null;
      //var url = "content1.html";
      //loadUrl( url );
      document.forms[0].auto1.checked = autoAdvance;    
    }
    
    function toggleAuto() {
      //alert('auto= ' + document.forms[0].auto1.checked);
      autoAdvance = document.forms[0].auto1.checked;
    }

    function hideSoundButtons() {
      var pauseBut = document.getElementById('pause');
      pauseBut.style.visibility="hidden";
      var resumeBut = document.getElementById('resume');
      resumeBut.style.visibility="hidden";      
    }
    function showSoundButtons() {
      var pauseBut = document.getElementById('pause');
      pauseBut.style.visibility="visible";
      var resumeBut = document.getElementById('resume');
      resumeBut.style.visibility="visible";      
    }    

    function changeHeading1(newname) {
      var heading = document.getElementById('heading1');
      if (heading) {
        heading.innerHTML = newname;
      }
    }
    function changeHeading2(newname) {
      var heading = document.getElementById('heading2');
      if (heading) {
        heading.innerHTML = newname;
      }
    }    
    function go() {
    	box = document.forms[0].content;
    	destination = box.options[box.selectedIndex].value;
    	if (destination.length > 3) {
        //location.href = destination;
        //alert('destination '+ destination);
        var url = destination;
        if ( window.frames["slide"] ) {
          hideSoundButtons();
          soundManager.stopAll();
          currentSlide = box.selectedIndex;
          window.frames["slide"].location = url;   
        }        
      }
    }
    function nextSlide() {
      var bFlag = upIncr();
      if (!bFlag) {
        box = document.forms[0].content;
        if ( (currentSlide + 1) < box.options.length ) {
          currentSlide++ ;
        	destination = box.options[currentSlide].value;
        	if (destination) {
            //location.href = destination;
            //alert('destination '+ destination);
            var url = destination;
            if ( window.frames["slide"] ) {
              hideSoundButtons();
              soundManager.stopAll();
              window.frames["slide"].location = url;   
            } 
            box.selectedIndex = currentSlide;      
          } 
        }
      }           
    }
    function prevSlide() {
        box = document.forms[0].content;    //  document.myform.box1.checked = false;
        if ( currentSlide > 1 ) {
          currentSlide-- ;
        	destination = box.options[currentSlide].value;
        	if (destination) {
            //location.href = destination;
            //alert('destination '+ destination);
            var url = destination;
            if ( window.frames["slide"] ) {
              hideSoundButtons();
              soundManager.stopAll();
              window.frames["slide"].location = url;   
            }
            box.selectedIndex = currentSlide;        
          } 
        }          
    }    
    // ---
    function addEvent(oElm, strEvent, oFunction, bCapture){
    	if(oElm.addEventListener){
    		oElm.addEventListener(strEvent, oFunction, bCapture);
    	}
    	else if(oElm.attachEvent){
    		oElm.attachEvent(("on" + strEvent), oFunction);
    	}
    }    

    soundManager.url = '../js/soundmanager2.swf'; // override default SWF url
    soundManager.debugMode = false;
    soundManager.useConsole = true; 
    soundManager.consoleOnly = true;
    soundManager.allowPolling = true;

    var initialMp3 = '';
    
    function startMp3(mp3Path, n) {
      initialMp3 = mp3Path;
    }
    
    soundManager.onerror = function() {
      // soundManager failed to initialise (security restrictions, no support, missing SWF etc.)
      // Notify user if needed, disable sound-specific functionality etc.
      alert('soundManager failed.');
    }
    
    soundManager.onload = function() {
      // soundManager is initialised, ready to use. Create a sound for this demo page.
      if (initialMp3.length > 1) {
        soundManager.createSound({
         id: 'aSound',    // required
         url: initialMp3, // required
         // optional sound parameters here, see Sound Properties for full list
         volume: 50,
         autoPlay: false,
         onfinish: mp3Finished    // remember to omit comma on the last item
        });  
        soundManager.play('aSound');
        //showSoundButtons();      
      }
    }

    var arrT = new Array();
    var arrT2 = new Array();
    var arrImgs = new Array();
    var oImg=null;
    var oUl=null;
    var oUL2=null;
    var curitemC = "red";
    var previtemC = "#aaa";
    var inititemC = "#eee"
    //arrT[0] = 3000;
    //arrT[1] = 9500;
    //arrT[2] = 16000;    

    function fixTimes(o) {
        var liList = o.getElementsByTagName('li'); 
        var n, i, maxn;
        maxn = arrT.length-1;
        if (maxn <= 0) {
            arrT[0] = 2000;  
            maxn = 1;
        }
        //for(var j=oUl.childNodes.length-1; j>=0; j--) {
        for(var j=liList.length-1; j>=0; j--) {
            if (j > arrT.length-1) {
              n = arrT[maxn] + 1500;
              maxn++;
            } else {
              n = arrT[j]
            }
            arrT2[j] = n;
        }
        currentIncr = 0;    
    }

    function fixImgs(o1, o2) {
        var liList = o1.getElementsByTagName('li'); 
        currentIncr = 0;       
    }

    function hideItems(o) {
      var liList = o.getElementsByTagName('li'); 
      for(var j=0; j<liList.length; j++) {
        el = liList.item(j);
        el.style.display="none";
      }      
    }

    function incrInitC(o) {
      var liList = o.getElementsByTagName('li'); 
      for(var j=0; j<liList.length; j++) {
        el = liList.item(j);
        el.style.color = inititemC;
      }      
    }
        
    function fixArrays(arr1, arr2) {
      arrT = arr1;
      arrImgs = arr2;
      //d = document;
      //f = d.frames ? d.frames['slide'] : d.getElementById('slide');
      //p = f.document || f.contentWindow.document; contentDocument
      //oUl = p.getElementById("incrThings");
      d = window.frames["slide"].document;  
      oUl = d.getElementById("incrThings");
      oImg = d.getElementById("rimg");
      if (oUl) { 
        fixTimes(oUl);
        if (oImg) {
          fixImgs(oUl, oImg);
        }
      } else {
        oUL2 = d.getElementById("news");      
        if (oUL2) {
          fixTimes(oUL2);
          hideItems(oUL2);
        }
      }
    }
    
    function setItemColours(cur, prev, init) {
      curitemC = cur;
      previtemC = prev;
      inititemC = init;
    }
    
    function wplay() {
      if (oUl && autoAdvance) {
        liList = oUl.getElementsByTagName('li'); 
        if (currentIncr < liList.length) {
          if (this.position > arrT2[currentIncr]) {
            if (currentIncr > 0) {
              el = liList.item(currentIncr-1);
              el.style.color = previtemC;              
            }        
            el = liList.item(currentIncr);
            el.style.color = curitemC; 
            if (currentIncr <  arrImgs.length) {
              if (oImg) {
                oImg.src=arrImgs[currentIncr];
              }
            }
            currentIncr++;        
          }
        }
      }      
      if (oUL2 && autoAdvance) {
        liList = oUL2.getElementsByTagName('li'); 
        if (currentIncr < liList.length) {
          if (this.position > arrT2[currentIncr]) {
            if (currentIncr > 0) {
              el = liList.item(currentIncr-1);
              el.style.display="none";              
            }        
            el = liList.item(currentIncr);
            //alert('el: '+el + ' incr=' + currentIncr );
            el.style.display="block"; 
            currentIncr++;        
          }
        }
      }
    }

    function upIncr() {
      //alert('playing= ' + this.position);
      bReturn = false;
      if (oUl && !autoAdvance) { 
        liList = oUl.getElementsByTagName('li'); 
        if (currentIncr < liList.length) {
            if (currentIncr > 0) {
              el = liList.item(currentIncr-1);
              el.style.color = previtemC;              
            }        
            el = liList.item(currentIncr);
            //alert('el: '+el + ' incr=' + currentIncr );
            el.style.color = curitemC; 
            if (currentIncr <  arrImgs.length) {
              if (oImg) {
                oImg.src=arrImgs[currentIncr];
              }
            }            
            currentIncr++;
            bReturn = true;         
        }
      }       
      if (oUL2 && !autoAdvance) { 
        liList = oUL2.getElementsByTagName('li'); 
        //if (currentIncr < oUl.childNodes.length) {
        //el = oUl.childNodes[currentIncr];
        if (currentIncr < liList.length) {
            if (currentIncr > 0) {
              el = liList.item(currentIncr-1);
              el.style.display="none";              
            }        
            el = liList.item(currentIncr);
            //alert('el: '+el + ' incr=' + currentIncr );
            el.style.display="block"; 
            currentIncr++;
            bReturn = true;         
        }
      }      
      return bReturn;
    }
       
    function mp3Finished() {
      //alert('sound finished');
      currentId = '';
      if (autoAdvance) {
        nextSlide();
      }
    }
       
    function playMp3(path, n) {
      //       soundManager.play('mySound',path);
      // < script type="text/javascript">soundManager.createMovie();</ script> 
      //alert('in parent');
      //alert('path: ' + window.location.pathname);
      locpath = window.location.pathname;
      pArr = locpath.split("\\");
      pArr.pop();
      pArr.push(path);
      var strPath = pArr.join("\\");
      idname = 'slide'+n;
      currentId = idname;
      soundManager.stopAll();
      soundManager.createSound({
       id: idname, // required
       url: './'+path, // required
       // optional sound parameters here, see Sound Properties for full list
       volume: 50,
       autoPlay: false,
       whileplaying: wplay,
       onfinish: mp3Finished    // remember to omit comma on the last item
      });  
      showSoundButtons();
      soundManager.play(idname);    

    }

     function playMp3XX(path, n) {
      idname = 'slide'+n;
      soundManager.play('aCymbalSound');
     }
     
     function soundPause() {
      if (currentId.length > 1) {
        //alert('id='+currentId);
        soundManager.pause(currentId);
      }
     }
     function soundResume() {
      if (currentId.length > 1) {
        soundManager.resume(currentId);
      }     
     }
    function whichElement(e) {
      var targ
      if (!e) var e = window.event
      if (e.target) targ = e.target
      else if (e.srcElement) targ = e.srcElement
      if (targ.nodeType == 3) // defeat Safari bug
         targ = targ.parentNode
      var tname
      tname=targ.tagName
      alert("You clicked on a " + tname + " element.")
    }

    function documentClick(oEvent){	
    	var oEvent = (typeof oEvent != "undefined")? oEvent : event;
    	var oTarget = (typeof oEvent.target != "undefined")? oEvent.target : oEvent.srcElement;
    	//alert('oTarget= '+ oTarget.nodeName);
    	if(oTarget.nodeName.search(/input|select|option|\ba\b/i) == -1){
    		nextSlide();
    	}
    }
    // ---
    function documentKeyDown(oEvent){
    	var oEvent = (typeof oEvent != "undefined")? oEvent : event;
    	var oTarget = (typeof oEvent.target != "undefined")? oEvent.target : oEvent.srcElement;
    	if(oTarget.nodeName.search(/select|option/i) == -1){
    	   //8=backspace, 13=enter, 32=space, 37=left, 39=right
    		var intKeyCode = oEvent.keyCode;
    		switch(intKeyCode){			
    			case 8:
    			case 37:
    			case oEvent.altKey && 37:
    				oEvent.cancelBubble = true;
    				oEvent.returnValue = false;
    				if(oEvent.preventDefault){
    					oEvent.preventDefault();
    				}
    				prevSlide();				
    				return false;
    			case 13:
    			case 32:
    			case 39:
    			case oEvent.altKey && 37:
    				oEvent.cancelBubble = true;
    				oEvent.returnValue = false;
    				if(oEvent.preventDefault){
    					oEvent.preventDefault();
    				}
    				nextSlide();
    				break;
    			case 82:
    			  restart();
    			  break;
    		}
    	}	
    }    
    // ---
    function cancelDefaultNavigationEvents(oEvent){
    	var oEvent = (typeof oEvent != "undefined")? oEvent : event;
    	var oTarget = (typeof oEvent.target != "undefined")? oEvent.target : oEvent.srcElement;
    	if(oTarget.nodeName.search(/select|option/i) == -1){
    		var intKeyCode = oEvent.keyCode;
    		switch(intKeyCode){			
    			case 8:
    			case oEvent.altKey && 37:
    			case oEvent.altKey && 39:
    				oEvent.cancelBubble = true;
    				oEvent.returnValue = false;
    				if(oEvent.preventDefault){
    					oEvent.preventDefault();
    				}
    				documentKeyDown(oEvent);
    				break;
    		}	
    	}
    }
    // ---
    function setIframe(h) {
      var theIframe = document.getElementById('slide');
      alert('iframe= '+ theIframe)
      if (theIframe) {
        height = document.body.clientHeight;
        //viewport.getWinHeight();
        alert('iframe height= ' + height);
        //  both theIframe.height and theIframe.style.height seem to work 
        theIframe.style.height = Math.round( h * height ) + "px";
        theIframe.style.marginTop = Math.round( (height - parseInt(theIframe.style.height) )/2 ) + "px";
      }
    }
    
    function restart() {
      //alert('index');
      window.frames['slide'].location.reload(true);
    }
    // for sizing and positioning the iframe in the window
    // .5 for height="50%"
    //setIframe(.8);
    //$(document).ready(function() {
    //  setIframe(.8);
    //});
    
