///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Global Variables.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// For expandable/collapsable text.
var EXPANDED_CHAR = '-';
var COLLAPSED_CHAR = '+';

// Seperates menu items.
var MENU_SEPERATOR_CHAR = '|';

// The menu items.
var MENU_ITEMS = new Array("Home",
                           "Articles",
                           "Tank Journals",
                           "Calculators",
                           "Snapshots",
                           "Links");

var MENU_LINKS = new Array("/index.htm",
                           "/Contents/Articles/index.htm",
                           "/Contents/Tank Journal/index.htm",
                           "/Contents/Calculators/index.htm",
                           "/Contents/Snapshots/index.htm", 
                           "/Contents/Links/index.htm");

var MENU_DESCR = new Array("The front page of Hamza's Reef.",
                           "A list of articles and reviews that I have written.",
                           "A list of my marine aquariums and their individual journals.",
                           "An index of some useful aquarium calculators.",
                           "An index of images that have been on my front page.",
                           "A collection of useful links I have collected over time.");

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Date/Time Functions.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function set_midnight(date)
{
    date.setHours(0);
    date.setMinutes(0);
    date.setSeconds(0);
    date.setMilliseconds(0);
    return date;
}

function days_between(former, latter)
{
    var f = set_midnight(former).getTime();
    var l = set_midnight(latter).getTime();
    var days_ago = Math.floor( (l - f) / (1000 * 60 * 60 * 24) );
    return days_ago;
}

function months_between(former, latter)
{
    var months_ago = latter.getMonth() + (12 - (former.getMonth()));
    var corrected_year = former.getFullYear() + 1;
    if (corrected_year <= latter.getFullYear())
        months_ago += (latter.getFullYear() - corrected_year) * 12;
    return months_ago;
}

function numeric_to_text_month(numeric_month)
{
    var month_array = new Array( "January", 
                                 "February",
                                 "March",
                                 "April",
                                 "May",
                                 "June",
                                 "July",
                                 "August",
                                 "September",
                                 "October",
                                 "November",
                                 "December" );
    return month_array[numeric_month];
}

