
//名前空間用のオブジェクトを用意する
var CONTENTVIEW_MEMO = {};

CONTENTVIEW_MEMO.createMemoDialog = function(){
	CONTENTVIEW_MEMO.targetDiv.show();
	CONTENTVIEW_MEMO.targetDiv.html('');
	CONTENTVIEW_MEMO.targetDiv.append(
	'<aside id="memoWrapper" class="MemoIndexBox">'
	+ '	<h1 class="indexBoxHd">' + I18N.i18nText('txtMemo')
	+'		<a id="memoClosing2" class="delete"></a>'
	+'	</h1>'
	+'	<div id="memoArea" class="indexBoxBody_on">'
	+'		<textarea id="txaMemoContent" style="resize: none; height: 302px; width: 452px; margin-bottom: 10px"></textarea>'
	+'		<div style="width: 450px;">'
	+ '			<a id="Memo_btnCancel" style="float:right" class="lang cancelbtn" lang="dspCancel">' + I18N.i18nText('dspCancel') + '</a>'
	+ '			<a id="Memo_btnDel" style="float:right" class="lang cancelbtn" lang="dspDelete">' + I18N.i18nText('dspDelete') + '</a>'
	+ '			<a id="Memo_btnSave" style="float:right" class="lang cancelbtn" lang="dspSave">' + I18N.i18nText('dspSave') + '</a>'
	+'		</div>'
	+'	</div>'
	+'</aside>');
	
	$('#txaMemoContent').focus();
	
	CONTENTVIEW_MEMO.handleMemoEventFunction();
};

CONTENTVIEW_MEMO.handleMemoEventFunction = function(){
	$('#Memo_btnSave').click(CONTENTVIEW_MEMO.buttonSaveFunction);
	$('#Memo_btnDel').click(CONTENTVIEW_MEMO.MemoDelFunction);
	$('#Memo_btnCancel').click(CONTENTVIEW_MEMO.MemoCancelFunction);
	//$('.delete').click(CONTENTVIEW_MEMO.MemoCancelFunction);
	$("#memoClosing2").on({
		'click touchend': function(ev){
			CONTENTVIEW_MEMO.MemoCancelFunction(ev);
			return false;
		},
		'touchstart touchmove': function(){
			//これを入れないと次にダイアログを開くと表示位置が大きくズレる
			return false;
		}
	});
	
};

CONTENTVIEW_MEMO.memoSaveFunction = function(){

	var tempArr = [];	
	
	var memoObj = new MemoEntity();
	memoObj.pageNo = CONTENTVIEW_MEMO.pageid;
	memoObj.contentid = CONTENTVIEW_MEMO.conid;
	memoObj.Text = $('#txaMemoContent').val();
	var imagePt = CONTENTVIEW.screenToImage(CONTENTVIEW_MEMO.targetX, CONTENTVIEW_MEMO.targetY);
	
    //#11478 逆に縮める対応
    var canvasWidth = $('#offscreen').width();
    var canvasHeight = $('#offscreen').height();
    //var tempRatioWidth = canvasWidth / CONTENTVIEW_GENERAL.widthEachPageApi;
    var tempRatioWidth = CONTENTVIEW_GENERAL.widthEachPageApi / canvasWidth;
    if( tempRatioWidth > 1 ){
        tempRatioWidth = 1;
    }
    var tempRatioHeight = CONTENTVIEW_GENERAL.heightEachPageApi / canvasHeight;
    if( tempRatioHeight > 1 ){
        tempRatioHeight = 1;
    }
    memoObj.posX = Math.round(imagePt.x * tempRatioWidth);
    memoObj.posY = Math.round(imagePt.y * tempRatioHeight);
    //memoObj.posX = imagePt.x;
    //memoObj.posY = imagePt.y;
    
	//=== Start Function : No.17    Editor : Long   Date: 07/30/2013   Summary : Set UTC time and UUID when create new memo.
	memoObj.memoid = COMMON.getUUID();
	memoObj.registerDate = new Date();
	//=== End Function : No.17    Editor : Long   Date: 07/30/2013   Summary : Set UTC time and UUID when create new memo.
	tempArr = ClientData.MemoData();	
	tempArr.push(memoObj);	
	ClientData.MemoData(tempArr);
	
	if(CONTENTVIEW_MEMO.memoCallbackFunc){
		CONTENTVIEW_MEMO.memoCallbackFunc();
	}
};

