
//getTomatoes();


function getTomatoes() {
   var rotTomatoes = createRequest();
   rotTomatoes.load("tomatoes.html");
   var divTomatoes = document.getElementById("tomatoes");
   divTomatoes.innerHtml = rotTomatoes.response.html;

}


function getTime() {
  request = createRequest();
  var currTime = new Date();
  var url = "time.php?tor=" + currTime.getTime();
  request.open("GET", url, true);
  request.onreadystatechange = updateTimeInPage;
  request.send(null);
}


function updateTimeInPage() {
  if (request.readyState == 4) {
    var jaxtime = request.responseText;
    var timeE1 = document.getElementById("ajaxTime");
    replaceText(timeE1, jaxtime);   
  }
}

function toggleMap() {
   var elMap = document.getElementById("map");

   if (elMap.style.visibility == "hidden") {
      elMap.style.visibility = "visible";
   } else {
      elMap.style.visibility = "hidden";
   }
}


function addListeners() {
   var theaters = 0;
   var elTDs = document.getElementsByTagName("td");
    // iterate through td tags to find address class 
   for (i=0; i<elTDs.length; i++) {
      //if (elTDs[i].getAttribute("class-name") == "address") {
      if (elTDs[i].className == "address") {
         theaters = theaters + 1;
      //   elTDs[i].setAttribute("onclick", "clickAddress");
      //   elTDs[i].attachEvent("onclick", "clickAddress()");
      //   alert("HERE");
         elTDs[i].onclick = clickAddress;
      }
   }

 //  alert (theaters);


}


function clickAddress() {
   //var clickedAddress = this.childNodes.length;
   //var elMap = document.getElementById("map");
   //alert("Clicky Detected!");
   getMap(this.firstChild.nodeValue);
   //elMap.appendChild(document.createTextNode(this.firstChild.nodeValue));
   //toggleMap();

   //alert(this.firstChild.nodeValue);
}


function getMap(location) {
  objXHRmap = createRequest();
  var currTime = new Date();
  var url = "scripts/_getmap.php"
          + "?loc=" + location             //encodeURIComponent(location)
          + "&tor=" + currTime.getTime();
//alert(url);
  objXHRmap.open("GET", url, true);
  objXHRmap.onreadystatechange = onMapResponse;
  objXHRmap.send(null);
}

function onMapResponse() {
    if (objXHRmap.readyState ==4) {
       var srcResult = objXHRmap.responseXML.getElementsByTagName("Result");

 //alert(srcResult[0].firstChild.nodeValue);
   document.getElementById("mapimage").src = srcResult[0].firstChild.nodeValue;

   document.getElementById("map").style.visibility = "visible";

//      var urlResult = srcResult[0].firstChild.nodeValue;

//	window.open( urlResult, "Theater", "width=640,height=480, scrollbars=\"no\", "
//                     + " toolbar=\"no\", location=\"no\", directories=\"no\", status=\"no\", "
//                     + " menubar=\"no\", copyhistory=\"no\"");

   }
}

function _onMapResponse() {
    if (objXHRmap.readyState ==4) {
       var srcResult = objXHRmap.responseXML.getElementsByTagName("Result");

 //alert(srcResult[0].firstChild.nodeValue);
   document.getElementById("mapimage").src = srcResult[0].firstChild.nodeValue;

    document.getElementById("map").style.visibility = "visible";

   }
}


function getWeather() {
  objXHRweather = createRequest();
  var currTime = new Date();
  var zipcode = document.getElementById("zipcode").value;

  var url = "scripts/_getweather.php"
          + "?loc=" + zipcode;
          + "&tor=" + currTime.getTime();

  objXHRweather.open("GET", url, true);
  objXHRweather.onreadystatechange = onWeatherUpdate;
  objXHRweather.send(null);
}

