/* requires jQuery */

$(function() {
  $('#login-img').show();
  $('#login').hover(
    function() {$('#login-img').hide();},
    function() {$('#login-img').show();}
  );
  var sc = new sroller($('#banners'));
  initSubmenus();
});

function initSubmenus() {
  var margin = 4;
  $('.dropdown').hover(
    function(){
      var hWin = $(window).height()+$(window).scrollTop();
      var wWin = $(window).width()+$(window).scrollLeft();
      var sub = $('>ul',this);
      sub.show();
      if (!this.xDef) { // ulozeni vychozi pozice
        this.xDef = sub.position().left;
        this.yDef = sub.position().top;
      } else { // reset na vych pozici
        sub.css('left',this.xDef);
        sub.css('top',this.yDef);
      }
      // nejprve vyrovnani pri presahu vpravo a dole
      var xMax = sub.offset().left+sub.outerWidth();
      var yMax = sub.offset().top+sub.outerHeight();
      var rOverlap = xMax-wWin+margin;
      var bOverlap = yMax-hWin+margin;
      if (rOverlap>0) sub.css('left',function(i,v) {return parseInt(v)-rOverlap});
      if (bOverlap>0) sub.css('top',function(i,v) {return parseInt(v)-bOverlap});
      // pote vyrovnani pri presahu vlevo a nahore
      var xMin = sub.offset().left;
      var yMin = sub.offset().top;
      var lOverlap = $(window).scrollLeft()-xMin+margin;
      var tOverlap = $(window).scrollTop()-yMin+margin;
      if (lOverlap>0) sub.css('left',function(i,v) {return parseInt(v)+lOverlap});
      if (tOverlap>0) sub.css('top',function(i,v) {return parseInt(v)+tOverlap});
    },
    function(){
      $('>ul',this).hide();
    }
  );
}

function rolloutMenu(menuId,stayOpenedId) {
  if (!stayOpenedId) var idOpened= '123ihopenoonehasthisid789';
  var t = this;
  t.rocClosed = $('#'+menuId+'>li[id!='+stayOpenedId+']>.rollout-content');
  t.robClosed = $('#'+menuId+'>li[id!='+stayOpenedId+']>.rollout-btn');
  t.robClosed.toggleClass('closed');
  t.rocClosed.animate({height:'hide'}, 0);
  t.robClosed.click(function() {
    var roc2open = $('~.rollout-content',this);
    $(this).add(t.robOpened).toggleClass('closed');
    $(roc2open).add(t.rocOpened).animate({height:'toggle'},t.dur,t.ease);
    if (t.robOpened && (t.robOpened.get(0)==$(this).get(0))) {
      t.rocOpened = null;
      t.robOpened = null;
    } else {
      t.rocOpened = roc2open;
      t.robOpened = $(this);
    }
  });
}
rolloutMenu.prototype = {
  dur:350,
  ease:'swing'
}

function sroller(wrap) {

  var t = this;
  t.wrap = wrap;
  t.items = $('>.item',t.wrap);
  t.wrap.css('overflow','hidden');
  t.yEnd = t.wrap.innerHeight();
  if (t.wrap.css('position')=='static') t.wrap.css('position','relative');
  t.items.css('position','absolute');
  
  t.items.each(function() {
    var item = $(this);
    var h = item.outerHeight(true);
    var yBegin;
    var yLastNew = t.yLast+h;
    if (yLastNew>t.yEnd) {
      t.yBegin -= h;
      yBegin = t.yBegin;
    } else { 	
      yBegin = t.yLast;
      t.yLast = yLastNew;
    }
    var a = new animator(item,{property:'top',interval:60,speedMax:1,a:0,ease:false,finished:function() {
      a.set(t.yBegin);
      a.animate(t.yEnd);
    }});
    a.set(yBegin);
    a.animate(t.yEnd);
  });
}

sroller.prototype = {
  yBegin:0,
  yEnd:0,
  yLast:0
}

//
function addEvent(o,eventName,fnc) {
  if(o.addEventListener) {
    o.addEventListener(eventName,fnc,true);
    return true;
  } else if (o.attachEvent) {
    var r = o.attachEvent("on"+eventName,fnc);
    return r;
  } else {
    return false;
  }
}
function removeEvent(o,eventName,fnc) {
  if(o.removeEventListener) {
    o.removeEventListener(eventName,fnc,true);
    return true;
  } else if (o.detachEvent) {
    var r = o.detachEvent("on"+eventName,fnc);
    return r;
  } else {
    return false;
  }
}