CONTENTVIEW_MEMO.MemoDelFunction = function(){
	if(CONTENTVIEW_MEMO.saveMode == 'Copy'){
		//CONTENTVIEW_MEMO.targetDiv.dialog('close');
		CONTENTVIEW_MEMO.targetDiv.fadeOut('medium', function(){
			
		});
		
		CONTENTVIEW_GENERAL.isCopyMemo = false;
	}
	else{
		
		var resultArr = ClientData.MemoData();
		resultArr.splice(CONTENTVIEW_MEMO.EditIndex, 1);
		
		ClientData.MemoData(resultArr);
	
		//CONTENTVIEW_MEMO.targetDiv.dialog('close');	
		//if(CONTENTVIEW_MEMO.memoCallbackFunc){
		//	CONTENTVIEW_MEMO.memoCallbackFunc();
		//}
		
	}
	$("#overlay").hide();
	CONTENTVIEW_MEMO.targetDiv.children().remove();
	CONTENTVIEW_MEMO.targetDiv.hide();
	
	$("#pop_up_memo").hide();
	
	/* draw again */
	CONTENTVIEW.drawCanvas();	
	//START TRB00054 - EDITOR : Long - Date : 09/19/2013 - Summary : Fix for remove loading icon when del memo 
	CONTENTVIEW.drawCanvas(1);
	//END TRB00054 - EDITOR : Long - Date : 09/19/2013 - Summary : Fix for remove loading icon when del memo
	
	/* enable controls after finish copy */
	CONTENTVIEW.enableControlsCopyMemo();
	
};

CONTENTVIEW_MEMO.MemoCancelFunction = function(){
	//CONTENTVIEW_MEMO.targetDiv.dialog('close');
	$("#overlay").hide();	
	CONTENTVIEW_MEMO.targetDiv.children().remove();
	CONTENTVIEW_MEMO.targetDiv.hide();
	
	CONTENTVIEW_GENERAL.isCopyMemo = false;
	$("#pop_up_memo").hide();
	
	/* enable controls after finish copy */
	CONTENTVIEW.enableControlsCopyMemo();
	
	if(ClientData.IsAddingMemo() == true){	
		ClientData.IsAddingMemo(false); 	
		
	    //change class
        $('#imgaddmemo').removeClass();
        $('#imgaddmemo').addClass('memoAdd');	
	}	
};

CONTENTVIEW_MEMO.AddMemo = function(contentId,pageNo,targetId, posX, posY, callback) {
	CONTENTVIEW_MEMO.conid = contentId;
	CONTENTVIEW_MEMO.pageid = pageNo;
	CONTENTVIEW_MEMO.targetDiv = targetId;
	CONTENTVIEW_MEMO.targetX = posX;
	CONTENTVIEW_MEMO.targetY = posY;
	CONTENTVIEW_MEMO.memoCallbackFunc = callback;
	
	CONTENTVIEW_MEMO.createMemoDialog();
	CONTENTVIEW_MEMO.saveMode = 'New';
	$('#Memo_btnDel').css('display','none');
	
	//CONTENTVIEW_MEMO.targetDiv.dialog({width: 466, height: 390, modal: true, position: [CONTENTVIEW_MEMO.targetX, CONTENTVIEW_MEMO.targetY], resizable: false});	
	//CONTENTVIEW_MEMO.targetDiv.parent().removeClass('ui-draggable');
	$("#overlay").show();
	CONTENTVIEW.disableControlsCopyMemo();
	CONTENTVIEW_MEMO.targetDiv.css('z-index','1005');
	
	//START TRB00097
	if(CONTENTVIEW_MEMO.targetY >= $('#wrapper').height()/2){
		CONTENTVIEW_MEMO.targetDiv.css('top', $('#wrapper').height()/3);
		CONTENTVIEW_MEMO.targetDiv.css('left',CONTENTVIEW_MEMO.targetX - ($('#memoWrapper').width() /2 ));
	    
	}
	else{
		CONTENTVIEW_MEMO.targetDiv.css('top',CONTENTVIEW_MEMO.targetY);
		CONTENTVIEW_MEMO.targetDiv.css('left',CONTENTVIEW_MEMO.targetX - ($('#memoWrapper').width() /2 ));
	}
	//END TRB00097
	
	
	
	CONTENTVIEW_MEMO.targetDiv.draggable({ handle: "h1" });
	//CONTENTVIEW_MEMO.editJqueryUIDialog();
};

