function ZoomIn(){
    if(gZoomLevel >= maxZoomLevel){
        return;
    }    
    gZoomLevel++;
    Zoom();
    EnableZoomOut();
    EnableZoomReset();
    if(gZoomLevel >= maxZoomLevel){
        DisableZoomIn();
        return;
    }
}

function ZoomOut(){
    if(gZoomLevel < minZoomLevel){
        return;
    }    
    gZoomLevel--;
    Zoom();
    EnableZoomIn();
    if(gZoomLevel < minZoomLevel){
        DisableZoomOut();
        DisableZoomReset();
        return;
    }
}
function Zoom(){
    if(gZoomLevel<0)return;
    var newPageWidth, newPageHeight,newShadowWidth,newShadowHeight;
    if( (gZoomLevel == 1 || gZoomLevel == -1) && !zoom){
        zoom = true;
        var pageNo = 0;
        if(typeof currentPage != "undefined" && currentPage != null)
            pageNo = currentPage;

        var eleLeft  = null;
        var eleRight = null;
        var panningDiv      =   $("#innerImgContainer");
        var main            =   $("#panningDiv");
        var content         =   $("#contents");
        main
        .css({
            width           :   (content.width()-pageBorderWidth*2)+"px",
            height          :   (content.height())+"px",
            left            :   "0px",
            zIndex          :   100,
            top             :   "0px",
            borderWidth     :   pageBorderWidth+"px",
            borderColor     :   pageBorderColor,
            borderStyle     :   pageBorderStyle
        })
        .show();
        
        eleRight    =   $("#current_right");
        eleLeft     =   $("#current_left");

        $("#zoomLeftImage")
        .attr('src', eleLeft.attr('src'))
        .css({
            left                :   "0px",
            float               :   "left",
            border              :   $("#current_left_page_container").css('border'),
            borderTopWidth      :   "0px",
            borderBottomWidth   :   "0px",
            borderLeftWidth     :   "0px"
        });
        $("#zoomRightImage")
        .attr('src', eleRight.attr('src'))
        .css({
            float               :   "left",
            border              :   $("#current_right_page_container").css('border'),
            borderTopWidth      :   "0px",
            borderBottomWidth   :   "0px"
        });
        origPageWidth  = eleLeft.width();
        origPageHeight = eleLeft.height();


        $("#current_page_container").hide();

        MakeDraggable();
    }
    else if(gZoomLevel==0){
        ResetZoom();
        return;
    }
    //alert(currentCenter);zoom_page_shadow
    newPageWidth    = origPageWidth+origPageWidth*gZoomLevel*zoomRate;
    newPageHeight   = origPageHeight+origPageHeight*gZoomLevel*zoomRate;

    /*Adjust left image/page width,height*/
    $("#zoomLeftImage").attr({
        width       :   newPageWidth,
        height      :   newPageHeight
    });

    $("#zoomRightImage").attr({
        width       :   newPageWidth,
        height      :   newPageHeight
    });

    if(gZoomLevel < 0){
        $("#zoomLeftImage").css({
            top         :   ((origPageHeight - newPageHeight)/2) + "px",
            position    :   "absolute"
        });
    }
    $("#zoomRightImage").css({
        left        :   ($("#zoomLeftImage").position().left+newPageWidth) + "px",
        top         :   $("#zoomLeftImage").position().top + "px",
        position    :   "absolute"
    });

    //Zoom out image position reset
    idealRightPosition      =   parseInt($('#panningDiv').position().left    +   $('#panningDiv').width());
    positionRight           =   $("#zoomRightImage").position().left;
    innerImageContainerLeft =   $("#innerImgContainer").offset().left;
    currentRightPosition    =   parseInt( positionRight +   newPageWidth +  innerImageContainerLeft);

    if( currentRightPosition    < idealRightPosition){
        leftImagePosition   =   parseInt(idealRightPosition  -   positionRight   -   newPageWidth);
        $("#innerImgContainer").css({
            left        :   leftImagePosition + "px"
        });
    }

    idealBottomPosition     =   parseInt($('#panningDiv').position().top    +   $('#panningDiv').height());
    positionTop             =   $("#zoomLeftImage").position().top;
    innerImageContainerTop  =   $("#innerImgContainer").offset().top;
    currentBottomPosition   =   parseInt( positionTop +   newPageHeight +  innerImageContainerTop);
    if( currentBottomPosition    < idealBottomPosition){
        leftImagePositionTop    =   parseInt(idealBottomPosition  -   positionTop   -   newPageHeight);
        $("#innerImgContainer").css({
            top        :   leftImagePositionTop + "px"
        });
    }
    if(gZoomLevel<=0)
        DisableDraggable();
    else
        EnableDraggable();
}


function MakeDraggable()
{
    $(function() {
        $("#innerImgContainer").draggable();
    });

}
function DisableDraggable()
{
    $(function() {
        $("#innerImgContainer").draggable('disable');
    });

}
function EnableDraggable()
{
    $(function() {
        $("#innerImgContainer").draggable('enable');
    });

}
function RemoveDraggable()
{
    $(function() {
        $("#innerImgContainer").draggable('destroy');
    });

}

function ResetZoom(){
    if(!zoom)return false;
    zoom = false;
    var ele = $('#panningDiv');
    if(typeof ele != "undefined" && ele.length != 0){
        ele.hide();
        $("#zoomLeftImage").attr({
            src     :   "",
            width   :   origPageWidth,
            height  :   origPageHeight
        });
        $("#zoomLeftImage").css({
            left        :   "0px",
            top         :   "0px",
            position    :   "absolute"
        });
        $("#zoomRightImage").attr({
            src     :   "",
            width   :   origPageWidth,
            height  :   origPageHeight
        });
        $("#innerImgContainer").css({
            top     :   "0px",
            left    :   "0px"
        });
        RemoveDraggable();
        $("#current_page_container").show();

    }
    gZoomLevel = 0;
    EnableZoomIn();
    DisableZoomOut();
    DisableZoomReset();
}
function DisableZoomIn(){
    $('#btnZoomin').attr({
        'class'     :   'butt-zoom-in-inactive',
        'disabled'  :   true
    });
    EnableZoomOut();
}

function EnableZoomIn(){
    $('#btnZoomin').attr({
        'class'     :   'butt-zoom-in',
        'disabled'  :   false
    });
}
function DisableZoomOut(){
    $('#btnZoomout').attr({
        'class'     :   'butt-zoom-out-inactive',
        'disabled'  :   true
    });
    EnableZoomIn();
}
function EnableZoomOut(){
    $('#btnZoomout').attr({
        'class'     :   'butt-zoom-out',
        'disabled'  :   false
    });
}
function DisableZoomReset(){
    $('#btnZoomreset').attr({
        'class'     :   'butt-zoom-ac-inactive',
        'disabled'  :   true
    });
    EnableZoomIn();
}
function EnableZoomReset(){
    $('#btnZoomreset').attr({
        'class'     :   'butt-zoom-ac',
        'disabled'  :   false
    });
}
