Pricecalculator=
{
  init: function(AJAXurl, options)
  // Init Pricecalculator, variables, actions
  {
    this.AJAXurl=AJAXurl;
    this.basename=(typeof(options.basename) == 'undefined') ? '' : options.basename+'_';
    this.rates={ my: false, to: false };
    this.callbackCost=null;

    this.mysearch=$(this.basename+'my_search');
    this.myroute=$(this.basename+'my_route');
    this.mytypelandline=$(this.basename+'my_landline');
    this.mytypemobile=$(this.basename+'my_mobile');

    this.tosearch=$(this.basename+'to_search');
    this.toroute=$(this.basename+'to_route');
    this.tocaption=$(this.basename+'noactive');

    this.cost=$(this.basename+'cost');
    this.costdirectionmy=$(this.basename+'direction_my');
    this.costdirectionto=$(this.basename+'direction_to');
    this.costlandline=$(this.basename+'landline');
    this.costmobile=$(this.basename+'mobile');

    if (typeof(options.callbackLandline) == 'undefined')
      this.callbackLandline=true;
    else
    {
      this.callbackLandline=options.callbackLandline;
      this.changeCallback(true);
    }

    if (this.mysearch && this.myroute)
      this.mysearch.onkeyup=function() { Pricecalculator.doSearch(this, Pricecalculator.myroute); }
    if (this.tosearch && this.toroute)
      this.tosearch.onkeyup=function() { Pricecalculator.doSearch(this, Pricecalculator.toroute); }

    if (this.mytypelandline)
      this.mytypelandline.onchange=function() { Pricecalculator.callbackLandline=true; Pricecalculator.changeCallback(); }
    if (this.mytypemobile)
      this.mytypemobile.onchange=function() { Pricecalculator.callbackLandline=false; Pricecalculator.changeCallback(); }

    this.addHandlers();
  },

  addHandlers: function()
  // Add action handlers for all <tr>
  {
    ['my', 'to'].each(function (type)  // Two tables
    {
      $$('#'+Pricecalculator.basename+type+'_route tr').each(function(tr)
      {
        if (tr.hasClassName('expand'))   // Expand field, add AJAX-expander
          tr.onclick=function()
          {
            Pricecalculator.doAJAXUpdate(this.parentNode, { action: 'expand' },
               function() { setTimeout(Pricecalculator.addHandlers, 0); } );  // Reset handlers
          }
        else  // Make select
          tr.onclick=function() {Pricecalculator.doSelect(this, type)};
      });
    });

    Pricecalculator.changeCallback(true);
  },
  
  doSearch: function(input, table)
  // Send search query and update data
  {
    this.doAJAXUpdate(table, { search: input.value }, function() { setTimeout(Pricecalculator.addHandlers, 0); } );
  },

  doSelect: function(el, type)
  // Select one item
  {
    // Mark selected item
    $$('#'+this.basename+type+'_route tr.selected').each(function(tr) { tr.removeClassName('selected'); } );
    el.addClassName('selected');

    // Save selection
    this.rates[type] = {
      name: el.childNodes[0].innerHTML,
      landline: parseFloat(el.childNodes[3].innerHTML),
      mobile: parseFloat(el.childNodes[4].innerHTML)
    }

    if (type=='my')
      this.changeCallback();  // We've changed callback route

    this.showResults();
  },

  changeCallback: function(force)
  // We've changed callback, recompute prices
  {
    if ((this.myroute != null) && (! this.rates.my))
      return;  // We haven't got callback route

    var newCost=this.rates.my ? (this.callbackLandline ? this.rates.my.landline : this.rates.my.mobile) : 0;
    if (force || (this.callbackCost != newCost))  // Update destination rates
    {
      $$('#'+this.basename+'to_route tr').each(function(tr)
      {
        if (tr.childNodes.length==5)
        {
          // Landline:
          if (Pricecalculator.costlandline)
          {
            tr.childNodes[1].innerHTML=(newCost+parseFloat(tr.childNodes[3].innerHTML)).toFixed(2);
            Element.show(tr.childNodes[1]);
          }
          // Mobile
          if (Pricecalculator.costmobile)
          {
            tr.childNodes[2].innerHTML=(newCost+parseFloat(tr.childNodes[4].innerHTML)).toFixed(2);
            Element.show(tr.childNodes[2]);
          }
        }
      });

      if (Pricecalculator.tocaption)
      {
        if (Pricecalculator.costlandline)
          Element.show(Pricecalculator.tocaption.childNodes[1]);
        if (Pricecalculator.costmobile)
          Element.show(Pricecalculator.tocaption.childNodes[2]);
      }
    }

    this.callbackCost=newCost;

    this.showResults();
  },

  showResults: function()
  // If we have direction, show data
  {
    if ((! this.myroute || this.rates.my) && (! this.toroute || this.rates.to))
    {
      if (this.rates.my)
        this.costdirectionmy.innerHTML=this.rates.my.name;
      this.costdirectionto.innerHTML=this.rates.to.name;
      if (this.costlandline)
        this.costlandline.innerHTML=(this.callbackCost+this.rates.to.landline).toFixed(2);
      this.costmobile.innerHTML=(this.callbackCost+this.rates.to.mobile).toFixed(2);
      Element.show(this.cost);
    }
  },

  doAJAXUpdate: function(updateID, params, onSuccessFunc)
  // Make AJAX.Update
  {
    new Ajax.Updater(updateID, this.AJAXurl, {
      parameters: params,
      onSuccess: onSuccessFunc
    });
  }

}
