function initLoad(){
    resetCurrentPageLayout();
    resetNextPageLayout();
    resetPrevioustPageLayout();
    
    if(allowPageClick){
        $("#current_left_page_container").live('click',function(evt){
            startPreviousFolding();
            return false;
        });
        $("#current_right_page_container").live('click',function(evt){
            startNextFolding();
            return false;
        });
    }
    
    $('#panningDiv').hide();
    resetlargeImageSize();
    loadLargeImageByNo(0, false);
    initLoadThumbImages();

    $('#contents').append($('<div class="clear_both"></div>'));

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

}

function resetlargeImageSize(){
    var objCSSImageProperty = {
        width   :   myPageW + "px",
        height  :   myPageH + "px"
    }
    //image size change
    $('#current_left').css(objCSSImageProperty);
    $('#current_right').css(objCSSImageProperty);
    $('#previous_left').css(objCSSImageProperty);
    $('#previous_right').css(objCSSImageProperty);
    $('#next_left').css(objCSSImageProperty);
    $('#next_right').css(objCSSImageProperty);
}
function resetCurrentPageLayout(){
    $("#current_page_container")
    .css({
        overflow        :   "hidden",
        top             :   "0px",
        left            :   '0px',
        width           :   'auto',
        height          :   pHeight+"px",
        zIndex          :   0
    })

    $("#left_floating_div")
    .css({
        width           :   numPixelsToMove+"px",
        float           :   'left',
        borderWidth     :   pageBorderWidth+"px",
        borderColor     :   pageBorderColor,
        borderStyle     :   pageBorderStyle,
        borderRightWidth  :   "0px"
    })
    .hide();

    $("#current_left_page_container")
    .css({
        overflow        :   "hidden",
        top             :   "0px",
        width           :   myPageW + "px",
        height          :   myPageH + "px",
        float           :   'left',
        borderWidth     :   pageBorderWidth+"px",
        borderColor     :   pageBorderColor,
        borderStyle     :   pageBorderStyle

    })

    $("#current_right_page_container")
    .css({
        overflow        :   "hidden",
        top             :   "0px",
        float           :   'left',
        width           :   myPageW + "px",
        height          :   myPageH + "px",
        borderWidth     :   pageBorderWidth+"px",
        borderColor     :   pageBorderColor,
        borderStyle     :   pageBorderStyle

    })
    $("#right_floating_div")
    .css({
        width           :   numPixelsToMove+"px",
        float           :   'left',
        borderWidth     :   pageBorderWidth+"px",
        borderColor     :   pageBorderColor,
        borderStyle     :   pageBorderStyle,
        borderLeftWidth  :   "0px"
    })
    .hide();
}
function resetPrevioustPageLayout(){
    $("#previous_page_container")
    .css({
        overflow        :   "hidden",
        //        position        :   "absolute",
        top             :   "0px",
        left            :   '0px',
        width           :   'auto',
        height          :   pHeight+"px",
        zIndex          :   0
    })
    .hide();
    $("#previous_left_page_container")
    .css({
        overflow        :   "hidden",
        top             :   "0px",
        width           :   myPageW + "px",
        height          :   myPageH + "px",
        float           :   'left',
        borderWidth     :   pageBorderWidth+"px",
        borderColor     :   pageBorderColor,
        borderStyle     :   pageBorderStyle

    })
    .hide();
    $("#previous_right_page_container")
    .css({
        overflow        :   "hidden",
        top             :   "0px",
        float           :   'left',
        width           :   myPageW + "px",
        height          :   myPageH + "px",
        borderWidth     :   pageBorderWidth+"px",
        borderColor     :   pageBorderColor,
        borderStyle     :   pageBorderStyle
    })
    .hide();
}
function resetNextPageLayout(){
    $("#next_page_container")
    .css({
        overflow        :   "hidden",
        top             :   "0px",
        left            :   '0px',
        width           :   'auto',
        height          :   pHeight+"px",
        zIndex          :   0
    })
    .hide();
    $("#next_left_page_container")
    .css({
        overflow        :   "hidden",
        top             :   "0px",
        width           :   myPageW + "px",
        height          :   myPageH + "px",
        float           :   'left',
        borderWidth     :   pageBorderWidth+"px",
        borderColor     :   pageBorderColor,
        borderStyle     :   pageBorderStyle

    })
    .hide();
    $("#next_right_page_container")
    .css({
        overflow        :   "hidden",
        top             :   "0px",
        float           :   'left',
        width           :   myPageW + "px",
        height          :   myPageH + "px",
        borderWidth     :   pageBorderWidth+"px",
        borderColor     :   pageBorderColor,
        borderStyle     :   pageBorderStyle

    })
    .hide();
}

