/**
 * @package jalib
 * @author Adam van Dongen
 * @copyright (c) 2008 - Joomla-addons.org
 * @version $Id:$
 * 
 * --------------------------------------------------------------------------------
 * Joomla-addons programming library which will act as a 
 * compatibility layer with Joomla and other CMS applications
 * 
 * This library is distributed and released under the terms and conditions
 * of the GNU/Lesser General Public License version 2.1.
 * See LICENSE.php for an more information
 * --------------------------------------------------------------------------------
 */
;(function($) {
  $.jalib = {
    option: '',
    controller: '',
    ajax_request: null,
    document_unload: false,
    init: function(){
      $.ajaxSetup({cache: false, error: $.jalib.ajaxError, beforeSend: $.jalib.ajaxBeforeSend, complete: $.jalib.ajaxComplete});
      // neatly cancel ajax request, and don't display error message if one navigates away
      if(window.onbeforeunload){ var window_onbeforeunload = window.onbeforeunload; }
      window.onbeforeunload = function() { 
        $.jalib.document_unload = true;
        if($.jalib.ajax_request) { $.jalib.ajax_request.abort(); } 
        if(window_onbeforeunload) { return window_onbeforeunload(); } 
      }
      // init calendar selection stuff
      ja('.ja_date_selection').each(function(){
        $('#' + this.id).datepicker({yearRange: '-57:+3', dateFormat: 'yy-mm-dd', showButtonPanel: true, changeMonth: true, changeYear: true, firstDay: 1});
      });    
    },
    jabutton: function(button){
      $.jalib.jasubmit(button);
    },
    button: function(button){
      $.jalib.submit(button);
    },
    jasubmit: function(button){
      $.jalib.submit(button);
    },
    submit: function(button){
      var form = document.jaForm;
      
      // append option, controller and task to form action
      form.action += '?option=' + ja.jalib.option + '&controller=' + ja.jalib.controller + '&task=' + button;
      if(button == 'edit'){
        form.action += '&id=' + $('input[rel=ja_box]:checked').val();
      }
      
      try {
        document.jaForm.onsubmit();
      }
      catch(e){}
    
      form.submit();
    },
    mark_invalid: function(id, text){
      $('#' + id).after('<div class="ja_form_invalid">' + text + '</div>');
      $('#' + id).addClass('invalid_form_entry');
      $('#' + id).bind('focus', function(){
        ja.jalib.clear_invalid();
      });
    },
    clear_invalid: function(){
      $('.invalid_form_entry').removeClass('invalid_form_entry');
      $('.ja_form_invalid').remove();
    },
    update: function(file, application, id){
      $.get('index3.php?option=' + ja.jalib.option + '&controller=' + ja.jalib.controller + '&task=updatefile&no_html=1&application=' + application + '&file=' + file, function(data, status){
        if(data.length != 0){
          alert(data);
        } else {
          $('#' + id).fadeTo("slow", 0.33);
          $('#' + id + ' td:last').html("");
        }
      });
    },
    ajaxBeforeSend: function(xmlrequest){
      $.jalib.ajax_request = xmlrequest;
    },
    ajaxComplete: function(xmlrequest){
      $.jalib.ajax_request = null;
    },
    ajaxError: function(xmlrequest, textStatus, error){
      if(!$.jalib.document_unload){
        alert('Error processing xml ajax request:' + "\nType: " + textStatus + (error ? "\nError: " + error : '') + (xmlrequest.responseText ? "\nResponse: " + xmlrequest.responseText : ''));
      }
    }
  }
  jQuery(function(){ $.jalib.init(); });
})(jQuery);
