var initialHeight = 0;
var hasScroller = false;
$j(function() {

    $j('.gallery-scroller').jcarousel({
        scroll: 1,
        itemFallbackDimension: 550,
        initCallback: mycarousel_initCallback,
        visible: 1,
        wrap: null
    });

    if(!$j('body').hasClass('wordpress-post-view-index'))
    {
        // move all even* posts to right hand side
        var evenPosts = $j('.post-list ul.list-of-posts > li:odd').each(function() {
            var el = $j(this);

            if(!el.siblings().find('.gallery-scroller').length > 0) {
                el.appendTo('ul#post-list-secondary');
                el.find('.gallery-scroller').each(resizeObject);
                el.find('img').each(resizeObject);
                el.find('object').each(resizeObject);
                el.find('embed').each(resizeObject);
            }
        });


        $j('.entry').each(function() {
            var entry = $j(this);
            entry.data('naturalHeight', entry.height());
            entry.data('naturalGalleryHeight', entry.data('naturalHeight'));
            var item = null;

            var item = entry.find('.gallery-scroller, img, object, embed')[0];

            if($j(item).parent('a').length > 0) {
                item = $j(item).parent('a');
            }

            if($j(item).parent('p').length > 0) {
                $j(item).parent('p').remove();
            }

            entry.find('.postTitle').before(item);

            if(!$j(item).hasClass('gallery-scroller'))
            {
                // adjusts height of initial display to pos of first div
                var firstP = entry.find('p:eq(0)');
                var entryOffset = entry.offset();
                var firstPOffset = firstP.offset();

                initialHeight = (firstPOffset.top - entryOffset.top) + firstP.height();
                entry.data('initialHeight', initialHeight);
                entry.data('initialGalleryHeight', initialHeight);

                if(initialHeight <= entry.height()) {
                    entry.data('adjustedHeight', initialHeight);
                    entry.css({
                        'height' : initialHeight + 'px'
                    });
                } else {
                    $j.data(entry, 'adjustedHeight', entry.height());
                }
            }
            else
            {
                // Gallery scroller
                var container = $j(item).parents('li');
                var readMore = $j(container).find('a.read-more, a.read-less');
                $j(readMore).remove();
            }
        });

        $j('.read-more').live('click', function() {
            var link = $j(this);

            var entry = link.parent().prev();

            entry.animate({
                'height' : entry.data('naturalHeight')
            }, 750);

            link.removeClass('read-more')
            link.addClass('read-less').text('READ LESS');

            return false;

        });

        $j('.read-less').live('click', function() {
            var link = $j(this);

            var entry = link.parent().prev();

            entry.animate({
                'height' : entry.data('adjustedHeight')
            }, 750);

            link.removeClass('read-less')
            link.addClass('read-more').text('VIEW THE FULL ARTICLE');

            return false;

        });
    }

    var recentPostsMarkup = "<h3 class=\"heading\">Most Recent Articles<a href=\"/blog/feed\" title=\"RSS: Get new WIRED blog posts direct to your inbox\" id=\"rss-feed\">RSS</a></h3>";
    $j('.rightHandSide').prepend(recentPostsMarkup);
    var recentPosts = $j('ul#recent-posts');
    $j('h3.heading').after(recentPosts);

});

function resizeObject() {
    var el = $j(this);

    var width = el.width();
    var height = el.height();

    var ratio = height / width;

    var newWidth = 330;
    var newHeight = newWidth * ratio;

    el.css({
        'width' : newWidth,
        'height': newHeight
    });

    //resizes the right-hand column divs
    el.closest('.entry').data('initialHeight', (el.closest('.entry').data('initialGalleryHeight') / ratio));
    el.closest('.entry').data('adjustedHeight', (el.closest('.entry').data('naturalGalleryHeight') / ratio));
    el.closest('.entry').data('naturalHeight', (el.closest('.entry').data('naturalGalleryHeight') / ratio));

}

function mycarousel_initCallback(carousel, state)
{
    carousel_id = $j(carousel.container).attr('id');
    photoset_id = $j('#' + carousel_id).find('#flickr-photoset-id').val();

    var photoset_json_url = 'http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key=be523b59eb8a9b89fe4c5a76652b7253&photoset_id=' + photoset_id + '&per_page=100&page=1&format=json&extras=o_dims&jsoncallback=?';

    $j.getJSON(
        photoset_json_url,
        function(data) {
            var i = 0;

            $j.each(data.photoset.photo, function(){
                // getSizes of this particular image
                var imageRatio = $j(this).attr('o_height') / $j(this).attr('o_width');
                var widthConstraint = 540;

                var newImageHeight = widthConstraint * imageRatio;

                var photoImgUrl = 'http://farm'+$j(this).attr('farm')+'.static.flickr.com/'+$j(this).attr('server')+'/'+$j(this).attr('id')+'_'+$j(this).attr('secret')+'_z.jpg';
                var photoImgObject = '<img src="' + photoImgUrl + '" alt="' + $j(this).attr('title') + '" title="' + $j(this).attr('title') + '" height="' + newImageHeight + '" width="' + widthConstraint + '" />';

                // Position jcarousel-prev and jcarousel-next buttons to be 50% of height of first image
                if(i == 0)
                {
                    $j(carousel.buttonNext).css('top', (newImageHeight / 2) + 'px');
                    $j(carousel.buttonPrev).css('top', (newImageHeight / 2) + 'px');
                }

                carousel.add(i, photoImgObject);
                i++;
            });
            carousel.size(i);
        }
        );
}
