document.observe("dom:loaded", function() {

var logos = $('logos')
if (logos) {
  var logos1 = $('partneri'), logos2 = $('sponzori')
  logos.setStyle({ height: '108px' })
  logos2.hide()
  
  setInterval(function() {
    swap(logos1, logos2)
  }, 8000)
}

})

function swap() {
  var a = $(arguments[0]), b = $(arguments[1])
  if (b.visible()) {
    var c = a
    a = b
    b = c
  }
  
  new SetStyleEffect(a, {
    duration: .5,
    onComplete: function() {
      a.hide()
      var fx = new SetStyleEffect(b, { duration: .5 })
      b.setStyle({ opacity: 0 }).show()
      fx.start({ opacity: 1 })
    }
  }).start({ opacity: 0 })
}


// based on Bernie's Animator
var Effect = Class.create({
  initialize: function(options) {
    this.options = Object.extend({
      interval: 20,
      duration: 0.4,
      onComplete: function(){},
      transition: function(pos){ return ((-Math.cos(pos*Math.PI)/2) + 0.5) }
    }, options);
  },
  seekFromTo: function(from, to) {
    this.target = Math.max(0, Math.min(1, to));
    this.state  = Math.max(0, Math.min(1, from));
    if (!this.interval) {
      var _this = this, ticker = function(){ _this.tick() }
      this.interval = window.setInterval(ticker, this.options.interval);
    }
  },
  start: function() {
    this.seekFromTo(0, 1)
  },
  tick: function() {
    var movement = (this.options.interval / (this.options.duration * 1000)) * (this.state < this.target ? 1 : -1);
    if (Math.abs(movement) >= Math.abs(this.state - this.target)) {
      this.state = this.target;
    } else {
      this.state += movement;
    }
    
    try {
      this.transition(this.options.transition(this.state));
    } finally {
      if (this.target == this.state) {
        window.clearInterval(this.interval);
        this.interval = null;
        this.options.onComplete.call(this);
      }
    }
  },
  transition: function(value) {
    console.log(value)
  }
})

var SetStyleEffect = Class.create(Effect, {
  initialize: function($super, element, options) {
    $super(options)
    this.element = $(element)
  },
  transition: function(value) {
    var style = {}
    this.properties.each(function(pair) {
      var current = (pair.value[1] - pair.value[0]) * value + pair.value[0]
      style[pair.key] = current
      if (pair.key != 'opacity') style[pair.key] += 'px'
    })
    this.element.setStyle(style)
  },
  start: function($super, properties) {
    this.properties = new Hash
    $H(properties).each(function(pair) {
      var from = this.element.getStyle(pair.key)
      if (Object.isString(from)) from = Number(from.replace(/px|pt/, ''))
      this.properties.set(pair.key, [from, pair.value])
    }, this)
    $super()
  }
})
