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

    allowNavigation = selectNavigation;
    numPixelsToMove     =   Math.round(myPageW/imageFlipSteps);
    numPixels           =   numPixelsToMove;
    if(numPixels > 100){
        numPixels = 100;
    }
    initLoad();

    if($("#contents").length == 0){
        return;
    }

    $("#contents")
    .css({
        position        :   "relative",
        width           :   2*pWidth+"px",
        height          :   pHeight+"px",
        display         :   ""
    });
    contentResize();
}

function autoflipFromUrl(){
    var searchStr   = window.location.search;
    var searchArray = searchStr.substring(1,searchStr.length).split('&');
    var result      = '';
    for (var i=0; i<searchArray.length; i++) {
        result = searchArray[i].split('=');
        if(result[0]=="autoflip"){
            result  = result[1];
            break;
        }
    }
    return(result);
}
function flipToPage(newPage){
    /*
     *newPage must be fixed according to valid pag no before calling this function
     **/
    if(typeof newPage == "number"){
        lastPage = currentPage;
        currentPage = newPage;
    }
    else return;
    //    ResetZoom();
    loadLargeImageByNo(currentPage,true);
}

function continueZoom(){
    $("#zoomLeftImage").attr({
        src :   $("#current_left").attr('src')
    });
    $("#zoomRightImage").attr({
        src :   $("#current_right").attr('src')
    });
}



function startNextFolding(){
    pageNo=getNextPageNumber();
    if(pageNo==0) return;
    getPagination(pageNo);

    if(isMoving){
        return;
    }

    ResetZoom();
    currentPage = pageNo;
    isMoving     = true;

    rightFolding = true;
    leftFolding  = false;

    $("#current_page_container").css('position','absolute');

    currentRightPageContainer   =   $("#current_right_page_container");
    currentLeftPageContainer    =   $("#current_left_page_container");

    nextPageContainer                   =   $("#next_page_container");
    nextPageContainerLeft               =   $("#next_left_page_container");
    nextPageContainerRight              =   $("#next_right_page_container");

    nextLeftRevealImage             =   $("#next_left");

    rightFloatingContainer            =   $("#right_floating_div");
    rightFloatingImage          =   $("#right_floating_image");

    rightFloatingImage
    .attr('src',nextLeftRevealImage.attr('src'))
    .css({
        width       :   nextLeftRevealImage.css('width'),
        height      :   nextLeftRevealImage.css('height')
    })
    .show()
    ;

    nextPageContainer.show();
    nextPageContainerLeft.show();
    nextPageContainerRight.show();


    rightFloatingContainer
    .css({
        "zIndex"    :   199
    })
    .show();
        

    if(currentPage>=numberOfPages-1){
        currentRightPageContainer.unbind('click');
    }
    continueNextFolding(pWidth*2,0);
}
function continueNextFolding(currLeft,currWidth){
    if(currLeft <= numPixels*2){
        rightFolding = false;
        resetNextFolding();
        return;
    }

    currLeft  = currLeft-(numPixels*2);
    currWidth = currWidth+numPixels;
    rightFloatingContainer.css({
        "width" : currWidth+'px'
    });
    if(isHidden==true){
        currentLeftPageContainer.css('width',currentLeftPageContainer.width()-2*numPixels);
    }

    hideWidth = currLeft - pWidth;
    if(hideWidth >  0 ){
        $(currentRightPageContainer).css({
            "width"  :  hideWidth+'px'
        });
    }else{
        if(!isHidden){
            $(currentRightPageContainer).hide();
            isHidden = true;
        }
    }
    ID = window.setTimeout('continueNextFolding('+currLeft+','+currWidth+')',pSpeed);
}
function resetNextFolding(){
    window.clearTimeout(ID);

    rightFloatingContainer.hide();
    $("#right_floating_div").hide();
    rightFloatingContainer.css('width',numPixelsToMove+'px');

    loadCurrentPageLargeImagesWithoutAjax(currentPage);


    $("#current_page_container").show();
    $("#current_left_page_container").show();
    $("#current_right_page_container").show();

    $("#next_page_container").hide();
    $("#previous_page_container").hide();
    
    loadNextPageLargeImages(currentPage);
    loadPreviousPageLargeImages(currentPage);

    $("#current_page_container").css('position','');

    isHidden = false;
    isMoving = false;
}

