
/// <reference path="../common/js/jquery-ui-1.8.23.custom.min.js" />

/// <reference path="../common/js/common.js" />

/* init const */
var WIDTH_DIALOG_BOOKMARK = 300;
var HEIGHT_DIALOG_BOOKMARK = 700;
var HEIGHT_DIALOG_BOOKMARK_NO_BOOKMARK = 200;

var WIDTH_DIALOG_INDEX = 300;
var HEIGHT_DIALOG_INDEX = 700;
var HEIGHT_DIALOG_INDEX_NO_INDEX = 200;

var WIDTH_DIALOG_SEARCH = 300;
var HEIGHT_DIALOG_SEARCH = 700;
var HEIGHT_DIALOG_SEARCH_NO_SEARCH = 200;

var WIDTH_DIALOG_COPYTEXT = 300;
var HEIGHT_DIALOG_COPYTEXT = 500;
var HEIGHT_DIALOG_COPYTEXT_NO_COPYTEXT = 200;

/* init variable */
var pageImages = '';
var totalPage = 0;
var pageObjects = [];
var pageObjectsData = [];
var tool;
var isFullScreen = false;
var ctrlMode = false;
var dataWebContentPage;
var dataPageTitle;
var dataJsonType4;
var dataJsonType5;
var contentID;
var isOpenPopUpText = false;
var isOpenPopUpMemo = true;
var objEditMemo;
var isCopyMemo = false;
var isExistCopyMemo = false;
var posXPopupMemo = 0;
var posYPopupMemo = 0;
var contentName = '';
var nCountObjectLoad = 0;
var nAjaxLoad = 0;
var isClearDrawing = false;
var isDrawing = false;
var mediaType4_changeImage = 0;
var isLoadingObject = false;
var isFirstLoad = true;
var nPositionCanvas = [];
var arrImage = [];
var posTxtSearch = [];
var sx, sy;
var imgDrawingTemp;
var imgMarkerTemp;
var isSizingDrawCanvas = false;
var isDisplayBookMarkList = false;
var avwUserEnvObj = new UserEnvironment();
var canvas_marker;
var context_marker;
var canvas_draw;
var context_draw;
var canvas_main;
var context_main;
var canvas_offscreen;
var context_offscreen;
var isAddingMarking;
var penSize;
var penColor;
var makerSize;
var makerColor;
var eraseSize;
var markingType;
var isDisplayCopyText = false;
var isDisplayListIndex = false;
var isChangeSizeScreen = false;
var widthContentImage = 0;
var heightContentImage = 0;
var widthEachPage = 0;
var heightEachPage = 0;
var px = 0;
var py = 0;
var isPressLeftMouse = false;
var isMoveOutDrawCanvas = false;
var typeSelectMemo = 0;
var arrThumbnailsLoaded = [];
var isSendingData = true;
var isPlayBGMUnlock = false;
var isJumpNotFromFirstPage = false;
var standardRatio = 1;
var currentRatio = 1;
var resourceImage = new Image();

/* zoom video */
var pxVideo;
var pyVideo;
var wVideo;
var hVideo;
var videoScale;

//Start Function: No.12
//Content's types

var contentType;

var imageTypeData;

var nextPageImage = '';
var nextPageObjects = [];
var nextContent = new Content();
var prevPageObjects = [];
var prevPageImage = '';
var prevContent = new Content();
var animateTypeKeys = {
    Type_Slide : 0,
    Type_FadeIn : 1
};

var animateType;
var animatePeriod;

function getNextContent(){
    return nextContent;    
};

function getPrevContent(){
    return prevContent;  
};


//End Function: No.12


//Start Function : No.20
var _moveFlag = false;
var _moveNum = false;
var _isPageNaviTouch = false;
var _isPageZoomTouch = false;
var _clickFirstPos = null;
var _clickLastPos = null;
var _touchFirstPos = null;
var _touchLastPos = null;
var touchStartedTime = null;
var _touchMoveTimePeriod = 0; // 0 s
var _touchMoveTimePeriodInvalid = 5000; // 5 s
var _bHasNext = false; 
var _bHasPrev = false; 
var _startPoints = [];
var _bufferPoints = [];
var _bImageMainReload = true;   // to not run onload again on the next time on set src value 
var _bImageMainPreReload = true;    // to not run onload again on the next time on set src value
var _bImageMainNextReload = true;   // to not run onload again on the next time on set src value
var _lastDist = 0;
var _lastScaleDelta = 0;

