// borrowed soem code from 
// http://www.notgeeklycorrect.com/english/2009/05/18/beginners-guide-to-jquery-ruby-on-rails/
// public/javascripts/application.js
jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

jQuery.fn.submitWithAjax = function() {
  this.submit(function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

//Send data via get if <acronym title="JavaScript">JS</acronym> enabled
jQuery.fn.getWithAjax = function() {
  this.unbind('click', false);
  this.live("click", function() {
    $.get($(this).attr("href"), "_method=get", null, "script");
    return false;
  })
  return this;
};

jQuery.fn.getAndHideWithAjax = function(div_to_hide) {
  this.unbind('click', false);
  this.live("click", function() {
    $(div_to_hide).html('Loading....')
    $.get($(this).attr("href"), "_method=get", null, "script");
    return false;
  })
  return this;
};

//Send data via Post if <acronym title="JavaScript">JS</acronym> enabled
jQuery.fn.postWithAjax = function() {
  this.unbind('click', false);
  this.live("click", function() {
    $.post($(this).attr("href"), "_method=post", null, "script");
    return false;
  })
  return this;
};

jQuery.fn.putWithAjax = function() {
  this.unbind('click', false);
  this.live("click", function() {
    $.post($(this).attr('href'), "_method=put", null, "script");
    return false;
  })
  return this;
};

jQuery.fn.deleteWithAjax = function() {
  this.removeAttr('onclick');
  this.unbind('click', false);
  this.live("click", function() {
    $.post($(this).attr('href'), "_method=delete", null, "script");
    return false;
  })
  return this;
};

jQuery.fn.deleteWithAjaxAndConfirm = function() {
  this.removeAttr('onclick');
  this.unbind('click', false);
  this.live("click", function() {
    if (confirm('Zeker weten?')) {
      $.post($(this).attr('href'), "_method=delete", null, "script");
    }
    return false;
  })
  return this;
};

//This will "ajaxify" the links
jQuery( function() {
  // All non-GET requests will add the authenticity token
  // if not already present in the data packet
  $(document).ajaxSend(function(event, request, settings) {
    if (typeof(window._auth_token) == "undefined") return;
    // <acronym title="Internet Explorer 6">IE6</acronym> 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);
  });

  $('.ajaxForm').submitWithAjax();
  $('a.get').getWithAjax();
  $('a.post').postWithAjax();
  $('a.put').putWithAjax();
  $('a.delete').deleteWithAjax();
  $('a.delete_with_confirm').deleteWithAjaxAndConfirm();

  $('input.autocomplete').each(function(){
    var input = $(this);
    input.autocomplete(input.attr('autocomplete_url'), {
      matchContains:1,//also match inside of strings when caching
      //    mustMatch:1,//allow only values from the list
      selectFirst:false//select the first item on tab/enter
      // removeInitialValue:0,//when first applying $.autocomplete
    });
  });
  // http://webdeveloper.beforeseven.com/jquery/default-text-field-value-disappears-focus
  var active_color = '#000'; // Colour of user provided text
  var inactive_color = '#999'; // Colour of default text
  $('input.default-value').css("color", inactive_color);

  var default_values = new Array();
  $('input.default-value').focus(function() {
    if (!default_values[this.id]) {
      default_values[this.id] = this.value;
    }
    if (this.value == default_values[this.id]) {
      this.value = '';
      this.style.color = active_color;
    }
    $(this).blur(function() {
      if (this.value == '') {
        this.style.color = inactive_color;
        this.value = default_values[this.id];
      }
    });
  });

});


// http://polyrails.com/2008/11/19/animated-rails-flash-messages-w-jquery/
// function notify(flash_message)
// {
// // jQuery: reference div, load in message, and fade in
//     var flash_div = $("#flash");
//     flash_div.html(flash_message);
//     flash_div.fadeIn(400);
//     // use Javascript timeout function to delay calling
//     // our jQuery fadeOut, and hide
//     setTimeout(function(){
//         flash_div.fadeOut(500,function(){
//             flash_div.html("");
//             flash_div.hide()
//         })
//     },1400);
// }


