// stores reference to XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();

// from index.php

function initImage() {
  imageId = 'media_file';
  image = document.getElementById(imageId);
  setOpacity(image, 0);
  image.style.visibility = 'visible';
  fadeIn(imageId,0);
}

// ASC: copied from http://www.sean.co.uk/a/webdesign/javascriptdelay.shtm
function pausecomp(millis) {
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while(curDate-date < millis);
}  

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

function fadeIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    
  
    
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 8;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 60);
    }
 
  }
}

//Retrieves XMLHttpRequest object
function createXmlHttpRequestObject() {
  //will store the reference to the XMLHttpRequest object
    var xmlHttp;
    //if running Internet Explorer
    if(window.ActiveXObject) {
           try {
               xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
           }
        catch (e) {
            xmlHttp = false;
        }
    } else {
        // if running Mozilla or other
        try {
            xmlHttp = new XMLHttpRequest();
        }
        catch (e) {
            xmlHttp = false;
        }
    }
  //return the created object or display an error message
    if (!xmlHttp)
        alert("Error here");
    else
        return xmlHttp;
}
//alert("javascript is included");
//Make asynchronous HTTP request using the XMLHttpRequest object
function homepage_images(arrayID) {
    //alert("in homepage_images and arrayID is " + arrayID);
    //proceed only if xmlHttp object isn't busy
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    // execute vote.php from the server
        //alert("going to homepage_images.php?arrayID="+ arrayID);
        xmlHttp.open("GET", "homepage_images.php?arrayID="+ arrayID, true);
    //define the method to handle server responses
        xmlHttp.onreadystatechange = handleServerResponse;
    //make the server request
        xmlHttp.send(null);
    } 
}



//executed automatically when a message is received from the server
function handleServerResponse() {
    //move forward only if transaction has completed
    if (xmlHttp.readyState == 4) {
        //status of 200 indicates the transaction completed successfully
        if (xmlHttp.status == 200) {
            //extract the XML received from the server
            xmlResponseThis = xmlHttp.responseXML;
            //obtain the document element (the root element) of the XML structure
            xmlDocumentElementThis = xmlResponseThis.documentElement;
            //retList = xmlDocumentElement.firstChild.data;
            imgsrcArray = xmlDocumentElementThis.getElementsByTagName("imgsrc");
            array_id = xmlDocumentElementThis.getElementsByTagName("array_id").item(0).firstChild.data;
            //alert("array_id is " + array_id);
            max_number = xmlDocumentElementThis.getElementsByTagName("max_number").item(0).firstChild.data;
            var html = "";
            //iterate through arrays to create html structure
            for (var i=0; i<imgsrcArray.length; i++) {
                imgsrc = imgsrcArray.item(i).firstChild.data;
                html += imgsrc;    
                }
             
            document.getElementById("media_file").src = html;
            array_id++;
           
            if (array_id == max_number) {
                array_id = 0;
            }
            
            
            
            timeOuts = new Array();
            timeOuts[0] =setTimeout('setOpacity(document.getElementById("media_file"),90)', 4550);
            timeOuts[1] =setTimeout('setOpacity(document.getElementById("media_file"),80)', 4600);
            timeOuts[2] =setTimeout('setOpacity(document.getElementById("media_file"),70)', 4660);
            timeOuts[3] =setTimeout('setOpacity(document.getElementById("media_file"),60)', 4700);
            timeOuts[4] =setTimeout('setOpacity(document.getElementById("media_file"),50)', 4750);
            timeOuts[5] =setTimeout('setOpacity(document.getElementById("media_file"),40)', 4800);
            timeOuts[6] =setTimeout('setOpacity(document.getElementById("media_file"),30)', 4850);
            timeOuts[7] =setTimeout('setOpacity(document.getElementById("media_file"),20)', 4900);
            timeOuts[8] =setTimeout('setOpacity(document.getElementById("media_file"),10)', 4950);
            timeOuts[9] =setTimeout('setOpacity(document.getElementById("media_file"),0)', 5000);
            setOpacity(document.getElementById("media_file"),0);    
            
            timeOuts[10] =setTimeout('homepage_images(array_id)',5000)
 
        } else {
            //a http status different that 200 signals an error
            alert("problem: " + xmlHttp.statusText);
        }
    }
}


function clearAllTimeouts(){  
       //alert ("clearing timeouts");
       for(key in timeOuts ){  
            clearTimeout(timeOuts[key]);  
       }  
    } 

