
// These variables are later overridden by IE6/7 conditional comments.

var ie6 = false;
var ie7 = false;

// Preload images to speed up mouseover effect.

preload("/static/interface/menu/background_hover.png");
preload("/static/interface/menu/introduction_hover.png");
preload("/static/interface/menu/programs_hover.png");
preload("/static/interface/menu/newsletters_hover.png");
preload("/static/interface/menu/gallery_hover.png");
preload("/static/interface/menu/library_hover.png");
preload("/static/interface/menu/books_hover.png");
preload("/static/interface/menu/international_hover.png");
preload("/static/interface/menu/directions_hover.png");
preload("/static/interface/footer_hover.png");

// Assign all event handlers.

$(document).ready(function()
{
    // Remove top border from first content area.
    
    $("div.content").first().css("border-top", "0px");
    
    // Top-level Menu hover effect.
    
    $("#header_menu div.topmenu").hover(
        function()
        {
            var img = $(this).find("img").first();
            img.attr("src", img.attr("src").replace(".png", "_hover.png"));
            $(this).css("background", (ie6 ? "#0c1014" : "transparent url('/static/interface/menu/background_hover.png') top left no-repeat"));
            $("#sub" + $(this).attr("id")).fadeIn(100);
        },
        function()
        {
            var img = $(this).find("img").first();
            img.attr("src", img.attr("src").replace("_hover.png", ".png"));
            $(this).css("background", "transparent");
            $("#sub" + $(this).attr("id")).fadeOut(100);
        }
    );
    
    // Submenu hover effect.
    
    $("#header_menu div.submenu ul li").hover(
        function() { $(this).find("p.ko a").css("color", "#f3e100"); },
        function() { $(this).find("p.ko a").css("color", "#ffffff"); }
    );
    
    // Permanently highlight current menu item.
    
    $("#header_menu div.topmenu").each(function()
    {
        // If this is an error page, don't do anything.
        
        var errpage = $("#error_404");
        if (errpage.length) return;
        
        // Get the current page's URL, minus any query strings.
        
        var url = window.location.pathname;
        var urlparts = url.split("/");
        if (url == "/") return;
        
        // Get the URL assigned to this top-level menu.
        
        var menulink = $(this).find("a").first().attr("href");
        var menulinkparts = menulink.split("/");
        
        // If this menu's URL belongs to the same top-level as the current page's URL...
        
        if ((menulinkparts[1] != "etc" && menulinkparts[1] == urlparts[1])
            || (url == "/etc/directions" && menulink == url)
            || (url == "/etc/international" && menulink == url))
        {
            // Highlight the image and display submenu (copied from above).
            
            var img = $(this).find("img").first();
            img.attr("src", img.attr("src").replace(".png", "_hover.png"));
            $(this).css("background", (ie6 ? "#0c1014" : "transparent url('/static/interface/menu/background_hover.png') top left no-repeat"));
            $("#sub" + $(this).attr("id")).show();
            
            // Temporarily hide submenu when another top-level menu is shown.
            
            var submenu = $("#sub" + $(this).attr("id"));
            var submenu_fade_timeout;
            $("#header_menu div.topmenu").hover(
                function() { clearTimeout(submenu_fade_timeout); submenu.fadeOut(100); },
                function() { submenu_fade_timeout = setTimeout(function() { submenu.fadeIn(100); }, 50); }
            );
            
            // Remove unnecessary events from this top-level menu.
            
            $(this).unbind("mouseenter mouseleave");
            
            // Highlight matching submenu item, and remove unnecessary events from it.
            
            $(this).find("p.ko a[href='" + url + "']").each(function()
            {
                $(this).css("color", "#f3e100");
                $(this).parent().parent().unbind("mouseenter mouseleave");
            });
        }
    });
    
    // Allow navigation by mouse click in the general vicinity of a menu link.
    
    $("#header_menu div.submenu ul li").each(function()
    {
        var url = $(this).find("p.ko a").first().attr("href");
        $(this).find("p.en").wrapInner('<a href="' + url + '" />');
        $(this).click(function() { window.location.href = url; });
    });
    
    $("#footer_links ul li").each(function()
    {
        var url = $(this).find("a").first().attr("href");
        $(this).find("p.en").wrapInner('<a href="' + url + '" />');
        $(this).click(function() { window.location.href = url; });
    });
    
    // Attach events to gallery thumbnails.
    
    $("#gallery a").click(function(event)
    {
        if (ie6) return;  // Mwahaha!
        
        var fullsize_url = $(this).attr("href");
        var width = parseInt($(this).parent().find("span.width").text(), 10);
        var height = parseInt($(this).parent().find("span.height").text(), 10);
        lightbox(event, fullsize_url, width, height);
    });
    
    // Fix various issues with IE6/IE7.
    
    if (ie6)
    {
        // Remove menu transparency.
        
        $("div.submenu > div.background").remove();
        $("div.submenu").css("background", "#203040");
        
        // Fix cropped Korean text when line-height is 100%.
        
        $("div.content h3").css("margin-top", "1px");
        $("#footer_contact p.ko").css("margin-top", "1px").css("position", "relative").css("top", "-1px");
        
        // Fix footer links hover effect.
        
        $("#footer_links ul li").hover(
            function() { $(this).css("background", "transparent url('/static/interface/footer_hover.png') top left repeat-x"); },
            function() { $(this).css("background", "transparent"); }
        );
    }
    
    if (ie6 || ie7)
    {
        // Adjust margins at bottom of content panels.
        
        $("div.content").css("margin-bottom", "-12px");
        $("#footer").css("margin-top", "0px");
        
        // Adjust margins for gallery.
        
        $("#gallery").css("padding-top", "0px").css("padding-bottom", "20px");
    }
    
});

