// ***************************************************************************
// class and functions for establishing and manipulating asynchronious link

live_blog = {};

live_blog.server_connection = null;
live_blog.anim_interval_id = null;
live_blog.time_of_last_change = -1;

live_blog.check_intervals = Array;

live_blog.check_intervals[0] = 12000;
live_blog.check_intervals[1] = 8000;
live_blog.check_intervals[2] = 6000;
live_blog.check_intervals[3] = 5000;
live_blog.check_intervals[4] = 4000;
live_blog.check_intervals[5] = 3000;

live_blog.current_interval_index = 0;

live_blog.time_at_current_interval = 0;

live_blog.entryGetFinish = function () {
  
  if (live_blog.server_connection.isReady()) {
  
    // var response_text = server_connection.getResponseText();
        
    // process the json response
    var response = eval ('(' + live_blog.server_connection.getResponseText() + ')');   
    
    if (response["notice"] == "changed") {
      
      // alert ("changeed!");
      
      live_blog.time_of_last_change = response["time_of_change"];
      document.getElementById("article_body").innerHTML = response["article_body"];

      // hide the no javascript message
      document.getElementById ("live_blog_jscript_disabled_msg_1").style.display = "none";
      document.getElementById ("live_blog_jscript_disabled_msg_2").style.display = "none";
      document.getElementById ("live_blog_jscript_enabled_msg_1").style.display = "block";
      
      live_blog.speedInterval(); // attempt to go faster
      
    } else if (response["notice"] == "unchanged") {
        
       live_blog.slowInterval(); // this may or may not slow the interval down
      
    } else {
      
      errorHandler();
      
    }
    
    live_blog.server_connection = null;
  
    // alert (response_text);
    
    // document.getElementById("article_body").innerHTML = response_text;
    
    
    
    /*    
    if (response_text == "done") {
      
      window.clearInterval(anim_interval_id);
      
    } else if (response_text == "no_change") {
    
      server_connection = null;
      
    } else {
    
      document.getElementById("article_id").innerHTML = response_text;
        
      server_connection = null;
    
    }
    */
    
    
  }  
  
  
}

live_blog.errorHandler = function() {
  
  live_blog.slowInterval(true); // force the interval to change

}

live_blog.testError = function() {
  

}



live_blog.getAndReplaceArticleText = function () {
    
  if (live_blog.server_connection == null) {
    
    var parameters = "article_dir=" + encodeURIComponent (live_blog.article_dir) + "&time_of_last_change=" + encodeURIComponent(live_blog.time_of_last_change);
    
    live_blog.server_connection = new asynchConnection(live_blog.errorHandler);
    
    live_blog.server_connection.sendPostRequest (live_blog.server_location + "util/live_blog_get_update.php", parameters, live_blog.entryGetFinish);
    
    live_blog.time_at_current_interval++;
    
  }

}







mobile_connection = null;
mobile_interval_id = null;

mobileGetFinish = function () {
  
  if (mobile_connection.isReady()) {
  
    // process the json response
    var response = eval ('(' + mobile_connection.getResponseText() + ')');   
    
    if (response["notice"] == "changed") {
      
      document.getElementById("mobile_blog_image_list").innerHTML = response["mobile_blog_body"];
      
    }
    
    mobile_connection = null;
  
  }   
  
}

getAndReplaceMobileImages = function () {

  if (mobile_connection == null) {
    
    var parameters = "article_dir=" + encodeURIComponent (live_blog.article_dir) + "&time_of_last_change=" + encodeURIComponent(live_blog.time_of_last_change);
    
    mobile_connection = new asynchConnection(live_blog.testError);
    
    mobile_connection.sendPostRequest (live_blog.server_location + "util/live_mobile_get_update.php", parameters, mobileGetFinish);
    
  }


}



live_blog.slowInterval = function (force) {
  
  if (force) {
  
    if (live_blog.check_intervals[live_blog.current_interval_index-1]) {
      live_blog.current_interval_index--;
      live_blog.setWindowInterval();
    }
    
    live_blog.time_at_current_interval = 0;
      
  } else {
  
    if (live_blog.time_at_current_interval >= live_blog.current_interval_index) {
  
      if (live_blog.check_intervals[live_blog.current_interval_index-1]) {
        live_blog.current_interval_index--;
        live_blog.time_at_current_interval = 0;
        live_blog.setWindowInterval();
      }
      
    }
  
  }    
  
}

live_blog.speedInterval = function () {
  
  if (live_blog.time_at_current_interval >= live_blog.current_interval_index) {

    if (live_blog.check_intervals[live_blog.current_interval_index+1]) {
      live_blog.current_interval_index++;
      live_blog.time_at_current_interval = 0;
      live_blog.setWindowInterval();
    }
  
  }
  
}


live_blog.setWindowInterval = function () {

  if (live_blog.anim_interval_id != null)
    window.clearInterval(live_blog.anim_interval_id);
    
  live_blog.anim_interval_id = window.setInterval(live_blog.getAndReplaceArticleText, live_blog.check_intervals[live_blog.current_interval_index]);

}




live_blog.onload = function() {
    
  // hide the no javascript message
  document.getElementById ("live_blog_jscript_disabled_msg_1").style.display = "none";
  document.getElementById ("live_blog_jscript_disabled_msg_2").style.display = "none";
  document.getElementById ("live_blog_jscript_enabled_msg_1").style.display = "block";
    
  // set a timer to call the get function 
  live_blog.setWindowInterval ();
  
  mobile_interval_id = window.setInterval(getAndReplaceMobileImages, 1000);
      
}


util.addLoadEvent (live_blog.onload);











// asynch connection stuff

asynchConnection = function(error_handler) {
  
	this.xmlHttp = new XMLHttpRequest();	
  
  var me = this;
  
  // the base return function calls the user return function with me as a parameter
  this.baseReturnFunction = function (req) { me.returnFunction (me); }	
  
  this.xmlHttp.onreadystatechange = this.baseReturnFunction;
  
  this.error_handler = error_handler;
  
}


// this will be redefined when the user makes a post or get request
asynchConnection.prototype.returnFunction = function(connection) { }


asynchConnection.prototype.isReady = function() {

  return ((this.xmlHttp.readyState == 4) && (this.xmlHttp.status == 200));
	
    if (this.xmlHttp.readyState == 4) {  
      try {  
         if (xhr.status == 200) {
            return true;
         }  
         }catch(e) {
          
          this.error_handler();
         
         }  
     }    	
	
}


asynchConnection.prototype.getResponseText = function() {
	
	return this.xmlHttp.responseText;
  	
}


asynchConnection.prototype.sendPostRequest = function(url, parameters, return_function) {
	
  // Open a connection to the server
  this.xmlHttp.open("POST", url, true);
	
  // this.xmlHttp.setRequestHeader("Content-type", "text/html; charset=utf-8");
  this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");  
  
  // set the user defined function to the passed value
  this.returnFunction = return_function;
  
  // Send the request
  this.xmlHttp.send(parameters);
  
}




