// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// Unobtrustive jQuery
//
// Here's how we use it:
// View
//   <a href='/post/12345/rate/4' class='put'>I'm giving this a 4!</a>
//
// Controller:
//   # PUT /post/:id/rate/:rating
//   def rate
//     respond_to do |wants|
//       wants.js do
//         Post.find(params[:id]).rate(params[:rating].to_i, current_user)
//         head :ok
//       end
//     end
//   end

// Crappy Race Condition Behaviours
$(document).ready(function() {
  $('#film-carousel').scrollTo(0, {axis:'x'});
  $('#film-carousel-controls a:first').addClass('active')
});

// Behaviours
$(document).ready(function() {
  // All non-GET requests will add the authenticity token
  // if not already present in the data packet
  $("body").bind("ajaxSend", function(elm, xhr, s) {
    if (s.type == "GET") return;
    if (s.data && s.data.match(new RegExp("\\b" + window._auth_token_name + "="))) return;
    if (s.data) {
      s.data = s.data + "&";
    } else {
      s.data = "";
      // if there was no data, jQuery didn't set the content-type
      xhr.setRequestHeader("Content-Type", s.contentType);
    }
    s.data = s.data + encodeURIComponent(window._auth_token_name)
                    + "=" + encodeURIComponent(window._auth_token);
  });
});

// All ajax requests will trigger the format.js block of respond_to do |format|
jQuery.ajaxSetup({
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
});

// More behaviours
$(document).ready(function() {
  // All A tags with class 'get', 'post', 'put' or 'delete' will perform an ajax call
  $('a.get').livequery('click', function() {
    $.getScript($(this).attr('href'));
    return false;
  }).attr("rel", "nofollow");

  $('a.get-data').livequery('click', function() {
    $.get($(this).attr('href'));
    return false;
  }).attr("rel", "nofollow");

  ['post', 'put'].forEach(function(method) {
    $('a.' + method).livequery('click', function() {
      if ($(this).metadata().confirm &&
          !confirm($(this).metadata().confirm)) return false;
      $.post($(this).attr('href'), "_method=" + method, function(data, textStatus) {
        jQuery.globalEval(data);
      });
      return false;
    }).attr("rel", "nofollow");
  });

  ['delete'].forEach(function(method) {
    $('a.' + method).livequery('click', function() {
      if ($(this).metadata().confirm &&
          !confirm($(this).metadata().confirm)) return false;
      $.prompt('Delete this item?<input type="hidden" id="target" name="target" value="'+ $(this).attr('href') +'" />',
               { opacity: 0.8, buttons: { Delete: true, Cancel: false }, focus: 1,
                 callback: function(v,m) {
        if (v) {
          $.post(m.find('#target').val(), "_method=delete", function(data, textStatus) {
            jQuery.globalEval(data);
          });
        }
      } });
      return false;
    }).attr("rel", "nofollow");
  });

});

// Application Behaviours
$(document).ready(function() {
// Navigation
  $('#nav li').click(function() {
    $(this).addClass('clicked');
  });
 $('a.delete').removeAttr('onclick');

// Comments
  var comment_action = document.location.href.split('comments/');
  if (comment_action.length == 2 || comment_action.length == 3) {
    $.scrollTo('ul #comment_' + comment_action[1], 800, {offset:-100, easing:'easeInOutQuart'});
    original = $('ul #comment_' + comment_action[1]).parent('ul').css('background-color');
    $('ul #comment_' + comment_action[1])
        .animate({ backgroundColor: "#6b9ab3" }, 500)
        .animate({ backgroundColor: original }, 500)
        .animate({ backgroundColor: "#6b8ab3" }, 500)
        .animate({ backgroundColor: original }, 500)
        .animate({ backgroundColor: "#6b8ab3" }, 500)
        .animate({ backgroundColor: original }, 500)
  }
  $('.reply-action a').click(function() {
    $(this).hide();
  });

// Films
  $('.banner').hover(function() {
    $('#details').fadeOut(600)
  }, function() {
    $('#details').fadeIn(500)
  });

  $('.gallery a.to-image').fancybox({
    'hideOnContentClick': true,
    'overlayShow': true,
    'overlayOpacity': 0.80
  });

// Gallery
  $('.gallery .actions a').hide();
  $('.gallery .actions').hover(function() {
    $(this).find('a').show();
  }, function() {
    $(this).find('a').hide();
  });

// Home
  $('#headblock').serialScroll({
    target: '#film-carousel',
    items: 'div.image',
    navigation: '#film-carousel-direct a',
    axis:'xy',
    duration: 600,
    constant: false,
    easing:'easeInOutQuart',
    lock: false,
    cycle: true,
    onBefore: function(e, elem, $pane, $items, pos) {
      $('#film-carousel-direct').children('a').removeClass('active');
      $('#film-carousel-direct').children('a').eq(pos).addClass('active');
    }
  });
  $('#film-carousel-controls').append('<a href=\"#\" class=\"next\">&gt;&gt;</a>');
  $('#film-carousel-controls').prepend('<a href=\"#\" class=\"prev\">&lt;&lt;</a>');
  $('a.next').click(function() {
    $('#film-carousel').trigger('next');
    return false;
  });
  $('a.prev').click(function() {
    $('#film-carousel').trigger('prev');
    return false;
  });
  $('#carousel').hover(function() {
      $('#film-carousel .image-overlay').fadeIn(200);
      $('#film-carousel-controls').fadeIn(200);
  }, function() {
      $('#film-carousel .image-overlay').fadeOut(200);
      $('#film-carousel-controls').fadeOut(200);
  });

// Posts
  function fbs_click() {u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;}
  $('#post-page .post-body').append('<div class="share"><a href="http://www.facebook.com/share.php?u='+document.location+'"class="fb_share_button" onclick="return fbs_click()" target="_blank" style="text-decoration:none;"></a>Enjoy the article? Share it.</div>');
  $('#new_subscriber').ajaxForm({
    dataType: 'script'
  }); 
});