function searchArray(searchValue,haystack){
    for(i=1;i<=haystack.length;i++){
        curr_value = haystack[i];
        if(curr_value==searchValue)
            return true;
    }
    return false;
}
function inCash(pageNo){
    return searchArray(pageNo,pagesCache);
}

function loadLargeImageByNo(pageNo,ajax){
    if(ajax ==  true)   loadCurrentPageLargeImages(pageNo);
    else                loadCurrentPageLargeImagesWithoutAjax(pageNo);
    loadPreviousPageLargeImages(pageNo);
    loadNextPageLargeImages(pageNo);
}

function loadPreviousPageLargeImages(pageNo){
    if(pageNo==0){
        $("#previous_left").attr('src', allPagesArr[numberOfPages-1]).css('display','block');
        $("#previous_right").attr('src',blankImageUrlTrans).css('display','none');
        $("#previous_left_page_container").css('width',myPageW).css('border',pageBorderFullCSS);
        $("#previous_right_page_container").css('width',myPageW).css('border',pageBorderTransFullCSS);
    }
    else if(pageNo==1){
        $("#previous_left").attr('src',blankImageUrlTrans).css('display','none');
        $("#previous_right").attr('src', allPagesArr[0]).css('display','block');
        $("#previous_left_page_container").css('width',myPageW).css('border',pageBorderTransFullCSS);
        $("#previous_right_page_container").css('width',myPageW).css('border',pageBorderFullCSS);
    }
    else{
        $("#previous_left").attr('src', allPagesArr[pageNo-2]).css('display','block');
        $("#previous_right").attr('src', allPagesArr[(pageNo-1)]).css('display','block');
        $("#previous_left_page_container").css('width',myPageW).css('border',pageBorderFullCSS);
        $("#previous_right_page_container").css('width',myPageW).css('border',pageBorderFullCSS);
    }
}
function loadCurrentPageLargeImagesWithoutAjax(pageNo){
    if(pageNo==0){
        $("#current_left").attr('src',blankImageUrlTrans);
        $("#current_right").attr('src', allPagesArr[0]);
        $("#current_left_page_container").css('width',myPageW).css('border',pageBorderTransFullCSS);
        $("#current_right_page_container").css('width',myPageW).css('border',pageBorderFullCSS);
    }
    else if(pageNo==numberOfPages-1){
        $("#current_left").attr('src', allPagesArr[numberOfPages-1]);
        $("#current_right").attr('src',blankImageUrlTrans);
        $("#current_left_page_container").css('width',myPageW).css('border',pageBorderFullCSS);
        $("#current_right_page_container").css('width',myPageW).css('border',pageBorderTransFullCSS);
    }
    else{
        $("#current_left").attr('src', allPagesArr[pageNo]);
        $("#current_right").attr('src', allPagesArr[(pageNo+1)]);
        $("#current_left_page_container").css('width',myPageW).css('border',pageBorderFullCSS);
        $("#current_right_page_container").css('width',myPageW).css('border',pageBorderFullCSS);
    }

             
}
function loadCurrentPageLargeImages(pageNo){

    if(pageNo==0){
        current_left_src    =   blankImageUrlTrans;
        current_right_src   =   allPagesArr[0];
    }
    else if(pageNo==numberOfPages-1){
        current_left_src    =   allPagesArr[numberOfPages-1];
        current_right_src   =   blankImageUrlTrans;
    }
    else{
        current_left_src    =   allPagesArr[pageNo];
        current_right_src   =   allPagesArr[(pageNo+1)];
    }

    innerHtml   =   '<table width="'+2*pWidth+'px">\n\
                        <tr><td class="middle_center" height="'+pHeight+'px">\n\
                        <img src="'+siteUrl+'images/loading_wheel.gif" />\n\
                        </td></tr>\n\
                    </table>';

    $('#current_page_container').html(innerHtml);
    $.post(
        siteUrl+"ajaxrequest.php",
        {
            action                  :   "loadCurrentPageLargeImage",
            current_left_img_src    :   current_left_src,
            current_right_img_src   :   current_right_src,
            current_left_width      :   myPageW,
            current_right_width     :   myPageW
        },
        function(ajaxResponse) {
            $('#current_page_container').html(ajaxResponse);
            resetCurrentPageLayout();
            resetlargeImageSize();

            if(pageNo==0){
                $("#current_left_page_container").css('width',myPageW).css('border',pageBorderTransFullCSS);
                $("#current_right_page_container").css('width',myPageW).css('border',pageBorderFullCSS);
            }
            else if(pageNo==numberOfPages-1){
                $("#current_left_page_container").css('width',myPageW).css('border',pageBorderFullCSS);
                $("#current_right_page_container").css('width',myPageW).css('border',pageBorderTransFullCSS);
            }else{
                $("#current_left_page_container").css('width',myPageW).css('border',pageBorderFullCSS);
                $("#current_right_page_container").css('width',myPageW).css('border',pageBorderFullCSS);
            }

            if(zoom){
                //continueZoom();
                ResetZoom();
            }
        }
        );

}
function loadNextPageLargeImages(pageNo){

    if(pageNo   ==  numberOfPages-1){
        $("#next_left").attr('src',blankImageUrlTrans).css('display','none');
        $("#next_right").attr('src', allPagesArr[0]).css('display','block');
        ;
        $("#next_left_page_container").css('width',myPageW).css('border',pageBorderTransFullCSS);
        $("#next_right_page_container").css('width',myPageW).css('border',pageBorderFullCSS);
    }
    else if(pageNo==numberOfPages-3){
        $("#next_left").attr('src', allPagesArr[numberOfPages-1]).css('display','block');
        ;
        $("#next_right").attr('src',blankImageUrlTrans).css('display','none');
        $("#next_left_page_container").css('width',myPageW).css('border',pageBorderFullCSS);
        $("#next_right_page_container").css('width',myPageW).css('border',pageBorderTransFullCSS);
    }
    else if(pageNo  ==  0){
        $("#next_left").attr('src', allPagesArr[pageNo+1]).css('display','none');
        $("#next_right").attr('src', allPagesArr[(pageNo+2)]).css('display','block');
        ;
        $("#next_left_page_container").css('width',myPageW).css('border',pageBorderTransFullCSS);
        $("#next_right_page_container").css('width',myPageW).css('border',pageBorderFullCSS);
    }
    else{
        $("#next_left").attr('src', allPagesArr[pageNo+2]).css('display','block');
        ;
        $("#next_right").attr('src', allPagesArr[(pageNo+3)]).css('display','block');
        ;
        $("#next_left_page_container").css('width',myPageW).css('border',pageBorderFullCSS);
        $("#next_right_page_container").css('width',myPageW).css('border',pageBorderFullCSS);
    }
}

