/*#------------------------------------------------------------------------ # Template: Braincode Clean # Copyright: (C) 2007-2012 Braincode.it. All Rights Reserved. # License: GNU General Public License version 2 or later; see LICENSE.txt # Website: http://www.braincode.it # Support: info@braincode.it #------------------------------------------------------------------------ */ var bcMegaMenu = new Class({ Implements: [Options, Events], options: { menu:'bf-megamenu', hideInt: 500, considerShadow:false, findBlock:'main' }, initialize: function(options){ this.setOptions(options); this.menu=$(this.options.menu); this.opened=[]; this.alreadyLoaded=false; this.elements=null; this.templateMainBlock=this.getMainBlock(); this.prepareMenu(); }, getMainBlock: function(){ if(!this.menu) return null; var parent=this.menu; while(parent=parent.getParent()){ if(parent.hasClass(this.options.findBlock)){ return parent; } } return parent; }, prepareMenu: function(){ if(!this.menu) return false; //menu not exists var images = this.menu.getElements('img'); if(images.length){ // preload images for fix img height var imgSrc=[]; images.each(function(element){ imgSrc.push(element.src); }) new Asset.images(imgSrc, { onComplete: function(){ this.startMenu(); }.bind(this) }); this.startMenu.delay(3500, this); }else{ this.startMenu(); } }, startMenu : function(){ if(this.alreadyLoaded) return; //prevent multiple initialize of this function this.alreadyLoaded=true; this.elements=this.menu.getElements('li.hasChilds'); this.elements.each (function(el) { el.wr1=el.getElement('.bf-mega-submenu'); el.wr2=el.wr1.getElement('.bf-mega-submenu-wrap'); el.wr3=el.wr2.getElement('.bf-mega-submenu-inner'); el.width= this.itemWidth(el.wr3); el.height=this.itemHeight(el.wr3); el.isTop=(el.getParent().hasClass('level-0')) ? true : false; el.wr1.setStyles ({'width':el.width , 'height':el.height}); el.wr2.setStyles ({'width':el.width}); el.wr1.setStyles ({'left':'auto','overflow':'hidden'}) if(el.isTop){ el.wr2.setStyles ({'margin-top': - el.height}); }else{ el.wr1.setStyles({'margin-left': el.getSize().x - 20}); el.wr2.setStyle ('margin-left', - el.width); } el.menufx = new Fx.Morph (el.wr2, {duration: 300, transition: Fx.Transitions.linear, onComplete: function(){ this.animation(el)}.bind(this)}); el.op = {}; el.cl = {}; if (el.isTop) { el.op ['margin-top'] = 0; el.cl ['margin-top'] = -el.height; }else{ el.op['margin-left'] = 0; el.cl['margin-left'] = -el.width; } el.timeout = null; el.addEvent('mouseenter', function(e) { clearTimeout (el.timeout); this.showEl(el); }.bind(this)); el.addEvent('mouseleave',function(){ el.timeout = this.hideEl.delay(this.options.hideInt, this, el); }.bind (this)); }.bind(this)); this.menu.getElements('.bf-mega-submenu').setStyle('display','none'); }, setDirection : function(el){ el.wr1.setStyle('display','block'); var position = el.getCoordinates(); var mc=this.templateMainBlock.getCoordinates(); var tmpW=mc.width;//(this.templateMainBlock.getSize().x > window.getWidth())? this.templateMainBlock.getSize().x : window.getWidth() ; var shadowOffset=(this.options.considerShadow) ? 10 : 0; if(el.isTop){ var limit=mc.left+mc.width; var elLimit=position.left + el.width + shadowOffset; if((elLimit ) > (limit)) { var moveLeft=((elLimit) - limit ); el.wr1.setStyles({'margin-left': - moveLeft}); }else{ el.wr1.setStyles({'margin-left': ''}); } }else{ var limit=mc.left+mc.width; var elLimit=position.left + position.width + el.width + shadowOffset; if((elLimit ) > (limit)) { var moveLeft=el.width; //el.wr1.setStyles({'left': 'auto','margin-left': - moveLeft}); el.wr1.setStyles({'margin-left': - moveLeft}); }else{ //el.wr1.setStyles({'right': ''}); } } }, itemWidth: function(item){ var width=item.getSize().x; width += item.getStyle('padding-left').toInt() + item.getStyle('padding-right').toInt() + item.getStyle('border-left').toInt()+ item.getStyle('border-right').toInt(); var parent=item; while (parent=parent.getParent()) { if (parent.hasClass ('bf-mega-submenu')){ break; }else{ width += parent.getStyle('padding-left').toInt() + parent.getStyle('padding-right').toInt() + parent.getStyle('border-left').toInt()+ parent.getStyle('border-right').toInt(); } } return width; }, itemHeight: function(item){ var height=item.getSize().y; height += item.getStyle('padding-top').toInt() + item.getStyle('padding-bottom').toInt() + item.getStyle('border-top').toInt() + item.getStyle('border-bottom').toInt(); var parent=item; while (parent=parent.getParent()) { if (parent.hasClass ('bf-mega-submenu')){ break; }else{ height += parent.getStyle('padding-top').toInt() + parent.getStyle('padding-bottom').toInt() + parent.getStyle('border-top').toInt() + parent.getStyle('border-bottom').toInt(); } } return height; }, showEl: function(item){ clearTimeout(item.timeout); if(item.status == 'open') return; item.status = 'open'; this.opened.push(item); this.hideEls(item); this.setDirection(item); item.wr1.setStyle ('display', 'block'); item.wr1.setStyles ({'overflow': 'hidden'}); item.menufx.cancel(); item.menufx.start(item.op); }, hideEls: function (el) { this.opened.each (function(li) { try{ if (((li != el && !li.hasChild(el)))) { this.hideEl(li); } }catch(e){ //console.log(li); } }.bind(this)); }, hideEl: function (li) { clearTimeout(li.timeout); li.status = 'close'; this.opened.erase(li); li.wr1.setStyles ({'overflow': 'hidden'}); li.menufx.cancel(); li.menufx.start(li.cl); }, animation: function(li){ if (li.status == 'close'){ li.wr1.setStyles ({'display': 'none'}); }else if (li.status == 'open'){ li.wr1.setStyles ({'overflow': ''}); li.wr1.setStyles ({'display': 'block'}); } }, equalizeColumns: function(li){ var cols=li.getElements('.colonna'); var r=this.getChildRow(cols,li); if(r.length > 1){ setMinHeight(r); } }, getChildRow: function(els,li){ var childs=[]; if(!els.length) return false; els.each(function(item){ var parent=item; var add = true; while (parent=parent.getParent()) { if((parent.get('tag')=='li' && parent!=li)){ add=false; } } if(add){ childs.push(item); } }.bind(this)); return childs; } }) window.addEvent('domready',function(){ var mega=new bcMegaMenu({menu:'bf-megamenu',findBlock:'anief-header-bottom',considerShadow:false}); })