// --------------------------------------------------------------
/**
 * Klasse zur Erzeugung eines Carousels
 *
 * Klasse zur Erzeugung eines Carousels
 *
 * $Rev: 6081 $ $Date: 2009-04-07 12:16:15 +0200 (Di, 07. Apr 2009) $ $Author:
 * wojewsky $ $URL:
 * http://wb4serv.heinze.de/projects_and_applications/bhp_BauherrenPortal/frontend/branches/10.1%20rc1/js/mootoggle.js $
 */
// --------------------------------------------------------------
var HeinzeCarousel = new Class({
  Implements: [Events, Options],
  options: {
      speed: 200,
      delay: 5000,
      auto: false,
      direction: 'horizontal',
      classItem: "item",
      idPrevious: 'previous',
      idNext: 'next',
      addZoomOut: false,
      addRollover: false,
      onClickPrevious: Class.empty,
      onClickNext: Class.empty,
      onPrevious: Class.empty,
      onNext: Class.empty
  },
  initialize: function(el,options){
    this.setOptions(options);
    this.el = $(el);
    this.items = this.el.getElements('li');
    var w = 0;
    var h = 0;
    if(this.options.direction.toLowerCase()=='horizontal') {
      h = this.el.getSize().y;
      this.items.each(function(li,index) {
        w += li.getSize().x;
      });
    } else {
      w = this.el.getSize().x;
      this.items.each(function(li,index) {
        h += li.getSize().y;
      });
    }
    this.el.setStyles({
      position: 'relative',
      top: 0,
      left: 0
    });
    this.current = 0;
    if(this.options.idPrevious != "undefined" && $(this.options.idPrevious))
      $(this.options.idPrevious).addEvent("click", function(event) {
        new Event(event).stop();
        this._previous(true);
        this.fireEvent("onClickPrevious", this, 20);
      }.bind(this));// check if value is not "undefined" before start search the dom with $()
    if(this.options.idNext != "undefined" && $(this.options.idNext))
      $(this.options.idNext).addEvent("click", function(event) {
        new Event(event).stop();
        this._next(true);
        this.fireEvent("onClickNext", this, 20);
      }.bind(this));
    //only if addZoomOut is enabled in the top settings
    if(this.options.addZoomOut == true) {
      this.addZoomOut(this.el.getElements('.' + this.options.classItem));
    };
    //only if addRollover is enabled in the top settings (liam)
    if(this.options.addRollover == true) {
      this.addRollover(this.el.getElements('.' + this.options.classItem));
    };
    if (this.options.auto) {
      this._next();
    }
  },
  _next: function(click) {
    this.current++;
    if (this.current >= this.items.length) this.current = 0;
    this.fx = new Fx.Morph(this.el,{duration:this.options.speed, onComplete:function() {
      var i = (this.current==0)?this.items.length:this.current;
      this.items[i-1].injectInside(this.el);
      this.el.setStyles({
        left:0,
        top:0
      });
    }.bind(this)});
    this.fx.start({
      top: (this.options.direction.toLowerCase()=='horizontal') ? 0 : -this.items[this.current].getSize().y,
          left: (this.options.direction.toLowerCase()=='horizontal') ? -this.items[this.current].getSize().x : 0
    });
    if (click) {
      $clear(this.timer);
      this.options.auto = false;
      this.fireEvent("onNext", this, 20);
    } else {
      if (this.options.auto) {
        this.timer = this._next.bind(this).delay(this.options.delay+this.options.speed);
      }
    }
  },
  _previous: function(click) {
    this.current--;
    if (this.current < 0) this.current = (this.items.length - 1);
    this.items[this.current].inject(this.el, 'top');
    this.el.setStyles({
      top: (this.options.direction.toLowerCase()=='horizontal') ? 0 : -this.items[this.current].getSize().y,
      left: (this.options.direction.toLowerCase()=='horizontal') ? -this.items[this.current].getSize().x : 0
    });
    this.fx = new Fx.Morph(this.el,{duration:this.options.speed});
    this.fx.start({
      top: 0,
      left: 0
    });
    if (click) {
      $clear(this.timer);
      this.options.auto = false;
      this.fireEvent("onPrevious", this, 20);
    }
  },
  addZoomOut:function(el) {
    el.each(function(el,i) {
      //if link contains an image ad zoom
      if(el.getElement('img')) {
        var image = el.getElement('img');
        var imageWidth = Number(image.get('width'));
        var imageHeight = Number(image.get('height'));
        var imagePaddingTop = Number(image.getStyle('padding-top').replace('px', ''));
        //add event listener
        el.addEvents ({
          'mouseenter': function() {
            var imagePadding = imagePaddingTop - 8;
            if (imagePadding < 0) {
              imagePadding = 0;
            }
            image.set('morph',{duration:200,transition:Fx.Transitions.linear}).morph({width: (imageWidth + 16), height: (imageHeight + 16), 'padding-top': (imagePadding)});
          },
          'mouseleave': function() {
            image.set('morph',{duration:200,transition:Fx.Transitions.linear}).morph({width: imageWidth, height: imageHeight, 'padding-top': imagePaddingTop});
          }
        });
      };
    });
  },
  addRollover:function(el) {
    el.each(function(el,i) {
      //if link contains an image ad overlay
      if(el.getElement('img')) {
        //add event listener
        el.addEvents ({
          'mouseenter': function() {
            el.getElement('img').set('tween',{duration:200,transition:Fx.Transitions.linear}).tween('opacity',0.5);
          },
          'mouseleave': function() {
            el.getElement('img').set('tween',{duration:400,transition:Fx.Transitions.linear}).tween('opacity',1);
          }
        });
      };
    });
  }
});