﻿$(document).ready(function () {
    if ($('.left_menu').length != 0) {
        initMenu('.left_menu');
    }

    if ($(".language_select").length != 0) {
        initLanguageSelect();
    }

    loadLightboxes();
});

function initMenu(elementClass) {
    //close everything
    $(elementClass + '>li>ul').hide(); //close "parents"
    $(elementClass + '>li>ul>li>ul').hide(); //close subitems

    //add "icon" classes
    $(elementClass + '>li>a').addClass('open');

    //show the selected item
    if ($(elementClass + ' .selected').length != 0) {
        $(elementClass + ' .selected').parents().show(); //open all parents

        if ($(elementClass + ' .selected').children('ul:first').length > 0) { //open next submenu (if it exists)
            $(elementClass + ' .selected').children('ul:first').show();
        }

        //change "icon" op top parent
        var rootElement = $(elementClass + ' .selected').parents('.level_1');
        rootElement.show();
        rootElement.prev("a").addClass('close');
    }

    //click events
    $(elementClass + '>li>a').click(function () {
        $(elementClass + '>li>ul').slideUp('normal'); //first hide all subs
        $(elementClass + '>li>a').removeClass('close'); //change "icon"
        $(elementClass + '>li>a').addClass('open');

        $(this).next('ul').slideDown('normal'); //then show the one on which we clicked
        $(this).next('ul').prev('a').removeClass('open'); //change "icon"
        $(this).next('ul').prev('a').addClass('close');
    });

    $(elementClass + '>li>ul>li>a').click(function () {
        $(elementClass + '>li>ul>li>ul').slideUp('normal'); //first hide all subs
        $(this).next('ul').slideDown('normal'); //then show the one on which we clicked
    });
}

function initLanguageSelect() {
    //Show only default logo
    $('.logo .block').hide();
    $('.logo .default').show();

    //Show only default text
    $('.language_text .block').hide();
    $('.language_text .default').show();

    $('a').hover(function () {
        var id = this.id;

        var languageId = id.split('_')[1];
        var type = id.split('_')[2];

        var textElement = $("#lang_" + languageId + "_" + type + "_text");
        if (!textElement.is(':visible')) {
            $('#language_text_' + type + ' .block').slideUp('fast');
            textElement.slideDown('fast');
        }

        var logoElement = $("#lang_" + languageId + "_" + type + "_logo");
        if (!logoElement.is(':visible')) {
            $('#header_banner_logo_' + type + ' .block').hide();
            logoElement.show();
        }
    });
}

function loadLightboxes() {
    $("a[rel^='lightbox']").prettyPhoto({
        animationSpeed: 'fast',
        padding: 50,
        opacity: 0.85,
        allowresize: false,
        counter_separator_label: ' / ',
        theme: 'facebook',
        social_tools: '',
        deeplinking: false
    });
}
