// This method makes sure that the AUTH_TOKEN is transmitted
$(document).ajaxSend(function(event, request, settings) {
  if (settings.type == 'GET' || settings.type == 'get' || typeof(AUTH_TOKEN) == "undefined") return;
  // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
  settings.data = settings.data || "";
  settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});


// $.fn.with:
// Used to process child nodes while
// retaining parent within chain. 
// TODO: Issue with IE7
//$.fn.with = function(elems,action){ 
//   return this.each(function(){
//        $(elems,this).each(action);
//    });
//}

// dan webb delegate method 
$.delegate = function(rules) {
  return function(e) {
    var target = $(e.target);
    for (var selector in rules)
      if (target.is(selector)) return rules[selector].apply(this, $.makeArray(arguments));
  }
}

// spin is a method to append a spinner 
$.fn.spin = function(append) {
  if ($("#spinner").length == 0) {
    if (append)
      jQuery(this).append('<img id="spinner" src="/images/spinner_green_bg.gif"/>')
      else
    jQuery(this).after(' <img id="spinner" src="/images/spinner_green_bg.gif"/>')
  }
}


$(function () {
  $('.coming-soon').bt( {
    padding: 10,
    width: 100,
    spikeLength: 40,
    spikeGirth: 40,
    cornerRadius: 20,
    positions: 'bottom',
    fill: 'rgba(0, 0, 0, .8)',
    strokeWidth: 2,
    strokeStyle: '#F5631B',
    cssStyles: {color: '#FFF'}
  });
  
  $('.why').click(function() {
    $('#' + $(this).attr('href')).toggle(); 
    return false;
  });

  //$.preloadCssImages();
  $('.destroy').click(function() {
    destroy(this, $(this).attr('href'));
    return false;
  });
});

// create a form on the fly and properlly destroy method on a controller
function destroy(element, action) {
  var f = document.createElement('form');
  f.style.display = 'none';
  element.parentNode.appendChild(f);
  f.method = 'POST';
  f.action = action;
  var m = document.createElement('input');
  m.setAttribute('type', 'hidden');
  m.setAttribute('name', '_method');
  m.setAttribute('value', 'delete');
  f.appendChild(m);
  f.submit();
}

