/*jslint browser:true */
/*global $, jQuery, alert*/

function mplStyleGallery(table) {
    'use strict';

    var ESCAPE_KEY = 27,
        imageDatabase = {};

    function createImageDatabase(table) {
        // Transform gallery table to an image database
        //
        // The image database returns image urls when indexed as:
        //
        //    imageDatabase[<style-name>][<type-name>]

        var row = [],
            style = '',
            row = [],
            cell = {},
            imageDatabase = {};

        for (var iRow = 0; iRow < table.length; iRow++) {
            style = table[iRow][0];
            row = table[iRow][1];

            imageDatabase[style] = {}
            for (var iCell = 0; iCell < row.length; iCell++) {
                cell = row[iCell];
                imageDatabase[style][cell.type] = cell.url;
            }
        }

        return imageDatabase;
    }

    imageDatabase = createImageDatabase(table)

    $(document).ready(function () {
        // Replace anchor targets with lighbox display

        var $lightbox = $('#lightbox');

        // Click on any lightbox-viewable to open lightbox window.
        $('a.lightbox-viewable').click(function (event) {

            var $anchor = $(this),                  // clicked anchor
                imageHref = $anchor.attr('href'),  // clicked link target
                imageUrls = {},
                $thumbnailDiv = $('#lightbox-thumbnails'),
                clickedImg = $anchor.children('img')[0],
                style = clickedImg.getAttribute('data-style');

            // Prevent default (hyperlink) action
            event.preventDefault();

            // Set lightbox image source to the clicked image.
            $('#content img').attr('src', imageHref);

            // All image URLs for the selected style.
            imageUrls = imageDatabase[style]

            for (var type in imageUrls) {
                console.log(imageUrls[type])
            }

            $lightbox.show();

        });

        // Press ESCAPE to hide the lightbox window.
        $(document).keyup(function (event) {
            if (event.keyCode == ESCAPE_KEY) {
                $lightbox.hide();
            }
        });

    });
};

@@ -158,809 +158,8 @@
 %7D;%0A%0A
-    function createImageDatabase(table) %7B%0A        // Transform gallery table to an image database%0A        //%0A        // The image database returns image urls when indexed as:%0A        //%0A        //    imageDatabase%5B%3Cstyle-name%3E%5D%5B%3Ctype-name%3E%5D%0A%0A        var row = %5B%5D,%0A            style = '',%0A            row = %5B%5D,%0A            cell = %7B%7D,%0A            imageDatabase = %7B%7D;%0A%0A        for (var iRow = 0; iRow %3C table.length; iRow++) %7B%0A            style = table%5BiRow%5D%5B0%5D;%0A            row = table%5BiRow%5D%5B1%5D;%0A%0A            imageDatabase%5Bstyle%5D = %7B%7D%0A            for (var iCell = 0; iCell %3C row.length; iCell++) %7B%0A                cell = row%5BiCell%5D;%0A                imageDatabase%5Bstyle%5D%5Bcell.type%5D = cell.url;%0A            %7D%0A        %7D%0A%0A        return imageDatabase;%0A    %7D%0A%0A    imageDatabase = createImageDatabase(table)%0A%0A
     
@@ -433,78 +433,8 @@
 var 
-$anchor = $(this),                  // clicked anchor%0A                
 imag
@@ -442,22 +442,22 @@
 Href = $
-anchor
+(this)
 .attr('h
@@ -461,17 +461,17 @@
 ('href')
-,
+;
   // cli
@@ -490,219 +490,8 @@
 rget
-%0A                imageUrls = %7B%7D,%0A                $thumbnailDiv = $('#lightbox-thumbnails'),%0A                clickedImg = $anchor.children('img')%5B0%5D,%0A                style = clickedImg.getAttribute('data-style');
 %0A%0A  
@@ -697,210 +697,8 @@
 );%0A%0A
-            // All image URLs for the selected style.%0A            imageUrls = imageDatabase%5Bstyle%5D%0A%0A            for (var type in imageUrls) %7B%0A                console.log(imageUrls%5Btype%5D)%0A            %7D%0A%0A
     

Remove unused image database