CONTENTVIEW_MEMO.EditMemo = function(index, posXPlus, posYPlus, targetId, callback){
	CONTENTVIEW_MEMO.targetDiv = targetId;
	CONTENTVIEW_MEMO.targetX = ClientData.MemoData()[index].posX + posXPlus;
	CONTENTVIEW_MEMO.targetY = ClientData.MemoData()[index].posY + posYPlus;
	
	CONTENTVIEW_MEMO.EditIndex = index;
	CONTENTVIEW_MEMO.memoCallbackFunc = callback;
	
	CONTENTVIEW_MEMO.createMemoDialog();
	CONTENTVIEW_MEMO.getMemoForEdit();
	CONTENTVIEW_MEMO.saveMode = 'Edit';
	$('#Memo_btnDel').css('display','block');
	
	//CONTENTVIEW_MEMO.targetDiv.dialog({width: 466, height: 390, modal: true, position: [CONTENTVIEW_MEMO.targetX, CONTENTVIEW_MEMO.targetY], resizable: false});
	//CONTENTVIEW_MEMO.targetDiv.parent().removeClass('ui-draggable');
	$("#overlay").show();
	CONTENTVIEW.disableControlsCopyMemo();
	CONTENTVIEW_MEMO.targetDiv.css('z-index','1005');
		
	var pt = CONTENTVIEW.imageToScreen(CONTENTVIEW_MEMO.targetX, CONTENTVIEW_MEMO.targetY);	
	
   //START TRB00097
    if(pt.y >= $('#wrapper').height()/2){
    	CONTENTVIEW_MEMO.targetDiv.css('top', $('#wrapper').height()/3);
    	CONTENTVIEW_MEMO.targetDiv.css('left',pt.x - ($('#memoWrapper').width() /2 ));       
    }
    else{
    	CONTENTVIEW_MEMO.targetDiv.css('top',pt.y);
    	CONTENTVIEW_MEMO.targetDiv.css('left',pt.x - ($('#memoWrapper').width() /2 ));
    }
    //END TRB00097
	
    CONTENTVIEW_MEMO.targetDiv.draggable({ handle: "h1" });
	//CONTENTVIEW_MEMO.editJqueryUIDialog();
};

CONTENTVIEW_MEMO.CopyMemo = function(index,contentId,pageNo,targetId, posX, posY, callback){
	CONTENTVIEW_MEMO.conid = contentId;
	CONTENTVIEW_MEMO.pageid = pageNo;
	CONTENTVIEW_MEMO.targetDiv = targetId;
	CONTENTVIEW_MEMO.targetX = posX;
	CONTENTVIEW_MEMO.targetY = posY;
	CONTENTVIEW_MEMO.EditIndex = index;
	CONTENTVIEW_MEMO.memoCallbackFunc = callback;
	
	CONTENTVIEW_MEMO.createMemoDialog();
	//CONTENTVIEW_MEMO.getMemoForEdit();
	$('#txaMemoContent').val(index);
	CONTENTVIEW_MEMO.saveMode = 'Copy';
	$('#Memo_btnDel').css('display','none');
	//CONTENTVIEW_MEMO.targetDiv.dialog({width: 466, height: 390, modal: true, position: [CONTENTVIEW_MEMO.targetX, CONTENTVIEW_MEMO.targetY], resizable: false});
	//CONTENTVIEW_MEMO.targetDiv.parent().removeClass('ui-draggable');
	$("#overlay").show();
	CONTENTVIEW.disableControlsCopyMemo();
	CONTENTVIEW_MEMO.targetDiv.css('z-index','1005');
	CONTENTVIEW_MEMO.targetDiv.css('top',CONTENTVIEW_MEMO.targetY);
	CONTENTVIEW_MEMO.targetDiv.css('left',CONTENTVIEW_MEMO.targetX - ($('#memoWrapper').width() /2 ));
	
	CONTENTVIEW_MEMO.targetDiv.draggable({ handle: "h1" });
	//CONTENTVIEW_MEMO.editJqueryUIDialog();
};

