/**
 * @version $Id: djmegamenu.js 24 2014-05-27 11:02:15Z szymon $
 * @package DJ-MegaMenu
 * @copyright Copyright (C) 2013 DJ-Extensions.com, All rights reserved.
 * @license DJ-Extensions.com Proprietary Use License
 * @author url: http://dj-extensions.com
 * @author email contact@dj-extensions.com
 * @developer Szymon Woronowski - szymon.woronowski@design-joomla.eu
 */
(function($){var j=this.DJMegaMenu=function(a,b){this.options={delay:500,animIn:'fadeIn',animOut:'fadeOut',animSpeed:'normal',duration:450,wrap:null,direction:'ltr',event:'mouseenter',touch:(('ontouchstart'in window)||(navigator.MaxTouchPoints>0)||(navigator.msMaxTouchPoints>0))};this.init(a,b)};j.prototype.init=function(c,d){var e=this;e.options=Object.merge(e.options,d);if(!c)return;e.options.direction=window.getComputedStyle(document.body).getPropertyValue('direction');switch(e.options.animSpeed){case'fast':e.options.duration=250;break;case'slow':e.options.duration=650;break}c.addClass(e.options.animSpeed);var f=c.getChildren('li.dj-up');e.kids=[];if(!e.options.wrap)e.options.wrap=c;else e.options.wrap=$(e.options.wrap);f.each(function(a,b){e.kids[b]=new k(a,0,e,e.options)});if(e.options.fixed&&!e.options.touch){window.addEvent('load',e.makeSticky.bind(e,c))}};j.prototype.makeSticky=function(a){var b=this;b.sticky=false;var c=new Element('div',{id:a.get('id')+'sticky'});c.addClass('dj-megamenu');c.addClass('dj-megamenu-sticky');c.setStyles({position:'fixed',top:b.options.offset,left:0,width:'100%'});var d=a.getPosition().y-b.options.offset;var e=a.clone(true);e.set('id',a.get('id')+'placeholder');e.setStyle('opacity',0);var f=b.options.direction=='rtl'?'right':'left';window.addEvent('scroll',b.scroll.bind(b,[c,a,e,d,f,false]));window.addEvent('resize',b.scroll.bind(b,[c,a,e,d,f,true]))};j.prototype.scroll=function(a,b,c,d,e,f){var g=this;if(window.getScroll().y>d){if(!g.sticky){var h=b.getCoordinates();var i=e=='left'?h.left:window.getSize().x-h.left-h.width;b.setStyle(e,i);c.inject(b,'before');a.wraps(b);g.sticky=true}else if(f){var h=c.getCoordinates();var i=e=='left'?h.left:window.getSize().x-h.left-h.width;b.setStyle(e,i)}}else if(g.sticky){b.replaces(c);b.setStyle(e,'');a.dispose();g.sticky=false}};var k=function(a,b,c,d){this.options={};this.init(a,b,c,d)};k.prototype.init=function(a,b,c,d){var f=this;f.options=Object.merge(f.options,d);f.menu=a;f.level=b;f.parent=c;f.timer=null;f.sub=a.getElement('.dj-subwrap');var g='mouseenter';if(f.options.touch||f.options.event=='click_all'){g='click';var h=a.getElement('a');if(h&&h.getParent()==a){if(a.hasClass('separator'))h.setStyle('cursor','pointer');h.addEvent('click',function(e){if(f.sub&&!f.menu.hasClass('hover'))e.preventDefault()})}}else if(f.options.event=='click'&&a.hasClass('separator')){var h=a.getElement('a');if(h&&h.getParent()==a)h.setStyle('cursor','pointer');g='click'}f.menu.addEvent(g,f.showSub.bind(f));f.menu.addEvent('mouseleave',f.hideSub.bind(f));if(f.sub){f.kids=[];f.initKids()}};k.prototype.showSub=function(){var a=this;clearTimeout(a.timer);if(a.menu.hasClass('hover')&&!a.sub.hasClass(a.options.animOut)){return}clearTimeout(a.animTimer);a.menu.addClass('hover');if(a.sub&&!a.DirDone)a.checkDir();a.hideOther();if(a.sub){a.sub.removeClass(a.options.animOut);a.sub.addClass(a.options.animIn)}};k.prototype.hideSub=function(){var a=this;if(a.sub){a.timer=setTimeout(function(){a.sub.removeClass(a.options.animIn);a.sub.addClass(a.options.animOut);a.animTimer=setTimeout(function(){a.menu.removeClass('hover')},a.options.duration)},a.options.delay)}else{a.menu.removeClass('hover')}};k.prototype.checkDir=function(){var a=this;a.DirDone=true;var b=a.sub.getCoordinates();var c=a.options.wrap.getCoordinates();if(a.options.wrap.hasClass('dj-megamenu')){var d=$(a.options.wrap.get('id')+'placeholder');if(d)c=d.getCoordinates()}if(a.options.direction=='ltr'){var e=b.left+b.width-c.width-c.left;if(e>0){if(a.level){a.sub.setStyle('right',a.menu.getSize().x);a.sub.setStyle('left','auto')}else{a.sub.setStyle('margin-left',-e)}}}else if(a.options.direction=='rtl'){var e=b.left-c.left;if(e<0){if(a.level){a.sub.setStyle('left',a.menu.getSize().x);a.sub.setStyle('right','auto')}else{a.sub.setStyle('margin-right',e)}}}};k.prototype.initKids=function(){var c=this;var d=c.sub.getChildren('.dj-subwrap-in > .dj-subcol > ul.dj-submenu > li');d.each(function(a,b){c.kids[b]=new k(a,c.level+1,c,c.options)})};k.prototype.hideOther=function(){var b=this;b.parent.kids.each(function(a){if(a.menu.hasClass('hover')&&a!=b){if(a.sub){a.hideOtherSub();a.sub.removeClass(a.options.animIn);a.sub.addClass(a.options.animOut);a.animTimer=setTimeout(function(){a.menu.removeClass('hover')},b.options.duration)}else{a.menu.removeClass('hover')}}})};k.prototype.hideOtherSub=function(){var b=this;b.kids.each(function(a){if(a.sub){a.hideOtherSub();a.sub.removeClass(a.options.animIn);a.sub.removeClass(a.options.animOut)}a.menu.removeClass('hover')})}})(document.id);