// ClassicGallery JavaScript Function Library      //
// (c) 2005, by Louise Dade - Classical Webdesigns //


// FUNCTION: make popup windows
function makePopup()
{
    if (document.getElementById('thumbnails'))
    {
        var thumbs = document.getElementById('thumbnails');
        var links = thumbs.getElementsByTagName('a');
        var imgs = thumbs.getElementsByTagName('img');

        for (var i=0; i<links.length; i++)
        {
            links[i].onclick = function()
            {
                if (document.getElementById('usepopup').checked)
                {
                    var url = this.getAttribute('href');
                    
                    // RegEx sequences
                    // 1. Replace gallery url with popup url
                    url = url.replace('galleryview.html','gallerypopup.html');

                    // 2. When Mod_Rewrite enabled
                    re = /(gallery\/[0-9]+)\.html/gi; // 
                    url = url.replace(re,'$1popup.html');

                    var options = "width=540, height=450, left=0, top=0, scrollbars=yes, resizable=yes";
                    var gallery=window.open(url, 'Gallery', options);
                    gallery.window.focus();
                    return false;
                }
            }
        }
    }
}

function addToggle()
{
    if (document.getElementById('togglepopup'))
    {
        var tog = document.getElementById('togglepopup');

        tog.innerHTML = "<form id=\"toggleform\">\n<fieldset>\n<legend>Toggle Popup Window</legend>\n"
                + "<p><input type=\"checkbox\" name=\"usepopup\" id=\"usepopup\" value=\"1\" />\n"
                + "<label for=\"usepopup\">Select box to open photos in a popup window</label>\n"
                + "(deselect to cancel popups).</p>\n</fieldset>\n</form>\n\n";
    }
}

function slideNav(key)
{
        var links = document.getElementsByTagName('a');
        var b = document.getElementsByTagName('body');

        for (var i=0; i<links.length; i++)
        {
            if (links[i].getAttribute('accesskey') == "n")
            {
                var next = links[i].getAttribute('href');
            }

            if (links[i].getAttribute('accesskey') == "p")
            {
                var prev = links[i].getAttribute('href');
            }

            if (links[i].getAttribute('accesskey') == "f")
            {
                var first = links[i].getAttribute('href');
            }

            if (links[i].getAttribute('accesskey') == "l")
            {
                var last = links[i].getAttribute('href');
            }

            if (links[i].getAttribute('accesskey') == "c")
            {
                var close = 1;
            }
        }

        if (!key) {
            key = event;
            key.which = key.keyCode;
        }

        if (key.which == 70) {
        //  letter 'f'
            if(first)
            {
                document.location.href = first;
            }
        }

        if (key.which == 76) {
        // letter 'l'
            if(last)
            {
                document.location.href = last;
            }
        }

        if (key.which == 78) {
        // letter 'n'
            if(next) {
                document.location.href = next;
            }
        }

        if (key.which == 80) {
        // letter 'p'
            if(prev)
            {
                document.location.href = prev;
            }
        }

        if (key.which == 67) {
        // letter 'c'
            if(close)
            {
                window.close();
            }
        }
}

// Run script when window has finished loading.
window.onload = function() {
    makePopup(); // Make the popup windows
    addToggle(); // Add the popup toggle
    document.onkeyup = slideNav; // Enable keyboard navigation
}
