/// コンテンツ閲覧画面_マーキングツールバーオーバーレイ
/// <reference path="../common/js/avweb.js" />

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

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

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

/// <reference path="../common/js/jquery-1.8.1.min.js" />

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

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

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


/// ===============================================================================================
/// Marking group [start]
/// ===============================================================================================

// Set default value for easer.
function Marking_SetDefaultValue() {
};

/*
----------------------------------------------------------------------------
Event groups [start]
----------------------------------------------------------------------------
*/
function dlgMarking_dspSave_click() {
    imgMarkerTemp = canvas_marker.toDataURL("image/png;");
	
	var img = new Image();
	img.onload = function () {
	    //TRB00098
	    context_draw.drawImage(img, 0, 0, canvas_draw.width, canvas_draw.height);
        //TRB00098
        
	    
        //START TRB00098         
        var saveImg = new Image();
        var saveImgUrl = canvas_draw.toDataURL("image/png");  
        
        saveImg.onload = function(){
            /*create new entity marking */
            var marking = new MarkingEntity();
            marking.contentid = contentID;
            if(contentType == ContentTypeKeys.Type_Image){         
                marking.pageNo = 1;
            }else{
                marking.pageNo = changePageIndex(getPageIndex());
            }
            var saveCanvas = document.createElement('canvas');
            //#11478
            //saveCanvas.width = canvas_offscreen.width;
            //saveCanvas.height = canvas_offscreen.height;
            saveCanvas.width = widthEachPageApi;
            saveCanvas.height = heightEachPageApi;
            
            var saveContext = saveCanvas.getContext('2d');
            saveContext.drawImage(saveImg, 0, 0, saveCanvas.width, saveCanvas.height);
            
            marking.content = saveCanvas.toDataURL("image/png");     
            //END TRB00098       
            //=== Start Function : No.17    Editor : Long   Date: 07/30/2013   Summary : Set UTC time and UUID when create new marking/maker.
            marking.markingid = getUUID();
            marking.registerDate = new Date();
            //=== End Function : No.17    Editor : Long   Date: 07/30/2013   Summary : Set UTC time and UUID when create new marking/maker.    
            /* insert marking */
            var arr = ClientData.MarkingData();
            var nIndexMarking = -1;
    
            for (var nIndex = 0; nIndex < arr.length; nIndex++) {
                if (arr[nIndex].contentid == contentID
                && arr[nIndex].pageNo == changePageIndex(getPageIndex())) {
                    nIndexMarking = nIndex;
                    break;
                }
            }
    
            if (isDrawing == true) {/* if has draw image */
                if (isClearDrawing == true) {
                    arr.splice(nIndexMarking, 1);
                } else {
                    /* case not exist marking */
                    if (nIndexMarking == -1) {
                        arr.push(marking);
                    } else {
                        /* case exist marking */
                       //=== Start Function : No.17    Editor : Long   Date: 07/30/2013   Summary : Set UTC time when edit marking/maker.
                       var editedMarkingEntity = arr[nIndexMarking];
                       //#11478
                       //editedMarkingEntity.content = canvas_draw.toDataURL("image/png");
                       editedMarkingEntity.content = marking.content;
                       editedMarkingEntity.registerDate = new Date();
                       
                       arr[nIndexMarking] =  editedMarkingEntity;                                        
                       //arr[nIndexMarking] = marking;  
                       //=== End Function : No.17    Editor : Long   Date: 07/30/2013   Summary : Set UTC time when edit marking/maker.                                                        
                    }
                }
            }
    
            /*add marking to local storage */
            ClientData.MarkingData(arr);
    
            // Close dialog
            //$("#dlgMarking").dialog('close');
            $("#dlgMarking").hide();
            ClientData.IsAddingMarking(false);
            $('#draw_canvas').css('display', 'none');
    
            /*set flag change marking */
            ClientData.isChangedMarkingData(true);
    
            ClientData.IsHideToolbar(false);
    
            /* draw again*/
            //drawCanvas();
            //disableAllControl();  
            handleDisplayToolbar();
            /* visible button fullscreen */
            //$('#control_screen_2').show();
    
            /* init clear drawing canvas */
            isClearDrawing = false;
        };
        saveImg.src = saveImgUrl;                                               
	};
	img.src = imgMarkerTemp;	
	
	context_marker.clearRect(0, 0, canvas_marker.width, canvas_marker.height);
	
	$('#marker_canvas').css('display','none');		 

};

