﻿/*
* MenuAndContent:        This script is for animated dynamic menu 
*                        and for body content dynamic displaying.
*
* Author:                David Horák 
* Contact:               david.horak@comtesys.cz
* Website:               http://www.comtesys.com
* Date:                  17.03.2011
* 
* Copyright 2011, David Horák
*/
function MenuAndContent() 
{
    // Global variables
    var currentContent;
    var initMenuOffSet;
    var scrollSpeed = 900;

    // Link table
    var linkTable = [];
    linkTable["o-spolecnosti"] = 1;
    linkTable["produkty"] = 2;
    linkTable["sluzby"] = 3;
    linkTable["kariera"] = 4;

    /* Get window Y croll offset ---------------------------------- */
    function getScrollTop() {
        if (typeof pageYOffset != 'undefined') {
            //most browsers
            return pageYOffset;
        }
        else {
            var B = document.body; //IE 'quirks'
            var D = document.documentElement; //IE with doctype
            D = (D.clientHeight) ? D : B;
            return D.scrollTop;
        }
    }

    /* Get Content by anchor ============================================================================================= */
    function GetContentByMenu(menu) {

        // If a required content isn't same like current content  
        if (menu != currentContent) 
        {
            // Set current content 
            currentContent = menu;

            var currentHeight = $("#inner_content").height();
            var currentScrollTop = getScrollTop();
            var currentMenuOffset = $("#menu").offset().top;

            // Jumping size Fix part1
            $("#content").css("height", "3000px");

            // Get content by menu
            $("#inner_content").css("display", "block");
            $("#inner_content").html($("#" + menu).html());

            // Get viewPort and new content Height 
            var viewportHeight = $(window).height();
            var contentHeight = $("#inner_content").height();

            // If content height is less then viewPort height, set footer margin-bottom 
            if (contentHeight < (viewportHeight - 124)) {           
                var margin = (viewportHeight - 124) - contentHeight;  
                $("#footer").css("margin-bottom", margin + "px");
            }
            else {
                $("#footer").css("margin-bottom", "0px");
            }

            // Jumping size Fix part2
            $("#content").css("height", "auto");

        }

        /* Slide a window to menu, if menu isn't slided on top ------------------------------------------------------- */
        if ((getScrollTop() != $("#menu").offset().top) && ($("#menu").offset().top != 0)) {
           
            //Chrome 
            if ($.browser.safari) {
                $('html,body').stop(false, true).animate({ scrollTop: $("#menu").offset().top }, scrollSpeed, "swing", function () {
                    // iPhone iPad fixed position fix part2
                    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
                        mobileFixedFix();
                    }
                });
            }
            //Others
            else {
                $('html,body').stop(false, true).animate({ scrollTop: initMenuOffSet }, scrollSpeed, "swing", function () {
                    // iPhone iPad fixed position fix part2
                    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
                        mobileFixedFix();
                    }
                });
            }

            //Show SideLogo
            $("#sideLogo").animate({ left: 0 }, 300, "swing");
        
        }

    }

    // Fix for fixed position in iPhone,iPad,iPod
    function mobileFixedFix() 
    {
        $("#sideNews").css("top", (iNewsTop + getScrollTop()) + "px");
        $("#sideNewsHover").css("top", (iNewsTop + getScrollTop()) + "px");
        $("#sideNewsContent").css("top", (iNewsTop + getScrollTop()) + "px");
        $("#sideLogo").css("top", (iLogoTop + getScrollTop()) + "px");
    }

    /* ================================================================================================================== */
  
    var h = $(window).height();

    
    // Fixed Side Menu 
    $("#sideLogo").css("top", (h / 2) - 4 + "px");

    // Add click event on side logo
    $("#sideLogo").click(function () {
        $('html,body').stop(false, true).animate({ scrollTop: 0 }, scrollSpeed, "swing", function () {
            // iPhone iPad fixed position fix part2
            if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
                mobileFixedFix();
                $("#sideLogo").animate({ left: -47 }, { queue: false, duration: 300, easing: "swing" });
            }
        });
    });

    // News side bar 
    $("#sideNews").css("top", (h / 2) - 191 + "px");
    $("#sideNews").css("left", "0px");

    // News side bar hover
    $("#sideNewsHover").css("top", (h / 2) - 191 + "px");

    // News side body 
    $("#sideNewsContent").css("top", (h / 2) - 191 + "px");

    // Add News mouse over event
    $("#sideNews").click(function () {
        $("#sideNewsHover").fadeIn(600);
        $("#sideNewsContent").stop(false, true).animate({ left: 47 }, 600);
    });

    // Add News mouse out event    
    $("#sideNewsHover").click(function () {
        $("#sideNewsContent").stop(false, true).animate({ left: -701 }, 600);
        $("#sideNewsHover").fadeOut(600);
    });


    // First message instant
    $("#sideNewsContent_body").html($("#sideNewsContent_container div").first().html());

    // Add animate function for News menu li
    $("#sideNewsContent_menu li").mouseover(function () 
    {
        $(this).stop(false, true).animate({ color: "#b92335" }, 500);

        var index = $("#sideNewsContent_menu li").index($(this)) + 1;
        $("#sideNewsContent_body").stop().fadeTo(300,0, function () {
            $("#sideNewsContent_body").html($('#sideNewsContent_container div.sideNewsContent_container_item:nth-child(' + index + ')').html());
            $("#sideNewsContent_body").fadeTo(300,1.0);
        });

    })
    .mouseout(function () {
        if ($(this).attr("class") != "news_selected")
            $(this).stop(false, true).animate({ color: "#6d6e71" }, 500);
    });


    // iPhone iPad fixed position fix part1
    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
        var iNewsTop = ($("#sideNews").position()).top;
        var iLogoTop = ($("#sideLogo").position()).top;
    }

    // Add Scroll Event on Window, for sideLogo
    $(window).scroll(function () {
        if (getScrollTop() < $("#menu").offset().top) {
            $("#sideLogo").animate({ left: -47 }, { queue: false, duration: 300, easing: "swing" });
        }
        else {
            $("#sideLogo").animate({ left: 0 }, { queue: false, duration: 300, easing: "swing" });
        }

        // iPhone iPad fixed position fix part2
        if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) ) {
            mobileFixedFix();
        }

    });


    initMenuOffSet = $("#menu").offset().top;

    /* Transform links to anchors -------------------------- */
    $(".menu_link").each(function () {
        $(this).attr("href", "#" + $(this).attr("href"));
    });
    /* ---------------------------------------------------- */


    // Get Hash and Query from URL
    var anchor = $.url.attr("anchor");

    // If request is with anchor, call immediately GetContentAndSlide 
    if (anchor != null) {

        anchor = anchor.replace("#", "");

        // Control check for anchor
        if (!linkTable[anchor]) anchor = "o-spolecnosti";
        var link = linkTable[anchor];

        /* Select menu by anchor ======================================================== */
        /* Required: implementation of LavaMenu.js before calling ======================= */
        //reset the selected item
        $('#menu ul li').removeClass('selected');

        // Set selected menu by anchor
        $('#menu ul li:nth-child(' + link + ')').addClass('selected');

        // Set link color
        $('a', '#menu ul li:nth-child(' + link + ')').css('color', '#ffffff');

        //Retrieve the selected item position and width
        var default_left = Math.round($("#menu ul li:nth-child(" + link + ")").offset().left - $('#menu').offset().left);

        //Set the floating bar position and width
        $('#box').css({ left: default_left });
        $('#box').css({ width: $('#menu ul li.selected').width() });
        /* =============================================================================== */

        GetContentByMenu(anchor);
    }

    // Set Click Event on all menu buttons
    $("#menu ul li a").click(function () {

        var parent = $(this).parent();

        //reset the selected item
        $('#menu li').removeClass('selected');

        //select the current item
        parent.addClass('selected');       

        //Get the position and width of the menu item
        left = Math.round(parent.offset().left - $('#menu').offset().left);
        width = parent.width();

        //Set the floating bar position, width and transition
        $('#box').stop(false, true).animate({ left: left, width: width }, 450, "swing");

        $('#menu li a').css("color", "#6d6e71");
        $(this).animate({ color: "#ffffff" }, 200);

        window.location.href = $(this).attr("href");
        GetContentByMenu(($(this).attr("href")).replace("#", ""));
    });

}
/* ========================================================================================= */