function onWeatherUpdate() {
    if (objXHRweather.readyState ==4) {

	//-- First Eliminate existing weather content
       var elYdiv = document.getElementById("yweather");
       while (elYdiv.firstChild) {
          elYdiv.removeChild(elYdiv.firstChild);
       }

       //-- Create Weather Title Div
	var elWeather = document.createElement("div");
       var elTitle   = document.createTextNode("Local Weather");
       elWeather.className = "menuheader";
       
       elWeather.appendChild(elTitle);
       document.getElementById("yweather").appendChild(elWeather);


       var elWBox = document.createElement("div");
       elWBox.id = "weather1";
       elWBox.className = "weather";
       document.getElementById("yweather").appendChild(elWBox);

       xml = objXHRweather.responseXML;
       xmlWeather = xml.getElementsByTagName("description");


       var icoWeather = document.createElement("img");
       icoWeather.src = "http://l.yimg.com/us.yimg.com/i/us/we/52/12.gif";
       elWBox.appendChild(icoWeather);

	var strCurConditions = document.createElement("div");
       var sectheading = document.createElement("b");
       sectheading.appendChild(document.createTextNode("Current Conditions:"));
       strCurConditions.appendChild(sectheading);
       strCurConditions.appendChild(document.createElement("br"));

       
       var cCondition = xml.getElementsByTagName("condition");
       if (cCondition.length == 0) {
          cCondition = xml.getElementsByTagName("yweather:condition");
       }

       var cUnits = xml.getElementsByTagName("units");
       if (cUnits.length == 0) {
          cUnits = xml.getElementsByTagName("yweather:units");
       }

       //-- Display Current Weather Conditions
       strCurConditions.appendChild(document.createTextNode(
             cCondition[0].getAttribute("text") + ", "
           + cCondition[0].getAttribute("temp") + " "
           + cUnits[0].getAttribute("temperature")));
       
       strCurConditions.appendChild(document.createElement("br"));
       elWBox.appendChild(strCurConditions);

       //-- Display Weather forcast
	var strForecast = document.createElement("div");
       var sectheading = document.createElement("b");
       sectheading.appendChild(document.createTextNode("Forecast:"));
       strForecast.appendChild(sectheading);
       strForecast.appendChild(document.createElement("br"));

       var cForecast = xml.getElementsByTagName("forecast");
       if (cForecast.length == 0) {
          cForecast = xml.getElementsByTagName("yweather:forecast");
       }

       for (var i=0; i < cForecast.length; i++) {
       var fDate = cForecast[i].getAttribute("date");
       var fDay  = cForecast[i].getAttribute("day");
       var fLow  = cForecast[i].getAttribute("low");
       var fHi   = cForecast[i].getAttribute("high");
       var fText = cForecast[i].getAttribute("text");

       strForecast.appendChild(document.createTextNode(fDay + " - " + fText + ", High: " + fHi + " Low: " + fLow));
       strForecast.appendChild(document.createElement("br"));  
       }

       strForecast.appendChild(document.createElement("br"));

       elWBox.appendChild(strForecast);

       //-- Create link for more weather related to this location
       var cLocation = xml.getElementsByTagName("location");
       if (cLocation.length == 0) {
          cLocation = xml.getElementsByTagName("yweather:location");
       }

       var yLink = document.createElement("a");
       yLink.href = xml.getElementsByTagName("link")[0].firstChild.nodeValue;
       yLink.appendChild(document.createTextNode("Yahoo! Weather for " + cLocation[0].getAttribute("city") 
                                               + ", " + cLocation[0].getAttribute("region")));
      
       elWBox.appendChild(yLink);
    
     //  document.getElementById("weather1").appendChild(xmlWeather[1]);
    }
}







function findMe() {
  objXHRfindME = createRequest();
  var currTime = new Date();
  var url = "myip.php?tor=" + currTime.getTime();

  objXHRfindME.open("GET", url, true);
  objXHRfindME.onreadystatechange = updateMyLocation;
  objXHRfindME.send(null);
}

function updateMyLocation() {
    if (objXHRfindME.readyState ==4) {
    replaceText(document.getElementById("lostandfound"), "Your IP address is " + objXHRfindME.responseText + ".");
  }
}

//-- When page first loads, we will try to guess the
//-- visitors geographic location and load a movie
//-- list based on the location.  If the response from
//-- the geolocator is city unknown, then we will
//-- default to ypsilanti (of course)
function firstList(ipaddress) {

  findMovies();

 //  alert(ipaddress);

}

function findMovies() {
  objXHRgetMovies = createRequest();
  var zipcode = document.getElementById("zipcode").value;
  var currTime = new Date();
  var url = "movielistings-proxy.php?zipcode=" + zipcode  + "&tor=" + currTime.getTime();

  objXHRgetMovies.open("GET", url, true);
  objXHRgetMovies.onreadystatechange = updateMyMovieList;
  objXHRgetMovies.send(null);
}

function updateMyMovieList() {
    if (objXHRgetMovies.readyState ==4) {

	// Never Ever do what I'm doing here...messing with innerHTML is asking for trouble
	document.getElementById("movielist").innerHTML = objXHRgetMovies.responseText;

	getWeather();

       // after adding theater movie listings, then add click listener events
       addListeners();
  }
}





