// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/*
Jquery and Rails powered default application.js
Easy Ajax replacement for remote_functions and ajax_form based on class name
All actions will reply to the .js format
Unostrusive, will only works if Javascript enabled, if not, respond to an HTML as a normal link
respond_to do |format|
  format.html
  format.js {render :layout => false}
end
*/
 
jQuery.ajaxSetup({ 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} })
 
function _ajax_request(url, data, callback, type, method) {
    if (jQuery.isFunction(data)) {
        callback = data;
        data = {};
    }
    return jQuery.ajax({
        type: method,
        url: url,
        data: data,
        success: callback,
        dataType: type
        });
}
 
jQuery.extend({
    put: function(url, data, callback, type) {
        return _ajax_request(url, data, callback, type, 'PUT');
    },
    delete_: function(url, data, callback, type) {
        return _ajax_request(url, data, callback, type, 'DELETE');
    }
});
 
/*
Submit a form with Ajax
Use the class ajaxForm in your form declaration
<% form_for @comment,:html => {:class => "ajaxForm"} do |f| -%>
*/
jQuery.fn.submitWithAjax = function() {
  this.die("submit");
  this.live('submit', function() {
  	jQuery('#wait_icon').show(10);	
	jQuery.post(this.action, jQuery(this).serialize(), null, "script");
	jQuery("#wait_icon").ajaxComplete(function(event,request, settings){
		jQuery('#wait_icon').hide(0);
    });
	return false;
  })
  return this;
};

jQuery.fn.submitWithAjaxWithoutWheel = function() {
  this.die("submit");
  this.live('submit', function() {
	jQuery.post(this.action, jQuery(this).serialize(), null, "script");
	return false;
  })
  return this;	
};
 
/*
Retreive a page with get
Use the class get in your link declaration
<%= link_to 'My link', my_path(),:class => "get" %>
*/
jQuery.fn.getWithAjax = function() {
  this.die("click");
  this.live('click', function() {
  	jQuery.get(jQuery(this).attr("href"), jQuery(this).serialize(), null, "script");
    return false;
  });
  return this;  
};
 
/*
Post data via html
Use the class post in your link declaration
<%= link_to 'My link', my_new_path(),:class => "post" %>
*/
jQuery.fn.postWithAjax = function() {
  this.die("click");
  this.live('click', function() {
  	jQuery.post(jQuery(this).attr("href"), jQuery(this).serialize(), null, "script");
    return false;
  });
  return this;
};

jQuery.fn.postWithAjaxGlossaire = function() {
  this.die("click");
  this.live('click', function() {
	jQuery('#wait_icon_glossaire').show(10);
    jQuery.post(jQuery(this).attr("href"), jQuery(this).serialize(), null, "script");
    jQuery("#wait_icon_glossaire").ajaxComplete(function(event,request, settings){
	 	jQuery('#wait_icon_glossaire').hide(0);
	 });
    return false;
  });
  return this;
};
 

jQuery.fn.postWithAjaxPack = function() {
  this.die("click");
  this.live('click', function() {
    jQuery('#wait_icon_' + jQuery(this).attr("id")).show(10);
	jQuery.post(jQuery(this).attr("href"), jQuery(this).serialize(), null, "script");
    jQuery('#wait_icon_' + jQuery(this).attr("id")).hide(0);
	return false;
  });
  return this;
};


jQuery.fn.postRadioWithAjax = function() {
  this.change(function() {
    jQuery.post(jQuery(this).attr("url"), jQuery(this).serialize(), null, "script");
    return false;
  })
  return this;
};

/*
Update/Put data via html
Use the class put in your link declaration
<%= link_to 'My link', my_update_path(data),:class => "put",:method => :put %>
*/
jQuery.fn.putWithAjax = function() {
  this.die("click");
  this.live('click', function() {
    jQuery.put(jQuery(this).attr("href"), jQuery(this).serialize(), null, "script");
    return false;
  });
  return this;	
};
 
/*
Delete data
Use the class delete in your link declaration
<%= link_to 'My link', my_destroy_path(data),:class => "delete",:method => :delete %>
*/
jQuery.fn.deleteWithAjax = function() {
  this.removeAttr('onclick');
  this.die("click");
  this.live('click', function() {
    jQuery.delete_(jQuery(this).attr("href"), jQuery(this).serialize(), null, "script");
    return false;
  });
  return this;	
};
 
/*
Ajaxify all the links on the page.
This function is called when the page is loaded. You'll probaly need to call it again when you write render new datas that need to be ajaxyfied.'
*/

function ajaxLinks(){
    jQuery('.ajaxForm').submitWithAjax();
    jQuery('.ajaxFormWw').submitWithAjaxWithoutWheel();
    jQuery('a.get').getWithAjax();
    jQuery('a.post').postWithAjax();
	jQuery('.pagination_ajax a').postWithAjax();		
    jQuery('a.postpack').postWithAjaxPack();
    jQuery('a.post_glossaire').postWithAjaxGlossaire();
    jQuery('a.put').putWithAjax();
    jQuery('a.delete').deleteWithAjax();
    jQuery('input.post').postRadioWithAjax();
}
 
jQuery(document).ready(function() {
// All non-GET requests will add the authenticity token
 jQuery(document).ajaxSend(function(event, request, settings) {
       if (typeof(window.AUTH_TOKEN) == "undefined") return;
       // IE6 fix for http://dev.jquery.com/ticket/3155
       if (settings.type == 'GET' || settings.type == 'get') return;
 
       settings.data = settings.data || "";
       settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(window.AUTH_TOKEN);
     });
 
  ajaxLinks();
});
