﻿Cufon.replace('.ucl-star-story-name', {
    fontFamily: 'Caflisch Script Pro'
});

hdp.starStoriesPaging = {
    init : function() {
        $('.ucl-star-story-paging').each(
            function(i) {
                if($(this).height() > $(this).find('.ucl-star-story-inner').height()) {
                    $(this).siblings('.ucl-star-story-buttons').hide();
                }
                else {
                    // this is a horrific function to make sure that the text doesn't overflow
                    // it was always breaking on one browser/platform or another
                    // since I couldn't find any safe cross-browser method of making sure
                    // the height of the box exactly fitted the text, jquery does it
                    var bottomOfPagingElement = $(this).offset().top + $(this).height();
                    $(this).find('span.cufon').each(
                        function(i) {
                            var bottomOfThisElement = $(this).offset().top + $(this).height();
                            if($(this).offset().top > bottomOfPagingElement) return false;
                            if(bottomOfThisElement > bottomOfPagingElement) {
                                var newHeight = bottomOfThisElement - $('.ucl-star-story-paging').offset().top;
                                if(navigator.userAgent.match("MSIE")) newHeight += 1;
                                var paging = $(this).closest('.ucl-star-story-paging');
                                paging.height( newHeight );
                                // we've resized the box, so maybe don't need to show more link
                                if(paging.height() >= paging.find('.ucl-star-story-inner').height()) {
                                    paging.siblings('div.ucl-star-story-buttons').hide();
                                }
                                // check to see if there's just one line of text one next screen
                                else if(paging.find('.ucl-star-story-inner').height() - paging.height() -paging.find('span.cufon').height() <= 2) {
                                    paging.height( paging.height()+paging.find('span.cufon').height() );
                                    paging.siblings('div.ucl-star-story-buttons').hide();
                                }
                                return false;
                            }
                        }
                    );
                }
            }
        );
        $('.ucl-star-story-button-more').click(
            function(e) {
                var page = $(this).parent().siblings('.ucl-star-story-paging');
                var paras = page.find('.ucl-star-story-inner');
                var parasHeight = parseInt(paras.css('top'));
                if(isNaN(parasHeight)) parasHeight = 0;

                var newTop = parasHeight - page.height();
                if(newTop + paras.height() > 0) {
                    paras.css( { top: newTop + 'px' } );
                    $(this).siblings('.ucl-star-story-button-back').show();
                }
                // reach the end, hide next
                if(newTop + paras.height() < page.height()) $(this).hide();
                e.preventDefault();
            }
        );
        $('.ucl-star-story-button-back').click(
            function(e) {
                var page = $(this).parent().siblings('.ucl-star-story-paging');
                var paras = page.find('.ucl-star-story-inner');
                var newTop = parseInt(paras.css('top')) + page.height();
                if(newTop < page.height()) {
                    paras.css( { top: newTop + 'px' } );
                    $(this).siblings('.ucl-star-story-button-more').show();
                }
                // at the beginning, hide back
                if(newTop >= 0) $(this).hide();
                e.preventDefault();
            }
        );
    }
}

$(document).ready(function(){
   hdp.starStoriesPaging.init();
});
