var g_initPage = "home";
var g_initLang = "cz";

/*
Initialization, dojo parsing and default page loading
*/
function fnInit()
{
  // Parse page
  dojo.parser.parse();
  
  // Try to get page name from URL
  var page = g_initPage;
  var indexFrom = window.location.href.indexOf("?");
  if( indexFrom > -1 )
  {
    page = window.location.href.substring( indexFrom + 6 );
  }
  // Try to load page, if error -> load initPage
  getPage( page, g_initLang );
}

/*
Main function used for navigation thru website
Ajax is used to retrieve page from server
*/
function getPage( page, lang ) 
{
  if(!lang)
  {
    lang = g_initLang;
    // alert( lang );
  }
  lang = ( lang == 'cz' ) ? "" : "_" + lang;
  var url = "include" + lang + "/" + page + ".html";
  dojo.xhrGet( {
    // The following URL must match that used to test the server.
    url: url, 

    handleAs: "text",
    timeout: 5000, // Time in milliseconds
  
    // The LOAD function will be called on a successful response.
    load: function(response, ioArgs) {
      // First destroy all widgets
      // dojo.parser.parse(dijit.byId('pageContent'));
      dojo.byId("pageContent").innerHTML = response;
      dojo.parser.parse(dijit.byId('pageContent'));
      // alert( response );
      return response;
    },
  
    // The ERROR function will be called in an error case.
    error: function(response, ioArgs) {
      // console.error("HTTP status code: ", ioArgs.xhr.status);
      // alert( "ERROR ! " + url + "\n" + response);
      // getPage( g_initPage, g_initLang );
      return response;
      }
    });
}
