var ajaxLoaded = true;
//unobtrusive javascript replacements
//setup for format.js block
$.ajaxSetup({
	'beforeSend': function(xhr) { xhr.setRequestHeader('Accept', 'text/javascript') }
});

//for replacing remote_form_form
$.fn.submitWithAjax = function(){
	this.live('submit',function(){
		$.ajax({
			url: $(this).attr("action"),
			type: ($(this.form).find("[name=_method]").attr('value') == undefined) ? 'post' : $(this.form).find("[name=_method]").attr('value'),
			data: $(this).serialize(),
			dataType: 'script'
		});
		return false;		
	});
}

//for replacing link_to_remote
$.fn.linkWithAjax = function(){
	//add new click binding
	this.live('click',function(){
		$.ajax({
			url: this.href,
			type: $(this).is('.delete') ? 'delete' : 'get',
			dataType: 'script'
		});
		return false;
	});
}


function bindFilters(){
	$('.filterselect').unbind('change');
	
	$('.filterselect').change(function(){
		$.ajax({
			url: $(this.form).attr('action'),
			type: $(this.form).find("[name=_method]").attr('value') == undefined ? 'post' : $(this.form).find("[name=_method]").attr('value'),
			data: $(this.form).serialize(),
			dataType: 'script'
		});
		return false;
	});	
	
}

//Bind links and forms on load
$(function(){
	
	$('a.remote').linkWithAjax();
	$('form.remote').submitWithAjax();
	
	bindFilters();
});