CONTENTVIEW_MEMO.getMemoForEdit = function(){
	var arrTemp = ClientData.MemoData();
	var tempEntity = arrTemp[CONTENTVIEW_MEMO.EditIndex];
	$('#txaMemoContent').val(tempEntity.Text);
};

CONTENTVIEW_MEMO.editMemoFunction = function(){
	var arrTemp = ClientData.MemoData();
	var tempEntity = arrTemp[CONTENTVIEW_MEMO.EditIndex];

	var editContent = $('#txaMemoContent').val();
	
	tempEntity.Text = editContent;	
	//=== Start Function : No.17    Editor : Long   Date: 07/30/2013   Summary : Set UTC time when edit memo.
	tempEntity.registerDate = new Date();	
	//=== End Function : No.17    Editor : Long   Date: 07/30/2013   Summary : Set UTC time when edit memo.
	arrTemp[CONTENTVIEW_MEMO.EditIndex] = tempEntity;
	ClientData.MemoData(arrTemp);
	
	if(CONTENTVIEW_MEMO.memoCallbackFunc){
		CONTENTVIEW_MEMO.memoCallbackFunc();
	}	
	
	/*refresh memo*/																	
	//CONTENTVIEW.drawCanvas();	
};

CONTENTVIEW_MEMO.buttonSaveFunction = function(){
	
	if(CONTENTVIEW_MEMO.saveMode == 'Edit'){
		CONTENTVIEW_MEMO.editMemoFunction();
	}
	else if(CONTENTVIEW_MEMO.saveMode == 'New'){
		CONTENTVIEW_MEMO.memoSaveFunction();
	}else if(CONTENTVIEW_MEMO.saveMode == 'Copy'){
		CONTENTVIEW_MEMO.memoSaveFunction();
	}
	//CONTENTVIEW_MEMO.targetDiv.dialog('close');
	$("#overlay").hide();
	CONTENTVIEW_MEMO.targetDiv.children().remove();
	CONTENTVIEW_MEMO.targetDiv.hide();
	
	CONTENTVIEW_GENERAL.isCopyMemo = false;
	$("#pop_up_memo").hide();
	
	/* enable controls after finish copy */
	CONTENTVIEW.enableControlsCopyMemo();	
};

//未使用
CONTENTVIEW_MEMO.editJqueryUIDialog = function(){
	$('.ui-dialog-titlebar').hide();
	CONTENTVIEW_MEMO.targetDiv.addClass('memoDialogImportantCss');
	CONTENTVIEW_MEMO.targetDiv.parent().addClass('parentMemoDialogImportantCss');
};

$(function () {
	//CONTENTVIEW_MEMO.ready();
});

CONTENTVIEW_MEMO.ready = function(){
	
	CONTENTVIEW_MEMO.targetDiv = undefined;
	CONTENTVIEW_MEMO.targetX = undefined;
	CONTENTVIEW_MEMO.targetY = undefined;
	CONTENTVIEW_MEMO.EditIndex = undefined;
	CONTENTVIEW_MEMO.saveMode = undefined;
	CONTENTVIEW_MEMO.memoCallbackFunc = undefined;
	CONTENTVIEW_MEMO.conid = undefined;
	CONTENTVIEW_MEMO.pageid = undefined;
	
};