function startPreviousFolding(){
    pageNo=getPreviousPageNumber();
    if(pageNo    ==  numberOfPages-1) return;
    getPagination(pageNo);

    if(isMoving){
        return;
    }

    isMoving     = true;
    leftFolding  = true;
    rightFolding = false;

    ResetZoom();
    currentPage = pageNo;

    if($.browser.msie){
        initializePreviousFoldingForIE();
    }else{
        initializePreviousFolding();
    }
}
function initializePreviousFolding(){

    previousPageContainer     = $("#previous_page_container");
    previousLeftPageContainer     = $("#previous_left_page_container");
    previousRightPageContainer     = $("#previous_right_page_container");

    previousPageContainer.show();
    previousLeftPageContainer.show();
    previousRightPageContainer.show();


    currentPageContainer =   $("#current_page_container");
    currentLeftPageContainer =   $("#current_left_page_container");
    currentRightPageContainer =   $("#current_right_page_container");

    nextLeftRevealImage             =   $("#previous_right");

    leftFloatingContainer            =   $("#left_floating_div");
    leftFloatingImage          =   $("#left_floating_image");

    leftFloatingImage
    .attr('src',nextLeftRevealImage.attr('src'))
    .css({
        width       :   nextLeftRevealImage.css('width'),
        height      :   nextLeftRevealImage.css('height'),
        float       :   'right'
    })
    .show()
    ;

    leftFloatingContainer
    .css({
        zIndex      :   199,
        width       :   '0px'
    })
    .show();

    $("#current_left").css('float','right');
    $("#current_right").css('float','right');
    currentPageContainer.css('float','right');

    if(currentPage  ==  0){
        currentLeftPageContainer.unbind('click');
    }
    continuePreviousFolding(0,0);
  
}
function continuePreviousFolding(currLeft,currWidth){
    if(currLeft <= numPixels*2 && isHidden){
        leftFolding = false;
        resetPreviousFolding();
        return;
    }
    if(!isHidden){
        currLeft  = currLeft+numPixels;
        currWidth = currWidth+numPixels;
    }else{
        currLeft  = currLeft-2*numPixels;
    }
    hideWidth = pWidth-currWidth;

    if(currWidth  <= (pWidth-numPixels)){
        $(currentLeftPageContainer).css({
            width   :   hideWidth-numPixels+'px'
        });
        leftFloatingContainer.css({
            width   :   currWidth/2+'px'
        });
    }else if(!isHidden){
        currentLeftPageContainer.hide();
        leftFloatingContainer.css({
            width   :   currWidth/2+'px'
        });
        isHidden = true;
        currLeft = pWidth;
    }
    else{
        currentRightPageContainer.css('width',currLeft+'px');
        leftFloatingContainer.css({
            width   :   (leftFloatingContainer.width()+numPixels)+'px'
        });
    }
    ID = window.setTimeout('continuePreviousFolding('+currLeft+','+currWidth+')',pSpeed);
}
function resetPreviousFolding(){
    window.clearTimeout(ID);

    leftFloatingContainer.hide();

    leftFloatingImage.css('float','left');
    $("#current_left").css('float','left');
    $("#current_right").css('float','left');
    currentPageContainer.css('float','left');

    loadCurrentPageLargeImagesWithoutAjax(currentPage);

    $("#current_left_page_container").show();
    $("#current_right_page_container").show();
    $("#current_page_container").show();

    $("#next_page_container").hide();
    loadNextPageLargeImages(currentPage);

    $("#previous_page_container").hide();
    loadPreviousPageLargeImages(currentPage);

    isHidden = false;
    isMoving = false;
    
    return;
}
function initializePreviousFoldingForIE(){

    previousPageContainer       =   $("#previous_page_container");
    previousLeftPageContainer   =   $("#previous_left_page_container");
    previousRightPageContainer  =   $("#previous_right_page_container");

    previousPageContainer.show();
    previousLeftPageContainer.show();
    previousRightPageContainer.show();


    currentPageContainer        =   $("#current_page_container");
    currentLeftPageContainer    =   $("#current_left_page_container");
    currentRightPageContainer   =   $("#current_right_page_container");

    nextLeftRevealImage         =   $("#previous_right");

    leftFloatingContainer       =   $("#left_floating_div");
    leftFloatingImage           =   $("#left_floating_image");

    leftFloatingImage
    .attr('src',nextLeftRevealImage.attr('src'))
    .css({
        width       :   nextLeftRevealImage.css('width'),
        height      :   nextLeftRevealImage.css('height')
    })
    .show()
    ;

    leftFloatingContainer
    .css({
        width       :   '0px'
    })
    .show();

    currentPageContainer.css('float','right');

    if(currentPage  ==  0){
        currentLeftPageContainer.unbind('click');
    }

    $("#current_left_page_container_inner_div").css({
        'right'     :   '0px',
        'position'  :   'absolute'
    });

    $("#current_right_page_container_inner_div").css({
        'right'     :   '0px',
        'position'  :   'absolute'
    });
    $("#left_floating_div_inner_div").css({
        'right'     :   '0px'
        
    });

    $("#current_left_page_container").css({
        'position'  :   'relative'
    });
    $("#current_right_page_container").css({
        'position'  :   'relative'
    });
    //    $("#left_floating_div").css({
    //        'position'  :   'relative'
    //    });
    
    continuePreviousFoldingForIE(0,0);
}
function continuePreviousFoldingForIE(currLeft,currWidth){
    if(currLeft <= numPixels*2 && isHidden){
        leftFolding = false;
        resetPreviousFolding();
        return;
    }
    if(!isHidden){
        currLeft  = currLeft+numPixels;
        currWidth = currWidth+numPixels;
    }else{
        currLeft  = currLeft-2*numPixels;
    }
    hideWidth = pWidth-currWidth;

    if(currWidth  <= (pWidth-numPixels)){
        $(currentLeftPageContainer).css({
            width   :   hideWidth-numPixels+'px'
        });
        leftFloatingContainer.css({
            width   :   currWidth/2+'px'
        });
    }else if(!isHidden){
        currentLeftPageContainer.hide();
        leftFloatingContainer.css({
            width   :   currWidth/2+'px'
        });
        isHidden = true;
        currLeft = pWidth;
    }
    else{
        currentRightPageContainer.css('width',currLeft+'px');
        leftFloatingContainer.css({
            width   :   (leftFloatingContainer.width()+numPixels)+'px'
        });
    }
    ID = window.setTimeout('continuePreviousFoldingForIE('+currLeft+','+currWidth+')',pSpeed);
}
function resetPreviousFoldingForIE(){
    window.clearTimeout(ID);

    leftFloatingContainer.hide();

    leftFloatingImage.css('float','left');
    $("#current_left").css('float','left');
    $("#current_right").css('float','left');
    currentPageContainer.css('float','left');

    loadCurrentPageLargeImagesWithoutAjax(currentPage);

    $("#current_left_page_container").show();
    $("#current_right_page_container").show();
    $("#current_page_container").show();

    $("#next_page_container").hide();
    loadNextPageLargeImages(currentPage);

    $("#previous_page_container").hide();
    loadPreviousPageLargeImages(currentPage);

    isHidden = false;
    isMoving = false;

    return;
}


function focused(elemId){
    $("#"+elemId).select();
}
