//_______________________________________________________________________
//   |       |
//   | knipp |           Knipp Medien und Kommunikation GmbH
//    -------
//
//
//    Project: Pöppelmann Relaunch 2010
//
//    Copyright (C) 2009 by Knipp Medien und Kommunikation GmbH
//
//    initial autor:   Björn Mönikes
//    version:         $Revision: 786 $
//                     $Date: 2011-10-07 11:15:41 +0200 (Fr, 07 Okt 2011) $
//                     $Author: Bjoern.Moenikes $
//


(function ($){
  $.fn.extend ({
    startAnimPlugin : function (options)
    {
      if (!$.event._startanimCleanup) $.event._startanimCleanup = [];

      // extend options by default values
      options = $.extend( {
        initialTimeout : 2000,
        animationDuration : 1500,
        eventPause : 500
      },
      options );

      return this.each (function () {
        if (!this._startanimController) {
          this._startanimController = new StartAnimPlugin (this);
          $.event._startanimCleanup[$.event.guid] = this._startanimController;
          $.event._startanimCleanup[$.event.guid++].init (options);
        }
      });
    },


    _startanimDestroy : function ()
    {
      return _eachCall.call (this, 'cleanup');
    }
  });


  var _eachCall = function (f, a1, a2, a3)
  {
    return this.each (function () {
      if (this._startanimController)
        this._startanimController[f](a1, a2, a3);
    });
  };


  $(window).bind('unload', function() {
    var els = $.event._startanimCleanup || [];
    for (var i in els) {
      $(els[i].ele)._startanimDestroy();
    }
  });


  function StartAnimPlugin(ele)
  {
    this.ele = ele;
    this.$ele = $(this.ele);
  };

  $.extend(StartAnimPlugin.prototype, {
    bindToObj: function(fn) {
      var self = this;
      return function() {
        return fn.apply(self, arguments)
      };
    },

    init : function(s)
    {
      this.initialTimeout = s.initialTimeout;
      this.animationDuration = s.animationDuration;
      this.eventPause = s.eventPause;
      this.$anims = $(".pp_home_division");

      if (this.$anims.length != 4)
        C.log("Warning: $anims.length != 4 in StartAnimPlugin.init");

      this.currentColumn = 0;
      this.step = [];
      for (var i = 0; i < this.$anims.length; i++)
      {
        this.step[i] = 1;
        var $anim = $( this.$anims.get(i) );
        $anim.attr("index",i);

        // break if no images are found
        if ($anim.length == 0) {
          C.log("Error: $anim.length == 0 for step "+i+" in StartAnimPlugin.init");
      	  return;
        }

        // duplicate first image to allow "round trips"
        var $img = $("div.scrollpane img:first", $anim).clone();
        var $last = $("div.scrollpane img:last", $anim);
        $img.insertAfter($last);
      }

      // start animation after initialTimeout
      this.$ele.oneTime(this.initialTimeout, "KSAinit", this.bindToObj(this.startNextAnimPhase)); 

    },

    startNextAnimPhase : function() {
      var $active = $( this.$anims.get(this.currentColumn) );
      if ($active.length == 0)
        C.log("Warning in startNextAnimPhase, $active.length = 0");
      else
        this.animColStart($active);
    },

    animPhaseStop : function ()
    {
      this.currentColumn = ( this.currentColumn + 1 );
      if (this.currentColumn > 3)
        this.currentColumn = 0;
      this.$ele.oneTime(this.eventPause, "animPhaseStop", this.bindToObj( this.startNextAnimPhase ));
    },

    animColStart : function ($container)
    {
      var col = this.currentColumn;

      var val = - this.step[col] * 155;
      if (this.step[col] == 2)
          val -= 3;
      val = "" + val + "px";

      this.step[col] ++;

      $("div.scrollpane",$container).animate( {"margin-left": val}, this.animationDuration); 
      var $cont = $container;

      var wrapper = function() {	
        this.animColStop($cont);
      };

      $container.oneTime(this.animationDuration+this.eventPause, "animColStart", this.bindToObj( wrapper ));

    },

    animColStop : function ($container)
    {
      var col = this.currentColumn;
      if (this.step[col] >= $("img",$container).length )
      {
        this.step[col] = 1;
        $("div.scrollpane", $container).css("margin-left", "0px");
      }
      this.animPhaseStop();
    },


    imageClickHandler : function (){
    },

    cleanup : function()
    {
    }
  });
})(jQuery);


// init Plugin
$(function()
{
  $(".tx-knipppphome-pi1-listrow").startAnimPlugin({
    initialTimeout : 2000,
    animationDuration : 800,
    eventPause : 400
  });
});

