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

/* Extend jQuery with functions for PUT and DELETE requests.
   From http://homework.nwsnet.de/news/9132_put-and-delete-with-jquery */
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
        });
}

jQuery.extend({
    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');
    }
});

// wrap the Twitter-provided callback with one that shows the div
function twitterCallback2WithShow(something) {
  twitterCallback2(something);
  $('#twitter-feed').css('visibility', 'visible');
}

// Start jQuery no-conflicting
(function($) {

/* Extend jQuery to allow arbitrary delays before executing actions.
   From http://james.padolsey.com/javascript/jquery-delay-plugin/ */
$.fn.delay = function(time, callback){
  // Empty function:
  jQuery.fx.step.delay = function(){};
  // Return meaningless animation, (will be added to queue)
  return this.animate({delay:1}, time, callback);
}

/* TWITTER */
// make sure there's a div to play with
$(document).ready( function() {
  if($('#twitter-feed').size() > 0) {
    // load the Twitter js
    $.getScript('http://thedartmouth.com/javascripts/twitter.js');
  
    // load the Twitter data and use the wrapped callback
    $.getScript('http://thedartmouth.com/twitter.json');
  }
});

// ping ads that have been viewed
$(document).ready( function() {
  if(typeof(hit_count_urls) != 'undefined')
    for (url in hit_count_urls)
      $.put(hit_count_urls[url]);
});

// frontpage todayd rotation
// on the container with all the individual todayd elements, do $('#todayd').todayd_rotation();
// excepts photos to have id todayd-num-#, and a contents todayd-nav.

$.fn.todayd_rotation = function() {
  var 
    counter = 0,
    MAX_ROTATIONS = 50,
    container = $(this),
    photos = $('.todayd-photo-box', container),
    nav = $('#todayd-nav', container),
    max_num = photos.eq(photos.size()-1).attr('id').match(/todayd\-num\-([0-9]+)/i)[1],
    INTERVAL = 9000,
    pause_rotation = false;
    
  nav.hide();
  photos.hide().eq(0).addClass('current').show();
  
  container.hover(
    function() {
      nav.fadeIn('fast');
      pause_rotation = true;
    },
    function() {
      nav.fadeOut('fast');
      pause_rotation = false;
    }
  );
  
  var show_todayd = function(next_num) {
    var
      current = photos.filter('div.current').eq(0),
      next = $('#todayd-num-'+next_num, container);
    
    // console.log(current, next, next_num);
    current.removeClass('current').css({'z-index': 5}).fadeOut('fast');
    next.addClass('current').css({'z-index': 10}).fadeIn('fast');
    
    nav.find('ul li').removeClass('current').eq(next_num-1).addClass('current');
  };
  
  nav.find('ul li').click(function(e) {
    show_todayd(nav.find('ul li').index($(e.target).parents('li'))+1);
    e.preventDefault();
    $(e.target).blur();
    clearTimeout(timeout);
  });
  
  var timeout = setInterval(function() {
    // stop the rotations after a while
    if(++counter == MAX_ROTATIONS) {
      clearTimeout(timeout);
      return;
    }
    // pause rotations if mouseover container
    if(pause_rotation) {
      return;
    }
    
    var
      current = photos.filter('div.current').eq(0),
      current_num = parseInt(current.attr('id').match(/todayd\-num\-([0-9]+)/i)[1], 10),
      next_num = current_num+1 == max_num ? 1 : current_num+1;
    
    show_todayd(next_num);
    
  }, INTERVAL);
};
if($('body').hasClass('frontpage') || $('body').hasClass('archives')) {
  $(function() {
    if ($('#todayd').length != 0)
      $('#todayd').todayd_rotation();
  });
}

})(jQuery);