Drupal.researchTool = {
  activeTab: null,
  activeTabId: null,
  section: null,
  loadSection: null,
  symbol: null,
  currentRequest: null,
  prevWasHuge: false,
  init: function() {
    var temp = $('table.research-tool.page:not(.processed)').addClass('processed').id();
    if(temp) {
      temp = temp.match(/(\w+)_([^_]+)/);
      Drupal.researchTool.symbol = temp[2];
      Drupal.researchTool.setActive();
      Drupal.researchTool.initTabs();
    }
  },
  processForms: function() {
    $('#research-tool-content form:not(.processed)').each(
    function(i) {
      $(this).submit(
      function() {
        return Drupal.researchTool.post(this.elements[0]);
      }
      ).addClass('processed');
    }
    );
  },
  setActive: function() {
    Drupal.researchTool.activeTab = $(".research-tool-tabs td.active");
    //Drupal.researchTool.activeTab.unbind('click');
    Drupal.researchTool.activeTabId = Drupal.researchTool.activeTab.id();
    Drupal.researchTool.setSection(Drupal.researchTool.activeTabId);
  },
  setSection: function(input) {
    var m = input.match(/(\w+)_(\w+)/);
    Drupal.researchTool.section = m[2];
    var loader = true;
    if(Drupal.researchTool.currentRequest) {
      if(Drupal.researchTool.currentRequest.readyState != 4) {
        Drupal.researchTool.currentRequest.abort();
        loader = false;
      }
    }
    if (loader) Drupal.researchTool.setContent('<div class="messages status loading">تحميل ...  الرجاء الانتظار...  </div>');

    Drupal.researchTool.currentRequest = $.ajax({
      type: "GET",
      url: '?q=json/research-tool/'+Drupal.researchTool.symbol+'/get-section/'+Drupal.researchTool.section,
      success: function(json){if(json)Drupal.researchTool.setContent(json); else Drupal.researchTool.setError();},
      dataType: "json"
    });
  },
  initTabs: function() {
    $('.research-tool-tabs td:not(.active)').each(
    function() {
      $(this).unbind('click');
      $(this).click(function(){Drupal.researchTool.activate(this);});
    }
    );
  },
  activate: function(element) {
    if(Drupal.researchTool.activeTab) Drupal.researchTool.activeTab.removeClass('active');
    $(element).addClass('active');
    Drupal.researchTool.setActive();
    Drupal.researchTool.initTabs();
  },
  setContent: function(content) {
    var delay = 250;
    var anim = true;

    if(content.length > 25000) {
      anim = false;
      Drupal.researchTool.prevWasHuge = true;
    } else if(Drupal.researchTool.prevWasHuge) {
      anim = false;
      Drupal.researchTool.prevWasHuge = false;
    }
    if(!anim) {
      $('#research-tool-content').slideUp();
      $('#research-tool-content').empty();
      $('#research-tool-content').append(content);
      $('#research-tool-content').slideDown();
      Drupal.researchTool.processForms();
    } else {
      $('#research-tool-content').slideUp(delay,function(){
        $('#research-tool-content').empty();
        $('#research-tool-content').append(content);
        $('#research-tool-content').slideDown(delay);
        Drupal.researchTool.processForms();
      });
    }
  },
  setError: function() {
    Drupal.researchTool.setContent('<div class="messages error">Content could not be loaded. We apologize for the inconvenience.</div>');
  },
  get: function(path) {
    var loader = true;
    if(Drupal.researchTool.currentRequest) {
      if(Drupal.researchTool.currentRequest.readyState != 4) {
        Drupal.researchTool.currentRequest.abort();
        loader = false;
      }
    }
    if (loader) Drupal.researchTool.setContent('<div class="messages status loading">تحميل ...  الرجاء الانتظار...  </div>');
    Drupal.researchTool.currentRequest = $.ajax({
      type: "GET",
      url: '?q='+path,
      success: function(json){if(json)Drupal.researchTool.setContent(json); else Drupal.researchTool.setError();},
      dataType: "json"
    });
    return false;
  },
  post: function(caller) {
    var form_values = {};
    switch(Drupal.researchTool.section) {
      case 'historical':
      var max = document.getElementById('edit-max-page');
      if(max) {
        max = max.value;
        var page = document.getElementById('edit-page');
        var pageVal = Math.round(parseInt(page.value));
        if(isNaN(pageVal)) {
          page.value = 1;
          pageVal = 1;
        }
        if(pageVal > max) {
          page.value = max;
        } else if(pageVal <= 0) {
          page.value = 1;
        } else {
        	page.value = pageVal;
        }
      }
      break;
    }
    if(caller.form) {
      var form_values = {};
      var form = caller.form;
      var count = form.elements.length;
      for(var i = 0; i < count; i++) {
        if(form.elements[i].type == 'select') {
          form_values[form.elements[i].name] = form.elements[i].options[form.elements[i].selectedIndex].value;
        } else {
          form_values[form.elements[i].name] = form.elements[i].value;
        }
      }
    } else {
      //form_values = data;
    }
    var loader = true;
    if(Drupal.researchTool.currentRequest) {
      if(Drupal.researchTool.currentRequest.readyState != 4) {
        Drupal.researchTool.currentRequest.abort();
        loader = false;
      }
    }
    if (loader) Drupal.researchTool.setContent('<div class="messages status loading">تحميل ...  الرجاء الانتظار... </div>');

    //var go_to = (form.action != ) '?q=json/research-tool/'+Drupal.researchTool.symbol+'/get-section/'+Drupal.researchTool.section
    Drupal.researchTool.currentRequest = $.ajax({
      type: "POST",
      url: form.action,
      data: form_values,
      success: function(json){if(json)Drupal.researchTool.setContent(json); else Drupal.researchTool.setError();},
      dataType: "json"
    });
    return false;
  }
};

$(document).ready(Drupal.researchTool.init);