function dlgMarking_dspCancel_click() {
    // Close dialog    
    $("#dlgMarking").hide();  	
    
    /* init clear drawing canvas */
    isClearDrawing = false;
    
    ClientData.IsAddingMarking(false);
	$('#draw_canvas').css('display','none');    
   
    /*$('#imgmarkingtoolbar').removeClass('markingToolbar');
	$('#imgmarkingtoolbar').addClass('markingToolbar_off');
	$("#imgmarkingtoolbar").removeAttr("style")*/
	
    ClientData.IsHideToolbar(false);
    context_draw.clearRect(0, 0, canvas_draw.width, canvas_draw.height);
    
    /* draw again*/
    //drawCanvas();
    //disableAllControl();
    handleDisplayToolbar();
    /* visible button fullscreen */
	//$('#control_screen_2').show();   
	
	$('#marker_canvas').css('display','none');	
};

function dlgMarking_dspPgClear_click() {
    
    // Close dialog
     context_draw.clearRect(0, 0, canvas_draw.width, canvas_draw.height);
     context_marker.clearRect(0, 0, canvas_marker.width, canvas_marker.height);

     isClearDrawing = true;
     
     /* clear on local */
     var arr = ClientData.MarkingData();	
};

/* draw marker canvas to draw canvas */
function canvasMarkerToCanvasDraw(){
    imgMarkerTemp = canvas_marker.toDataURL("image/png;");
	
	var img = new Image();
	img.onload = function () {
	    context_draw.drawImage(img, 0, 0, canvas_draw.width, canvas_draw.height);
	};
	img.src = imgMarkerTemp;	
	
	context_marker.clearRect(0, 0, canvas_marker.width, canvas_marker.height);
	
	 $('#marker_canvas').css('display','none');		 
};


function dlgMarking_imgPen_click() {
    // Choose pen
    $("#dlgMarking_imgMaker").removeClass();
    $("#dlgMarking_imgPen").addClass('toolbar_icon_on');
    $("#dlgMarking_imgEraser").removeClass();

    ClientData.MarkingType("pen");
    markingType = "pen";
    
    canvasMarkerToCanvasDraw();
    
    setDefaultImageForMarking();
};
function dlgMarking_imgPenOption_click() {
	/*disable button */
	disableButtonMarking();
	//ClientData.IsAddingMarking(false);
	/* display dialog overlay */
    $("#overlay").show();
    isAddingMarking = ClientData.IsAddingMarking();
	
    // Show option of pen
    ShowPen($('#dlgPen'));
    
    /* draw maker canvas */
    if(markingType == "maker"){
   		canvasMarkerToCanvasDraw();
    }
    
    ClientData.MarkingType("pen");
    markingType = "pen";
    
    $('#dlgMarking_imgPenOption').removeClass();
	$('#dlgMarking_imgPenOption').addClass('pen_option_selected');
	
	setDefaultImageForMarking();
	
};
function dlgMarking_imgMaker_click() {
    // Choose maker
    $("#dlgMarking_imgMaker").addClass('toolbar_icon_on');
    $("#dlgMarking_imgPen").removeClass();
    $("#dlgMarking_imgEraser").removeClass();

    ClientData.MarkingType("maker");
    markingType = "maker";
    $('#marker_canvas').css('display','block');    
    
    setDefaultImageForMarking();
};
function dlgMarking_imgMakerOption_click() {
	/*disable button */
	disableButtonMarking();
	//ClientData.IsAddingMarking(false);
	 /* display dialog overlay */
    $("#overlay").show();
    isAddingMarking = ClientData.IsAddingMarking();
	
    // Show option of maker
    ShowMaker($('#dlgMaker'));
    
    ClientData.MarkingType("maker");
    markingType = "maker";
    $('#marker_canvas').css('display','block');   
    
    $('#dlgMarking_imgMakerOption').removeClass();
	$('#dlgMarking_imgMakerOption').addClass('maker_option_selected');
	
	setDefaultImageForMarking();
};
function dlgMarking_imgEraser_click() {
    // Choose eraser
    $("#dlgMarking_imgMaker").removeClass();
    $("#dlgMarking_imgPen").removeClass();
    $("#dlgMarking_imgEraser").addClass('toolbar_icon_on');

    ClientData.MarkingType("eraser");
    markingType = "eraser";
    canvasMarkerToCanvasDraw(); 
    
    setDefaultImageForMarking(); 
};
function dlgMarking_imgEraserOption_click() {
	/*disable button */
	disableButtonMarking();
	//ClientData.IsAddingMarking(false);
	/* display dialog overlay */
    $("#overlay").show();
	isAddingMarking = ClientData.IsAddingMarking();
	
    // Show option of maker
    ShowEraser();
    
    /* draw maker canvas */
    if(markingType == "maker"){
   		canvasMarkerToCanvasDraw();
    }
    
    ClientData.MarkingType("eraser");
    markingType = "eraser";
    
    $('#dlgMarking_imgEraserOption').removeClass();
	$('#dlgMarking_imgEraserOption').addClass('eraser_option_selected');
	
	setDefaultImageForMarking();
};

