/*!
* stagerotator - v0.3 - 21.10.2011
*
* Copyright (c) 2011 ]init[ AG
* Author: Sebastian Baranowski
*/
/*
* Usage:
***********************
*
* $("div.stageRotator").stagerotator();
* $("div.stageRotator").stagerotator( { rotationDelay: 6000, rotationSpeed: 800, fadeTime: 800 } );
*
*/

(function($) {

$.fn.stagerotator = function(options) {
  var defaults = {
    rotationDelay: 10000,
    rotationSpeed: 400,
    fadeTime: 800
  };

  return this.each(function() {
    if(options) {
      $.extend(defaults, options);
    }
    initialize($(this));
  });
  var self,
  rotating,
  paused,
  teaserUlRef,
  contentUlRef,
  teaserHeight,
  eventArray;
  function initialize(target) {
    self = target;

    rotating = true;
    paused = false;

    teaserUlRef = self.find(".rightSide ul");
    contentUlRef = self.find(".leftSide ul");
    teaserHeight = teaserUlRef.find("li").height();
  
    if(teaserUlRef.find("li").length != 4) {
      self.find("*").remove();
      self.html("<p style='color:#fff;text-align:center;margin-top:120px;'>Bitte nur genau 4 Teaser einh&auml;ngen!</p>");
      return false;
    }
 
    eventArray = new Array();
 
    teaserUlRef.find("li").each(function(index) {
      $(this).find("a").attr("href", "javascript:void(0);");
      $(this).attr("data-id", index);
   $(this).css("cursor", "pointer");
      $(this).click(function() {
        switchToTeaser($(this));
    $(this).blur();
      });
      eventArray.push($(this));
    });
 
    // content click as link
    contentUlRef.find("li").each(function() {
      var temp = $(this).find("a").attr("href");
      $(this).find("a").hide();
      if(temp) {
        $(this).css("cursor", "pointer");
        $(this).click(function() {
          window.location.href = temp;
        });
      }
    })
 
    contentUlRef.find("li:eq(0)").addClass("active");
    self.css("overflow", "hidden");
    teaserUlRef.css("top", "0px");
    swapTeaser();

    // dont ask
    self.hover(
      function() {
        if(!rotating) {
          teaserUlRef.stop(true,true);
        }
        else {
          removeEvents();
          paused = true;
        }
      },
      function() {
        paused = false;
        nextTeaser();
      }
    );
 
    nextTeaser();
  }
  function nextTeaser() {
    if(!paused) {
      rotating = false;
   
      teaserUlRef.css("top", "0px");
      teaserUlRef.css("width", "100%");

      teaserUlRef.animate(
        { width:"100%" },
        { duration: defaults.rotationDelay, complete:function() { rotating = true; } }
      ).animate(
        { top: teaserHeight * (-1) },
        {
          duration: defaults.rotationSpeed, complete: function() {
            var temp = parseInt(teaserUlRef.find("li:eq(0)").attr("data-id"));
            crossfadeContent(temp);
            swapTeaser();
            rotating = false;
            nextTeaser();
          }
        }
      );
 
    }
  }
  function swapTeaser() {
    if(paused || rotating) {
      var temp = teaserUlRef.find("li:eq(0)").detach();
      temp.appendTo(teaserUlRef);
      teaserUlRef.css("top", "0px");
      teaserUlRef.css("width", "100%");
    }
  }
  function crossfadeContent(id) {
    removeEvents();
    contentUlRef.find("li").eq(id).fadeIn(defaults.fadeTime, function() {
      $(this).addClass("active");
    });
    contentUlRef.find("li.active").fadeOut(defaults.fadeTime, function() {
      $(this).removeClass("active");
      addEvents();
    });
  }
  function switchToTeaser(obj) {
    var max = obj.index();
    var id = parseInt(obj.attr("data-id"));
    var temp = 0;
 
    for(var i = 0; i < max; i++) {
      temp = teaserUlRef.find("li:eq(0)").detach();
      temp.appendTo(teaserUlRef);
    }
 
    temp = teaserUlRef.find("li:eq(0)").detach();
    temp.appendTo(teaserUlRef);
 
    crossfadeContent(id);
  }
  function addEvents() {
    $.each(eventArray, function() {
      $(this).click(function() {
        switchToTeaser($(this));
      });
    });
  }
  function removeEvents() {
    $.each(eventArray, function() {
      $(this).unbind();
    });
  }

};
  /*
  // public function: call with  $.fn.stagerotator.doFoo()
  $.fn.stagerotator.doFoo = function() {
  console.log("doFoo: " + defaults.teaserVisible);
  return this;
  };
  // private function
  var initialize = function(object, defaults) {
  obj = object;
  startRotation();
  }
  */
})(jQuery);
