// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
//jQuery.noConflict() // not needed because prototype.js is not used

/*jslint white: true, indent: 2, onevar: true, browser: true, undef: true, nomen: false, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: true, newcap: true, immed: true */
/*global jQuery, $, window*/

"use strict";

jQuery.ajaxSetup({
  'beforeSend': function (xhr) {
    xhr.setRequestHeader("Accept", "text/javascript");
  }
});

jQuery.extend(function () {
  // private
  function ajax_request(url, data, callback, type, method) {
    if (jQuery.isFunction(data)) {
      callback = data;
      data = {};
    }
    return jQuery.ajax({
      type: method,
      url: url,
      data: data,
      success: callback,
      dataType: type
    });
  }
  return {
    put: function (url, data, callback, type) {
      return ajax_request(url, data, callback, type, 'PUT');
    },
    delete_: function (url, data, callback, type) {
      return ajax_request(url, data, callback, type, 'DELETE');
    }
  };
}());

jQuery.fn.submitWithAjax = function () {
  this.unbind('submit', false);
  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.click(function () {
    $.get($(this).attr("href"), $(this).serialize(), 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.click(function () {
    $.post($(this).attr("href"), $(this).serialize(), null, "script");
    return false;
  });
  return this;
};

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

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

$(document).ready(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);
  });

  // "ajaxify" the links
  $('.ajax-form').submitWithAjax();
  $('a.get').getWithAjax();
  $('a.post').postWithAjax();
  $('a.put').putWithAjax();
  $('a.delete').deleteWithAjax();

  // autofocus
  $('.autofocus:first').focus();
  
  // peek links
  function enable_peek(names) {
    $.each(names, function (index, name) {
      var peeked = $('.' + name + ' a'); // eg. '.posts-tab a'
      $('a[class~=peek-' + name + ']')
        .hover(
          function () {
            peeked.addClass('peeked');
          },
          function () {
            peeked.removeClass('peeked');
          }
        );
    });
  }
  enable_peek(['shop-tab', 'posts-tab', 'sell-tab', 'fund-tab', 'about-tab']);
  
  // scroll to anchor
  
  $.localScroll.defaults.hash = true;
  $.localScroll();
  
});

