// $Id: ajaxtabs.js,v 1.0 2008/02/08 17:02:21 $

/**
  * @file
  * This file is used to change drupal tabs to ajax
  * 
  *
  * @author
  * hamedf
  * @version
  * 1.0
  */
  
Drupal.behaviors.ajaxTabs = function (context) {
  $("ul.tabs.primary", context).find("a:not(.primarytabs-ajax, .ajax_tabs_ignore)").each(function() {
    $(this).addClass("primarytabs-ajax");
    $(this).click(function(e){
      e.preventDefault();
      ajaxtabs_clicked(e, this, 1);
    });
  });
  
  $("ul.tabs.secondary", context).find("a:not(.secondarytabs-ajax, .ajax_tabs_ignore)").each(function() {
    $(this).addClass("secondarytabs-ajax");
    $(this).click(function(e){
      e.preventDefault();
      ajaxtabs_clicked(e, this, 0);
    });
  });    
}
  
function ajaxtabs_clicked(e, link, getTabs2) {
  var targetUrl = String(e.target);
  var current_height = $("#content-panel").css("height");
  $("#content-panel").empty().append('<div style="height: '+current_height+';"><div>Please wait...</div>'+
                    '<div style="background-image: url(\'/misc/progress.gif\')">&nbsp</div>'+
                    '</div>');
  var tabName = "";
  if (getTabs2 == 1) {
    tabName = "ul.tabs.primary";
  }
  else {
    tabName = "ul.tabs.secondary";
  }
  $(tabName)
    .find("li.active > a") 
    .removeClass("active")
    .parent()
    .removeClass("active");
  $(link).parent().addClass("active");
  $(link).addClass("active");
  if (typeof (self.alterTabs) == 'function') {
    alterTabs();
  }
  var i = targetUrl.indexOf('?q=', 10);
  if (i > -1) {
    i += 3;
  }
  if(i == -1) {
    i = targetUrl.indexOf('/', 10);
    i++;
  }
  if (i > -1) {
    targetUrl = String(targetUrl.substr(0, i) + 'ajaxtabs/handle/' + targetUrl.substr(i));
  }
  $.get(targetUrl, {'getTabs2': getTabs2}, function(returned_value){
    var response = Drupal.parseJson(returned_value);  
    //add content    
    $("#content-panel").empty().append(response.data);
    //update help section      
    if (response.help != null && response.help != "") {        
      $("#help-panel").empty().append(response.help).show();
    }
    else {
      $("#help-panel").hide();
    }
          
    //if we have second tab set, update it
    if (getTabs2 == 1) { 
      $("#tabs2-panel").empty();
      if (response.tabs2 != null && response.tabs2 != "") {
        $("#tabs2-panel").append(response.tabs2).show();
      }
      else {
        $("#tabs2-panel").hide();
      }
    }              
    imediaAjaxProcessReturn(response, $("#content-panel"));
  });
}


