//
// FORKLORE JAVASCRIPT LIBRARY
//


var AJAX_PATH = '/ajax/';
var USER_RENEW = 30000;
var FORK_RENEW_TIME = 30000;
var GET_EDITOR_TIME = 30000;




//
// CORE SCRIPT MANAGEMENT
//

var included_files = new Array();

function include_dom ( script_filename ) {
  var html_doc = document.getElementsByTagName( 'head' ).item( 0 );
  var js = document.createElement( 'script' );
  js.setAttribute( 'language', 'javascript' );
  js.setAttribute( 'type', 'text/javascript' );
  js.setAttribute( 'src', script_filename );
  html_doc.appendChild( js );
  return false;
}

function include_once ( script_filename ) {
  if ( !in_array( script_filename, included_files ) ) {
    included_files[included_files.length] = script_filename;
    include_dom( script_filename );
  }
}

function in_array( needle, haystack ) {
  for ( var i = 0; i < haystack.length; i++ ) {
    if ( haystack[i] == needle ) {
      return true;
    }
  }
  return false;
}




//
// GENERAL FUNCTIONS
//

function toggle_help ( element ) {
  height = Element.getStyle( element, 'height' );
  if ( height == null ) {
    Element.setStyle( element, { height: '1.5em' } );
  } else {
    Element.setStyle( element, { height: 'auto' } );
  }
}

function user ( hash ) {
  // updates status for logged in users. hash generated on login.
  
  new Ajax.Request( '/ajax/', { method:'post', postBody:'user=' + hash } );
  setTimeout( "user( '"+hash+"' );", USER_RENEW );
}




//
// FORK EDITING
//

var renewNotify = function( t ) {
  //console.debug( t.responseText );
}

function forkLock ( hash ) {
  forkRenew( hash );
}

function forkRenew ( hash ) {
  new Ajax.Request( '/ajax/', { method:'post', postBody:'forkrenew=' + hash, onSuccess:renewNotify } );
  setTimeout( "forkRenew( '"+hash+"' );", FORK_RENEW_TIME );
}

function getEditors ( projectid ) {
  new Ajax.Updater( 'editors', '/ajax/', { method:'post', postBody:'geteditors=' + projectid } );
  setTimeout( "getEditors( '"+projectid+"' );", GET_EDITOR_TIME );
}



//
// DEFAULT INCLUDES
//

include_once( '/js/prototype.js' );