var _bWin8TouchEnabled = false;
var _bTransitionEnable = true;
//End Function : No.20


//Start Function : No.9 
var _object3DImageArr = [];

//End Function : No.9
/*disable all control on page */
function disableAllControl() {
    /* set cursor pointer*/
    $("#imgHome").css('cursor', 'default');
    $("#imgBack").css('cursor', 'default');
    $("#listbookmark").css('cursor', 'default');
    $("#imgbookmark").css('cursor', 'default');
    $("#listindex").css('cursor', 'default');
    $("#copytext").css('cursor', 'default');
    $("#imgmemo").css('cursor', 'default');
    $("#imgaddmemo").css('cursor', 'default');
    $("#imgmarking").css('cursor', 'default');
    $("#imgmarkingtoolbar").css('cursor', 'default');
    $("#firstpage").css('cursor', 'default');
    $("#prevpage").css('cursor', 'default');
    $("#nextpage").css('cursor', 'default');
    $("#lastpage").css('cursor', 'default');
    $("#zoomfit").css('cursor', 'default');
    $("#zoomin").css('cursor', 'default');
    $("#zoomout").css('cursor', 'default');
    $("#control_screen").css('cursor', 'default');
    $("#control_screen_2").css('cursor', 'default');
    $("#slider_page").css('cursor', 'default');
    $(".ui-slider-handle").css('cursor', 'default');

    $('#imgHome').unbind('click');
    $('#imgHome').removeClass();
    $('#imgHome').addClass('home_off');

    /* back button */
    $('#imgBack').unbind('click');
    $('#imgBack').removeClass();
    $('#imgBack').addClass('back_off');

    /*list bookmark */
    $('#listbookmark').unbind('click');
    $('#listbookmark').removeClass();
    $('#listbookmark').addClass('bmList_off');

    $('#imgbookmark').unbind('click');
    $('#imgbookmark').removeClass();
    $('#imgbookmark').addClass('bmAdd_off');

    $('#listindex').unbind('click');
    $('#listindex').removeClass();
    $('#listindex').addClass('index_off');

    $('#copytext').unbind('click');
    $('#copytext').removeClass();
    $('#copytext').addClass('copy_off');

    /* imgage display memo */
    $('#imgmemo').unbind('click');
    $('#imgmemo').removeClass();
    $('#imgmemo').addClass('memoDisplay_off');

    /* image add memo */
    $('#imgaddmemo').unbind('click');
    $('#imgaddmemo').removeClass();
    $('#imgaddmemo').addClass('memoAdd_off');

    $('#imgmarking').unbind('click');
    $('#imgmarking').removeClass();
    $('#imgmarking').addClass('marking_off');

    $('#imgmarkingtoolbar').unbind('click');
    $('#imgmarkingtoolbar').removeClass();
    $('#imgmarkingtoolbar').addClass('markingToolbar_off');

    $('#firstpage').unbind('click');
    $('#firstpage').removeClass();
    $('#firstpage').addClass('begin_off');
    $('#prevpage').unbind('click');
    $('#prevpage').removeClass();
    $('#prevpage').addClass('prev_off');
    $('#nextpage').unbind('click');
    $('#nextpage').removeClass();
    $('#nextpage').addClass('next_off');
    $('#lastpage').unbind('click');
    $('#lastpage').removeClass();
    $('#lastpage').addClass('last_off');

    $('#zoomfit').unbind('click');
    $('#zoomfit').removeClass();
    $('#zoomfit').addClass('fit_off');
    $('#zoomin').unbind('click');
    $('#zoomin').removeClass();
    $('#zoomin').addClass('expansion_off');
    $('#zoomout').unbind('click');
    $('#zoomout').removeClass();
    $('#zoomout').addClass('reduction_off');
    if (avwUserEnvObj.os != "android") {
        $("#slider_page").slider("option", "disabled", true);
    }

    disable('#txtSearch', '#txtSlider');

    $('#button_next_canvas').css('display', 'none');
    $('#button_pre_canvas').css('display', 'none');

};

