/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * menu.js
 *
 * PHP version 5
 *
 * LICENSE: This source file is subject to version 1.0 of the OpenFlyers
 * license that is available through the world-wide-web at the following
 * URI: http://www.openflyers.com/license/semifreelicense1_0.txt. If you did not receive a
 * copy of the OpenFlyers License and are unable to obtain it through the
 * web, please send a note to contact@openflyers.com so we can mail you
 * a copy immediately.
 *
 * @category    Javascript
 * @package     OpenFlyers
 * @author      Christophe LARATTE <christophe.laratte@openflyers.com>
 * @author      Lydie TREMBLET <lydie.tremblet@openflyers.com>
 * @copyright   2008 OPENFLYERS S.A.R.L. <contact@openflyers.com>
 * @license     http://www.openflyers.com/license/semifreelicense1_0.txt  OpenFlyers License
 * @link       http://www.openflyers.com
 * @since      Wed Nov 19 2008
 */

function submit_menu(action, parameter, tsStartDate, confirm)
{
    var current=document.getElementById("values");
    current.menuAction.value=action;
    current.menuParameter.value=parameter;
    if (arguments.length==3) {
        current.tsStartDate.value=tsStartDate;
    }
    if ((arguments.length==4) && (confirm==true)) {
        if (window.confirm('Confirm ?')) {
           current.submit();
        }
    }
    else {
       current.submit();
    }
}


/**
 * addMinimizeClass()
 *
 * Set .minimize class on small screen
 *
 * @return void
 */
function addMinimizeClass() {
    if ($(window).width() < 1024) {
        $('.main-menu-container').addClass('minimize');
        $('.secondary-bar').addClass('minimize');
        $('.main-menu-container').removeClass('show hide');
        $('.secondary-bar').removeClass('show hide');
    } else {
        $('.main-menu-container').removeClass('minimize');
        $('.secondary-bar').removeClass('minimize');
    }
}

/**
 * changeMenuAtScroll()
 *
 * Changes appearance of menu and other elements at page scroll up or down on large screen
 *
 * @return void
 */
function changeMenuAtScroll() {
    var position = $(window).scrollTop();
    $(window).scroll(function() {
        var scroll = $(window).scrollTop();
        // Scroll down
        if(scroll > position) {
            $('body').removeClass('topBarShow');
            $('.top-bar').removeClass('show');
            $('.main-menu-container').addClass('hide');
            $('.secondary-bar').addClass('hide');
            $('.main-menu-container').removeClass('show');
            $('.secondary-bar').removeClass('show');
            $('html.platform-test:not(.expendableDesign) .platform-test-banner').addClass('hide');
        // Scroll up
        } else {
            $('body').addClass('topBarShow');
            $('.top-bar').addClass('show');
            $('.main-menu-container').removeClass('hide');
            $('.secondary-bar').removeClass('hide');
            // When top-bar is out of viewport
            if($(this).scrollTop() > 45) {
                $('.main-menu-container').addClass('show');
                $('.secondary-bar').addClass('show');
            }
            // When top-bar is in viewport
            if($(this).scrollTop() <= 45) {
                $('.main-menu-container').removeClass('show');
                $('.secondary-bar').removeClass('show');
                $('html.platform-test:not(.expendableDesign) .platform-test-banner').removeClass('hide');
            }
        }
        position = scroll;
    });
}

/**
 * clearOpened()
 *
 * Clear .opened class and other elements related to opened menu on small screen when loading page on large screen
 *
 * @return void
 */
function clearOpened() {
    if ($(window).width() >= 1024) {
        $('.main-menu-container').removeClass('opened');
        $('.main-menu-container .overlay').css('display', 'none');
    }
}

/**
 * closeWithOverlay()
 *
 * Closes menu by clicking on .overlay (small screens)
 *
 * @return opened
 */
function closeWithOverlay() {
    if ($(window).width() < 1024) {
        $('.main-menu-container .overlay').click(function() {
            $('.main-menu-container').removeClass('opened');
            $(this).css('display', 'none');
            $('body').css('overflow-y', 'auto');
            $('body').css('overflow-x', 'auto');
            opened = !opened;
        });
    }
}

/**
* isElementPartiallyHidden()
*
* Test if element is partialy hidden in viewport width
*
* @return boolean
*/
function isElementPartiallyHidden(el) {
   if (typeof jQuery !== 'undefined' && el instanceof jQuery) el = el[0];
   rect = el.getBoundingClientRect();
   windowWidth = (window.innerWidth || document.documentElement.clientWidth);
   partiallyHidden = (rect.right > windowWidth);
   return (partiallyHidden);
}

