var Tags = {
    availableTags: [],
    $tagInput: $('#tagInput'),
    $addTag: $('#addTag')
}

var readOnly = true;

function ShowElement(elementObject, bShow) {
    if (bShow) {
        elementObject.style.display = "";
    }
    else {
        elementObject.style.display = "none";
    }
}
//
function OnSourceTipClick() {
    var sourceTip = document.getElementById("sourceTip");
    sourceTip.style.display = "none";
    var sourceEdit = document.getElementById("sourceEdit");
    sourceEdit.style.display = "";
    sourceEdit.focus();
}

function OnNoteSourceClick() {

  var noteSource = document.getElementById("noteSource");
  YNote.OpenLink(noteSource.innerText);

}
function OnNoteSourceClickEdit() {
    var noteSourceOuter = document.getElementById("noteSourceOuter");
    noteSourceOuter.style.display = "none";
    var noteSource = document.getElementById("noteSource");
    var sourceEdit = document.getElementById("sourceEdit");
    sourceEdit.style.display = "";
    sourceEdit.value = noteSource.innerText;
    sourceEdit.focus();
}

function OnSourceEditKeydown() {
    if (event.keyCode == 13) {
        EndSourceEditInput();
    }
}

function OnSourceEditBlur() {
    EndSourceEditInput();
}

function EndSourceEditInput() {
    var sourceEdit = document.getElementById("sourceEdit");
    SetNoteSource(sourceEdit.value);
    sourceEdit.style.display = "none";
    YNote.SaveSource(sourceEdit.value);
}

function SetNoteSource(sourceValue) {
    var noteSourceOuter = document.getElementById("noteSourceOuter");
    var noteSource = document.getElementById("noteSource");
    var sourceTip = document.getElementById("sourceTip");
	var sourceTipButton = document.getElementById("editsourceButton"); 
    if (sourceValue.length > 0) {
        sourceTip.style.display = "none"
        noteSourceOuter.style.display = "inline-block";
        noteSource.innerHTML = sourceValue;
    }
    else {
        sourceTip.style.display = "";
        noteSourceOuter.style.display = "none";
    }
	
    if( readOnly )
	{
	    sourceTip.className = "disable1";
		sourceTip.onclick = null;
		sourceTipButton.style.display="none";
	}
}

//

var authorTipElementId = "authorTip";
var authorElmentId = "noteAuthor";
var authorEditorId = "authorEdit";
function OnAuthorTipClick() {
    var authorTip = document.getElementById(authorTipElementId);
    ShowElement(authorTip, false);
    var authorEdit = document.getElementById(authorEditorId);
    ShowElement(authorEdit, true);
    authorEdit.focus();
}

function OnNoteAuthorClick() {
    var author = document.getElementById(authorElmentId);
    ShowElement(author, false);
    var authorEdit = document.getElementById(authorEditorId);
    ShowElement(authorEdit, true);
    authorEdit.value = author.innerText;
    authorEdit.focus();
}

function OnAuthorEditKeydown() {
    if (event.keyCode == 13) {
        EndAuthorEditInput();
    }
}

function OnAuthorEditBlur() {
    EndAuthorEditInput()
}

function EndAuthorEditInput() {
    var authorEdit = document.getElementById(authorEditorId);
    SetNoteAuthor(authorEdit.value);
    ShowElement(authorEdit, false);
    YNote.SaveAuthor(authorEdit.value);
}

function SetNoteAuthor(authorValue) {
    var author = document.getElementById(authorElmentId);
    var authorTip = document.getElementById(authorTipElementId);
    if (authorValue.length > 0) {
        author.style.display = "inline-block";
        ShowElement(authorTip, false);
        author.innerHTML = authorValue;
    }
    else {
        ShowElement(author, false);
        ShowElement(authorTip, true);
    }
	
	if( readOnly )
	{
	    authorTip.className = "disable1";
		authorTip.onclick = null;
		author.onclick = null;
	}
}

function OnViewModeSelectChange(object) {
    YNote.ChangeEditorMode(object.value);
}

function OnScaleSelectChange(object) {
    YNote.ChangeEditorScale(object.value);
}

function OnNoteHistoryClick() {
    YNote.ShowNoteHistory();
}

function OnNoteCharNumClick() {
    YNote.ShowNoteCharNum();
}

function OnClickDeleteTag(object) {
    var tagObject = object;
    YNote.DeleteTag(tagObject.getAttribute("data-tag-id"));
}

