var anchors;
var fadetime  = 250;
var intervals = Array();
var http = new Array();
var urls = new Array();

/**
 * init              - searches for all anchors who have : class="ajax" & rel="someid"
 *                     and ajaxifies them.
 * @access public
 * @return void
 */
function initAjax( sessionId ){
  var baseurl = "http://"+document.location.hostname+document.location.pathname;
  // quick hack to prevent malicious requests done not from root
  if(document.location.pathname.length != 1)
    return;
  anchors = document.getElementsByTagName("a");
  for( i = 0; i < anchors.length; i++ ){
    if( anchors[i].className == "ajax" ){
      if( anchors[i].rel == "" ){
        alert("ajax.js: anchor with href='"+anchors[i].href+"' misses REL-tag");
        continue;
      }
      anchors[i].onclick = function(){ 
        var url =  new String(this.href).replace( baseurl, "" );
        url    += url.match("\\?") ? "&SESSION_ID="+sessionId : "\\?SESSION_ID="+sessionId;
        intervals.push( setTimeout( "doRequest( 'ajax/"+url+"', '', 'GET', '"+this.rel+"' )", fadetime ) );
        return false;
      };
    }
  }
}

/*
 * createRequestObject - get DOM XMLHttpRequest
 */
function createRequestObject()
{
  var ro;
  var browser = navigator.appName;
  if(browser == "Microsoft Internet Explorer"){
          ro = new ActiveXObject("Microsoft.XMLHTTP");
  }else{
          ro = new XMLHttpRequest();
  }
  return ro;
}

/*
 * doRequest
 * 
 * arguments  : 
 *                url     => string
 *                args    => post or get args
 *                method  => "post" or "get"
 *                elname  => id name of element to fill up with response
 */
function doRequest(url, args, method, elname )
{
  // clear previous intervals, and load anim
  for( i = 0; i < intervals.length; i++ )
    clearInterval( intervals[i] );
  loading(true);
  changeOpacity( 100, elname );
  shiftOpacity( elname, fadetime );
  urls[ elname ] = url;
  http[ elname ] = createRequestObject();
  http[ elname ].open( method, url, true);
  http[ elname ].setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  http[ elname ].onreadystatechange = function(){ 
    if( http[ elname ].readyState == 4 )
      intervals.push( setTimeout( "requestComplete( '"+elname+"','"+url+"' )", 500 ) );
  }
  http[ elname ].send(args);
}

        
/**
 * requestComplete 
 * 
 * @param elname DOM id of element to fill up
 * @param url  url (is needed to search for result)  
 */
function requestComplete( elname, url ){
  // skip old requests
  if( url != urls[ elname ] ) return;

  var el = document.getElementById( elname );
  if( !el ) return alert("AJAX: could not find element with id '"+elname+"'");
  el.innerHTML = http[ elname ].responseText;
  shiftOpacity( elname, fadetime );
  intervals.push( setTimeout( "loading()", fadetime ) );
  // remove the calls below which need re-init
  tooltip.init( true );
  if( document.getElementById("quote") )
    document.getElementById("quote").innerHTML = "\""+quote+"\"";
}

function loading( state ){
  var el = document.getElementById("ajax");
  if( el )
    el.src = "lib/core/ajax/gfx/loading-"+ (state ? "on" : "off") +".gif";
}