/**
 * openCloseItems()
 *
 * Show or hide menu items on small screens
 *
 * @return void
 */
function openCloseItems() {
    if ($(window).width() < 1024) {
        $('.main-menu-container .level1 > .item.has-children').removeClass('active');
        $('.main-menu-container .level1 > .item.has-children .level2').css('display', 'none');
        menuWithChildren = $('.main-menu-container.minimize .level1 > .item.has-children');
        menuWithChildren.each(function(index) {
            $(this).off('click');
            $(this).on('click', function() {
                if(!$(this).hasClass('active')) {
                    menuWithChildren.removeClass('active');
                    menuWithChildren.find('.level2').hide();
                    $(this).addClass('active');
                    $(this).find(' > .level2').show();
                } else {
                    $(this).removeClass('active');
                    $(this).find(' > .level2').hide();
                }
            });
        });
    }
    if ($(window).width() >= 1024) {
        $('.main-menu-container .level1 > .item.has-children').removeClass('active');
        $('.main-menu-container .level1 > .item.has-children .level2').css('display', 'block');
    }
}

/**
 * openMenuOnHide()
 *
 * Shows menu when clicking on menu-button when menu is on .hide at page scroll down
 *
 * @return void
 */
function openMenuOnHide() {
    // For large screens
    if ($(window).width() >= 1024) {
        $('.main-menu-container .menu-btn').click(function() {
            $('.main-menu-container').removeClass('hide');
            $('.secondary-bar').removeClass('hide');
            $('.main-menu-container').addClass('show');
            $('.secondary-bar').addClass('show');
        });
    } else {
        $('.main-menu-container .menu-btn').click(function() {
            $('.main-menu-container').removeClass('hide');
            $('.secondary-bar').removeClass('hide');
            $('.main-menu-container').removeClass('show');
            $('.secondary-bar').removeClass('show');
        });
    }
}

/**
 * openMenuSmallScreen()
 *
 * Set class to open and close menu on small screen
 *
 * @return void
 */
function openMenuSmallScreen() {
    if ($(window).width() < 1024) {
        mainMenuContainerMinimize = $('.main-menu-container.minimize');
        opened = false;
        mainMenuContainerMinimize.find('.menu-btn' ).off('click');
        mainMenuContainerMinimize.find('.menu-btn' ).on('click', function() {
            if(opened) {
                mainMenuContainerMinimize.removeClass('opened');
                $('.main-menu-container .overlay').css('display', 'none');
                // When menu is opened, authorize body scrolling
                $('body').css('overflow-y', 'auto');
                $('body').css('overflow-x', 'auto');
            } else {
                mainMenuContainerMinimize.addClass('opened');
                $('.main-menu-container .overlay').css('display', 'block');
                if($(window).scrollTop() <= 45) {
                    $('.main-menu-container .overlay').css('top', 45 - $(window).scrollTop());
                } else {
                    $('.main-menu-container .overlay').css('top', 0);
                }
                // When menu is opened, prevents body scrolling
                $('body').css('overflow-y', 'hidden');
                $('body').css('overflow-x', 'hidden');
            }
            opened = !opened;
        });
    } else if ($(window).width() > 767) {
        $('body').css('overflow', 'auto');
    }
}

/**
 * setItemInViewportClass()
 *
 * Add class to submenu depending on its presence in viewport or not
 *
 * @return void
 */
function setItemInViewportClass(el) {
    elementPartialyHidden = isElementPartiallyHidden(el);
    if(elementPartialyHidden == true) {
        el.addClass('outside-viewport');
    } else {
        el.removeClass('outside-viewport');
    }
}

/**
 * showItemsInViewport()
 *
 * Add class to each submenu depending on its presence in viewport or not
 *
 * @return void
 */
function showItemsInViewport() {
    menuItem = $('.main-menu-container .level1 > .item > .level2.item-list');
    menuItem.each(function(index) {
        setItemInViewportClass($(this));
    });
}

/**
 * Constructor
 *
 * Init code on page load
 *
 * @return void
 */
 $(document).ready(function() {
    // At page load
    clearOpened();
    addMinimizeClass();
    openMenuSmallScreen();
    closeWithOverlay();
    openCloseItems();
    changeMenuAtScroll();
    openMenuOnHide();
    showItemsInViewport();

    // When resizing window
    $(window).resize(function() {
        clearOpened();
        addMinimizeClass();
        openMenuSmallScreen();
        closeWithOverlay();
        openCloseItems();
        changeMenuAtScroll();
        openMenuOnHide();
        showItemsInViewport();
    });

});
