
var currentPage = 'bookpage-1';
var isLoaded = false;

function loadBookInside(data)
{
    var boxHeight = parseInt(jQuery(window).height());
    jQuery('#book-container').css('height', boxHeight - 160);
	
    jQuery(window).resize(function()
    {
        if(isLoaded){
            jQueryheight = jQuery(window).height();
            if( jQueryheight < 400 ) jQueryheight = 400;
			
            jQuery("#bookInside").height(jQueryheight-90);
            jQuery("#book-container").height(jQueryheight-110);
        }
    });

    var boxHeight = parseInt(jQuery(window).height());
    jQuery('#book-container')
    .modal({
        containerId : "bookInside",
        dataId : "bookInsideData",
        overlayId: "bookInsideOverlay",
        maxHeight: boxHeight,
        maxWidth: 1000,
        escClose: false,
        overlayClose: false,
        opacity: 40,
        onOpen : function(obj) {
            jQuery("#bookInsideOverlay").show();
				
            //obj.data.before('<div class="bookInsideTopNav"><span id="bookInsideCurrentPage">' + data.pageLabel +' 1</span><button onclick="' + data.buyButtonAction + '; jQuery(\'#bookInside\').find(\'.modalCloseImg\').click();" type="button">' + data.buyButtonLabel + '</button></div>');
            if(data.buyButtonAction){
                obj.data.before('<div class="bookInsideTopNav"><a class="bookInsideBuy" onclick="' + data.buyButtonAction + '; jQuery.modal.close();" type="button">' + data.buyButtonLabel + '</a></div>');
            }
            obj.data.before('<a class="bookInsideButtonPrev disabled" id="bookInsideButtonPrev" href="javascript:void(0);" onclick="bookInsidePrevPage(this);">Prev</a>');
            obj.data.before('<a class="bookInsideButtonNext" id="bookInsideButtonNext" href="javascript:void(0);" onclick="bookInsideNextPage(this);">Next</a>');
				
            jQuery('.modalCloseImg').html(data.closeButtonLabel);
				
            jQuery('#book-container').load(data.url, function(){
                obj.container.show();
                obj.data.show();
                jQuery(window).trigger('resize');
                jQuery('#bookpage-1').find('img').first().attr('src', jQuery('#bookpage-1').find('img').first().attr('original')).removeClass('page-loading');
                jQuery('#book-container').scrollTop(-1);
            });
            currentPage = 'bookpage-1';
        },
        onShow: function() {
            isLoaded = true;
        }
    });
	
    jQuery('#book-container').scroll(function(e){
        var tempH=0, scrollH=0;
        scrollH =  jQuery('#book-container').scrollTop();
        jQuery('#book-pages').find('li').each(function(){
            tempH += jQuery(this).height();
            if ( tempH > scrollH+400 ) {
                currentPage = jQuery(this).attr('id');
                //jQuery('#bookInsideCurrentPage').html(data.pageLabel + " " + currentPage.replace("bookpage-",""));
                if (jQuery(this).find('img').first().hasClass('page-loading')) {
                    jQuery(this).find('img').first().attr('src', jQuery(this).find('img').first().attr('original')).removeClass('page-loading');
                }
                return false;
            }
        });
		
        var page = parseInt(currentPage.substring(currentPage.indexOf("-")+1));
        if (page>1 && page<jQuery('#book-pages').find('li').length) {
            jQuery('#bookInsideButtonPrev').removeClass('disabled');
            jQuery('#bookInsideButtonNext').removeClass('disabled');
        }
        else if (page == 1){
            jQuery('#bookInsideButtonPrev').addClass('disabled');
        }
        else if (page == jQuery('#book-pages').find('li').length){
            jQuery('#bookInsideButtonNext').addClass('disabled');
        }
    });
}

function bookInsideNextPage(o){
    if (jQuery(o).hasClass('disabled')) {
        return false;
    }


    var nextPage;
    nextPage = parseInt(currentPage.substring(currentPage.indexOf("-")+1))+1;

    if (jQuery('#bookpage-'+nextPage).is('li')){
        currentPage = 'bookpage-'+nextPage;
        var tempH=0;
        jQuery('#book-pages').find('li').each(function(index){
            if ( index == nextPage - 1 ) {
                jQuery('#book-container').scrollTop(tempH);
                return false;
            }
            tempH += jQuery(this).height();
        });
    }
    else {
        jQuery('#book-container').scrollTop(jQuery('#book-pages').height());
    }
}

function bookInsidePrevPage(o){
    if (jQuery(o).hasClass('disabled')) {
        return false;
    }
	
    var prevPage;
    prevPage = parseInt(currentPage.substring(currentPage.indexOf("-")+1))-1;
	
    if (jQuery('#bookpage-'+prevPage).is('li')){
        currentPage = 'bookpage-'+prevPage;
        var tempH=0;
        jQuery('#book-pages').find('li').each(function(index){
            if (index == prevPage-1) {
                jQuery('#book-container').scrollTop(tempH);
                return false;
            }
            tempH += jQuery(this).height();
        });
    }
}

