/*
 * dailyTips plugin for "Kernel of the Day"
 * Requires jQuery cycle plugin
*/
(function($) {
/****/

Array.prototype.remove = function(index) {
  return this.splice(index, 1)[0];
}

$.fn.dailyTips = function(options) {
  return initialize(this, options);

  function initialize(el, options) {
    options   = $.extend($.fn.dailyTips.options, options);
    var tips  = $.fn.dailyTips.slice(options);
    var html  = $.map(tips, function(value, key) {
      return '<div class="tip">' + value + '</div>';
    }).join("");
    
    $('.items', el).append(html).cycle({
      fx: 'none',
      timeout: 0,
      pager: $('.pager', el),
      prev:  $('.prev', el),
      next:  $('.next', el)
    }).width('auto')
  } // end initialize
}

$.fn.dailyTips.slice = function(options) {
  var tips    = options.tips.slice(0);
  var visible = parseInt(options.visible);
  var start   = Date.parse(options.start);
  var now     = new Date().getTime();
  var delta   = Math.abs(now - start);
  var offset  = parseInt(delta / 86400000);

  offset %= tips.length; 

  if (options.random) {
    var result = [];
    result.push(tips.remove(offset));
    visible--;
    
    for(var i = 0; i < visible; i++) {
      offset = (Math.random() * tips.length) - 1;
      result.push(tips.remove(offset))
    }
    
    return result
  } else {
    return tips.slice(offset, offset + visible);
  }  
} // end sliceTips


$.fn.allTips = function(options) {
  return initialize(this, options);

  function initialize(el, options) {
    options   = $.extend($.fn.dailyTips.options, options);
	var tips  = options.tips;
    var html  = $.map(tips, function(value, key) {
      return '<li class="clearfix">' + value + '</li>';
    }).join("");

    $('.items', el).append(html);
  } // end initialize
}


/****/
})(jQuery)