var rid = "";
var ankh = "";

var toevaltemp;

//uses comments.php to pull comments for the given rand id and then replaces
//the ankh with the comments html
//
//assumes getData is included
function comments( rrid, aankh ) {
  //set the rid and ankh
  rid = rrid;
  ankh = aankh
  //clear thedata
  thedata = "";

  //create the loading text
  ankh.appendChild( document.createTextNode( " (loading...)" ) );

  //getData( "http://www.ximwix.net/design16/scripts/comments.php?rid=" + rid ); //put comments.php in the same dir as this script
  var status = getData( "comments.php?rid=" + rid + "&u=" + new Date().getTime() );

  waitForData( "makeComments();" );
}

function waitForData( toeval ) {
  toevaltemp = toeval;
  //alert( "waitfordata()" );
  if ( thedata == "" || thedata.length == 0 ) {
    setTimeout( "waitForData( toevaltemp );", 500 );
  } else {
    //alert( "from waitfordata: " + thedata );
    eval( toeval );
  }
}


function makeComments() {
  //should have returned something to be eval'd
  eval( thedata );

  //start creating tags
  var thecomments = document.createElement( "div" );
  thecomments.className = "commentbg";
  thecomments.setAttribute( "class", "commentbg" );
  var a = document.createElement( "a" );
  a.appendChild( document.createTextNode( "Comments (click to hide)" ) );
  a.setAttribute( "class", "commentlink" );
  a.className = "commentlink";
  //a.onclick = function(){ hideComments( rid, this ); };
  eval( "a.onclick = function(){ hideComments( " + rid + ", this ); };" );
  timestamp = new Date().getTime();
  a.setAttribute( "name", timestamp );
  a.name = timestamp;
  thecomments.appendChild( a );
  thecomments.appendChild( document.createElement( "br" ) );

  //should have created an array called commentdata
  for( i = 0; i < commentdata.length; i += 2 ) {
    var tinyspan = document.createElement( "span" );
    tinyspan.setAttribute( "class", "tiny" );
    tinyspan.className = "tiny";
    tinyspan.appendChild( document.createTextNode( commentdata[i] ) );
    thecomments.appendChild( tinyspan );
    thecomments.appendChild( document.createElement( "br" ) );
    for ( j = 0; j < commentdata[i+1].split( "\n" ).length; j++ ) {
      thecomments.appendChild( document.createTextNode( commentdata[i+1].split( "\n" )[j] ) );
      thecomments.appendChild( document.createElement( "br" ) );
    }
    thecomments.appendChild( document.createElement( "br" ) );
  }

  thecomments.appendChild( makePost( rid ) );

  //replace the ankh's parent with the comments
  ankh.parentNode.parentNode.replaceChild( thecomments, ankh.parentNode );

  //scroll the window to the comments
  //location = "#" + timestamp;
  //location = "#ctop" + rid;
  //thecomments.getElementsByTagName( "a" )[0].focus();

  //clear rid and ankh (may be superfluous)
  rid = "";
  ankh = "";
}

//replaces a comments block with a link to show comments
function hideComments( rrid, aankh ) {
  //create the text to replace the ankh
  var nocomments = document.createElement( "div" );
  nocomments.className = "commentbg";
  nocomments.setAttribute( "class", "commentbg" );
  var a = document.createElement( "a" );
  a.setAttribute( "class", "commentlink" );
  a.className = "commentlink";
  eval( "a.onclick = function(){ comments( " + rrid + ", this ); };" );
  a.appendChild( document.createTextNode( "Click for comments" ) );
  nocomments.appendChild( a );

  //replace the ankh with a link to show the comments
  aankh.parentNode.parentNode.replaceChild( nocomments, aankh.parentNode );
}
