function randUsername(num){
  new Ajax.Request('/brawler/random/text',{
    onSuccess: function(transport){
      var response = transport.responseText || "no response text";
      $('brawler_'+num).setValue(response);
    },
    onFailure: function(){ alert('Something went wrong...') }
  });
  
}

function addU(username){
  if($('brawler_0').value == '')
  {
    $('brawler_0').setValue(username);
  }
  else
  {
    $('brawler_1').setValue(username);
    Effect.ScrollTo('main_brawl');
  }
}

var response = '';

function preBrawl()
{
  $('brawl_input_button').disable();
  if($('main_brawl_right_results').visible()){$('main_brawl_right_results').fade();}
  if($('main_brawl_right_failure').visible()){$('main_brawl_right_failure').fade();}
  if($('main_brawl_right').visible()){$('main_brawl_right').fade();}
  $('main_brawl').morph('background-color:#000;');

}

function brawlAjax(){
  preBrawl();
  new Ajax.Request('/brawling/a',
    { 
      parameters: $('brawl').serialize(true),
      onSuccess: function(transport){
        $raw = transport.responseText || "no response text";
        response = $raw.evalJSON();
        if(response.result != 'success')
        {
          setTimeout("brawlFailed(response);", 1500);
        }
        else
        {
          callExternalInterface();
          setTimeout("brawlResults(response);", 6500);
        }
      },
      onFailure: function(){ alert('Something went wrong...') }
    }
  );
}

function brawlResults(response)
{
  $winner = response.brawl.Winner;
  $losers = response.brawl.Losers;
  $('results_winner').innerHTML = $winner.username + ' won!';
  $('results_link').innerHTML = "<a href='"+response.brawl.uri+"'>Check out the history of this brawl.</a>";
  $('brawler_cards').update();
  tempEle = new Element('div',{'id':'winner_card'});
  tempEle.insert(new Element('img',{ 'src': $winner.profile_image_url }).addClassName('large mid'));
  tempEle.insert(new Element('a',{ 'href': '/brawler/'+$winner.username }).update($winner.name));
  tempEle.insert(new Element('span').update('&nbsp;record: '+$winner.wins+'-'+$winner.losses+'&nbsp;'));
  tempEle.insert(new Element('a',{ 'href': 'http://twitter.com/'+$winner.username,'id':'winner_follow_link','target':'_blank' }).update($winner.username + ' on twitter'));
  $('brawler_cards').insert(tempEle);
  
   //Storage for XML data document
   var strXML = "<chart formatNumberScale='0' yAxisName='followers'>";
   //Here, we build the XML data document from data array using string concatenation
   //.... Build ....
   
   strXML = strXML + "<set label='"+$winner.username+"' value='"+$winner.followers_count+"' color='5272A5' />";
   $losers.each(function(loser, index)
   {
     strXML = strXML + "<set label='"+loser.username+"' value='"+loser.followers_count+"' color='8E0001' />";
     //$('loser_image_'+index).setAttribute('src', loser.profile_image_url);
     tempEle = new Element('div');
     tempEle.insert(new Element('img',{ 'src': loser.profile_image_url }).addClassName('medium mid'));
     tempEle.insert(new Element('a',{ 'href': '/brawler/'+loser.username }).update(loser.name));
     tempEle.insert(new Element('span').update('&nbsp;record: '+loser.wins+'-'+loser.losses));
     $('brawler_cards').insert(tempEle);
   });
   
   var $tweet_this     = '@'+$winner.username+' just beat @'+$losers[0].username+' in a #tweetbrawl at '+response.brawl.url;
   var $tweet_this_url = '@'+$winner.username+' just beat @'+$losers[0].username+' in a %23tweetbrawl at '+response.brawl.url;
   $('brawler_cards').insert('<div><a href="http://twitter.com/home?status='+$tweet_this_url+'"><img src="/images/tweet_this_button.gif" alt="tweet this!" class="mid" id="tweet_this_button" /></a><div id="tweet_this_box">'+$tweet_this+'</div></div>')
   
   //Closing Chart Element
   strXML = strXML + "</chart>";

   //Get reference to chart object using Dom ID "ChId1"
   //var chartObj = getChartFromId("ChId1");
   //Update its XML
   //chartObj.setDataXML(strXML);
   var chart1 = new FusionCharts("/flash/charts/Column3D.swf", "ChId1", "400", "200", "0", "1");
   //Start Chart with empty data as we\'ll later update using JavaScript
   chart1.setDataXML(strXML);
   chart1.render("bar_chart");
   $('main_brawl_right_results').appear();
   setTimeout("postBrawl();", 5000);
}

function brawlFailed(response)
{
  $('main_brawl_right_failure_message').innerHTML = response.message;
  $('main_brawl_right_failure').appear();
  postBrawl();
}

function postBrawl()
{
  $('brawl_input_button').enable();
  $('main_brawl').morph('background-color:#fff;');
}

function FC_Rendered(DOMId){
   //This method is called whenever a FusionCharts chart is loaded.
   //Check if it\'s the required chart using ID
   if (DOMId=="ChId1"){
       //Invoke updateChart() method to update chart with new data
       //updateChart();
   }
}

function brawlTwo(b0,b1)
{
  Effect.ScrollTo('main_brawl');
  $('brawler_0').setValue(b0);
  $('brawler_1').setValue(b1);
  brawlAjax();
}

function callExternalInterface()
{
  thisMovie("brawl_flash").playBrawl();
}

function thisMovie(movieName)
{
  if(navigator.appName.indexOf("Microsoft") != -1)
  {
    return window[movieName];
  }
  else if (document.getElementById) {
	    return document.getElementById(movieName);
  }
  else
  {
    return document[movieName];
  }
}