/*enable all control on page */
function enableAllControl() {
    disableAllControl();

    /* set cursor pointer*/
    $("#imgHome").css('cursor', 'pointer');
    $("#imgBack").css('cursor', 'pointer');
    $("#listbookmark").css('cursor', 'pointer');
    $("#imgbookmark").css('cursor', 'pointer');

    //Start Function: No.12
    if (contentType == ContentTypeKeys.Type_PDF) {
        $("#listindex").css('cursor', 'pointer');
        $("#copytext").css('cursor', 'pointer');
    }
    //End Function: No.12

    $("#imgmemo").css('cursor', 'pointer');
    $("#imgaddmemo").css('cursor', 'pointer');
    $("#imgmarking").css('cursor', 'pointer');
    $("#imgmarkingtoolbar").css('cursor', 'pointer');
    $("#firstpage").css('cursor', 'pointer');
    $("#prevpage").css('cursor', 'pointer');
    $("#nextpage").css('cursor', 'pointer');
    $("#lastpage").css('cursor', 'pointer');
    $("#zoomfit").css('cursor', 'pointer');
    // $("#zoomin").css('cursor', 'pointer');
    // $("#zoomout").css('cursor', 'pointer');
    $("#control_screen").css('cursor', 'pointer');
    $("#control_screen_2").css('cursor', 'pointer');

    //Start Function: No.12
    if (contentType == ContentTypeKeys.Type_PDF || contentType == ContentTypeKeys.Type_NoFile) {
        $("#slider_page").css('cursor', 'pointer');
        $(".ui-slider-handle").css('cursor', 'pointer');
    }
    //End Function: No.12

    $("#liAddMemo").css('cursor', 'pointer');
    $("#liCopyMemo").css('cursor', 'pointer');
    $("#liDeleteMemo").css('cursor', 'pointer');

    $("#control_screen").removeClass();
    $("#control_screen_2").removeClass();

    $('#imgHome').bind('click', imgHome_click);
    $('#imgHome').removeClass();

    $('#imgBack').bind('click', imgBack_click);
    $('#imgBack').removeClass();

    /*list bookmark */
    $('#listbookmark').bind('click', showListBookMark);
    $('#listbookmark').removeClass();


    $('#imgbookmark').bind('click', bookmarkPage);
    $('#imgbookmark').removeClass();

    //Start Function: No.12
    if (contentType == ContentTypeKeys.Type_PDF) {
        $('#listindex').bind('click', showListPageIndex);
        $('#listindex').removeClass();

        $('#copytext').bind('click', showCopyText);
        $('#copytext').removeClass();
    }
    //End Function: No.12

    /* image memo */
    $('#imgmemo').bind('click', handleMemo);
    $('#imgmemo').removeClass();

    /* image add memo */
    $('#imgaddmemo').bind('click', handleAddMemo);
    $('#imgaddmemo').removeClass();

    $('#imgmarking').bind('click', imgmarking_click);
    $('#imgmarking').removeClass();

    if (ClientData.serviceOpt_marking() == 'Y') {
        $('#imgmarkingtoolbar').bind('click', handleDrawCanvas);
        $('#imgmarkingtoolbar').removeClass();
    }

    checkDisableButtonZoom();
    //Start Function: No.12
    if (contentType == ContentTypeKeys.Type_PDF || contentType == ContentTypeKeys.Type_NoFile) {
        $("#slider_page").slider("option", "disabled", false);
        
        if(contentType == ContentTypeKeys.Type_PDF){
           enable('#txtSearch', '#txtSlider'); 
        }        
        else if(contentType == ContentTypeKeys.Type_NoFile){
            enable('#txtSlider');
        }               
    }
   
    
    if (avwUserEnvObj.os != "ipad" && avwUserEnvObj.os != "android") {
        $('#button_next_canvas').css('display', 'block');
        $('#button_pre_canvas').css('display', 'block');
    }

    if (isTouchDevice() == true) {/* set css for device */
        $('#imgHome').addClass('home_device');
        $('#imgBack').addClass('back_device');

        $("#control_screen").addClass('toolbar_device');
        $("#control_screen_2").addClass('toolbar_device');

        var nBookmarkId = isExistBookmark();
        if (nBookmarkId == -1) {
            $('#imgbookmark').addClass('bmAdd_device');
        } else {
            $('#imgbookmark').addClass('bmAdd_hover');
        }

        if (isDisplayBookMarkList == true) {
            $('#listbookmark').addClass('bmList_hover');
        } else {
            $('#listbookmark').addClass('bmList_device');
        }

        if (isDisplayCopyText == true) {
            $('#copytext').addClass('copy_hover');
        } else {
            $('#copytext').addClass('copy_device');
        }

        if (isDisplayListIndex == true) {
            $('#listindex').addClass('index_hover');
        } else {
            $('#listindex').addClass('index_device');
        }

        if (ClientData.IsDisplayMemo() == true) {
            $('#imgmemo').addClass('memoDisplay_hover');
        } else {
            $('#imgmemo').addClass('memoDisplay_device');
        }

        if (ClientData.IsAddingMemo() == true) {
            $('#imgaddmemo').addClass('memoAdd_hover');
        } else {
            $('#imgaddmemo').addClass('memoAdd_device');
        }

        if (ClientData.IsDisplayMarking() == true) {
            $('#imgmarking').addClass('marking_hover');
        } else {
            $('#imgmarking').addClass('marking_device');
        }
        if (ClientData.serviceOpt_marking() == 'Y') {
            if (ClientData.IsAddingMarking() == true) {
                $('#imgmarkingtoolbar').addClass('markingToolbar_hover');
            } else {
                $('#imgmarkingtoolbar').addClass('markingToolbar_device');
            }
        }
    } else { /* set css for PC */
        $('#imgHome').addClass('home');
        $('#imgBack').addClass('back');
        /*$('#firstpage').addClass('begin');
        $('#prevpage').addClass('prev');
        $('#nextpage').addClass('next');
        $('#lastpage').addClass('last');*/
        $("#control_screen").addClass('toolbar');
        $("#control_screen_2").addClass('toolbar');

        var nBookmarkId = isExistBookmark();
        if (nBookmarkId == -1) {
            $('#imgbookmark').addClass('bmAdd');
        } else {
            $('#imgbookmark').addClass('bmAdd_hover');
        }

        if (isDisplayBookMarkList == true) {
            $('#listbookmark').addClass('bmList_hover');
        } else {
            $('#listbookmark').addClass('bmList');
        }

        if (isDisplayCopyText == true) {
            $('#copytext').addClass('copy_hover');
        } else {
            $('#copytext').addClass('copy');
        }

        if (isDisplayListIndex == true) {
            $('#listindex').addClass('index_hover');
        } else {
            $('#listindex').addClass('index');
        }

        if (ClientData.IsDisplayMemo() == true) {
            $('#imgmemo').addClass('memoDisplay_hover');
        } else {
            $('#imgmemo').addClass('memoDisplay');
        }

        if (ClientData.IsAddingMemo() == true) {
            $('#imgaddmemo').addClass('memoAdd_hover');
        } else {
            $('#imgaddmemo').addClass('memoAdd');
        }

        if (ClientData.IsDisplayMarking() == true) {
            $('#imgmarking').addClass('marking_hover');
        } else {
            $('#imgmarking').addClass('marking');
        }
        if (ClientData.serviceOpt_marking() == 'Y') {
            if (ClientData.IsAddingMarking() == true) {
                $('#imgmarkingtoolbar').addClass('markingToolbar_hover');
            } else {
                $('#imgmarkingtoolbar').addClass('markingToolbar');
            }
        }
    }
    checkExistNextPrePage();
};

/*
Clear canvas
*/
function clearCanvas(targetCanvas) {
    targetCanvas.width = targetCanvas.width;
};

function encode(data) {
    var str = String.fromCharCode.apply(null, data);
    return btoa(str).replace(/.{76}(?=.)/g, '$&\n');
};

function showDialog(modal) {
    $("#overlay").show();
    $("#dialog").fadeIn(300);

    if (modal) {
        $("#overlay").unbind("click");
    }
    else {
        $("#overlay").click(function (e) {
            hideDialog();
        });
    }
};

function hideDialog() {
    $("#overlay").hide();
    $("#dialog").fadeOut(300);
    $('#dialog').children().remove();
};