/*
Javascript file for WVTC 
*/

var songUpdateIntervalID;

/*
This method uses a couple different methods of instantiating the
XMLHttpRequest object. The reason we do this is so we can support
multiple browser (I've only tested in IE and Firefox).
*/
function GET_XMLHTTPRequest()
{
  var request;
    
  // Lets try using ActiveX to instantiate the XMLHttpRequest object
  try {
    request = new ActiveXObject("Microsoft.XMLHTTP");
  } catch(ex1) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(ex2) {
      request = null;
    }
  }

  // If the previous didn't work, lets check if the browser 
  // natively support XMLHttpRequest 
  if(!request && typeof XMLHttpRequest != "undefined"){
    //The browser does, so lets instantiate the object
    request = new XMLHttpRequest();
  }
  return request;
}

//--------------------------------------------------------------------------

//This function updates the now playing song.
//var divHandleCurrentSong = document.getElementById("currentsong");
function UpdateCurrentSong()
{
  var CurrentSongXMLHttpRequest;
    //Instantiate a new XMLHttpRequest object
    CurrentSongXMLHttpRequest = GET_XMLHTTPRequest();
    //Make sure the XMLHttpRequest object was instantiated or fail silently
    if(!CurrentSongXMLHttpRequest){
      return;
    }
    //Tell the XMLHttpRequest object what we want it to do.
    //In the first parameter we're telling it to use HTTP GET for the request
    //In the second parameter we're telling it what page to request
    //In the third parameter we're telling it to do the request asychronously
    CurrentSongXMLHttpRequest.open("GET", "currentsong.cgi", true);
    
    //Lets define the method that gets called when the request finishes
    CurrentSongXMLHttpRequest.onreadystatechange = function (aEvt) {
      //Any time the readyState of the XMLHttpRequest object changes this method is called.
      //Loading is complete when the readyState equals 4, so that's the only value we care about right now.
      if(CurrentSongXMLHttpRequest.readyState == 4){
    	  //Sweet! The page loaded. Now lets set the contents of the request to the TextArea
        //document.getElementById("currentsong").value = CurrentSongXMLHttpRequest.responseText;
        document.getElementById("currentsong").innerHTML = CurrentSongXMLHttpRequest.responseText;
      }
    };
	  
	  //Lets fire off the request
    CurrentSongXMLHttpRequest.send(null);
}

//makes now playing refresh
songUpdateIntervalID = setInterval('UpdateCurrentSong()', (15*1000));

//-------------------------------------------------------------------------------------

var requestUpdateInterval = null;
function StartUpdatingCurrentRequests()
{
  requestUpdateInterval = setInterval('UpdateCurrentRequests()' , (60*1000));
}

function UpdateCurrentRequests()
{
    var myXMLHttpRequest;
    //Instantiate a new XMLHttpRequest object
    myXMLHttpRequest = GET_XMLHTTPRequest();
    if(!myXMLHttpRequest){
      //fail silently if HMLHttpRequest was not instantiated
      return;
    }
    //Tell the XMLHttpRequest object what we want it to do.
    //In the first parameter we're telling it to use HTTP GET for the request
    //In the second parameter we're telling it what page to request
    //In the third parameter we're telling it to do the request asychronously
    myXMLHttpRequest.open("GET", "requests.cgi?xml=1", true);
    
    //Lets define the method that gets called when the request finishes
	  myXMLHttpRequest.onreadystatechange = function (aEvt) {
	    //Any time the readyState of the XMLHttpRequest object changes this method is called.
      //Loading is complete when the readyState equals 4, so that's the only value we care about right now.
      if(myXMLHttpRequest.readyState == 4){
        //Sweet! The page loaded. Now lets set the contents of the request to the TextArea
        //document.getElementById("currentrequests").value = myXMLHttpRequest.responseText;
        document.getElementById("currentrequests").innerHTML = myXMLHttpRequest.responseText;
      }
    }
    //Lets fire off the request
    myXMLHttpRequest.send(null);
}

//-------------------------------------------------------------------------------------

function popUp(URL) {
  day = new Date();
  id = day.getTime();
  window.open(URL,  id, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=418,height=115,left = 422.5,top = 265');
}

//-------------------------------------------------------------------------------------


function wait(delay,next){
if (next ==0) { next = 8; }
setTimeout('swapphotos(' + next + ')',delay);
}


function swapphotos(item){
  tempfile = 'http://wvtc.net/video.jpg' + '?' + new Date().getTime();
  document['webshothidden'].src = tempfile;
}

//-------------------- o blargh?

//Used by snd_request(), this is global var so we can clear it
//and prevent two timeouts for the footer overlaping
var request_timeout = null;

//snd_request() adds a request - updates footer for feedback to user
function snd_request(pdt, name){
  var footer = document.getElementById("footer");
  //make sure we can request with xmlhttp, otherwise fail gracefully
  var xhttp = GET_XMLHTTPRequest();
  if(!xhttp){
    location.href = "add_req.cgi?name="+name+"&pdt="+pdt;
    return;
  }
  //We can start proccessing the request stop updating the footer
  clearInterval(songUpdateIntervalID);
  //throw this in the footer so the user knows that something is happening.
  footer.innerHTML ="<div id='requestFooter'>" +
                    "<img src='images/throbber.gif' />" +
                    "&nbsp;&nbsp;Requesting Song</div>";
  xhttp.onreadystatechange=function(){
    if(xhttp.readyState == 4){
	  //xhttp has completed the request, show the user what we got
      footer.innerHTML = "<div id='requestFooter'>" +
                          xhttp.responseText + "</div>";
      //wait some time then put the footer back in and update it
      //(First, if we've already set this, clear it)
      if(request_timeout != null){
        request_timeout = clearTimeout(request_timeout);
      }
      request_timeout = setTimeout(function(){
        //reset the footer back to normal
        footer.innerHTML = "<div id=\"currentsong\"></div>";
        UpdateCurrentSong();
        songUpdateIntervalID = setInterval('UpdateCurrentSong()', 15000);
        }, 5000);
    }
  }
  //send the http request
  xhttp.open("GET", "add_req.cgi?xml=1&name="+name+"&pdt="+pdt, true);
  xhttp.send(null);
}