function last_updated()
{
    // Compute the number of days that have passed since the last update.
    var lm = new Date(document.lastModified);
    var days_ago = days_between(lm, new Date());
    
    // Compute the string date.
    var string_date = numeric_to_text_month(lm.getMonth()) + " " + 
                      lm.getDate() + ", " + 
                      lm.getFullYear();
    
    // Write the date the website was last updated (centered).
    document.writeln("<h5>");
    document.writeln("    <div align=\"center\">");
    
    if (days_ago == 0)
        document.writeln("        This page was last updated today (<i>" + string_date + "</i>)<br />");
    else if (days_ago == 1)
        document.writeln("        This page was last updated yesterday (<i>" + string_date + "</i>)<br />");
    else
        document.writeln("        This page was last updated " + days_ago + " days ago on <i>" + string_date + "</i><br />");
    
    document.writeln("    </div>");
    document.writeln("</h5>");
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Footer Functions.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function standard_footer()
{
    // We use an invisible table to provide a two-column look-and-feel.
    document.writeln("<table border=\"0\" width=\"100%\" callpadding=\"0\">");
    document.writeln("    <tr>");
    
    // Write the left column.
    document.writeln("        <td width=\"50%\" valign=\"top\">");
    document.writeln("            <h5>");
    document.writeln("                Copyright © 2010 Muhammad H. Arian<br />");
    document.writeln("                <i>My images</i> may be used freely with attribution.<br />");
    document.writeln("            </h5>");
    document.writeln("        </td>");
    
    // Write the right column.
    document.writeln("        <td width=\"50%\" valign=\"top\">");
    document.writeln("            <h5>");
    document.writeln("                <div align=\"right\">");
                                          contact_information();
    document.writeln("                </div>");
    document.writeln("            </h5>");
    document.writeln("        </td>");
    
    // End the table.
    document.writeln("    </tr>");
    document.writeln("</table>");
    
    // Write the date the website was last updated (centered).
    last_updated();
    
    // Make all the blog entries open by default.
    open_all_blog_entries();
    
    // Write the Brinkster Site Statistics code.
    write_brinkster_site_statistics();
}

function open_all_blog_entries()
{
    var all_divs = document.getElementsByTagName("div");
    
    for (var i = 0; i < all_divs.length; i++)
    {
        if (all_divs[i].className == "controlling_div")
            all_divs[i].onclick();
    }
}

function calculator_footer()
{
    // We use an invisible table to provide a two-column look-and-feel.
    document.writeln("<table border=\"0\" width=\"100%\" callpadding=\"0\">");
    document.writeln("    <tr>");
    
    // Write the left column.
    document.writeln("        <td width=\"50%\" valign=\"top\">");
    document.writeln("            <h5>");
    document.writeln("                Copyright © 2010 Muhammad H. Arian<br />");
    document.writeln("                These calculators are constantly being updated.<br />");
    document.writeln("                If you find any bugs, <b>please let me know</b>.<br />");
    document.writeln("            </h5>");
    document.writeln("        </td>");
    
    // Write the right column.
    document.writeln("        <td width=\"50%\" valign=\"top\">");
    document.writeln("            <h5>");
    document.writeln("                <div align=\"right\">");
                                          contact_information();
    document.writeln("                </div>");
    document.writeln("            </h5>");
    document.writeln("        </td>");
    
    // End the table.
    document.writeln("    </tr>");
    document.writeln("</table>");
    
    // Write the date the website was last updated (centered).
    last_updated();
    
    // Write the Brinkster Site Statistics code.
    write_brinkster_site_statistics();
}

function write_brinkster_site_statistics()
{
    // COPYRIGHT (C) 2002-2005, Brinkster Site Statistics Corp.
    
    var pagetitle = document.title;
    var action = "";
    var amount = "0";
    var order = ""; 

    var scriptlocation = "/stats/track.asp";

    var pagedata = 'mtpt=' + escape(pagetitle) + 
                   '&mtac=' + escape(action) + 
                   '&mta=' + amount + 
                   '&mto=' + escape(order) + 
                   '&mtr=' + escape(document.referrer) + 
                   '&mtt=2&mts=' + window.screen.width + 'x' + window.screen.height + 
                   '&mti=1&mtz=' + Math.random(); 
    
    document.write ('<img height=1 width=1 ');
    document.write ('src="' + scriptlocation + '?' + pagedata + '">');
}

function contact_information()
{
    // Store e-mail address in reverse and dynamically flip it.
    var direct_contact_reversed = ">a/<yltcerid>\"moc.liamg" + "" + "@" + "" + "azmaheht:otliam\"=ferh a<";
    var direct_contact = "";
    for (var i = direct_contact_reversed.length - 1; i >= 0; i--)
        direct_contact += direct_contact_reversed.charAt(i);
    
    // Write contact information.
    document.writeln("For more information, please contact me " + direct_contact + " or as:<br />");
    document.writeln("\"TheH\" on <a href=\"http://www.marshreef.com\">www.marshreef.com</a><br />");
    document.writeln("\"TheH\" on <a href=\"http://www.reefcentral.com\">www.reefcentral.com</a><br />");
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Header Functions.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function standard_header()
{
    // Write the opening.
    document.writeln("<h4>");
    document.writeln("    <div align=\"right\">");
    
    // Write the individual menu elements.
    for (var i = 0; i < MENU_ITEMS.length; i++)
    {
        document.writeln("        <a href=\"" + MENU_LINKS[i] + "\"");
        document.writeln("           onMouseOver=\"show_menu_subtitle(" + i + ")\"");
        document.writeln("           onMouseOut =\"hide_menu_subtitle()\"");
        document.writeln("        >" + MENU_ITEMS[i] + "</a> ");
        
        if ((i + 1) < MENU_ITEMS.length)
            document.writeln(MENU_SEPERATOR_CHAR);
    }
    
    // Write the menu subtitle span.
    document.writeln("        <br />");
    document.writeln("        <span class=\"menu_subtitle\" id=\"menu_subtitle\">&nbsp;</span>");
    
    // Write the closing.
    document.writeln("    </div>");
    document.writeln("</h4>");
}

function show_menu_subtitle(nSelected)
{
    document.getElementById("menu_subtitle").innerHTML = MENU_DESCR[nSelected];
}

function hide_menu_subtitle()
{
    document.getElementById("menu_subtitle").innerHTML = "&nbsp;";
}            

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Expand/Collapse Functions.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Display/Hide the controlled_div and update controlling_div.
function toggle(controlling_div, controlled_div)
{
    var current_style = controlled_div.style;
        
    if ((current_style.display == 'none') || (current_style.display == ''))
    {    
        current_style.display = 'block';
        controlling_div.innerHTML = controlling_div.innerHTML.replace(COLLAPSED_CHAR, EXPANDED_CHAR);
    }
    else
    {   
        current_style.display = 'none';
        controlling_div.innerHTML = controlling_div.innerHTML.replace(EXPANDED_CHAR, COLLAPSED_CHAR);
    }
}

// Display/Hide the controlled_div (with no update to a controlling_div).
function toggle_no_controller(controlled_div, bDisplay)
{
    if (bDisplay == true)
        controlled_div.style.display = 'block';
    else
        controlled_div.style.display = 'none';
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// End.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////