function dlgMarking_imgMin_click() {
    //$("#dlgMarking_body").slideToggle('fast');
    $("#dlgMarking_imgMin").hide();  
    $("#dlgMarking_imgMax").show();
	$("#dlgMarking_body").removeClass('markingBoxBody_on');
    $("#dlgMarking_body").addClass('markingBoxBody_off');
    //$('#dlgMarking').dialog({ height: 80});
    //$('#dlgMarking').css('height', '29px');
};
function dlgMarking_imgMax_click() {
    //$("#dlgMarking_imgMin").slideToggle('fast');
    $("#dlgMarking_imgMax").hide();
    $("#dlgMarking_imgMin").show();
	$("#dlgMarking_body").addClass('markingBoxBody_on');
    $("#dlgMarking_body").removeClass('markingBoxBody_off');
    //$('#dlgMarking').dialog({height: 350});
    //$('#dlgMarking').css('height', '300px');
};
function dlgMarking_imgClose_click() {
    $("#dlgMarking").hide();
    ClientData.IsHideToolbar(true);
    disableAllControl();
    
};
/*
----------------------------------------------------------------------------
Event groups [ end ]
----------------------------------------------------------------------------
*/

function setDefaultImageForMarking(){	
	/* pen */
	if(markingType == "pen"){
		$('#dlgMarking_imgPen').removeClass();
		$('#dlgMarking_imgPen').addClass('pen_selected');
		
	}else{
		$('#dlgMarking_imgPen').removeClass();
		$('#dlgMarking_imgPen').addClass('pen_hover');
	}
	
	/* maker */
	if(markingType == "maker"){
		$('#dlgMarking_imgMaker').removeClass();
		$('#dlgMarking_imgMaker').addClass('maker_selected');
		
	}else{
		$('#dlgMarking_imgMaker').removeClass();
		$('#dlgMarking_imgMaker').addClass('maker_hover');
	}
	
	/* maker */
	if(markingType == "eraser"){
		$('#dlgMarking_imgEraser').removeClass();
		$('#dlgMarking_imgEraser').addClass('eraser_selected');
		
	}else{
		$('#dlgMarking_imgEraser').removeClass();
		$('#dlgMarking_imgEraser').addClass('eraser_hover');
	}
};

/* disable all button on dialog marking */
function disableButtonMarking(){
	$('#dlgMarking_dspSave').unbind('click');
	$('#dlgMarking_dspCancel').unbind('click');
	$('#dlgMarking_dspPgClear').unbind('click');
	$('#dlgMarking_imgMin').unbind('click');
	$('#dlgMarking_imgMax').unbind('click');
	$('#dlgMarking_imgClose').unbind('click');
	$('#dlgMarking_imgPen').unbind('click');
	$('#dlgMarking_imgPenOption').unbind('click');
	$('#dlgMarking_imgMaker').unbind('click');
	$('#dlgMarking_imgMakerOption').unbind('click');
	$('#dlgMarking_imgEraser').unbind('click');
	$('#dlgMarking_imgEraserOption').unbind('click');
	
	/* set cursor default*/
	$("#dlgMarking_dspSave").css('cursor', 'default');
	$("#dlgMarking_dspCancel").css('cursor', 'default');
	$("#dlgMarking_dspPgClear").css('cursor', 'default');
	$("#dlgMarking_imgMin").css('cursor', 'default');
	$("#dlgMarking_imgMax").css('cursor', 'default');
	$("#dlgMarking_imgClose").css('cursor', 'default');
	$("#dlgMarking_imgPen").css('cursor', 'default');
	$("#dlgMarking_imgPenOption").css('cursor', 'default');
	$("#dlgMarking_imgMaker").css('cursor', 'default');
	$("#dlgMarking_imgMakerOption").css('cursor', 'default');
	$("#dlgMarking_imgEraser").css('cursor', 'default');
	$("#dlgMarking_imgEraserOption").css('cursor', 'default');
	
	
};

