/*
    The Manila Observatory Navigation Script
    by Arielle B Cruz <http://www.abcruz.com>
    
    This file contains functions that modify the default styling and behaviour
    of the website's main navigation links (i.e., About Us, Research, etc.).
    Basically, the sub-menus are hidden on page load and they are faded-in when
    the user puts focus on the main section heading in the nav— just for effect.
    
    This script requires jQuery.
    
    Created: 04 September 2011 by abc
    Last Modified: ----

*/

// Settings.
var anim_panel = 800;     // Dashboard fadeIn animation delay.
var anim_subnavs = 400;   // Sub-navigation links fadeIn delay.

var panel_active = 0;
var panel_delay = 5000;
var panel_interval = null;
var panel_length = 0;

function open_editor(table_name, record_id) {
    /* Opens the TMS quick access editor. */
    props = "status=0,toolbar=0,menubar=0,scrollbars=1,height=600,width=720";
    uri = "/cgi-bin/static_editor.py?t=";
    uri += table_name + "&r=" + record_id;
    window.open(uri, "editor_window", props);
}

function prep_articles() {
    /* Initiator for the article-highligher in the homepage. */
    $("#mo-articlehighs-source").css("display", "none");
    panel = $("#mo-articlehighs-panel");
    subpanel = $('<div id="mo-articlehighs-subpanel"></div>');
    nav = $("#mo-articlehighs-nav");
    src = $("#mo-articlehighs-source").find("li");
    panel_length = src.length;
    // Don't set up if we have no source articles.
    if (src.length > 0) {
        panel.append($('<a href="#" id="mo-articlehighs-link"><img id="mo-articlehighs-img" src="/res/article-image.jpg" alt="Feature image." /></a>'));
        panel.append(subpanel);
        subpanel.append($('<div id="mo-articlehighs-title">Loading... </div>'));
        subpanel.append($('<div id="mo-articlehighs-desc">Loading... </div>)'));
        for (var i=0; i<src.length; i++) {
            var ttl = $($(src[i]).find("span")[0]).text().split(" ");
            if (ttl.length > 3) {
                ttl = [ttl[0], ttl[1], ttl[2]].join(" ") + "...";
            } else {
                ttl = ttl.join(" ");
            }
            var a = $('<a href="javascript:show_article(' +i+ ', true);">' +ttl+ '</a>')
            a.addClass("norm");
            nav.append(a);
        }
        // Bind an onload event to the image.
        $("#mo-articlehighs-img").bind("load", function() {
            panel.fadeIn(anim_panel);
        });
        // Show the first panel.
        show_article(0);
    }
    // If we have more than one highlighted article, start the interval.
    if (panel_length > 1) panel_interval = setInterval(rotate_article, panel_delay + anim_panel);
}

function prep_navs() {
    /*
        Put a mouseover and focus function on the divs that contain the section
        links, then hide everything. Called on document.ready.
    */
    var navs = $("body > nav > ul > li");
    // Put a down-arrow on menu items that have sub-menus.
    for (var i=0; i<navs.length; i++) {
        var sub_nav = $(navs[i]).find("ul").length;
        if (sub_nav) $($(navs[i]).find("div > a")[0]).addClass("with-subs");
    }
    // Add mouseover and focus events to each main menu item.
    $("body > nav > ul > li div a").mouseover( function() { show_subnavs(this); });
    $("body > nav > ul > li div a").focus( function() { show_subnavs(this); });
    $("body > div").mouseover( function() { reset_navs(); });
    // Hide all sub-menus.
    reset_navs();
}

function reset_navs() {
    // Hides all sub-navigation menus. Called by prep_navs and show_subnavs.
    $("body > nav > ul > li ul").css({
        "display": "none",
        "position": "absolute",
        "z-index": 100,
    });
}

function rotate_article() {
    /* Called at intervals in the home page; flips article highlights. */
    if (panel_active < panel_length-1) panel_active += 1;
    else panel_active = 0;
    show_article(panel_active, false);
}

function show_article(num, stop_interval) {
    /* Home page: shows the summary of the selected article. */
    panel = $("#mo-articlehighs-panel");
    navs = $("#mo-articlehighs-nav").find("a");
    src = $($("#mo-articlehighs-source").find("li")[num]).find("span");
    // Let's set all nav links to white, then highlight the active link.
    for (var i=0; i<navs.length; i++) {
        if (i==num) $(navs[i]).addClass("shown");
        else $(navs[i]).removeClass("shown");
    }
    panel.fadeOut(anim_panel, function() {
        // We need to determine if the Read More link will open a new window.
        if ($(src[2]).text().substring(0,4) == "http") {
            $("#mo-articlehighs-link").attr("target", "_blank");
        } else {
            $("#mo-articlehighs-link").removeAttr("target");
        }
        $("#mo-articlehighs-link").attr("href", $(src[2]).text());
        $("#mo-articlehighs-title").text($(src[0]).text());
        $("#mo-articlehighs-desc").html($(src[1]).text());
        $("#mo-articlehighs-img").attr("src", $(src[3]).text());
        if (stop_interval) clearInterval(panel_interval);
    });
}

function show_subnavs(which_li) {
    // Shows any sub-navigation list when a section link is focused.
    reset_navs();
    var ul = $($(which_li).parent().parent()).find("ul");
    if (ul.length > 0) $(ul[0]).fadeIn(anim_subnavs);
}


