var lastContentChangeKey = null;
var contentLocArray = new Array();
contentLocArray[0] = 'center';
contentLocArray[1] = 'left';
contentLocArray[2] = 'right';
contentLocArray[3] = 'bottom';

function goContentLoop() {
// determine loc
do {
  loc = pickContentLoc();
}
while (loc == lastContentChangeKey);
// set last holder
lastContentChangeKey = loc;
// setTimeout
setTimeout("goAjax('"+loc+"');", Math.floor(Math.random()*rotateTimeoutMin) +rotateTimeoutMax);
}

function pickContentLoc() {
var randElement = contentLocArray[Math.floor(Math.random()*contentLocArray.length)];
return randElement;
}

function establishAjaxConnection() {
var ajaxError = false;
try {
  requester = new XMLHttpRequest();  // GLOBAL
}
catch (error) {
  try {
    requester = new ActiveXObject("Microsoft.XMLHTTP");  // GLOBAL
  }
  catch (error) {
    ajaxError = true;
  }
}
return ajaxError;
}

function goAjax(loc) {
// establish new connection
var ajaxError = establishAjaxConnection();
if (ajaxError) {
goContentLoop();
}
else {
// open file
var ajaxFile = 'ajaxContent.php?loc='+loc+'&countriesid='+countriesid+'&languagesid='+languagesid;
requester.open("GET", ajaxFile);
// send file
requester.send(null);
// event handler
requester.onreadystatechange = stateHandler;
}
}

function stateHandler() {
 if (requester.readyState == 4) {
   if (requester.status == 200) {
     success();
   }
   else {
     failure();
   }
 }
 return true;
}

function failure() {
goContentLoop();
}

function success() {

  // grab text
  var mytext = requester.responseText;

  if (mytext == '') {
  goContentLoop();
  }
  else {
	
    // extract elementId
    var a = mytext.indexOf('***');
    var loc = mytext.substr(0, a);
    var a = a + 3;
    mytext = mytext.substr(a);
    // extract first URL
    if (mytext.indexOf('***') != -1) {
		  a = mytext.indexOf('***');
      var firstUrl = mytext.substr(0, a);
		}
		else {
		  var firstUrl = mytext;
		}
    a = a + 3;
    secondUrl = mytext.substr(a);

    // place content
    if (loc == 'center' || loc == 'caption') {
      document.getElementById("imgCenter").src  = firstUrl;
    	document.getElementById("imgCaption").src = secondUrl;
    }
    else {
      var elementId = 'img'+capFirstLetter(loc);
    	document.getElementById(elementId).src = firstUrl;
    }
    goContentLoop();
		
  }
}

function capFirstLetter(val) {
var newVal = val.substring(0,1).toUpperCase() + val.substring(1,val.length);
return newVal;
}