$(function () {
    
    jQuery.validator.addMethod("zip", function (value, element) { 
            return this.optional(element) || /^\d{5}(?:-\d{4})?$/.test(value); 
        }, "5-digit US Zip Code or Zip+4, please.");
    
    var make_buy_prints_modal, thumbnails,
        modal_box, make_link_lightboxed;
    
    thumbnails = $('#thumbnail-container li.thumbnail');
    make_buy_prints_modal = function () {
        var buy_prints, buy_prints_button;
        buy_prints = $('#buy-prints');
        buy_prints_button = document.getElementById('buy-prints-button');
        if (buy_prints.length && typeof(jQuery.ui) !== 'undefined') {
            buy_prints.find('input[type=submit]').hide();
            buy_prints.show().dialog({
                autoOpen: false,
                modal: true,
                width: "25em",
                height: 'auto',
                title: "Select a Print Size",
                buttons: {
                    "Add To Cart": function () {
                        buy_prints.submit();
                        $(this).dialog('close');
                    },
                    Cancel: function () {
                        $(this).dialog('close');
                    }
                }
            });
            if (buy_prints_button) {
                buy_prints_button.onclick = '';
                $(buy_prints_button).unbind('click').click(function () {
                    buy_prints.dialog("open");
                    return false;
                });
            }
        }
    };
    make_buy_prints_modal();
    
    
    if (thumbnails.length) {
        thumbnails.hover(function () {
            $('#infobox').text('');
            var object = zc_gallery_storage[this.id],
                infobox = $('#infobox'),
                key;
            $(infobox).append('<p class="header">information</p>');
            for (key in object) {
                if (object[key]) {
                    $('<p class="attribute" style="display:none"><strong>' + key + ':</strong> ' + object[key] + '</p>').appendTo(infobox).fadeIn();
                }
            }
        }, function () {
            $('#infobox').text('');
        });
    }
    
    
    if (thumbnails.length && typeof(jQuery.ui) !== 'undefined') {
        modal_box = $('<div id="modal-box"></a>').prependTo('body');
        
        make_link_lightboxed = function (url) {
            if (document.body.clientHeight < 750) {
                window.scrollTo(0, 0);
            }
            modal_box.html("");
            $('#buy-prints').dialog('destroy').remove();
            modal_box.load(url, function () {
                var the_image = $('#the-image'),
                    image, image_loaded,
                    links = the_image.find('#previous-image, #next-image');
                make_buy_prints_modal();
                links.click(function () {
                    var url = $(this).find('a')[0].href;
                    modal_box.parents('.ui-dialog').fadeOut(function () {
                        make_link_lightboxed(url);
                    });
                    return false;
                });
                image = the_image.find('img.image')[0];
                image_loaded = function (image) {
                    modal_box.parent().fadeIn();
                    if (image.complete && image.width) {
                        modal_box.dialog('close').dialog('option', 'width', image.width+310).dialog('open');
                    } else {
                        setTimeout(function () {
                            image_loaded(image);
                        }, 50);
                    }
                };
                image_loaded(image);
            });
        };
        
        modal_box.dialog({
            autoOpen: false,
            modal: true,
            width: 'auto',
            resizable: false,
            draggable: false
        });
        
        thumbnails.click(function () {
            var url = $(this).find('a')[0].href;
            if (document.body.clientHeight < 750) {
                window.scrollTo(0, 0);
            }
            make_link_lightboxed(url);
            return false;
        });
    }
});
