GavinSmithPhotography = {

    frame_height : 700,

    init : function()
    {
        this.top_padding = parseInt($('#thepage').css('padding-top'));

        this.resizeFrame();
        this.realign();
        this.photoBrowser();

        $(window).resize(function() {
            GavinSmithPhotography.resizeFrame();
            GavinSmithPhotography.realign();
        });
    },

    realign : function()
    {
        var container_height = (this.top_padding*2) + parseInt($('#thepage').height()) + parseInt($('#thepage').css('padding-bottom'));

        var window_height = $(window).height();
        var difference = window_height - container_height;
        if (difference < 0) difference = 0;
        var padding_top = (window_height > container_height) ? (difference/2)+this.top_padding : this.top_padding;

        $('#thepage').css('padding-top', padding_top);
    },

    resizeFrame : function()
    {
        var padding = (this.top_padding*2) + parseInt($('#frame-br').css('padding-top')) + parseInt($('#frame-br').css('padding-bottom'));
        if ($('#portfolio-thumbs').length) {
            padding += parseInt($('#portfolio-thumbs').height());
        }

        var window_height = $(window).height();
        var wrapper_height =  parseInt($('#wrapper').css('min-height'));

        if (wrapper_height > window_height) window_height = wrapper_height;

        var difference = window_height - padding;

        var frame_width = $('#frame-br').width();
        var frame_height = (difference < this.frame_height) ? difference : this.frame_height;

        $('#frame-br').height(frame_height + 'px');

        $('#navwrap ul').css('top', (frame_height - $('#navwrap ul').height()) + 'px');

        $('#portfolioslides img').each(function() {

            var width = 'auto';
            var height = 'auto';

            var original_width = $(this).data('original-width');
            var original_height = $(this).data('original-height');

            if (frame_width < original_width || frame_height < original_height) {

                var image_ratio = original_height / original_width;
                var frame_ratio = frame_height / frame_width;

                if (image_ratio > frame_ratio) {
                    height = frame_height;
                } else {
                    width = frame_width;
                }
            }

            $(this).width(width).height(height);
        });
    },

    photoBrowser : function()
    {
        if ($('#portfolioslides').length > 0 && $('#portfolio-thumbs').length > 0) {

            $('#portfolioslides').cycle({
                slideResize : 0,
                containerResize: 0,
                before : function() {

                    $('#image-details h3').text($('img:first', this).attr('alt'));

                    $('#portfolio-thumbs .current').animate({ opacity : 0.4 }).removeClass('current');

                    var index = $('#portfolioslides li').index(this);
                    $('#portfolio-thumbs .thumb:eq('+index+')').addClass('current').animate({ opacity : 1 });
                }
            });

            this.addControls();
            this.setupThumbs();
        }
    },

    addControls : function()
    {
        var next = $('<a href="#" class="next">Next</a>');
        var prev = $('<a href="#" class="prev">Previous</a>');
        var play = $('<div class="slideshow-control play"><a href="#">Play</a></div>');
        var stop = $('<div class="slideshow-control stop"><a href="#">Stop</a></div>');

        prev.click(function(event) {
            event.preventDefault();
            GavinSmithPhotography.stop();
            if (!$('#portfolioslides').data('cycle.opts').busy) {
                $('#portfolioslides').cycle('prev');
            }
        });

        next.click(function(event) {
            event.preventDefault();
            GavinSmithPhotography.stop();
            if (!$('#portfolioslides').data('cycle.opts').busy) {
                $('#portfolioslides').cycle('next');
            }
        });

        $('a', stop).click(function(event) {
            event.preventDefault();
            GavinSmithPhotography.stop();
        });

        $('a', play).click(function(event) {
            event.preventDefault();
            $('#portfolioslides').cycle('resume');
            $('#frame .play').hide();
            $('#frame .stop').show();
        });

        play.hide();
        $('#frame').append(play).append(stop);

        $('#image-details').append(
            $('<ul />')
                .append($('<li />').append(prev))
                .append($('<li />').append(next))
            );
    },

    setupThumbs : function()
    {
        $('#portfolio-thumbs .thumb')
            .css('opacity', 0.4)
            .hover(function() {
                if (!$(this).hasClass('current')) $(this).stop().animate({ opacity : 1 });
            }, function() {
                if (!$(this).hasClass('current')) $(this).stop().animate({ opacity : 0.4 });
            });

        $('#portfolio-thumbs .thumb').click(function(event) {
            event.preventDefault();

            if (!$('#portfolioslides').data('cycle.opts').busy) {
                GavinSmithPhotography.stop();

                var index = $('#portfolio-thumbs .thumb').index(this);
                $('#portfolioslides').cycle(index);
            }
        });
    },

    stop : function()
    {
        $('#portfolioslides').cycle('pause');
        $('#frame .play').show();
        $('#frame .stop').hide();
    }
}

$(function() {
    GavinSmithPhotography.init();
});
