
/*!
 *
 * ja.fastEater v1.0.0
 * created by Jonathan Armstrong (http://jonathanarmstrong.com)
 *
 * requires jQuery v1.4.2 and ja.base
 */

// init namespaces
JA.FastEater = JA.FastEater || {};

// doc ready
$.document.ready(function() {

	var scrollBuffer = 0;
    var scrollPosition = 0;
	var intervalLength = 300;
	var imageWidth = JA.TumblrStream.imageWidths.small;

	// do special things for apple mobile devices
	if (JA.utils.isAppleMobileDevice) {
		$.document
			.find('#content')
				.addClass('iDevice');

		// scroll buffer needs to be increased to account for mobile safari's inclusion of the browser toolbars in the
        // document height
		scrollBuffer += 100;
		//
		imageWidth = JA.TumblrStream.imageWidths.medium;
	}

	// create tumblr stream
	var tumblrStream = new JA.TumblrStream({
		'containingElement':$.document.find('#posts'),
		'dataUrl':'http://fasteater.tumblr.com/api/read/json',
		'intervalLength':intervalLength,
		'imageWidth':imageWidth,
		'init':function() {
			// bind stream events
			$.document.find('#posts')
				.bind({
                    'JA.TumblrStream.fetch': function(e) {
                        $.document.find('#loading').fadeIn(300);
                        $(this).trigger('JA.TumblrStream.disable', true);
                    },
                    'JA.TumblrStream.complete': function(e, doEnable) {
                        $.document.find('#loading').fadeOut(300);
                        if (typeof(doEnable) == 'boolean' && doEnable) {
                            $(this).trigger('JA.TumblrStream.enable');
                        }
                    },
                    'JA.TumblrStream.enable': function(e) {
                        $.document.find('#logo')
                            .removeClass('disabled')
                            .find('h1')
                                .attr('title', 'reload feed');
                    },
                    'JA.TumblrStream.disable': function(e) {
                        $.document.find('#logo')
                            .addClass('disabled')
                            .find('h1')
                                .attr('title', 'slow reader, fast eater');
                    }
                });
		}
	});

    // bind element events
	$.document
        // bind logo click event
        .find('#logo')
            .bind('click', function() {
                if (!$(this).hasClass('disabled')) {
                    tumblrStream.reset();
                    if ($.document.find('#gototop:visible').length > 0) {
                        $.document.find('#gototop').fadeOut(intervalLength);
                    }
                }
            })
        .end()
        // bind info open/close link click event
        .find('#ja, #info .close')
            .bind('click', function() {
                var posts = $.document.find('#posts');
                tumblrStream.isDisabled = !tumblrStream.isDisabled;
                if (tumblrStream.isDisabled) {
                    scrollPosition = $.document.scrollTop();
                } else {
                    posts.scrollTop(0)
                }
                $.document
                    .find('#logo')
                        .toggleClass('alt')
                    .end()
                    .find('#info')
                        .height(Math.max($.window.height(), $.document.find('#info').height()))
                        .stop(true, true)
                        .slideToggle(intervalLength)
                    .end()
                    .find('#posts')
                        .trigger('JA.TumblrStream.' + (!tumblrStream.isDisabled ? 'enable' : 'disable'))
                        .toggleClass('background')
                    .end()
                    .scrollTop(!tumblrStream.isDisabled ? scrollPosition : 0);
                if (tumblrStream.isDisabled) {
                    posts.scrollTop(scrollPosition)
                }
            })
        .end()
        // bind say hello link click event
        .find('.sayHelloLink')
            .bind('click', function() {
                document.location = ['mail','to',':','hello','@','jonathan','armstrong','.','com','?body=via slowreaderfasteater'].join('');
            })
        .end()
        // bind 'top' link click event
        .find('#gototop')
            .bind('click', function() {
                $.document.find('html, body').animate({'scrollTop':0}, 500, function() {
                    $.window.trigger('scroll');
                });
            });

	// handle window events
	$.window
		.bind({
            'scroll': function(e) {
                var topLink = $.document.find('#gototop');
                var scrollTop = $.document.scrollTop();

                // determine if 'top' link should appear
                if (scrollTop > $.window.height()) {
                    if (!topLink.is(':visible')) {
                        topLink.fadeIn(intervalLength);
                    }
                } else if (topLink.is(':visible')) {
                    topLink.fadeOut(intervalLength);
                }

                // do special things for apple mobile devices
                if (JA.utils.isAppleMobileDevice) {
                    $.document
                        .find('#logo, #menu')
                            .css('top', scrollTop);
                }

                // determine if we're at the bottom of the page
                if (tumblrStream.keys.length < tumblrStream.totalPosts && scrollTop + $.window.height() >= $.document.height() - scrollBuffer) {
                    tumblrStream.getData();
                }
            },
            'resize': function(e) {
                tumblrStream.reflow();
                $.document.find('#info').height($.window.height());
            }
		});
});

