/*
 * jQuery functions for flyover menus and balancing of up to 3 columns
 *
 * Copyright 2009-2010 by COMMUNITOR Internetservice GmbH
 * All rights reserved.
 * http://www.communitor.com/
 */
(function(a){a.flyoverMenu=function(c,g,b,d,e){if(typeof g==="undefined"){g="aHover"}if(typeof b==="undefined"){b="flyover_menu_sub"}if(typeof d==="undefined"){d="sub_"}if(typeof e==="undefined"){e=6}var f=a(c).outerWidth(true);a.flyoverMenu.timer=null;a.flyoverMenu.over=function(){var j=a(this).attr("id"),i,h;a.flyoverMenu.closeAll();a(this).addClass(g);if(j==""){return}i=a("#"+d+j);if(i.length){if(!i.hasClass(b)){i.addClass(b).mouseover(a.flyoverMenu.cancel).mouseout(a.flyoverMenu.out);a.flyoverMenu.balanceColumns(i);h=a(this).position();h.top+=a(this).outerHeight(true);width=i.outerWidth(true);if(h.left+width>f){h.left=f-width}i.css(h)}i.show()}};a.flyoverMenu.out=function(){a.flyoverMenu.cancel();a.flyoverMenu.timer=setTimeout(a.flyoverMenu.closeAll,250)};a.flyoverMenu.closeAll=function(){a.flyoverMenu.cancel();a("."+b).hide();a(c+" ."+g).removeClass(g)};a.flyoverMenu.cancel=function(){if(a.flyoverMenu.timer){clearTimeout(a.flyoverMenu.timer)}};a(document).ready(function(){a(c+" > ul > li").mouseover(a.flyoverMenu.over).mouseout(a.flyoverMenu.out)});a.flyoverMenu.balanceColumns=function(h){var j,q=3,o=[],r={},n=[];h=h.get(0);a.each([".firstcol",".secondcol",".thirdcol"],function(s,t){t=a(t,h).get(0);if(t){n.push(a(t))}});r.base={cols:1,per_col:e,total:0};a(n[0]).children("li").each(function(t,s){j=1+a("li",s).length;a(s).attr("li_length",j);o.push(j);r.base.total+=j;if(j>r.base.per_col){r.base.per_col=j}});r[1]={per_col:r.base.total,diff:0};r[2]=a.flyoverMenu.getTwoBalancedColumns(r.base.total,r.base.per_col,o.slice());if(r[1].per_col-r[2].per_col<e){r.base.cols=1}else{r[3]=a.flyoverMenu.getThreeBalancedColumns(r.base.total,r.base.per_col,o);if(r[2].per_col>=30||r[3].coeff<r[2].coeff){r.base.cols=3}else{r.base.cols=2}r.base.per_col=r[r.base.cols].per_col;var m=0,l=0,k=parseInt(a(n[0]).css("width"));if(!isNaN(k)){k+=a(n[m]).outerWidth(true)}a(n[0]).children("li").each(function(t,s){j=parseInt(a(s).attr("li_length"));if(l+j>r.base.per_col&&m<2){n[++m].show();l=0;if(!isNaN(k)){k+=parseInt(a(n[0]).css("width"))+a(n[m]).outerWidth(true)}}l+=j;if(m>0){n[m].append(s)}});if(!isNaN(k)){a(h).width(k)}}for(var p=0;p<r.base.cols;++p){n[p].append(a("<div/>").addClass("clearer"))}};a.flyoverMenu.getTwoBalancedColumns=function(o,n,l){var m,h=0,k={per_col:n,diff:-1},i,j;k.per_col=Math.max(Math.ceil(o/2),k.per_col);while(m=l.shift()){if(h+m<=k.per_col){h+=m}else{i=Math.abs(2*(h+m)-o);j=Math.abs(2*h-o);if(i<=j){k.per_col=h+m;k.diff=i}else{k.per_col=Math.max(h,o-h,k.per_col);k.diff=Math.max(k.per_col-h,k.per_col-(o-h))}break}}k.coeff=k.per_col-Math.ceil(o/2)+k.diff;return k};a.flyoverMenu.getThreeBalancedColumns=function(o,n,l){var m,h=0,k={per_col:n,diff:-1},j,i;k.per_col=Math.max(Math.ceil(o/3),k.per_col);while(m=l.shift()){if(h+m<=k.per_col){h+=m}else{if(2*(h+m)>=o){l.unshift(m);j=a.flyoverMenu.getTwoBalancedColumns(o-h,k.per_col,l);k.per_col=j.per_col;k.diff=Math.max(k.per_col-h,j.diff)}else{j=a.flyoverMenu.getTwoBalancedColumns(o-h-m,h+m,l.slice());l.unshift(m);i=a.flyoverMenu.getTwoBalancedColumns(o-h,k.per_col,l.slice());if(j.per_col<i.per_col){k.per_col=j.per_col;k.diff=Math.max(k.per_col-(h+m),j.diff)}else{if(j.per_col==i.per_col){if(j.diff<=i.diff){k.per_col=j.per_col;k.diff=Math.max(k.per_col-(h+m),j.diff)}else{k.per_col=i.per_col;k.diff=Math.max(k.per_col-h,i.diff)}}else{k.per_col=i.per_col;k.diff=Math.max(k.per_col-h,i.diff)}}}break}}k.coeff=k.per_col-Math.ceil(o/3)+k.diff;k.per_col=k.per_col;return k}}})(jQuery);
