var lastSectionRequested = null;
var sections = new Object;


function initSections () {
	
	  sections.s_names = new Array ("about", "contact", "folio");
	
	  sections['about'] = new Object;
	  sections['contact'] = new Object;
	  sections['folio'] = new Object;
	  
	  sections['about']['file'] = 'about_content.html';
	  sections['about']['html'] = '';
	  
	  sections['contact']['file'] = 'contact_content.html';
	  sections['contact']['html'] = '';

	  sections['folio']['file'] = 'folio_content.html';
	  sections['folio']['html'] = '';
	  
	  sections.length = sections.s_names.length; 
	  sections.allLoaded = false;
	  
}
  
  
function getNextContent () {
	if (!sections.allLoaded) {
	
	  var total = 0;
	  for (var i =0 ; i<sections.s_names.length ; i++) {
		s_name = sections.s_names[i];
	
	    if (sections[s_name]['html'] == '') {
		  getContent(s_name);
		  return;
	    }
	    total++;
	  }
	
	  if (total >= sections.length) { // last one done
	    sections.allLoaded = true;
		preloadAjaxImages();
	  }
	}
}

function getContent (section) {
	  lastSectionRequested = section;
	  sendRequest(sections[section]['file']);
}


// sends a request
function sendRequest(url)
{
  xmlHttp=GetXmlHttpObject();
  if (xmlHttp==null)
    {
     alert ("Your browser does not support AJAX!");
     return;
    } 
  xmlHttp.onreadystatechange=stateChanged;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
} 

// proceses the data returned from the voting - the overall results of the vote
function stateChanged() 
{ 
  if (xmlHttp.readyState==4)
  { 
    var res = trim(xmlHttp.responseText);
	
	  if (res.indexOf('error') < 0) {
		  loadContent(lastSectionRequested, res);
	  }
  }
}

function loadContent(section, content) {
	
    //alert("ajax: section " + section + " content loaded: " + content);
	sections[section]['html'] = content;
	
	getNextContent();
	
	// reload the content to make sure that the folio initialises if it is the current section
	switchContent ();
	
}

function GetXmlHttpObject()
{
  var xmlHttp=null;
  try   {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  } catch (e)   {
    // Internet Explorer
    try  {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }  catch (e)     {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}


function trim(s){
  return s.replace(/^\s*(.*?)\s*$/,"$1");
}
