$(function() {

    // Setup Fancybox

    var body = $('body'),
    content = $('#content');
	
    if (body.hasClass('page-id-37')) {

        content.find('a:has(img)').attr('rel', 'designers').fancybox();

    } else if (body.hasClass('page-id-11') || body.hasClass('page-id-13') || body.hasClass('page-id-311')) {

        content.find('h3').each(function(index) {

            var h3 = $(this),

                elements = h3.nextUntil('h3, .edit-link'),

                container = elements.wrap('<div />'),

                openText = 'Show \u25bc',

                closeText = 'Hide \u25b2',

                id = h3.text().toLowerCase().replace(/\s+/g, '-').replace(/[^\w-]+/g, ''),

                a = $('<a />', {'href': '#' + id}),

                button = $('<span />', {
                    'html': openText
                });

            a = h3.contents().filter(function(){return this.nodeType == 3;}).wrap(a).parent();

            button.appendTo(a);

            container.find('a').attr('rel', id).fancybox();

            a.click(function(event) {
                event.preventDefault();

                var hash = this.hash.substring(1);

                container.slideToggle(500, function() {
                    
                    if ($(this).is(':visible')) {
                        button.text(closeText);
                        window.location.hash = hash;
                    } else {
                        button.text(openText);
                        window.location.hash = '';
                    }
                    
                });


            });

            var hash = window.location.hash.substring(1);

            if ( ! hash && index == 0 || hash == id) {
                button.text(closeText);

            } else {
                container.hide();

            }

        });

    }

});