function OnClickAddTag(object) {
    ShowElement(object, false);
    var objectTagInput = document.getElementById("tagInput");

    Tags.$tagInput.autocomplete({
        source: Tags.availableTags
    });

    Tags.$tagInput.val('');
    Tags.$tagInput.show();
    Tags.$tagInput.focus();
}

function OnTagInputKeydown(object) {
    if (event.keyCode == 13) {
        object.blur();
    }
}

function OnTagInputBlur(object) {
    EndTagInput(object);
}

function EndTagInput(object) {
    Tags.$tagInput.hide();
    $('.ui-autocomplete').hide();
    if (object.value != undefined && object.value != null) {
        YNote.AddTags(object.value);
        console.log("end tag input");
    }
    ShowElement(object, false);
    document.getElementById("addTag").style.display = "inline-block";
}

//
function SetElementInnerTextById(strElementId, strInnerText) {
    var element = document.getElementById(strElementId);
    if (element) {
        element.innerText = strInnerText;
    }
}

function SetElementValueById(strElementId, value) {
    var element = document.getElementById(strElementId);
    if (element) {
        element.value = value;
    }
}

function SetCreateTime(strCreateTime) {
    SetElementInnerTextById("createTime", strCreateTime);
}

function SetUpdateTime(strUpdateTime) {
    SetElementInnerTextById("updateTime", strUpdateTime);
}

function SetReadModeSelect(selValue) {
    SetElementValueById("viewModeSelect", selValue);
}

function SetZoomScaleSelect(selValue) {
    SetElementValueById("scaleSelect", selValue);
}

function SetTags(tags) {

    if( readOnly )
	{
	    var addTag = document.getElementById("addTag");
		addTag.onclick = null;
		var addTagTip = document.getElementById("addTagTip");
		addTagTip.className = "disable1";
		return;
	}
    var i;
    var tagsHtml = "";

    for (i = 0; i < tags.length; ++i) {
        var tagSpan = '<span onclick="OnClickDeleteTag(this)" class="tag limitShow" data-tag-id="' + tags[i].tagId + '">' + tags[i].tagName + ' </span>'
        tagsHtml += tagSpan;
    }

    var tagsObject = document.getElementById("tags");
    if (tagsObject) {
        tagsObject.innerHTML = tagsHtml;
    }
}
function setNotebookIdAndName(notebookId, notebookName) {
    var notebookNameobj = document.getElementById('notebookName');
    notebookNameobj.setAttribute('notebookId', notebookId);
    notebookNameobj.innerHTML = notebookName;
	
    if( readOnly )
	{
	    var notebookDiv = document.getElementById('notebook');
		notebookDiv.className = "notebookDisable";
		notebookNameobj.className = "limitShow disable1";
		notebookNameobj.onclick = null;
		var notebookIcon = document.getElementById('iconChangeNotebook');
		notebookIcon.onclick = null;
		var notebookDropDown = document.getElementById('notebookDropDown');
		notebookDropDown.onclick = null;
	}
}
function onChangeNotebook() {
    obj = document.getElementById("notebookName");
    var id = obj.getAttribute("notebookId");
    obj = document.getElementById("iconChangeNotebook");
    var rect = obj.getBoundingClientRect();
    YNote.SelectNotebook(id, rect.left + 3, rect.bottom);
}
function UpdateNoteHistory()
{
    if( readOnly )
	{
	    var hitoryObj = document.getElementById("showHistory");
		hitoryObj.onclick = null;
		hitoryObj.className = "disable1";
	}
}
listApis["SetUserInfoData"] = function (datas) {
    var vJson = JSON.parse(datas);

	readOnly = vJson.readOnly;
	
    SetNoteSource(vJson.source);

    SetNoteAuthor(vJson.author);

    SetCreateTime(vJson.createTime);

    SetUpdateTime(vJson.updateTime);

    //SetReadModeSelect(vJson.editorMode);

    //SetZoomScaleSelect(vJson.zoomScale);

    setNotebookIdAndName(vJson.notebookId, vJson.notebookName);

    SetTags(vJson.tags);

    Tags.availableTags = vJson.allTags;
	
	UpdateNoteHistory();

    YNote.ResetWindowHeight(document.body.clientHeight);
    console.log("reset window height in html");
};

listApis["setNotebook"] = function (notebookId, notebookName) {
    setNotebookIdAndName(notebookId, notebookName);
}















