/**
 *Load some content into a page after the page is loaded. Depends on the
 *synchronous AJAX loading behaviour of javascript.
 */

/**
 *Constructor.
 */
function AutoContentLoader(){
  
  //this.id = '';
};

/**
 *Populate an element with the result.
 */
AutoContentLoader.prototype.populate = function( response, id ){
  
  dojo.byId( id ).innerHTML = response;
  
  //Instantiate any Dojo content.- seems unnecessary now that dojo 1.0.1 is here.
  //dojo.parser.parse( dojo.byId( id ) );
}

/** 
 *Load something.
 */
AutoContentLoader.prototype.run = function( wotzit ){
  
  //Plug in an activity/progress indicator.
  dojo.byId( wotzit.id ).innerHTML = "<p>Loading ... &nbsp; <img alt='Progress or Activity indicator' src='" + CapsoolGlobalVariables.AppBase + "/graphics/img/progress/indicator.gif' width='32' height='32' /></p>";
  
  //Run the before function if present.
  if( wotzit.before )
    wotzit.before();
  
  //Stage to run the after function if present.
  var afterwards = wotzit.afterwards;
  
  //this.id = wotzit.id;
  var id = wotzit.id;
  
  var args = {
    error:   function(response, ioArgs ){
      window.alert('An error has occurred while auto loading content');
      //console.log( response );
      //console.log( ioArgs );
    },
    content: wotzit.params,
    load:    function( response, ioArgs ){
      autoContentLoader.populate(response, id);
      
      if( afterwards )
        afterwards();
    },
    url:     wotzit.url
  };
  
  if( wotzit.type == 'get' )
    dojo.xhrGet( args );
  
  else if( wotzit.type == 'post' )
    dojo.xhrPost( args );
};

autoContentLoader = new AutoContentLoader();