/* enable all button con dialog marking */
function enableButtonMarking(){
	disableButtonMarking();
	$('#dlgMarking_dspSave').bind('click',dlgMarking_dspSave_click);
	$('#dlgMarking_dspCancel').bind('click',dlgMarking_dspCancel_click);
	$('#dlgMarking_dspPgClear').bind('click',dlgMarking_dspPgClear_click);
	$('#dlgMarking_imgMin').bind('click',dlgMarking_imgMin_click);
	$('#dlgMarking_imgMax').bind('click',dlgMarking_imgMax_click);
	$('#dlgMarking_imgClose').bind('click',dlgMarking_imgClose_click);
	$('#dlgMarking_imgPen').bind('click',dlgMarking_imgPen_click);
	$('#dlgMarking_imgPenOption').bind('click',dlgMarking_imgPenOption_click);
	$('#dlgMarking_imgMaker').bind('click',dlgMarking_imgMaker_click);
	$('#dlgMarking_imgMakerOption').bind('click',dlgMarking_imgMakerOption_click);
	$('#dlgMarking_imgEraser').bind('click',dlgMarking_imgEraser_click);
	$('#dlgMarking_imgEraserOption').bind('click',dlgMarking_imgEraserOption_click);	
	
	/* set cursor pointer*/
	$("#dlgMarking_dspSave").css('cursor', 'pointer');
	$("#dlgMarking_dspCancel").css('cursor', 'pointer');
	$("#dlgMarking_dspPgClear").css('cursor', 'pointer');
	$("#dlgMarking_imgMin").css('cursor', 'pointer');
	$("#dlgMarking_imgMax").css('cursor', 'pointer');
	$("#dlgMarking_imgClose").css('cursor', 'pointer');
	$("#dlgMarking_imgPen").css('cursor', 'pointer');
	$("#dlgMarking_imgPenOption").css('cursor', 'pointer');
	$("#dlgMarking_imgMaker").css('cursor', 'pointer');
	$("#dlgMarking_imgMakerOption").css('cursor', 'pointer');
	$("#dlgMarking_imgEraser").css('cursor', 'pointer');
	$("#dlgMarking_imgEraserOption").css('cursor', 'pointer');
};

function ShowMarking() {
    //$("#dlgMarking_body").show();
    //$("#dlgMarking_body").addClass('markingBoxBody_on');
    //$("#dlgMarking_body").removeClass('markingBoxBody_on');
    
    
    //$('#dlgMarking').dialog({ height: 350 });
    //$('#dlgMarking').css('height', '300px');

    //$('#dlgMarking').dialog("open");
    $('#dlgMarking').css('top', '40px');
	$('#dlgMarking').css('left', '10px');
    $('#dlgMarking').show();
    //$('#dlgMarking').fadeIn('fast');	
	    
//    $('#dlgMarking').center();
//    $('#dlgMarking').top();
//    $('#dlgMarking').bottom();

    //$(".ui-dialog-titlebar").hide();
    Marking_SetDefaultValue();
    //$("#dlgMarking_imgMin").show();
   // $("#dlgMarking_imgMax").hide();

    dlgMarking_imgPen_click();
    
    setDefaultImageForMarking();
    
	$('#dlgMarking_imgPenOption').removeClass();
	$('#dlgMarking_imgPenOption').addClass('pen_option_hover');
	
	$('#dlgMarking_imgMakerOption').removeClass();
	$('#dlgMarking_imgMakerOption').addClass('maker_option_hover');
	
	$('#dlgMarking_imgEraserOption').removeClass();
	$('#dlgMarking_imgEraserOption').addClass('eraser_option_hover');
};

// Setting dialog
$(function () {
	
    $("#dlgMarking_dspSave").click(dlgMarking_dspSave_click);
    $("#dlgMarking_dspCancel").click(dlgMarking_dspCancel_click);
    $("#dlgMarking_dspPgClear").click(dlgMarking_dspPgClear_click);

    // Icons
    $("#dlgMarking_imgMin").click(dlgMarking_imgMin_click);
    $("#dlgMarking_imgMax").click(dlgMarking_imgMax_click);
    $("#dlgMarking_imgClose").click(dlgMarking_imgClose_click);
    $("#dlgMarking_imgPen").click(dlgMarking_imgPen_click);
    $("#dlgMarking_imgPenOption").click(dlgMarking_imgPenOption_click);
    $("#dlgMarking_imgMaker").click(dlgMarking_imgMaker_click);
    $("#dlgMarking_imgMakerOption").click(dlgMarking_imgMakerOption_click);
    $("#dlgMarking_imgEraser").click(dlgMarking_imgEraser_click);
    $("#dlgMarking_imgEraserOption").click(dlgMarking_imgEraserOption_click);
    /*
    $('#dlgMarking').dialog({
        autoOpen: false,
        title: '',
        modal: false,
        resizable: false,
        width: 300,
        height: 350
    });
    */
    $('#dlgMarking').draggable({
        revert: false
    });
   

});
/// ===============================================================================================
/// Marking group [ end ]
/// ===============================================================================================


