$(document).ready(function(){
    // Initialize navigation
    initializeNavigation();

    // Initialize modal windows 
    initializeModalWindows();
    
    // Search input
    $('.search_input').click(function(){
        $(this).focus();
        $(this).select();
    });
});

function initializeNavigation() {
    
    // Test for MSIE x.x;
//    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
        // Capture x.x portion and store as a number
//        var ieversion=new Number(RegExp.$1)
//    }
//    if (ieversion<=7) {
        // Do not initilize the navigation menu if the browser is MSIE version 7 or earlier
//    } else{
    // When the mouse enters a navigation anchor, make the respective navigation panel visible
    $("#main-navigation ul li a").mouseenter(
        function() {
            $(this).parent().find("div").css("visibility", "visible");
        }
    );
    // When the mouse leaves a navigation anchor, hide the respective navigation panel
    $("#main-navigation ul li a").mouseleave(
        function() {
            $(this).parent().find("div").css("visibility", "hidden");
        }
    );
    // When the mouse enters a visible navigation panel, maintain its visibility
    $("#main-navigation .main-navigation-panel").mouseenter(
        function() {
            $(this).css("visibility", "visible");
            // Maintain the :hover style of the panel's sibling anchor by appending .selected to it
            $(this).prev("a").addClass("selected");
        }
    );
    // When the mouse leaves a visible navigation panel, hide that panel
    $("#main-navigation .main-navigation-panel").mouseleave(
        function() {
            $(this).css("visibility", "hidden");
            // Remove the :hover style of the panel's sibling anchor by removing .selected from it
            $(this).prev("a").removeClass("selected");
        }
    );
//    }
}

function initializeModalWindows(){
    // Determine width and height of the window
    var browserWindowWidth = $(window).width();
    var browserWindowHeight = $(window).height();
    var documentHeight = $(document).height();
    $('#modal-window-page-mask').css({ 'width' : browserWindowWidth, 'height' : documentHeight });
    // Center each modal window
    $('.modal-window').each(
        function(){
            // Determine width and height of the modal window
            var modalWindowWidth = $(this).outerWidth();
            var modalWindowHeight = $(this).outerHeight();
            // Calculate the left and top offsets
            var left = (browserWindowWidth-modalWindowWidth)/2;
            var top = (browserWindowHeight-modalWindowHeight)/2;
            // Apply left and top values to CSS
            $(this).css({'top' : top , 'left' : left});
        }
    );
    $('.activate-modal-window').click(
        function(){
            // Retrieve the modal window's id from the name value of the activating element
            var modalWindowID = $(this).attr('name');
            // Pass the modal window's id to the showModalWindow function
            showModalWindow(modalWindowID);
        }
    );
    $('.close-modal-window').click(
        function(){
            closeModalWindows();
        }
    );
    $(window).resize(
        function(){
            // Determine width and height of the window
            browserWindowWidth = $(window).width();
            browserWindowHeight = $(window).height();
            documentHeight = $(document).height();
            $('#modal-window-page-mask').css({ 'width' : browserWindowWidth, 'height' : documentHeight });
            // Center each modal window
            $('.modal-window').each(
                function(){
                    // Determine width and height of the modal window
                    modalWindowHeight = $(this).outerHeight();
                    modalWindowWidth = $(this).outerWidth();
                    // Calculate the left and top offsets
                    var top = (browserWindowHeight-modalWindowHeight)/2;
                    var left = (browserWindowWidth-modalWindowWidth)/2;
                    // Apply left and top values to CSS
                    $(this).css({'top' : top , 'left' : left});
                }
            );
        }
    );
}
function closeModalWindows(){
    // Hide the mask
    $('#modal-window-page-mask').fadeOut(250);
    // Hide any modal windows
    $('.modal-window').fadeOut(250);
}
function showModalWindow(modalWindowID){
    // Set display to block and opacity to 0
    $('#modal-window-page-mask').css({ 'display' : 'block', opacity : 0});
    // Fade mask to opacity 0.8
    $('#modal-window-page-mask').fadeTo(250,0.8);
    // Show the modal window
    $('#'+modalWindowID).fadeIn(250);
}