function initLoadThumbImages(){
    $('#contents').append(
        $('<div></div>')
        .attr({
            id : ("thumb_image_container")
        })
        .css({
            display         :   "none",
            top             :   "0px",
            left            :   "0px"
        })
        );
    /*
     * Load starting pages
     */
    $('#thumb_image_container').append(
        $('<img></img>')
        .attr({
            id      :   ("thumb_image0"),
            src     :   thumbPagesArr[0]
        })
        );
    thumbCacheImages.push(0);

    for(i = 0; i < thumbCacheSize; i++){
        loadThumbImage(i+1);
    }
    /*
     * Load ending pages
     */
    $('#thumb_image_container').append(
        $('<img></img>')
        .attr({
            id      :   ("thumb_image" + (numberOfPages-1)),
            src     :   thumbPagesArr[numberOfPages-1]
        })
        );
    thumbCacheImages.push(numberOfPages+1);
    for(i = numberOfPages-2; i > numberOfPages-thumbCacheSize; i--){
        loadThumbImage(i+1);
    }
}
function loadThumbImageByNo(pageNo){
    if(pageNo<0 || pageNo>numberOfPages){
        pageNo = numberOfPages+1-(loadThumbImagePerRequest*2);
    }
    else if(pageNo>0){
        pageNo = ((pageNo-loadThumbImagePerRequest)>=1)?(pageNo-loadThumbImagePerRequest+2):1;
    }
    for(loadingPage=pageNo;loadingPage<pageNo+loadThumbImagePerRequest;loadingPage++){
        if(!searchArray(loadingPage,thumbCacheImages))loadThumbImage(loadingPage);
    }
}
function loadThumbImage(laodingPage){
    if($("#thumb_image" + laodingPage).length == 0){
        $('#thumb_image_container').append(
            $('<img></img>')
            .attr({
                id      :   ("thumb_image" + laodingPage),
                src     :   thumbPagesArr[laodingPage]
            })
            );
        thumbCacheImages.push(laodingPage);
    }
}

function initAfterPageLoad(){
    allowPageClick = true; //allow clicking page directly
    pageShadow     = true; //true or false, display shadow along the length of inside right page
    ini();
    if(openPage != 0){
        getImageInformation(openPage);
    }
}
