var AbsoluteArtGallery = {
    thumbsetHashPrefix: "tmb",
    artistPageState: { thumbIndex: 0, activeTab: '' },

    init: function () {
        var self = this;
        self.fixHeightColumns();
        $(window).resize(self.fixHeightColumns);

        self.initAutoLinks();

        if ($('#artist-detail').length != 0) {
            self.initArtistPage();
        }
        
    	// Init form validation
    	$('form').validate();
    	
    	// Init colorbox
    	$('a.colorbox').colorbox();
    },

    initAutoLinks: function () {
        $('.auto-link').children().click(function (e) {
            document.location.href = $('a:first', this).attr('href');
        });
    },

    initArtistPage: function () {
        var self = this;

        $('.artist-ajax').click(function (e) {
            $link = $(this);
            self.artistPageState.activeTab = $link.attr('rel');
            $(window).trigger('artistPageStateChange');
            self.showArtistTab();
            return false;
        });

        // reload last position
        if (document.location.hash) {
            var loadState = jQuery.deparam.fragment();
            if (typeof loadState != 'undefined') {
                if (loadState.thumbIndex) {
                    self.artistPageState.thumbIndex = loadState.thumbIndex;
                }

                if (loadState.thumbIndex) {
                    self.artistPageState.activeTab = loadState.activeTab;
                    self.showArtistTab();
                }
            }
        }

        $('#detail-images').find('.scrollable, .item').css('height', $('#detail-images img:first').height());

        self.initScrollable($('#detail-images')); // do this one first, then it get the keyboard focus
        var thumbsScrollableApi = self.initScrollable($('#thumbs'), self.artistPageState.thumbIndex);
        $('.scroll-parent').fadeTo('fast', 1); // fadeIn - in css we set opacity to 0 to work around the FOUC of the initialIndex

        // bind event to register last position
        $('#thumbs').bind("onSeek", function (e) {
            self.artistPageState.thumbIndex = $('#thumbs .scrollable').data("scrollable").getIndex();
            $(window).trigger('artistPageStateChange');
        });

        $(window).bind('artistPageStateChange', function () {
            document.location.href = "#" + $.param(self.artistPageState);
        });

        $('#thumbs a').live('click', function () {
            var href = $(this).attr('href');
            if (href.indexOf('#') == -1) {
                $(this).attr('href', href + document.location.hash);
            }

            return true;
        });
    },

    showArtistTab: function () {
        if (this.artistPageState.activeTab != '') {
            var $link = $('a[rel=' + this.artistPageState.activeTab + ']');
            var $dest = $('#' + this.artistPageState.activeTab);
            $link.parents('.artist-ajax-parent').slideUp(400, function () {
                if ($dest.offset().top > 0) {
                    $('html,body').animate({ scrollTop: 100 }, 300);
                }
                $dest.hide().removeClass('hidden').slideDown();
            });
        }
    },

    initScrollable: function (obj, index) {
        // initialIndex is undocumented but works in the init, ...
        // however the .next does not get the .disabled class when index is the last one
        var api = $('.scrollable', obj).scrollable({ circular: false, api: true });
        if ($('.item', obj).length > 1) {
            // speed 0 reverts to the default speed, therefor we use 1ms
            if (index) api.seekTo(index || 0, 1);
            // add small delay for the .disabled class updates after seekTo
            setTimeout(function () {
                $('.prev, .next', obj).fadeIn();
            }, 200);
        }
    },

    fixHeightColumns: function () {
        var minHeight = $(document).height() - $('header').height();
        window.log('fixHeightColumns to ' + minHeight);
        $('#main > div').each(function (idx, item) {
            var $item = $(item);
            var h = minHeight - parseInt($item.css('padding-top')) - parseInt($item.css('padding-bottom'));
            if ($('html').hasClass('ie8')) h -= 4;
            $item.css('min-height', h);
        });
    }
};

$(document).ready(function(){
	AbsoluteArtGallery.init();
});
