﻿/* mercNavigation.js - 07/25/2007 15:10 */

/* 
   This script provides navigation functionality for the online
   web comic "The Mercurials" http://www.themercurials.net
*/

// Initialize script-wide variables
var pageAdjustment = 0;
var maxAdjustment = 0;
var srcStub = "";
var contentImg = "";
var pageNo = "";
var pageNoV = "";

// Defines variables and events AFTER the page finishes loading.
//
window.onload = initFrames; 

// Defines variables and events
//
function initFrames()
{
    // Variables for the Horizontal Navigation bar
    //
    var nextPage = document.getElementById("nextPage");
    var backPage = document.getElementById("backPage");
    var firstPage = document.getElementById("firstPage");
    var lastPage = document.getElementById("lastPage");

    // Variables for the Vertical Navigation Bar
    //
    var nextPageV = document.getElementById("nextPageV");
    var backPageV = document.getElementById("backPageV");
    var firstPageV = document.getElementById("firstPageV");
    var lastPageV = document.getElementById("lastPageV");

    contentImg = document.getElementById("content");
    pageNo = document.getElementById("pageNo");
    pageNoV = document.getElementById("pageNoV");

    // The value for total pages is written by the page's initial
    // PHP script into a hidden <div> for retreaval by this script
    //
    var totalPages = document.getElementById("totalPages");
    maxAdjustment = totalPages.innerHTML;

    // Setting functions for events originating in the Horizontal Navbar
    //
    nextPage.onclick = goNext; 
    backPage.onclick = goBack; 
    firstPage.onclick = goFirst; 
    lastPage.onclick = goLast; 

    // Setting functions for events originating in the Vertical Navbar
    //
    nextPageV.onclick = goNext; 
    backPageV.onclick = goBack; 
    firstPageV.onclick = goFirst; 
    lastPageV.onclick = goLast; 

    // Instantiating the Drag and Drop On Top object from the Yahoo
    // User Interface open-source library = Horizontal navbar
    //
    var dd1 = new YAHOO.example.DDOnTop("navBar");
    dd1.setHandleElId("mover");

    // Instantiating the Drag and Drop On Top object from the Yahoo
    // User Interface open-source library = Vertical navbar
    //
    var dd2 = new YAHOO.example.DDOnTop("navBarV");
    dd2.setHandleElId("moverV");
}

// Add zeroes to the current page number (pageAdjustment) to get a 
// three-digit string with leading zeroes.  This matches the naming schema
// for the image files.
//
function addZeroes()
{
    var tempNumber = new String(pageAdjustment);

    while (tempNumber.length<3)
    {
        tempNumber = "0" + tempNumber;
    }

    return tempNumber;

}

// After a navigation link is clicked, this function gets a three-digit string
// version of the current page number (pageAdjustment), then uses that to build
// a src and tag for the main image, adding it to the content <div>.  Then it 
// creates the new page numbwer display for the navigation bars.
//
function changeImage()
{
    var adjustWithZeroes = addZeroes();
  
    contentImg.innerHTML = "<img src=\"" + srcStub + adjustWithZeroes + ".jpg\" \/>";
    pageNo.innerHTML = pageAdjustment + " of " + maxAdjustment;
    pageNoV.innerHTML = pageAdjustment + " of " + maxAdjustment;
}

// Process the NEXT navigation link.  Uses the link's HREF property as the stub for the
// main image's url (used by changeImage()); increases the current page number by one 
// (if it's less than the maximum number of pages); and calls the changeImage function.
//
function goNext()
{
    srcStub = this.href;
    
    if (pageAdjustment<maxAdjustment)
    {
        pageAdjustment++;
    }

    changeImage();
   
    return false;
}


// Process the BACK navigation link.  Uses the link's HREF property as the stub for the
// main image's url (used by changeImage()); decreases the current page number by one 
// (if it's greater than zero); and calls the changeImage function.
//
function goBack()
{
    srcStub = this.href;

    if (pageAdjustment>0)
    {
        pageAdjustment--;
    }

    changeImage();   
    return false;
}

// Process the FIRST navigation link.  Uses the link's HREF property as the stub for the
// main image's url (used by changeImage()); changes the current page number to zero 
// (the first page of each episode); and calls the changeImage function.
//
function goFirst()
{
    srcStub = this.href;

    pageAdjustment = 0;

    changeImage();

    return false;
}

// Process the LAST navigation link.  Uses the link's HREF property as the stub for the
// main image's url (used by changeImage()); changes the current page number to equal 
// the last page (maxAdjustment); and calls the changeImage function.
//
function goLast()
{
    srcStub = this.href;

    pageAdjustment = maxAdjustment;

    changeImage();

    return false;
}
