var targetDiv;
var targetX;
var targetY;
var targetMemoId;
var EditIndex;
var saveMode;
var memoCallbackFunc;
var conid;
var pageid;

function createMemoDialog(){
	targetDiv.show();
	targetDiv.html('');
	targetDiv.append(
	'<aside id="memoWrapper" class="MemoIndexBox">'
	+ '	<h1 class="indexBoxHd">' + i18nText('txtMemo')
	+'		<a 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">' + i18nText('dspCancel') + '</a>'
	+ '			<a id="Memo_btnDel" style="float:right" class="lang cancelbtn" lang="dspDelete">' + i18nText('dspDelete') + '</a>'
	+ '			<a id="Memo_btnSave" style="float:right" class="lang cancelbtn" lang="dspSave">' + i18nText('dspSave') + '</a>'
	+'		</div>'
	+'	</div>'
	+'</aside>');
	
	$('#txaMemoContent').focus();
	
	handleMemoEventFunction();
};

function handleMemoEventFunction(){
	$('#Memo_btnSave').click(buttonSaveFunction);
	$('#Memo_btnDel').click(MemoDelFunction);
	$('#Memo_btnCancel').click(MemoCancelFunction);
	$('.delete').click(MemoCancelFunction);
};

function memoSaveFunction(){

	var tempArr = [];	
	
	var memoObj = new MemoEntity();
	memoObj.pageNo = pageid;
	memoObj.contentid = conid;
	memoObj.Text = $('#txaMemoContent').val();
	var imagePt = screenToImage(targetX, targetY);
	memoObj.posX = imagePt.x;
	memoObj.posY = imagePt.y;
	
	tempArr = ClientData.MemoData();	
	tempArr.push(memoObj);	
	ClientData.MemoData(tempArr);
	
	if(memoCallbackFunc){
		memoCallbackFunc();
	}
};

function MemoDelFunction(){	
	if(saveMode == 'Copy'){
		//targetDiv.dialog('close');
		targetDiv.fadeOut('medium', function(){
			
		});
		
		isCopyMemo = false;
	}
	else{
		var resultArr = ClientData.MemoData();
		resultArr.splice(EditIndex, 1);
		
		ClientData.MemoData(resultArr);
	
		//targetDiv.dialog('close');	
		if(memoCallbackFunc){				
			memoCallbackFunc();
		}
	}
	$("#overlay").hide();
	targetDiv.children().remove();
	targetDiv.hide();
	
	$("#pop_up_memo").hide();
	
	/* draw again */
	drawCanvas();	
	
	/* enable controls after finish copy */
	enableControlsCopyMemo();	
};

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

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

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

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

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

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

	var editContent = $('#txaMemoContent').val();
	
	tempEntity.Text = editContent;	
	
	arrTemp[EditIndex] = tempEntity;
	ClientData.MemoData(arrTemp);
	
	if(memoCallbackFunc){
		memoCallbackFunc();
	}	
	
	/*refresh memo*/																	
	//drawCanvas();	
};

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

function editJqueryUIDialog(){
	$('.ui-dialog-titlebar').hide();
	targetDiv.addClass('memoDialogImportantCss');
	targetDiv.parent().addClass('parentMemoDialogImportantCss');
};
