/* @license Copyright 2010 Jay Kerschner. Originally developed by Jay Kerschner, <jay@venivortex.com>.
 Available under the MIT license.

 ===MIT license===
 Copyright (c) <2010> <Jay Kerschner>

*/

/* ALWAYS ADD AN EXTRA SEMICOLON BEFORE CODE AFTER MINIFICATION (closure compiler removes it) */
;(function ($) {
  $.fn.rotator = function (opts) {
    var o = $.extend({}, $.fn.rotator.defaults, opts);
    return this.each(function (ind) {
      var $parent = $(this), $children = $parent.children(), itemnum = false, rand = Math.floor(Math.random() * $children.length), data = [];
      if (!$parent.is("ul") || $children.length === 0) {
        $.error("jQuery.rotator can only rotate content contained within <ul> tags which contain at least one <li>.");
      } else {
        switch (o.mode) {
          case "random": //Most common, so list first for speed.
            itemnum = rand;
            break;
          case "simplefade":
          case "crossfade": //Purposeful fallthrough
            if (isNaN(o.base_num)) {
              itemnum = rand;
            } else {
              itemnum = parseInt(o.base_num, 10) - 1;
            }
            break;
          case "datetime":
            var cd = new Date(), timeframe = {millisecond: cd.getMilliseconds(), second: cd.getSeconds(), minute: cd.getMinutes(), hour: cd.getHours(), day: cd.getDate(), month: cd.getMonth(), year: cd.getYear()}[o.switch_every];
            if (!timeframe && timeframe !== 0) {
              $.error("Invalid or missing value for the jQuery.rotator \"switch_every\" option. Must be one of the following: \"millisecond\", \"second\", \"minute\", \"hour\", \"day\", \"month\", or \"year\".");
              itemnum = false;
            } else {
              itemnum = Math.floor(timeframe % $children.length);
            }
            break;
          case "static":
            if (isNaN(o.base_num)) {
              $.error("When using the \"static\" mode of jQuery.rotator, the \"base_num\" option must be set to an integer value.");
            } else {
              itemnum = parseInt(o.base_num, 10) - 1;
            }
            break;
          default: 
            $.error("Unrecognized mode for jQuery.rotator. Accepted modes are: \"random\" (default), \"simplefade\", \"crossfade\", and \"static\".");
        }
        if (itemnum !== false) {
          $children.each(function (ind) {
            data[ind] = $(this).html();
          });
          $parent.replaceWith("<div class='jquery-rotator-active' style='position:relative;'><div class='jquery-rotator-content' style='margin:0;padding:0;'></div><div class='jquery-rotator-content-temp' style='margin:0;padding:0;'></div></div>");
          var $newparent = $(".jquery-rotator-active").eq(ind), $curcontent = $newparent.children(".jquery-rotator-content"), $tempcontent = $newparent.children(".jquery-rotator-content-temp"), boxsize = function (mode) {
            var content_height = Math.max($curcontent.height(), $tempcontent.height()), content_width = Math.max($curcontent.width(), $tempcontent.width());
            if (mode === "slide") {
              $newparent.animate({height: content_height, width: content_width});
            } else { // Typically defined as "instant"
              $newparent.css({height: content_height + "px", width: content_width + "px"});
            }
          };
          var css = {position: "absolute", top: 0, left: 0};
          $curcontent.html(data[itemnum]).css(css);
          $tempcontent.hide().css(css);
          boxsize("instant");
          
          if (o.mode === "simplefade") {
            setInterval(function () {
              itemnum += 1;
              if (itemnum > data.length - 1) {
                itemnum = 0;
              }
              $curcontent.fadeOut(o.fade_time / 2, function () {
                $curcontent.html(data[itemnum]).fadeIn(o.fade_time / 2);
                boxsize(o.box_sizechange_mode);
              });
            }, o.fade_interval);
          } else if (o.mode === "crossfade") {
            setInterval(function () {
              var fade1 = false, fade2 = false, fadeDone = function () {
                if (fade1 && fade2) {
                  $curcontent.html(data[itemnum]).show();
                  $tempcontent.hide();
                  boxsize(o.box_sizechange_mode);
                }
              };
              itemnum += 1;
              if (itemnum > data.length - 1) {
                itemnum = 0;
              }
              $tempcontent.html(data[itemnum]).fadeIn(o.fade_time, function () {
                fade1 = true;
                fadeDone();
              });
              $curcontent.fadeOut(o.fade_time, function () {
                fade2 = true;
                fadeDone();
              });
              boxsize(o.box_sizechange_mode);
            }, o.fade_interval);
          }
        } else {
          $.error("Invalid value for item number in jQuery.rotator; one or more options may be set incorrectly.");
        }
      }
    });
  };
  $.fn.rotator.defaults = {
    // base_num: 1, //Not defined by default; listed for completeness of examples
    box_sizechange_mode: "slide",
    fade_interval: 5000,
    fade_time: 1000,
    mode: "random",
    switch_every: "day"
  };
})(jQuery);
