box.store('lop').addModule('main-menu', function($, box) {
    var isInDOM = false,
        isReplaced = false,
        isOpened = false,
        sHTML, replaceMenu, oMenu;
    
    replaceMenu = function() {
        if(isInDOM && sHTML && !isReplaced) {
            $('#main-menu').replaceWith(sHTML);
            
            box.get('ui').create('mask.mainmenumask', {
                html: '<div id="main-menu-mask"></div>',
                insertStyles: { opacity: 0, width: 'viewport:content-box', height: 'document:content-box' },
                insertAnimStyles: { opacity: 0.8 },
                insertAnimDuration: 200,
                removeAnimStyles: { opacity: 0 },
                removeAnimDuration: 150,
                resizeStyles: { width: 'viewport:content-box', height: 'document:content-box' }
            });
            
            box.subscribe('addtodom>ui:mask.mainmenumask', function() {
                this.element.click(function() {
                    oMenu.close();
                });
            });
            
            $('#header').click(function(oEvt) {
                if(isOpened && (oEvt.target === this || oEvt.target.parentNode === this)) {
                    oMenu.close();
                }
            });
            
            $('#main-menu div.expand-footer a.close').click(function(oEvt) {
                oEvt.preventDefault();
                oMenu.close();
            });
            
            $('#main-menu li.category').each(function() {
                var jElm = $(this),
                    sId = this.id;
                
                if(jElm.find('div.expand').length) {
                    jElm.find('a.title').click(function(oEvt) {
                        oEvt.preventDefault();
                        oMenu.open(sId);
                    });
                    
                    if(jElm.find('div.expand-header').length) {
                        var jLi = jElm.find('div.expand-header li.on'),
                            oLink = jLi.find('a.subtitle')[0];
                        oMenu.currentSubmenuLink[sId] = jLi;
                        
                        if(oLink.hash && box.dom(oLink.hash).length) {
                            oMenu.currentSubmenuExpand[sId] = box.dom(oLink.hash);
                        }
                        
                        jElm.find('div.expand-header a.subtitle').click(function(oEvt) {
                            if(this.hash && box.getDoc().getElementById(this.hash.substring(1))) {
                                oEvt.preventDefault();
                                var jLi = box.dom(this.parentNode),
                                    jExpand = box.dom(this.hash);
                                oMenu.currentSubmenuLink[sId].removeClass('on');
                                if(oMenu.currentSubmenuExpand[sId]) {
                                    oMenu.currentSubmenuExpand[sId].removeClass('on');
                                }
                                oMenu.currentSubmenuLink[sId] = jLi.addClass('on');
                                oMenu.currentSubmenuExpand[sId] = jExpand.addClass('on');
                            }
                        });
                        
                        $('div.expand-header li').hover(function() {
                            $(this).addClass('hovered');
                        }, function() {
                            $(this).removeClass('hovered');
                        });
                    } else {
                        oMenu.currentSubmenuExpand[sId] = jElm.find('div.expand-body div.on');
                    }
                    
                    $(jElm).find('div.expand-category').each(function() {
                        var jListingElm = $(this),
                            sListingId = this.id;
                        
                        if(jListingElm.find('div.expand-category-menu').length) {
                            var jLi = jListingElm.find('div.expand-category-menu li.active'),
                                oLink = jLi.find('a')[0];
                            oMenu.currentSubmenuListingLink[sListingId] = jLi;
                            
                            if(oLink.hash && box.dom(oLink.hash).length) {
                                oMenu.currentSubmenuListingExpand[sListingId] = box.dom(oLink.hash);
                            }
                            
                            jListingElm.find('div.expand-category-menu a').click(function(oEvt) {
                                if(this.hash && box.getDoc().getElementById(this.hash.substring(1))) {
                                    oEvt.preventDefault();
                                    var jLi = box.dom(this.parentNode),
                                        jExpand = box.dom(this.hash);
                                    oMenu.currentSubmenuListingLink[sListingId].removeClass('active');
                                    if(oMenu.currentSubmenuListingExpand[sListingId]) {
                                        oMenu.currentSubmenuListingExpand[sListingId].removeClass('active');
                                    }
                                    oMenu.currentSubmenuListingLink[sListingId] = jLi.addClass('active');
                                    oMenu.currentSubmenuListingExpand[sListingId] = jExpand.addClass('active');
                                }
                            });
                        } else {
                            oMenu.currentSubmenuListingExpand[sListingId] = jListingElm.find('div.expand-body div.active');
                        }
                    });
                }
            });
            
            isReplaced = true;
        }
    };
    
    oMenu = {
        currentSubmenuLink: {},
        currentSubmenuExpand: {},
        currentSubmenuListingLink: {},
        currentSubmenuListingExpand: {},
        
        isInDOM: function() {
            return isInDOM;
        },
        
        isLoaded: function() {
            return !!sHTML;
        },
        
        isReplaced: function() {
            return isReplaced;
        },
        
        open: function(sId) {
            var jLi = box.dom('#' + sId),
                jExpand = jLi.find('div.expand');
            
            if(isOpened) {
                if(oMenu.currentItem[0] === jLi[0]) {
                    oMenu.close();
                } else {
                    oMenu.currentItem.find('div.expand').animate({
                        'opacity': 0
                    }, 200, function() {
                        oMenu.currentItem.removeClass('expanded');
                        jLi.addClass('expanded');
                        jExpand.css('opacity', '0').animate({ opacity: 1 }, 500, function() {
                            oMenu.currentItem = jLi;
                            isOpened = true;
                        });
                    });
                }
            } else {
                box.get('ui:mask.mainmenumask').show();
                jLi.addClass('expanded');
                jExpand.css('opacity', '0').animate({ opacity: 1 }, 500, function() {
                    oMenu.currentItem = jLi;
                    isOpened = true;
                });
            }
        },
        
        close: function() {
            if(oMenu.currentItem) {
                oMenu.currentItem.find('div.expand').animate({
                    'opacity': 0
                }, 500, function() {
                    var oMask = box.get('ui:mask.mainmenumask');
                    oMask.element.unbind('click');
                    oMask.hide();
                    oMenu.currentItem.removeClass('expanded');
                    oMenu.currentItem = null;
                    isOpened = false;
                });
            }
        }
    };
    
    $.ajax({
        url: l10n.url.mainMenu,
        success: function(sResponse) {
            sHTML = sResponse;
            replaceMenu();
        }
    });
    
    box.getJDoc().ready(function() {
        if(this.getElementById('main-menu')) {
            isInDOM = true;
            replaceMenu();
        }
    });
    
    return oMenu;
});

box.getJDoc().ready(function() {
    if(this.getElementById('header-logout')) {
        box.dom('#header-logout').click(function(e) {
            e.preventDefault();
            LOP.wsUserLogout({
                onSuccess: function() {
                    box.dom('#account-menu-vip div.actions').html('<a href="' + box.get('l10n:accountMenu').signInURL + '">' + box.get('l10n:accountMenu').signInText + '</a> <a href="' + box.get('l10n:accountMenu').registerURL + '" class="special-link">' + box.get('l10n:accountMenu').registerText + '</a>');
                    box.dom('#islogged').val('false');
                }
            });
        });
    }
    
    if(this.getElementById('account-menu-vip')) {
        box.dom('#account-menu-vip').hover(function() {
            box.dom(this).addClass('hovered');
        }, function() {
            box.dom(this).removeClass('hovered');
        });
    }
});
