/**
 * photoShow - extends fancyBox.js with additional features for use in a custom framework.
 * Author: gihrig - arkadia-systems
 * Date: 7/7/11
 * Time: 1:23 AM
 * Copyright (c) 2011 Arkadia Systems
 * Requires: jQuery v1.3+
 * Requires: jQuery.fancybox v1.3.4+ *
 */

$(document).ready(function() {

    // Slide show image group identifiers from rel attributes
    var groups = [];

    // Select all eligible anchor tags
    var elements = $('a[rel$="_group"]');

    // Build an array of rel attribute values
    $.each(elements, function(index, value) {
        groups[index] = $(value).attr('rel');
    });

    // Filter out duplicates
    $.unique(groups);

    // Build a FancyBox slide show for each a[rel] group
    $.each(groups, function(index, value) {
        $('a[rel=' + value + ']:gt(0)').addClass("visuallyhidden");
        $('a[rel=' + value + ']').fancybox({
            'transitionIn'      : 'none',
            'transitionOut'     : 'none',
            'scrolling'         : 'no',
            'showNavArrows'     : true,
            'titlePosition'     : 'inside',
            'titleFormat'        : function(title, currentArray, currentIndex, currentOpts) {
                var selected = 'navControlSelected';
                var imageRow;
                var html_code;

                // Generate slide show navigation controls
                if (currentArray.length > 1) {
                    html_code = '<div id="navTitle">Scroll or Click: &nbsp;&nbsp;' +
                            '<span class="navControl" onclick="$.fancybox.prev()">&lt;</span> &nbsp;';

                    // Create navigation links
                    $.each(currentArray,
                            function(index) {
                                // Create normal active link
                                if (index != currentIndex) {
                                    imageRow = '<span class="navControl" onclick="$.fancybox.pos(' +
                                            index + ')">' + (index + 1) + '</span> &nbsp;';
                                    // Create highlighted, inactive link
                                } else {
                                    imageRow = '<span class="navControl ' + selected + '" ' + ')">' +
                                            (index + 1) + '</span> &nbsp;';
                                }

                                html_code += imageRow;
                            }
                    );

                    html_code += '<span class="navControl" onclick="$.fancybox.next()">&gt;</span> </span></div>';
                } else {
                    html_code = '<div class="visuallyhidden" id="navTitle">&nbsp;</div>';
                }
                
                html_code += '<div id="title-text">' + title + '</div>';

                return html_code
            }
        });
    });
});
