//------------------
// AJAX
//------------------

var toggleLoading = function() { $("#ajax_loader").toggle() };
$(document).bind('ajaxSend', toggleLoading);
$(document).bind('ajaxComplete', toggleLoading);



//------------------
// MODAL
//------------------

var modal_a;
function closeModal() {
  $('#modal').hide();
  $('#modal-container').hide();
}
$(function(){
  var $modal = $('#modal'),
      $modal_container = $('#modal-container');

  // This bit can be confusing. Since Rails.js sends an accept header asking for
  // javascript, but we want it to return HTML, we need to override this instead.
  //$('.modal_enabled').live('ajax:beforeSend', function(e, xhr, settings){
	
   // xhr.setRequestHeader('accept', '*/*;q=0.5, text/html, ' + settings.accepts.html);
  //});
  // Handle modal links with the data-remote attribute
  $('.modal_enabled').live('ajax:success', function(xhr, data, status){
    $modal
      .html(data)
      .css('top', $(window).scrollTop() + 40)
      .show();

    $modal_container.show();
	//$modal.draggable();
    
    $('#modal-container').live('click', function() {
         closeModal();
         return false;
     });
     $('#modal').live('click', function(e) {
         e.stopPropagation();
     });
    

  }).live("click", function() {
	//
});

  // Hide close button click
  $('.close').live('click', function(){
    closeModal();
    return false;
  });

});

function openModal(htmlData) {
	var $modal = $('#modal'),
      $modal_container = $('#modal-container');
	
	$modal
      .html(htmlData)
      .css('top', $(window).scrollTop() + 40)
      .show();
    $modal_container.show();

    $modal_container.live('click', function() {
         closeModal();
          return false;
     });
     $modal.live('click', function(e) {
         e.stopPropagation();
     });

}

function openConfirmModal(htmlData, modal_a) {
	var $modal = $('#modal'),
      $modal_container = $('#modal-container');
	
	$modal
      .html(htmlData)
      .css('top', $(window).scrollTop() + 40)
      .show();
    $modal_container.show();
	//$modal.draggable();

    $modal_container.live('click', function() {
         closeModal();
          return false;
     });
     $modal.live('click', function(e) {
         e.stopPropagation();
     });

}
