
function insertMostRecentPhotos(modifyScript, numPhotos) {
    //var modifyScript = "ajaxCalls.php"
    var divId = "mostRecentPhotos";
    var loadingImgId = "loadingImage";
    var params = "action=mostRecentPhotos";
    if(numPhotos > 0)
      params += "&numPhotos=" + numPhotos;
    doGenericAjaxUpdate(divId, modifyScript, params, loadingImgId);
}

function changeBackgroundImage(divId, modifyScript) {
    new Ajax.Updater('', modifyScript,
      {
        onLoading:function(request) {
        },

        onComplete:function(request) {
          if(request.statusText == "OK" && request.responseText != "ERROR") {
            var theDiv = document.getElementById(divId);
            theDiv.style.backgroundImage =
                         "url('" + request.responseText + "')";
          }
          else {
          }
        },

        parameters:'action=randomHeader',

        evalScripts:true,

        asynchronous:true
      }
    )
}

function doGenericAjaxUpdate(divToUpdate, scriptToCall, parameters, loadingImgId) {
    new Ajax.Updater('', scriptToCall,
      {
        onLoading:function(request) {
          if(!(loadingImgId === undefined))
            Element.show(loadingImgId);
        },

        onComplete:function(request) {
          var theDiv = document.getElementById(divToUpdate);
          if(request.statusText == "OK" && request.responseText != "ERROR") {
            theDiv.innerHTML = request.responseText;
          }
          else {
            theDiv.innerHTML = "Unexpected error occurred. Please try again later.";
          }
          if(!(loadingImgId === undefined))
            Element.hide(loadingImgId);
        },

        parameters:parameters,

        evalScripts:true,

        asynchronous:true
      }
    )
}

function PhotoShower(divId, modifyScript, category) {
  this.divId = divId;
  this.modifyScript = modifyScript;
  this.category = category;

  this.showPhoto = function(id) {
    var loadingImgId = "loadingImage";
    var params = 'action=showPhoto&id=' + id;
    doGenericAjaxUpdate(divId, modifyScript, params, loadingImgId);
  }

  this.showCategoryPhotos = function(page) {
    var loadingImgId = "loadingCategory";
    var params = 'action=photosInCategory&category=' + this.category +
                 '&page=' + page;
    var theDiv = document.getElementById(this.divId);
    theDiv.innerHTML = "<img src='img/ajax-loader-body.gif' alt='Loading...' />";
    doGenericAjaxUpdate(this.divId, this.modifyScript, params, loadingImgId);
  }

  this.showCategories = function() {
    var loadingImgId = "loadingCategories";
    var params = 'action=photoCategories';
    doGenericAjaxUpdate(this.divId, this.modifyScript, params, loadingImgId);
  }
}


