/*

   * @author ortatherox - orta.therox@gmail.com
   * @version 1.7
   * @copyright ortatherox, 9 August, 2009
   updated for episode 3: Jan 21 2010
   and extras (FBO / battle_logging / General info) on Jan 31  2010
*/

var s = document.createElement('script');
s.src = 'http://code.jquery.com/jquery-latest.js';

var concentrate_on_extras = false;

s.onload = function() {
  
  battle_log("gaining sentinence");


  
  jQuery.noConflict();
  
  function makeADecision(){
        
    //  Add bot commands
    if(jQuery("#botstuff").length == 0){
     jQuery("#thread_properties table tbody").append("<tr id='botstuff'><td><a href='#' onclick='doExtras();' >Do all XSS and FBO</a></td></tr>");         
      jQuery("#battle_log").css("bottom", "200px");
    }
      

    /* if the battle is over then stop the bot */
    if( jQuery("#pwned_forum").length > 0 ){
      battle_log("awesome. I won. Fugg you man.");
      return;
    }

    /* showing list of threads? */
    if( jQuery("#thread_list").css("display") != "none"  ){
      battle_log("choosing from thread list");    
            
      jQuery("#thread_list td:eq(1) a")[0].onclick();
      setTimeout( makeADecision ,5000);
      return;
      
    } 
    
    /* showing exp */
    if( jQuery("#battle_container #cred").length > 0  ){
      battle_log("blah blag stats skiping");
      Battle.another_thread();
      setTimeout( makeADecision ,5000);
      return;
    } 
    
    // either the FBO stuff, or the site is slow
    if(Battle.doing_something || concentrate_on_extras){
     // battle_log("something else is thinking.");
      setTimeout( makeADecision ,5000);
      return;
    }
    

    // Check Health
    var hp = jQuery("#player_property_bar_ego_values").text().split(/\//m);
    var  health = hp[0].replace(/^\s+|\s+$/g,"");
    var  max_health = hp[1].replace(/^\s+|\s+$/g,"");
    var percent = health / max_health * 100;
    if (percent < 25){
      // Heal
      battle_log("under 25% health (" + percent + "% ) healing.");
      
      Toolbar.select_toolbar(7); 
      setTimeout( makeADecision ,8000);
      return;
    }
    
    
    /* find out availible moves */
    var i = 0;
    for(i=0; i < 8; i++ ){
      if( isAvailible(i) ){
        i++;
        battle_log("attacking item " + i);
        Toolbar.select_toolbar(i); 
        setTimeout( makeADecision ,8000);
        return;
      }
    }
    
    /* Panic! On the first time you load a battle all the selections fail
       so instead just call the leftmost item  */
    Toolbar.select_toolbar(1); 
    battle_log("making first move")
    setTimeout( makeADecision ,5000);
  }
  
  function isAvailible( key ){
    // battle_log(key + " " + jQuery("div#toolbar_item_" + key + " a").attr("turns") );
    if( jQuery("div#toolbar_item_" + key + " a").attr("turns") != 0){
      // battle_log("not availble " + key);
      return false;
    }
      // battle_log("availble " + key);
    return true;
  }

  makeADecision();
};

function doExtras(){
  // startup welcome
  if(concentrate_on_extras == false){
    battle_log("starting up XSS/FBO attacks");  
    concentrate_on_extras = true;
  }
  
  // this avoids annoying alerts
   if(Battle.doing_something){
      battle_log("forumwarz is thinking.");
      setTimeout( doExtras ,5000);
      return;
    }
    
    // grab stats for HBO bits
    var FBO = jQuery(".hiddenTable tr").eq(2).find("td").eq(1).text().split(/\//m);
    var FBO_current = FBO[0].replace(/^\s+|\s+$/g,"");
    var FBO_max = FBO[1].replace(/^\s+|\s+$/g,"");
    // battle_log("FBO current :" + FBO_current + " max " + FBO_max);
    
    if(FBO_current != FBO_max){
      jQuery("img[alt*=FBO]").parent().click();
      setTimeout( doExtras ,5000);
      return;
    }
    
    var XSS = jQuery(".hiddenTable tr").eq(1).find("td").eq(1).text().split(/\//m);
    var XSS_current = XSS[0].replace(/^\s+|\s+$/g,"");
    var XSS_max = XSS[1].replace(/^\s+|\s+$/g,"");
    // battle_log("XSS current :" + XSS_current + " max " + XSS_max);
    if(XSS_current != XSS_max){
      jQuery("img[alt*=XSS]").parent().click()
      setTimeout( doExtras ,5000);
      return;
    }
    
    
    // both are finished
    concentrate_on_extras = false;
  
}

function battle_log(text){
  jQuery("#log_list").append("<li stlye='info'><span>(Bot)</span> " + text + "</li>");
}



document.getElementsByTagName('head')[0].appendChild(s);