// Reset all menus on page unload.

$(window).unload(function()
{
    $("div.submenu").hide();
});

// Preload image function.

function preload(url)
{
    var img = document.createElement("img");
    img.src = url;
}

// Image lightbox function.

function lightbox(event, image_url, image_width, image_height)
{
    // Determine the size and location of the background and foreground boxes.
    
    var image_ratio = image_width / image_height;
    var window_width = $(window).width();
    var window_height = $(window).height();
    
    var lightbox_width = image_width;
    var lightbox_height = image_height;
    if (lightbox_width > window_width - 60)
    {
        lightbox_width = window_width - 60;
        lightbox_height = lightbox_width / image_ratio;
    }
    if (lightbox_height > window_height - 60)
    {
        lightbox_height = window_height - 60;
        lightbox_width = lightbox_height * image_ratio;
    }
    
    var lightbox_left = (window_width - lightbox_width) / 2;
    var lightbox_top = (window_height - lightbox_height) / 2;
    
    // Construct the lightbox.
    
    var lightbox_background = $('<div id="lightbox_background" style="position: fixed; left: 0; top: 0; opacity: 0.72; filter: alpha(opacity = 70); background-color: #000000; z-index: 10"></div>');
    lightbox_background.width(window_width);
    lightbox_background.height(window_height);
    lightbox_background.click(function() { $("#lightbox_background").remove(); $("#lightbox_foreground").remove(); });
    
    var lightbox_foreground = $('<div id="lightbox_foreground" style="position: fixed; z-index: 11"></div>');
    lightbox_foreground.css({ "left": (lightbox_left - 14) + "px", "top": (lightbox_top - 14) + "px" });
    
    var lightbox_image = $('<image id="lightbox_image" style="clear: both; margin: 0; border: 14px solid #000000" />');
    lightbox_image.width(lightbox_width);
    lightbox_image.height(lightbox_height);
    lightbox_image.attr("src", image_url);
    lightbox_image.click(function() { $("#lightbox_background").remove(); $("#lightbox_foreground").remove(); });
    lightbox_image.appendTo(lightbox_foreground);
    
    var lightbox_xbutton = $('<image src="/static/interface/x-button.png" style="cursor: pointer" alt="" />');
    lightbox_xbutton.css({"position": "absolute", "top": "-3px", "right": "-3px"});
    lightbox_xbutton.click(function() { $("#lightbox_background").remove(); $("#lightbox_foreground").remove(); });
    lightbox_xbutton.appendTo(lightbox_foreground);
    
    // Display the lightbox.
    
    lightbox_background.appendTo($("body"));
    lightbox_foreground.appendTo($("body"));
    
    // Attach events to window resize.
    
    $(window).resize(function()
    {
        var window_width = $(window).width();
        var window_height = $(window).height();
    
        var lightbox_width = image_width;
        var lightbox_height = image_height;
        if (lightbox_width > window_width - 60)
        {
            lightbox_width = window_width - 60;
            lightbox_height = lightbox_width / image_ratio;
        }
        if (lightbox_height > window_height - 60)
        {
            lightbox_height = window_height - 60;
            lightbox_width = lightbox_height * image_ratio;
        }
        
        var lightbox_left = (window_width - lightbox_width) / 2;
        var lightbox_top = (window_height - lightbox_height) / 2;
    
        $("#lightbox_background").width(window_width).height(window_height);
        $("#lightbox_foreground").css({ "left": (lightbox_left - 14) + "px", "top": (lightbox_top - 14) + "px" });
        $("#lightbox_image").width(lightbox_width).height(lightbox_height);
    });
    
    // Prevent the default action (page navigation).
    
    event.preventDefault();
}

