$ = jQuery.noConflict();
(function($) {
  $(document).ready(function() {
      // start
      $("a.pluss").click(function(e) {
        e.preventDefault();
        $(this).prev().trigger('click');
      });

      $("a.modal").click(function(e) {
        e.preventDefault();

        var id = $('.pop_up');
        var content = $(this).attr('href');

        var pos = $(this).parent().offset();

        var image = '<img src="'+content+'" alt="" />';
        $('#imgwrap').html(image);

        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        $('#mask').css({'width':maskWidth,'height':maskHeight});
        $('#mask').show();

        var winH = $(window).height();
        var winW = $(window).width();

        $(id).css('top', $(this).parent().offset().top - 140);
        $(id).css('left', $(this).offset().left  - $(window).scrollLeft() + $(this).parent().width());

        $(id).show();
      });

      $('.pop_up .close').click(function (e) {
        e.preventDefault();
        $('#mask, .pop_up').hide();
      });

      $('#mask').click(function () {
        $(this).hide();
        $('.pop_up').hide();
      });



      var name = "#cmi1";
      var menuYloc = null;
      var duration = 300;
      var offset = 0;
      var smaller = true;
      var lastScrollTop = 0;
      var difference = 0;

      $('#cmi1 li a').click(function() {
        $('#cmi1 li a').removeClass('active');
        $(this).addClass('active');
      });
    if($(name).length < 1) {
       menuYloc = 0;
    } else {
      menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")));      
    }
     
      smaller = (($(name).height() + menuYloc) < $(window).height()) ? true : false;
      difference = $(name).height() - $(window).height(); // toliko gleda cez

      $(window).scroll(function() {
        var scrolled = $(window).scrollTop(); 
        var st = $(this).scrollTop();     
        if(smaller) { // the menu is smaller than viewport, no special functionality needed.
          if(scrolled > menuYloc) {
            offset = $(document).scrollTop(); // 20 + 
            $(name).stop().animate({top:offset},{duration:duration,queue:false});
          } else {
            $(name).stop().animate({top:menuYloc},{duration:duration,queue:false});
          }
        } else {
          if (st > lastScrollTop){ // going down
            bottom = $(name).offset().top + $(name).height();
            offset = scrolled + $(window).height();      
            var position = $(document).scrollTop() - difference - 20; // - 20
            if(offset > bottom) {
              $(name).stop().animate({top:position},{duration:duration,queue:false});
            }
          } else { // going up
            offset = $(name).offset().top;      
            position = scrolled; // + 20
            if(offset > scrolled) {
              if(position > menuYloc){
                $(name).stop().animate({top:position},{duration:duration,queue:false});
              } else {
                $(name).stop().animate({top:menuYloc},{duration:duration,queue:false});
              }
            }
          }
          lastScrollTop = st;    
        }       
      });
      // end

  });
})(jQuery);

