/*****  filter page number for the system   *********/
function filterPageNo(pageNo){
    InputValue  =    Number(pageNo);

    if(InputValue <= 0){
        return 0;
    }else if(InputValue >= numberOfPages-1){
        return (numberOfPages-1);
    }else{
        InputValue   = (InputValue%2==0 ? InputValue-1 : InputValue);
        return InputValue;
    }
}

/**
 * document on load
 */
$(document).ready(function() {
    hotKeyAssign();
    $(document).bind("contextmenu",function(e){
        return false;
    });
});

function hotKeyAssign(){
    jQuery(document).bind('keydown', 'left',function (evt){
        showPreviousImage();
        return false;
    });
    jQuery(document).bind('keydown', 'right',function (evt){
        showNextImage();
        return false;
    });
    jQuery(document).bind('keydown', 'p',function (evt){
        startPreviousFolding();
        return false;
    });
    jQuery(document).bind('keydown', 'n',function (evt){
        startNextFolding();
        return false;
    });
}

function hotKeyDisable(){
    jQuery(document).unbind('keydown', 'left',function (evt){
        showPreviousImage();
        return false;
    });
    jQuery(document).unbind('keydown', 'right',function (evt){
        showNextImage();
        return false;
    });
    jQuery(document).unbind('keydown', 'p',function (evt){
        startPreviousFolding();
        return false;
    });
    jQuery(document).unbind('keydown', 'n',function (evt){
        startNextFolding();
        return false;
    });
}

/**
 * Resize the content when the window resize
 */
$(window).bind('resize', function () {
    contentResize();
});

function contentResize(){
    var minimumResizeWindowWidth    =   400;
    contentWidth    =   $(window).width()*.78;

    if( (contentWidth >=   minimumResizeWindowWidth) &&
        (((pWidth*2) < contentWidth*.95)  || ((pWidth*2) > contentWidth*1.05))){
        pWidth=  Math.round(contentWidth/2);
        myPageW  = pWidth-2*pageBorderWidth;

        changeRation=myPageW/maxWidth;
        myPageH = Math.round(maxHeight*changeRation);
        pHeight = myPageH + 2*pageBorderWidth;

        //image fliping step size
        numPixelsToMove     =   Math.round(myPageW/imageFlipSteps);
        numPixels           =   numPixelsToMove;

        $("#contents").css({
            width           :   2*pWidth+"px",
            height          :   pHeight+"px"
        });
        $("#current_page_container").css({
            height          :   pHeight+"px"
        })
        $("#current_left_page_container").css({
            width           :   myPageW + "px",
            height          :   myPageH + "px"
        })
        $("#current_right_page_container").css({
            width           :   myPageW + "px",
            height          :   myPageH + "px"
        });
        $("#next_page_container").css({
            height          :   pHeight+"px"
        });
        $("#next_left_page_container").css({
            width           :   myPageW + "px",
            height          :   myPageH + "px"
        });
        $("#next_right_page_container").css({
            width           :   myPageW + "px",
            height          :   myPageH + "px"
        });
        $("#previous_page_container").css({
            height          :   pHeight+"px"
        });
        $("#previous_left_page_container").css({
            width           :   myPageW + "px",
            height          :   myPageH + "px"
        });
        $("#previous_right_page_container").css({
            width           :   myPageW + "px",
            height          :   myPageH + "px"
        });

        //setting the previous and next page nevigation height
        $('#prev_page_navigation').css("height",pHeight+"px");
        $('#next_page_navigation').css("height",pHeight+"px");

        resetlargeImageSize();
    }
}


