var totalFights  = 0;
var currentEntry = 0; 

var opponent_a   = "";
var votes_a      = 0;
var percent_a    = 0;
var opponent_b   = "";
var votes_b      = 0;
var percent_b    = 0;


$(document).ready(function(){
  $("#areaA").click(voteA);
  $("#areaB").click(voteB);
  
  $("#areaA").mouseover(function(){
    $("#areaA").css("font-size", "150%");
    $("#areaB").css("font-size", "80%");
  });
  
  $("#areaA").mouseout(function(){
    $("#areaA").css("font-size", "100%");
    $("#areaB").css("font-size", "100%");
  });
  
  $("#areaB").mouseover(function(){
    $("#areaB").css("font-size", "150%");
    $("#areaA").css("font-size", "80%");
  });
  
  $("#areaB").mouseout(function(){
    $("#areaB").css("font-size", "100%");
    $("#areaA").css("font-size", "100%");
  });  
  
  $(".passLink").click(newFight);
  $(".anotherLink").click(newFight);
  
  getTotal();
});


function getFight(entry) {
  $(".pageContent").hide();
  
  myAjax("action=get&entry=" + entry, function(data){
    var fightData = $.xmlToJSON($.textToXML(data));
    
    try {
      opponent_a = fightData.opponent[0]["name"];
      votes_a    = parse(fightData.opponent[0]["votes"]);
      
      opponent_b = fightData.opponent[1]["name"];
      votes_b    = parse(fightData.opponent[1]["votes"]);
    } catch(e) {
      newFight();
    }
    
    percent_a = (votes_a / (votes_a + votes_b)) * 100;
    percent_b = (votes_b / (votes_a + votes_b)) * 100;
    
    if (opponent_a == "" || opponent_b == "") {
      newFight();
    } else {          
      $(".fightArea").show();
      $(".pageContent").show();
      
      $("#areaA").fadeTo(0, 1);
      $("#areaA").css("font-size", "100%");
      $("#areaA").show();
      $("#areaA .link").html(opponent_a);
      $("#areaA .percentage").html(votes_a);
      $("#areaA .percentage").css("width", "0");
      $("#areaA .percentage").css("background-color", percentColour(percent_a));
      $("#areaA .percentage").animate({ width: percent_a + "%" }, 1000);
      
      $("#areaB").fadeTo(0, 1);
      $("#areaB").css("font-size", "100%");      
      $("#areaB").show();
      $("#areaB .link").html(opponent_b);
      $("#areaB .percentage").html(votes_b);
      $("#areaB .percentage").css("width", "0");
      $("#areaB .percentage").css("background-color", percentColour(percent_b));
      $("#areaB .percentage").animate({ width: percent_a + "%" }, 1000);
    }
  });
}


function setFight(entry, opponent) {
  myAjax("action=set&entry=" + entry + "&opponent=" + opponent, function(data){
    newFight();
  });
}


function getTotal() {
  myAjax("action=total", function(data){
    totalFights = parse(data);          
    newFight();
  });
}


function newFight() {
  $(".fightArea").hide();
  
  var newEntry = 0;
  
  do {
    newEntry = rand(totalFights);
  } while(newEntry == currentEntry)
  
  currentEntry = newEntry;
  
  getFight(currentEntry);
  
  return false;
}


function voteA() {
  $("#areaA .percentage").html(votes_a + 1);
  $("#areaB").fadeTo("fast", 0, function(){
    setFight(currentEntry, "A");
  });
      
  return false;
}


function voteB() {
  $("#areaB .percentage").html(votes_b + 1);
  $("#areaA").fadeTo("fast", 0, function(){
    setFight(currentEntry, "A");
  });
  
  return false;
}      


function myAjax(data, func) {
  $.ajax({
    type:    "POST",
    url:     "www.asp",
    data:    "passkey=wh00shKA&" + data,
    success: function(data){
      func(data);
    }
  });            
}


function parse(str) {
  var n = 0;

  str = trim("" + str);

  while (str.charAt(0) == "0") {
    str = str.substring(1);
  }

  if (str == "") n = 0;	
  else n = parseInt(str);

  if (isNaN(n)) n = 0;

  return n;
}


function trim(str) {
  if (str == null) return "";
  else return str.replace(/^\s+|\s+$/g, '');
}


function rand(max) {
  return Math.floor(Math.random() * max);
}


function randRange(min, max) {
  return parseInt(Math.floor((max - min) * Math.random()) + min);
}

function percentColour(p) {
  var red = p<50 ? 255 : Math.round(256 - (p-50)*5.12);
  var green = p>50 ? 255 : Math.round((p)*5.12);
  
  return "rgb(" + red + "," + green + ",0)";
}