/*
  (c) SpuyMore Web/Applications
  http://www.spuymore.nl/
*/

var MyKickoffAccumulator = {
  blocked: false,

  getSubmitOptions: function()
  {
    return {
      semantic: true,
      dataType:     'xml',
      cache:        false,
      timeout:      16000,
      beforeSubmit: this.beforeSubmit,
      error:        this.handleError,
      success:      this.handleResponse
    };
  },

  /*static*/ beforeSubmit: function(oData, $form, oOptions)
  {
    // Block other manipulations while an update is running (default url == update url)
    if (MyKickoffAccumulator.blocked)
      return false;
    MyKickoffAccumulator.blocked = (oOptions['url'] == $form.get(0).action); 
      
    var $document = $(document),
        iY = $document.scrollTop(),
        iHeight = $(window).height(), 
        $accumulator = $('#MKOaccumulator'),
        iMinY = $accumulator.offset().top,
        iMaxY = iMinY + Math.min(iHeight, $accumulator.outerHeight()) - iHeight;
    if (iY > iMinY)
      $accumulator.get(0).scrollIntoView(true);
    else if (iY < iMaxY)
      $document.scrollTop(iMaxY);
    return true;
  },

  /*static*/ handleError: function(oObj)
  {
    /*alert('Sorry, an unexpected error occurred while processing your request.');*/
    MyKickoffAccumulator.blocked = false;
  },

  /*static*/ handleResponse: function(oXML)
  {
    var $form = $('#MKOaccumulator > form').eq(0);
    var oElm = oXML.documentElement;
    if (MyKickoffAccumulator.blocked = (oElm.nodeName == 'url'))
    {
      // Redirect to specified url (or reload current page if href attribute empty)
      var sURL = oElm.getAttribute('href');
      (sURL == '') ? document.location.reload() : document.location.replace(sURL);
    }
    else
    {
      // Replace accumulator and (re)bind Ajax handlers within form
      $form.children(':first').replaceWith('xml' in oElm ? oElm.xml : window.document.importNode(oElm, true));
      $form.ajaxForm(MyKickoffAccumulator.getSubmitOptions());
    }
  },

  /*static*/ handleCommand: function($event)
  {
    // 'this' references the object that fired the event
    var oOptions = MyKickoffAccumulator.getSubmitOptions();
    if ('href' in this)
    {
      oOptions['url'] = (($event.type == 'contextmenu') || ($event.button == 2)) ? this.href.replace(/\/add\//,'/tip/') : this.href;
      $event.preventDefault();
    }

    $('#MKOaccumulator > form').eq(0).ajaxSubmit(oOptions);
  },
  
  /*static*/ onLoad: function()
  {
    // Configure loader icon
    $('body')
     .ajaxStart(function(){ $(this).get(0).style.cursor = 'wait'; })
     .ajaxStop(function(){ $(this).get(0).style.cursor = 'auto'; });

    // Bind Ajax handlers
    var $form = $('#MKOaccumulator > form').eq(0);
    $form.get(0).onsubmit = null;  // Remove non-AJAX repeated onsubmit protection
    $form.ajaxForm(MyKickoffAccumulator.getSubmitOptions());
    $('a.AccumulatorCmd')
      .live('click', MyKickoffAccumulator.handleCommand)
      .live('contextmenu', MyKickoffAccumulator.handleCommand);
  }
};

try
{
  $(document).ready(MyKickoffAccumulator.onLoad);
}
catch(vErr)
{
  /* ignore */
}
