//Rewritten and commented originally by Ven.

$j(function()
{
  //This is required because it assumes ajax and doesn't refresh the page on address changes.
  //This is also run the first time the page loads.
  $j.address.change(openURL); //Hook address changes.

  //Create the tabbing ui.
  $j('#tabs').tabs({
    selected: null, //Gives me the ability to change the tab later.
    ajaxOptions:
    {
      success: function() //Stop showing the animated star thing.
      {
        $j('#tabs .tab_loading_msg').remove();
        $j('.main_tab_container').removeClass('vhidden');
      }
    },
    load: function(event, ui)
    {
      //Handle special addresses with a 'rel' tag.
      $j('a.ajax_link', ui.panel).live('click', function()
      {
        $j.address.value($j(this).attr("rel"));
        return false;
      });
      $j("#setting_tab").click(function()
      {
        window.location.replace($j(this).attr("rel"));
        return false;
      });
    }
  });

  //If a tab is selected, make sure to update the side bar.
  $j('#tabs').bind('tabsselect', function(event, ui)
  {
    //Show the animated star thing.
    $j('.main_tab_container').addClass('vhidden');
    $j('#tabs .tab_loading_msg').remove();
    $j('#tabs ul').after('<div class="tab_loading_msg"><img src="/img/ajax-loader.gif"></div>');

    if(ui.index == 0)
      switch_to_profile_tab();
    else
      switch_to_normal_tab();
  });
    
  function switch_to_profile_tab()
  {
    $j("#main_wrap #main").css('padding', '10px 10px');
    $j("#left_column").show();
    $j("#right_column").css('width', '680px');
    $j("#right_column #profile_image_wrap").hide();
  }
  
  function switch_to_normal_tab()
  {
    $j("#main_wrap #main").css('padding', '10px 10px');
    $j("#left_column").hide();
    $j("#right_column").css('width', '100%');
    $j("#right_column #profile_image_wrap").show();
  }
  
  function openURL()
  {
    //Show the animated star thing.
    $j('.main_tab_container').addClass('vhidden');
    $j('#tabs .tab_loading_msg').remove();
    $j('#tabs ul').after('<div class="tab_loading_msg"><img src="/img/ajax-loader.gif"></div>');

    //Figure out what is the tab of current url and open that url.
    var path_names = $j.address.pathNames();
    var path_name = path_names[0];

    var tab_index = 0;

    if(path_name == 'sponsorship')
      tab_index = 1;
    else if(path_name == 'fundraising')
      tab_index = 2;
    else if(path_name == 'analytics')
      tab_index = 2;
    else if(path_name == 'inbox')
      if (!$j('#analytics_tab').length && !$j('#fundraising_tab').length)
        tab_index = 3;
      else
        tab_index = 4;
    else if(path_name == 'media')
      if (!$j('#analytics_tab').length && !$j('#fundraising_tab').length)
        tab_index = 2;
      else
        tab_index = 3;
    
    //Set the side bar depending on which tab is currently open
    if(tab_index == 0)
      switch_to_profile_tab();
    else
      switch_to_normal_tab();

    //Open the current url.
    if(path_name == undefined)
      dir = '/info';
    else
      dir = $j.address.value();

    current_url = $j.address.baseURL() + dir;
    $j('#tabs').tabs('url', tab_index, current_url);  //Set the new url.
    $j('#tabs').tabs('select', tab_index);  //Set the tab.
    $j('#tabs').tabs('load', tab_index);  //Do it.
  }
  
});


$j.ajaxSetup({
  beforeSend: function(xhr) {
    $j(window).bind('beforeunload', function() {
      xhr.abort();
    });
  }
});
