// the user-specific settings
var USER_ID = "";
var USER_SCREEN_NAME = "";

var USER_RIGHTS_READ = false;
var USER_RIGHTS_EDIT = false;
var USER_RIGHTS_ADD = false;
var USER_RIGHTS_DELETE = false;
var USER_RIGHTS_COMMENT = false;
var USER_RIGHTS_SETUP = false;
var USER_RIGHTS_MODERATE = false;

// site map container array
// this array structure:
// SITE_MAP[documentName] as array, containing the array of document name indexed arrays inside
// the path to the document will be: /+documentName1(not 0 - it is root)+/+...+documentNameN+/+LANGUAGE+/
var SITE_MAP = new Array();

// documents descriptions container array
// this array structure:
// DOCUMENTS_DESCRIPTIONS[language][descriptionI]["url"]
// DOCUMENTS_DESCRIPTIONS[language][descriptionI]["path"]
// DOCUMENTS_DESCRIPTIONS[language][descriptionI]["title"]
// DOCUMENTS_DESCRIPTIONS[language][descriptionI]["summary"]
var DOCUMENTS_DESCRIPTIONS = new Array();

// this event occures when the site structure data is refreshed
// all elements, require this data, must add their refresh functions
// calls to this event
var onSiteStructureDataRefreshed = "javascript: ";

// reloads the site map and documents descriptions
function refreshSiteStructureData()
{
  buildDocumentsDescriptions( LANGUAGE );
}

// loads the site map
function buildSiteMap()
{
  sendUrlResponseXmlToFunction
  ( 
    getSiteMapRequestUrl,
    function( response )
    {
      if ( !response || typeof( response ) != "object" )
      {
        buildSiteMap();
        return;
      };
      
      buildSiteMapFromXml( response );
      try
      {
        eval( onSiteStructureDataRefreshed );
      } catch (e) { };
    }
  );
}

// loads the documents descriptions for the language, required
function buildDocumentsDescriptions( language )
{
  // optimization: there's no need to do anything if the
  // documents descriptions are already loaded for the language, spedified
  if ( !DOCUMENTS_DESCRIPTIONS[language] )
  {
    sendUrlResponseXmlToFunction
    ( 
      getDocumentsDescriptionsRequestUrl + language,
      function( response )
      {
        if ( !response || typeof( response ) != "object" )
        {
          buildDocumentsDescriptions( language );
          return;
        };
        
        buildDocumentsDescriptionsFromXml( response, language );
        buildSiteMap();
      }
    );
  };
}

// builds the site map from the XML document, got as the response
// from the sendUrlResponseXmlToFunction()
function buildSiteMapFromXml( response )
{
  SITE_MAP = new Array();

  var siteMap = response.getElementsByTagName("siteMap");

  if ( !( siteMap && siteMap.length == 1 ) )
    return false;

  buildSiteMapLevel( new Array(), getNodesWithName( siteMap[0], "document" )[0] );
    
}

// builds the site map level, given (as an array, indexed with level as
// integer and containing the site map level document name, the
// document is the XML node with the current document
// this function is designed to be called inside the buildSiteMapFromXml() 
// function and inside itself to perform tree structure treatment
function buildSiteMapLevel( siteMapLevel, document )
{
  var documents;
  try
  {
    documents = getNodesWithName( getNodesWithName( document, "documents" )[0], "document" );
  } catch (e) 
  { 
    return false;
  };

  // there are child documents
  for ( var i = 0; i < documents.length; i++ )
  {
    try
    {
      var documentName = documents[i].getAttribute("name");
      
      // configuring the current site map level
      var currentSiteMapLevel = new Array();
      for ( var levelI = 0; levelI < siteMapLevel.length; levelI++ )
        currentSiteMapLevel[levelI] = siteMapLevel[levelI];
      currentSiteMapLevel[currentSiteMapLevel.length] = documentName;

      var expression = "SITE_MAP";
      for ( var levelI = 0; levelI < currentSiteMapLevel.length; levelI++ )
        expression += "['" + currentSiteMapLevel[levelI] + "']";
      expression += " = new Array()";
      
      // storing the current document in the site map
      eval( expression );
      
      // moving deeper
      buildSiteMapLevel( currentSiteMapLevel, documents[i] );
      
    } catch (e) { };
  };
}

// builds the documents descriptions from the XML document, got as the response
// from the sendUrlResponseXmlToFunction()
// the language is the language, the documents with these descriptions will be
// shown for
function buildDocumentsDescriptionsFromXml( response, language )
{
  DOCUMENTS_DESCRIPTIONS[language] = new Array();

  var descriptions = response.getElementsByTagName("description");

  if ( descriptions && descriptions.length > 0 )
  {
    for ( var descriptionI = 0; descriptionI < descriptions.length; descriptionI++ )
    {
      var path = "";
      var url = "";
      var title = "";
      var summary = "";
      
      try
      {
        path = descriptions[descriptionI].getAttribute( "path" );
        url = URL_PREFIX + path + LANGUAGE;
      }
      catch (e) {};
      try
      {
        title = descriptions[descriptionI].getElementsByTagName("title")[0].firstChild.nodeValue;
      }
      catch (e) {};
      try
      {
        var summaries = descriptions[descriptionI].getElementsByTagName("summary")[0].childNodes;
        for ( var summaryI = 0; summaryI < summaries.length; summaryI++ )
          summary += summaries[summaryI].nodeValue;
      }
      catch (e) {};
      
      DOCUMENTS_DESCRIPTIONS[language][descriptionI] = new Array();
      DOCUMENTS_DESCRIPTIONS[language][descriptionI]["path"] = path;
      DOCUMENTS_DESCRIPTIONS[language][descriptionI]["url"] = url;
      DOCUMENTS_DESCRIPTIONS[language][descriptionI]["title"] = title;
      DOCUMENTS_DESCRIPTIONS[language][descriptionI]["summary"] = summary;
    };
  };
}

// the maximal length of the announce text
var maxAnnounceTextLength = 150;
       
// the maximal width of the images in the document, after which
// the will be scaled to this width
var MAX_IMAGE_WIDTH = 600;
// the maximal length of the word, which woun't be short-cutted
var MAX_WORD_LENGTH = 40;
// the maximal number of search results, displayed per page
var MAX_SEARCH_RESULTS = 7;

// initializing the data structure
onAfterLoad += "refreshSiteStructureData(); ";

