/*
 * RAAA JavaScript Library
 */

$.fn.clearForm = function(oArg) {
  /* defaults */
  args = { select:-1 }

  /* override the defaults if necessary */
  for (var argName in oArg) {
    args[argName] = oArg[argName];
  }

  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return $(':input',this).clearForm(args);
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = '';
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = args.select;
  });
};



function include( filename ) {
//    var js = document.createElementNS ? document.createElementNS('http://www.w3.org/1999/xhtml', 'script') : document.createElement('script');
    var js = document.createElement('script');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', filename);
//    js.setAttribute('defer', 'defer');
//    document.getElementsByTagNameNS ? document.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'head')[0].appendChild(js) : document.getElementsByTagName('head')[0].appendChild(js);
    document.getElementsByTagName('head')[0].appendChild(js);
 
    // save include state for reference by include_once
    var cur_file = {};
    cur_file[window.location.href] = 1;
 
    if (!window.include_js) window.include_js = {};
    if (!window.include_js.includes) window.include_js.includes = cur_file;
    if (!window.include_js.includes[filename]) {
        window.include_js.includes[filename] = 1;
    } else {
        window.include_js.includes[filename]++;
    }
 
    return window.include_js.includes[filename];
}



function include_once( filename ) {
    var cur_file = {};
    cur_file[window.location.href] = 1;
 
    if (!window.include_js) window.include_js = {};
    if (!window.include_js.includes) window.include_js.includes = cur_file;
    if (!window.include_js.includes[filename]) {
        if(include(filename)){
            return true;
        }
    } else{
        return true;
    }
    return false;
}



function hijak_link() {
  scroll(0,0);
  var hash = this.href;
  hash = hash.replace(/http:\/\/[^/]*/, '');
  $.history.load(hash);
  return false;
}



function getajax(hash) {
  scroll(0,0);
  if (typeof(defaultTab) == 'undefined') { defaultTab = 0; }
  if (typeof(animalTab) == 'undefined') { animalTab = 2; }
  if (typeof(memberTab) == 'undefined') { memberTab = 3; }
  type = hash.replace(/^\/([^\/]*)\/.*/, "$1");
  if(type == "animal") {
    $('#animal-details').html('<p class="loading">Loading&#8230;</p>');
    $('#tabs').tabs('enable', animalTab);
    $('#tabs').tabs('select', animalTab);
    $.Tache.Get({
      url: hash,
      success: function(data, status) {
        $('#animal-details').html($("<div/>").append(data).find('#animal-container'));
        $('#animal-details a.reglink').click(hijak_link);
        $('#animal-details a.mbrlink').click(hijak_link);
        $('#animal-details table#XRefs tbody tr:odd').addClass('odd');
        $('#animal-details table#Owners tbody tr:odd').addClass('odd');
        $('#animal-details .tooltip').tooltip({delay: 250});
        $('#animal-details .tooltip-short').tooltip({delay: 250, extraClass: "short"});
      }
    });
  } else if(type == "member") {
    $('#member-details').html('<p class="loading">Loading&#8230;</p>');
    $('#tabs').tabs('enable', memberTab);
    $('#tabs').tabs('select', memberTab);
    $.Tache.Get({
      url: hash,
      success: function(data, status) {
        $('#member-details').html($("<div/>").append(data).find('#member-container'));
      }
    });
  } else {
    $('#tabs').tabs('select', defaultTab);
//    $('#tabs').tabs('disable', 2);
//    $('#tabs').tabs('disable', 3);
//    $('#animal-details').empty();
//    $('#member-details').empty();
  }
}
