// JavaScript Document
//$.noConflict();
//(function($) {
  $(document).ready(function(){

    /*** first-last elements ***/

    $("ul").each(function(){
      $("li:first" ,this).addClass("first");
      $("li:last" ,this).addClass("last");
    });

    /*** END ***/


    /*** dropdown menu ***/
    var dd_timeout_value = 300;
    var dd_mainitem_out_timeout = null;
    var dd_mainitem_out_item = null;
    function dd_mainitem_out() {
      var $this = dd_mainitem_out_item;
      if ($this === null) {
        return;
      }
      var index = $this.parent().children('li').index($this);
      var ddi = $dropdowns_items.get(index);

      $this.removeClass('item-hover');
      $this.removeClass("item-hover-first");
      $this.next().removeClass("item-hover-next");
      if (typeof(ddi) != 'undefined') {
        var $ddi = $(ddi);
        $ddi.css({
          top :-9999,
          left:-9999
        });
      }
    }
    var $dropdowns = $('<div id="dropdown-menu-wrapper"></div>');
    $('body').append($dropdowns);
    $("div.block-bothead-menu > ul > li > h2 > a").each(function(index){
      var $a = $(this);
      $ul = $a.parent().next('ul');
      if ($ul.length > 0) {
        if ($ul.hasClass('dd-menu-region')) {
          var $wrap = $('<div id="dropdown-menu-wrapper-'+ index +'" class="dropdown-menu-item dd-menu-region-item"></div>');
        }
        else {
          var $wrap = $('<div id="dropdown-menu-wrapper-'+ index +'" class="dropdown-menu-item"></div>');
        }
        if (!$ul.hasClass('noborder')) {
			$wrap.append('<div class="block-dropdown"></div>');
		}
        $wrap.append($ul);
        $dropdowns.append($wrap);
      }
    });
    var $dropdowns_items = $dropdowns.children('div');
    $dropdowns_items.each(function(){
      var $this = $(this);
      var $ul = $this.children('ul');
      var $le = $this.children('div.block-dropdown');
      $le.width(
        $ul.width()
      );
    });

    $('li', $dropdowns).each(function(){
      var $this = $(this);
      var $a = $this.children('a');
      $a.width(
        parseInt($this.width())
        - parseInt($a.css('paddingLeft'))
        - parseInt($a.css('paddingRight'))
      );
    });

    $dropdowns_items.hover(
      function() {
        clearTimeout(dd_mainitem_out_timeout);
      },
      function(){
        dd_mainitem_out_timeout = setTimeout(function(){
          dd_mainitem_out();
        }, dd_timeout_value);
      }
    );

    $('li', $dropdowns).hover(
      function(e){
        var $this = $(this);
        $this.addClass("item-hover");
        if ($this.hasClass('first')) {
          $this.addClass('item-hover-first');
        }
        $(this).find('ul').css({
          visibility: 'visible'
        });

        var $child_ul = $this.children('ul');
        if ($child_ul.length > 0) {
          var $main = $('div.block-bothead-menu');

          var main_width = parseInt($main.width());
          var main_left  = parseInt($main.offset().left);
          var this_left  = parseInt($this.offset().left);
          var this_width = parseInt($this.width());
          var chul_width = parseInt($child_ul.width());

          if (main_left + main_width <= this_left + this_width + chul_width) {
            var $parent_ul = $this.parents('ul:last');
            $parent_ul.addClass('dropdown-ul-fright');
            $child_ul.css('left', -1 * $child_ul.width());
          }
        }
        clearTimeout(dd_mainitem_out_timeout);
      },
      function(e){
        var $this = $(this);
        $this.removeClass("item-hover");
        if ($this.hasClass('first')) {
          $this.removeClass('item-hover-first');
        }
        $this.find('ul').css({
          visibility: 'hidden'
        });
      }
    );

    $("div.block-bothead-menu ul li").hover(
      function(){
        var $this = $(this);
        if ($this != dd_mainitem_out_item) {
          dd_mainitem_out();
          dd_mainitem_out_item = null;
        }
        $this.addClass('item-hover');
        if ($this.hasClass('first')) {
          $this.addClass("item-hover-first");
        }
        $this.next().addClass("item-hover-next");
        var index = $this.parent().children('li').index($this);
        var ddi = $dropdowns_items.get(index);

        var _height = parseInt($this.height());
        var _left = parseInt($this.offset().left);
        var _top  = parseInt($this.offset().top);

        var $wrapper = $(this).parent();
        var _wleft = _left - parseInt($wrapper.offset().left);
        var _width = $wrapper.width();

        if (typeof(ddi) != 'undefined') {
          var $ddi = $(ddi);
          var width_ddi = $ddi.width();
          var uleft = _left;
          if (_wleft + width_ddi >= _width) {
            uleft = _left - (_wleft + width_ddi - _width);
            $ddi.children('ul').addClass('dropdown-ul-fright');
          }
          $ddi.css({
            top : _top + _height,
            left: uleft + 1
          });
        }
      },
      function(){
        var $this = $(this);
        dd_mainitem_out_item = $this;
        dd_mainitem_out_timeout = setTimeout(function(){
          dd_mainitem_out();
        }, dd_timeout_value);
      }
    );

    /*** END ***/


    /*
     * global
     * add specific class to each last li in the line
     * so we will get something like:
     * ul
     *  li | li | li
     *  li
     *  li | li | li | li
     *
     * with this it is possible to hide background right border for each last li in line
     */

    $('ul.custom-list-lines').each(function(){
      var cuslils = 0;
      $('li', this).each(function() {
        var $this = $(this);
        var left = parseInt($this.offset().left, 10);
        if (left < cuslils) {
          $this.prev().addClass('last-p');
        }
        cuslils = left;
      });
      $('li:last', this).addClass('last-p');
    });


     $('div.block-two-items').each(function(){
           $(this).find('div.block-items').equalize({
              step:2
            });
      });

     $('div.block-custom').each(function(){
           $(this).find('div.eq').equalize({
              step:2
           });
     });


}); // document.ready END

   
//})(jQuery);


