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;
};
$(document).ajaxSend(function(event, request, settings) {
  if (typeof(AUTH_TOKEN) == "undefined") return;
  // remove this line and watch internet explorer cry. 
  if (settings.type == 'GET') 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);
});

$(document).ready(function(){ //Start script when ready

  $(".story_link").bind('click',function(){
    largeImage = $(this).attr('rel');
    storyText  = $(this).children('img').attr('alt');
    storyTitle = $(this).children("span").text();
    $(".story-holder > img").attr('src',largeImage)
    $(".story-text > h3").html(storyTitle)
    $(".story-text > p").html(storyText)
    return false;
  });

  $(".order-popup").click( function(){
    var url = $(this).attr('href');
    $.getScript(url);
    return false;
  });

  $(".sample-popup").click( function(){
    var url = $(this).attr('href');
    $.getScript(url);
    return false;
  });


  $('.delete-quote-item').livequery('click', function(){
    var url = $(this).attr('href'); 
    if ( confirm("This will remove one item from your quote.") )
      $.ajax({
        url: this.href.replace('/delete', ''),
        type: 'post',
        dataType: 'script',
        data: { authenticity_token : AUTH_TOKEN, '_method': 'delete' },
      });
    return false;
  });

  $('.delete-sample-item').livequery('click', function(){
    var url = $(this).attr('href'); 
    if ( confirm("This will remove one item from your samples request.") )
      $.ajax({
        url: this.href.replace('/delete', ''),
        type: 'post',
        dataType: 'script',
        data: { authenticity_token : AUTH_TOKEN, '_method': 'delete' },
      });
    return false;
  });


  $("#new_newsletter_signup").submitWithAjax();

}); 


