/* Copyright (c) 2006-2007 Ido Kanner under the Modified MIT license
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this 
 * software (the "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge,  publish, distribute,
 * sub-license, and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT 
 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */

var navigator_debug        = false; 
var navigator_address      = location.href.replace(/^(http:\/\/.*?\/).*?$/,"$1");
var navigator_content_name = "navigator";

function navigator_printDebug(message)
{
  if (navigator_debug)
  {
    Log.add ("navigator: " + message);
    Log.displayLog(); 
  } // if (navigator_DEBUG)
} // function navigator_printDebug(message

function toggleNavigator()
{
  var div = document.getElementById('navigator_container');
  var img = document.getElementById('img_nav');
  if (div.style.display == '' || div.style.display == 'none')
  {
    div.style.display = 'block';
    img.className     = 'site_navigation_down';
  }
  else
  {
    div.style.display = 'none';
    img.className     = 'site_navigation';
  }
}

function generateNavigator(xml)
{
  if (xml == null)
    return false;
  
  var Pages = xml.getElementsByTagName('pages').item(0);
  function generateEntry(node)
  {
    var content = '<ul class="navigator">\n';
    for (var i = 0; i<= node.length; i ++)
    {
      var item = node[i];
      if (item != null && item.nodeName == "page" && 
          (item.hasAttribute('address') && item.hasAttribute('title') && item.hasAttribute('caption')))
      {

        content += '<li class="navigator"> - ';
        content += '<a href="' + item.getAttribute("address")+ 
                   '" title="' + item.getAttribute("title") + '">' +
                   item.getAttribute("caption") + "</a>\n";
                 
        if (item.hasChildNodes())
          content += generateEntry(item.childNodes, false);

        content += "</li>\n";

      } // if (item.nodeName == "page")
    } // for (var i = 0; i<= node.childNodes.length; i ++)
    content += "</ul>";
    return content;
  }; //  generateEntry(node)

  var navigator_container       = document.getElementById(navigator_content_name);
  navigator_container.innerHTML = generateEntry(Pages.childNodes, true);

  return true;
}

function getNavigatorXML()
{
  navigator_printDebug("Set navigator_address to: " + navigator_address);
  navigator_printDebug("About to create AJAX to generate the navigator");
  var navigator              = new ikajax(navigator_address);
  navigator.page             = "site_navigator.xml";
  navigator.method           = ajax_async;
  navigator.contentType      = "text/xml";
  navigator.sendContentType  = "text/xml";
  navigator.head             = ajax_HEADER_get;
  navigator_printDebug("Creating callback function: onContentChanged");

  navigator.onContentChanged = function () 
  {
    navigator_printDebug("Inside onContentChanged");
    var navigator_container = document.getElementById(navigator_content_name);
    if (navigator.returnContentText != "")
    {
      navigator_printDebug("About to write the content of the navigator: ");
      if (generateNavigator(navigator.returnContentXML))
        navigator_printDebug("Wrote the content of the navigator.");
      else
      { 
         navigator_printDebug("Unable to generate the navigator content.");
         navigator_container.innerHTML = "Unable to generate the navigator content.";
      } // else
    } // if (navigator.returnContentText != "")
    else
    {
      navigator_printDebug("We do not have the navigator content yet.");
    } // else
  };
  
  navigator_printDebug("Going to start a communication: ");
  navigator.openConnection();
  navigator_printDebug("Done.");
}

var link_delicious = "delicious";
var link_digg      = "digg";
var link_reddit    = "reddit";

function generateLink(type, address, title)
{
  var link = '<a title="Save this page at ';
  if (type == link_delicious)
  {
    link += 'del.icio.us" href="http://';
    link += 'del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=' + encodeURIComponent('address') +
            '&amp;title=' + encodeURIComponent(title) + '">';
    link += '<img src="/img/delicious.small.gif" alt="save this page at del.icio.us" /></a>';
    
  }
  else if (type == link_digg)
  {
    link += 'digg.com" href="http://';
    link += 'digg.com/submit?phase=2&amp;url=' + encodeURIComponent(address) +
            '&amp;title=' + encodeURIComponent(title) + '">';
    link += '<img src="/img/10x10-digg-thumb.png" alt="save this page at digg.com"/></a>';
  }
  else if (type == link_reddit)
  {
    link += 'reddit.com" href="http://';
    link += 'reddit.com/submit?url=' + encodeURIComponent(address) +
            '&amp;title=' + encodeURIComponent(title) + '">';
    link += '<img src="/img/reddit.gif" alt="save this page at reddit.com" /><a/>';
  }

  return link;
}
