(function($) {
    $.fn.lightbox = function(options) {
        var html = '<div id="overlay"></div><div id="lightbox"><div id="lightbox-content"></div><a class="close" href="#">Close</a><a class="prev" href="#"></a><a class="next" href="#"></a></div>';
        
        var current;
        var elements = this;
        
        var settings = $.extend({
            size: 320
        }, options);
        
        function open(url) {
            $('body').append(html);
            
            $('#lightbox-content').load(url, function() {
                $(this).delay('slow').fadeIn('fast');
            }).hide();
            
            $('#overlay, #lightbox .close').click(close);
            
            $('#lightbox .prev').click(navigate);
            $('#lightbox .next').click(navigate);
            
            $('#lightbox')
                .hide()
                .css('height', settings.size).css('width', settings.size * 2).css('margin-left', -settings.size)
                .fadeIn('fast');
                
            $('#overlay').css('background', '#ffffff').css('opacity', 0);
            
            $('body').css('height', '100%');
            
            $('html').css('overflow-x', 'hidden');
            
            fadeDown();
            
            updateControls();
        }
        
        function close(event) {
            event.preventDefault();
            $('#overlay, #lightbox').remove();
            $('body').css('height', '');
            $('html').css('overflow-x', '');
            fadeUp();
        }
        
        function navigate(event) {
            event.preventDefault();
            var i = $(elements).index(current);
            var d = (this.className == 'next') ? 1 : -1;
            var j = i + d;
            if (j >= 0 && j < elements.length) {
                current = $(elements).eq(j);
                $('#lightbox-content').fadeOut('fast', function() {
                    $('#lightbox-content').load($(current).attr('href'), function() {
                        $('#lightbox-content').fadeIn('fast');
                    });
                });
            }
            updateControls();
        }
        
        function updateControls() {
            var i = $(elements).index(current);
            if (i == 0) {
                $('#lightbox .prev').hide();
            } else if (i == ($(elements).length - 1)) {
                $('#lightbox .next').hide();
            } else {
                $('#lightbox .prev, #lightbox .next').show();
            }
        }
        
        function fadeDown() {
            $('#nav, #subnav, .name').stop().fadeTo('fast', 0);
            $('#logo, h1, #photos').stop().fadeTo('fast', 0.2);
        }
        
        function fadeUp() {
            $('#nav, #subnav, .name').stop().fadeTo('fast', 1);
            $('#logo, h1, #photos').stop().fadeTo('fast', 1, function() {
                $(this).css('opacity', '');
            });
        }
        
        return this.each(function() {
            $(this).click(function(event) {
                event.preventDefault();
                current = this;
                open($(this).attr('href'));
            });
        });
    };
})(jQuery);

(function($) {
    $.fn.imagefade = function(options) {
        var elements = this;
        return this.each(function() {
            $($(this)).hover(
                function() {
                    $(elements).not(this).find("img, .name").stop().fadeTo("fast", 0.2);
                },
                function() {
                    var selectors = ($("#overlay").length) ? "img" : "img, .name";
                    $(elements).find(selectors).stop().fadeTo("fast", 1);
                }
            );
        });
    };
})(jQuery);

$(document).ready(function() {
    var cookieMatches = document.cookie.match(/screenwidth=(\d+)/);
    if (!cookieMatches || cookieMatches[1] != screen.width) {
        var date = new Date();
        date.setTime(date.getTime() + (28 * 24 * 60 * 60 * 1000)); // 28 days
        document.cookie = "screenwidth=" + screen.width + ";expires=" + date.toGMTString() + "; path=/";
    }
    
    $("#photos li a:first-child").lightbox({size: detailImageSize});
    $("#photos li a:first-child").imagefade();
    
    $("#content").hover(
        function() {
            $("#nav, #subnav").stop().fadeTo("fast", 0);
            $("#logo, h1, #flash-meta").stop().fadeTo("fast", 0.2);
        },
        function() {
            if ($("#overlay").length) return;
            $("#nav, #subnav").stop().fadeTo('fast', 1);
            $("#logo, h1, .name, #flash-meta").stop().fadeTo("fast", 1);
        }
    );
    
    if ($.browser.msie) {
        $("#families a").click(function() {
            window.location = $(this).attr("href"); 
        });
    }
});
