// the maximal width of the images in the document, after which
// the will be scaled to this width
var maxAnnouncesImageWidth = -1;
// the maximal length of the word, which woun't be short-cutted
var maxAnnouncesWordLength = -1;


function initAnnouncesBox()
{

  maxAnnouncesImageWidth = MAX_IMAGE_WIDTH / 3;
  maxAnnouncesWordLength = MAX_WORD_LENGTH;

  // dependency is loaded
  dependencyLoaded('announces');
}


// sends the request to login
function readRssIntoElementWithId( elementId, rssSubUrl, useNumberOfItems )
{
  // checking to have something to do
  if ( !elementId || !rssSubUrl )
    return;
     
  // adding the resource to the document
  sendUrlResponseXmlToFunction
  ( 
    rssSubUrl, 
    function( response )
    {
      // an empty response comes from time to time. Retrying then
      if ( !response )
      {
        //readRssIntoElementWithId( elementId, rssSubUrl, useNumberOfItems );
        return;
      } 
      else if ( typeof(response) == 'string' && 
        (
          response == RESPONSE_FORBIDDEN ||
          response == RESPONSE_RSS_NOT_FOUND
        )
      )
      {
        return;
      };
      
      var output = "";
      // the header
      var title = "";
      if ( response.getElementsByTagName("title").length > 0 )
        title = response.getElementsByTagName("title")[0].firstChild.nodeValue;
      var link = "";
      if ( response.getElementsByTagName("link").length > 0 )
        link = response.getElementsByTagName("link")[0].firstChild.nodeValue;
      var description = "";
      if ( response.getElementsByTagName("description").length > 0 )
      {
        var textParts = response.getElementsByTagName("description")[0].childNodes;
        if ( textParts.length > 0 )
          for ( textPartI = 0; textPartI < textParts.length; textPartI++  )
            if ( textParts[textPartI].nodeValue )
              description += textParts[textPartI].nodeValue;
        // nicing-up the content
        description = prepareText( description );
      };
        
      output += "<h1><a href='"+link+"'>"+title+"</a></h1>\n";
      
      // the content
      var messages = response.getElementsByTagName("item");
      if ( messages.length > 0 )
      {
        for ( var messageI = 0; messageI < Math.min( messages.length, useNumberOfItems ); messageI++ )
        {
          var title = "";
          //if ( messages[messageI].getElementsByTagName("title").length > 0 )
          //  title = messages[messageI].getElementsByTagName("title")[0].firstChild.nodeValue;
          var link = "";
          if ( messages[messageI].getElementsByTagName("link").length > 0 )
            link = messages[messageI].getElementsByTagName("link")[0].firstChild.nodeValue;
          var description = "";
          if ( messages[messageI].getElementsByTagName("description").length > 0 )
          {
            var textParts = messages[messageI].getElementsByTagName("description")[0].childNodes;
            if ( textParts.length > 0 )
              for ( textPartI = 0; textPartI < textParts.length; textPartI++  )
                if ( textParts[textPartI].nodeValue )
                  description += textParts[textPartI].nodeValue;
            // nicing-up the content
            description = prepareText( description );
          };
          // building the output for the current item
          output += "<div class='announce'><h1>"+title+"</h1>"+description/*.slice(0,maxAnnounceTextLength)*/+"</div>\n";
        };
      };

      var element = obtainElementById( elementId );
      element.innerHTML = output;
    },
    0,
    true
  );
}

// INIT
addDependentCallback(function(){
  initAnnouncesBox();
},'constants, settings', false);
