
var xmlHttp = createXmlHttpRequestObject();

var updateInterval = 5; // how many seconds to wait to get new message

var errorRetryInterval = 30; // seconds to wait after server error

var debugMode = true;

var loader = "<div id='loader'><p align='center'><img src='images/loader.gif'></p></div>";

function createXmlHttpRequestObject() 
{
  var xmlHttp;
  try
  {
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

function display($message)
{
  myDiv = document.getElementById("ajax-content-div");
  myDiv.innerHTML = $message;
}

function displayPP($message)
{
  myDiv = document.getElementById("ajax-content-pp-div");
  myDiv.innerHTML = $message;
}

function displayError($message)
{
  display("Error retrieving the news message! Will retry in " +  errorRetryInterval + " seconds." +  (debugMode ? "<br/>" + $message : ""));
}



function read(what, id)
{
  if (xmlHttp)
  {
    try
    {
      display(loader);
      xmlHttp.open("GET", "ajaxread.php?what=" + what + "&id=" + id, true);
      xmlHttp.onreadystatechange = handleGettingContents;
      xmlHttp.send(null);
    }
    catch(e)
    {
      displayError(e.toString());
    }
  }
}


function handleGettingContents() 
{
  if (xmlHttp.readyState == 4) 
  {
    if (xmlHttp.status == 200) 
    {
      try
      {
        getContents();
      }
      catch(e)
      {
        displayError(e.toString());
      }
    } 
    else
    {
      displayError(xmlHttp.statusText);   
    }
  }
}

function getContents()
{
  var response = xmlHttp.responseText;
  if (response.indexOf("ERRNO") >= 0 || response.indexOf("error") >= 0  || response.length == 0)
    throw(response.length == 0 ? "Server error." : response);
  display(response);
}


// lettura da prima pagina


function readpp(what, id)
{

  if (xmlHttp)
  {
    try
    {
      displayPP(loader);
      xmlHttp.open("GET", "ajaxread.php?what=" + what + "&id=" + id, true);
      xmlHttp.onreadystatechange = handleGettingPPContents;
      xmlHttp.send(null);
    }
    catch(e)
    {
      displayError(e.toString());
    }
  }
}


function handleGettingPPContents() 
{
  if (xmlHttp.readyState == 4) 
  {
    if (xmlHttp.status == 200) 
    {
      try
      {
        getPPContents();
      }
      catch(e)
      {
        displayError(e.toString());
      }
    } 
    else
    {
      displayError(xmlHttp.statusText);   
    }
  }
}

function getPPContents()
{

  var response = xmlHttp.responseText;
  if (response.indexOf("ERRNO") >= 0 || response.indexOf("error") >= 0  || response.length == 0)
    throw(response.length == 0 ? "Server error." : response);
    displayPP(response);
	
var links = $$(".item");
		links.each(function(link, i){
			link.addEvent('click', function(e){
				this.addClass('selectedCont');
				links.each(function(other, j){
					if (other != link){
						other.removeClass('selectedCont');
					}
				});
			});
		});

}



window.addEvent('domready', function(){
		var links = $$(".item");
		links.each(function(link, i){
			link.addEvent('click', function(e){
				this.addClass('selectedCont');
				links.each(function(other, j){
					if (other != link){
						other.removeClass('selectedCont');
					}
				});
			});
		});


});

