$(function() {
var toggle = function(direction, display) {
       return function() {
         var self = this;
         var ul = $("ul", this);
         var oself = $(self).offset();
         ul.css({
             top:($(self).outerHeight() + oself.top)+'px',
             left:(oself.left)+'px'
           });
         if( ul.css("display") == display && !self["block" + direction] ) {
           self["block" + direction] = true;
           ul["slide" + direction]("fast", function() {
               self["block" + direction] = false;
             });
         }
       };
     }
     $("ul.menu li").hover(toggle("Down", "none"), toggle("Up", "block"));


$(document).bind('epdb:mepsready',function (e, elm) {
    var groups = $.data(elm,"groups");
    var countries = $.data(elm,"countries");
    var meps = $.data(elm,"meps");

  /* {{{ dropdown suggestion search */
  $('.mepsearch').suggest({
    minchar: 3,
    input: 'input',
    itext: 'MEP search...',
    select: function(o) {
        $mep = $("."+o.hclass+":first",o.hits);
      if (typeof $mep.attr('mepnr') == "undefined") {
        //not found
      }
      else {
        if (o.self.attr('url').charAt(0) == 'm' && o.self.attr('url').charAt(1) == 'v') { getMepVote(o.self.attr('url'),meps[$mep.attr('mepnr')].id); }
        else {
          document.location.href = $.sprintf(o.self.attr('url'),meps[$mep.attr('mepnr')].id);
        }
        o.input.val('').trigger('blur');
        o.hide(o);
      }

    },
    filter: function (o) {
      var lmeps = meps.length
      var result = new Array();
      var offSet = o.input.offset();
      o.hits.css({
          position: "absolute",
          top: offSet.top + o.input.outerHeight() + "px", left: offSet.left,
          width: 'auto', height: 'auto',
          zIndex: o.zindex
      });

      var r = eval('/'+o.value+'/ig');
      $.each(meps, function (i,mep) {
        if(res = mep.name.match(r)) {
          res = res+'';
          if(res.indexOf(',')>0) { res = res.substr(0,res.indexOf(',')) }
          result.push(
            '<div class="'+o.sclass+'" mepnr="'+i+'" style="background-image: url(/img/meps/st'+mep.id+'.jpg);">'+
            mep.name.replace(r,'<em>'+res+'</em>')+'<small>'+(countries[mep.cid].name)+', '+groups[mep.gid].name+'</small>'+
            '</div>'
          );
        }
      });
      if(result.length > 0 ) {
        while (result.length > 15) result.pop(); //show max 15 results at the time

        o.hits.html(result.join(''));
        if(o.hits.outerWidth() > o.input.outerWidth()) {
          o.hits.css('left',(offSet.left+2) - (o.hits.outerWidth()-o.input.outerWidth())+'px');
        } else {
          o.hits.css('width',o.input.outerWidth() + "px");
        } 

        $("div."+o.sclass+":first-child",o.hits).addClass(o.hclass);

        if(o.hits.height() >600) {
          o.hits.css({
            height:600+'px',
            overflow:'auto',
            width:(o.hits.outerWidth()+20)+'px',
            left:(offSet.left+2) - (o.hits.outerWidth()+20-o.input.outerWidth())+'px'
          });
        }

        var $list = $('div.'+o.sclass,o.hits);

        $list.bind("mouseover", function() {
          $list.removeClass(o.hclass);
          $(this).addClass(o.hclass);
        });
        $list.bind("mouseout", function () {
          $list.removeClass(o.hclass);
        });
        $list.bind("click", function () {
          o.select(o);
        });
      }
      else { 
        o.hits.html('<div class="error">None found</div>').
        css('width',o.input.outerWidth() + "px");
      }
    }
  });
  /* }}} dropdown suggestion search */
});
 });
