//
// 6/20/06 - couldn't get the opacity thing working without
//           flicker. Try again later, maybe, or find another
//           way to do it. Jeri
//
function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 
function shiftOpacity(id, millisec) { 
    //if an element is invisible, make it visible, else make it ivisible 
    if(document.getElementById(id).style.opacity == 0) { 
        opacity(id, 0, 100, millisec); 
    } else { 
        opacity(id, 100, 0, millisec); 
    } 
} 
//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 

//This is the actual AHAH get/paste into target code

	function ahah(url,target) {
   // native XMLHttpRequest object
   // document.getElementById(target).innerHTML = 'Updating ...';
   if (window.XMLHttpRequest) {
       req = new XMLHttpRequest();
       req.onreadystatechange = function() {ahahDone(target);};
       req.open("GET", url, true);
       req.send(null);
   // IE/Windows ActiveX version
   } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
       if (req) {
           req.onreadystatechange = function() {ahahDone(target); ;};
           req.open("GET", url, true);
           req.send();
       }
   }
	// execute any <SCRIPT> that has just been updated via AHAH
	// try executing this selectively only when needed
	// for now, when calling complex_ahah to update the slideshow
	
}    

function ahahDone(target) {

   // only if req is "loaded"
   if (req.readyState == 4) {
       // only if "OK"
       if (req.status == 200 || req.status == 304) {
           results = req.responseText;
			  //OK - <SCRIPT> is here  . . .
			  //alert("RESULTS:        " +results);

           document.getElementById(target).innerHTML = results;
			  // IE is stripping out the <SCRIPT> stuff! !@*&$^@$(*&%^
			  // OK, <SCRIPT> tag can't be first thing returned in innerHTML - stupid ID bug
			  //http://tinymce.moxiecode.com/punbb/viewtopic.php?pid=14095
			  //alert("InnerHTML:        " +document.getElementById(target).innerHTML);

			  execJS(document.getElementById(target));	
			      } else {
           document.getElementById(target).innerHTML="ahah error:\n" +
               req.statusText;
       }
   }
}

//when using AHAH to update a page (complex_ahah)
//SCRIPT within the code snippet is not executed
//this will execute any scripts found within the 'target' node
function execJS(node)
{
  var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
  var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
  var bMoz = (navigator.appName == 'Netscape');

  if (!node) return;
  
  /* IE wants it uppercase */
  var st = node.getElementsByTagName('SCRIPT');
//  alert("Found " +st.length +" scripts");
 
  var strExec;
  for(var i=0;i<st.length; i++)
  {
    if (bSaf) {
      strExec = st[i].innerHTML;
      st[i].innerHTML = "";
    } else if (bOpera) {
      strExec = st[i].text;
      st[i].text = "";
    } else if (bMoz) {
      strExec = st[i].textContent;
      st[i].textContent;
    } else {
      strExec = st[i].text;
      st[i].text = "";
    }
	 //alert(strExec);

    try {
      var x = document.createElement("SCRIPT");
      x.type = "text/javascript";

      /* In IE we must use .text! */
      if ((bSaf) || (bOpera) || (bMoz))
        x.innerHTML = strExec;
      else x.text = strExec;

      document.getElementsByTagName("HEAD")[0].appendChild(x);
    } catch(e) {
      alert(e);
    }
  }
};
   

//
// Another variation to execute scripts returned via AHAH, AJAX, whatever
//http://nerd.newburyportion.com/2006/07/going-global
/** Execute a script in the global context. This installs all functions
    defined in this script into the global scope, unless they are
    explicitly created in different scopes.

    @param script   the actual script DOM element
 **/

function installScript( script )
{
    if (!script)
        return;

    if (script.src)
    {
        var head = document.getElementsByTagName("head")[0];
        var scriptObj = document.createElement("script");

        scriptObj.setAttribute("type", "text/javascript");
        scriptObj.setAttribute("src", script.src);  

        head.appendChild(scriptObj);

    }
    else if (script.innerHTML)
    {
        //  Internet Explorer has a funky execScript method that makes this easy
        if (window.execScript)
            window.execScript( script.innerHTML );
        else
            window.setTimeout( script.innerHTML, 0 );
    }
}
