﻿var firstClick = true;
$(function() {

    var ownerID = $("#OwnerID").val();
    var ownerName = $("#OwnerName").val();
    var guestID = $("#GuestID").val();
    var guestName = $("#GuestName").val();
    $("#newNoteOwner").attr("href", "http://" + guestName + ".anhso.net").text(guestName);
    if (guestID == "-1") {
        $("#btn_note").hide();
        $("#btn_delete").hide();
    }
    $('#abcxyz').css('height', $('#main_img').height() + 'px');
    $("#main_img").initContainerNote();
    showNote();
    $("div[id^=note]").hide();
    processBor(-1);

    $("#abcxyz").hover(function() { $("div[id^=note]").show(); }, function() {
        if (!$(".editNote").is(":visible")) {
            $("div[id^=note]").hide();
        }
    })

    $("#newNote").hover(function() { processBor("newNote") }, function() { })

    $("#abcxyz .drsElement").ajaxComplete(function() {
        $(".drsElement").not("#newNote").each(function() {
            $(this).hover(function() {
                $("div[id^=note] .content").hide();
                if (!$(".editNote", $(this)).is(":visible")) {
                    $(".content", $(this)).show();
                }
                processBor($(this).attr("id"));
            }, function() {
                $("div[id^=note] .content").hide();
                processBor(-1);
            })
        })

        $(".drsElement").not("#newNote").bind("dblclick", function() {
            var ownCom = $(".content a", this).text();
            $("#newNote").hide();
            $(".editNote").hide();
            $(".content", $(this)).hide();
            if (guestName == ownCom || guestName == ownerName) {
                var template = $("#tempEditNote").html();
                var noteID = $(this).attr("id").replace(/note_/g, '');
                template = template.replace(/{noteID}|%7BnoteID%7D/g, noteID);

                $(".editNote", $(this)).remove();
                $(this).append(template);
                $(".editNote", $(this)).show();

                $("#txtContentNote", $(this)).val($(".noteCom", $(this)).text());
                processBor($(this).attr("id"));
                $("#btnThem", $(this)).hide();
                $("#txtContentNote").trigger('focus');
            }
        })

    })
})
function showNote() {
    var imageID = $("#CurrImgID").val();
    var guestName = $("#GuestName").val();
    $.ajax({
        type: "POST",
        data: "{imageID:" + imageID + "}",
        contentType: "application/json; charset=utf-8",
        url: "/farmservice/photoservice.asmx/GetAllPhotoNotes",
        dataType: "json",
        success: function(data) {
            data = eval('(' + data.d + ')');
            for (i = 0; i < data[0].notes.length; i++) {
                $("#abcxyz").addNote(data[0].notes[i].X, data[0].notes[i].Y, data[0].notes[i].Width, data[0].notes[i].Height, data[0].notes[i].NoteID, data[0].notes[i].Username, data[0].notes[i].Comment);
            }           
        }
    });
}
function addNoteClick() {
    var ownerID = $("#OwnerID").val();
    var ownerName = $("#OwnerName").val();
    var guestID = $("#GuestID").val();
    var guestName = $("#GuestName").val();
    $("div[id^=note]").show();
    if (guestID != "-1") {
        $("#newNote").css({ "top":"0px", "left":"0px", "width":"50px", "height":"50px","display":"block"});
        $("#newNote .editNote").remove();
        $("div[id^=note] .editNote").hide();
        $("#newNote").append($("#tempEditNote").html());
        $("#newNote .editNote").show();
        processBor("newNote");
        $("#btnXoa").hide();
        $("#btnSua").hide();
        $("#txtContentNote").trigger('focus');
    }
}
function fncAddNote() {
    ownerID = $("#GuestID").val();
    ownerName = $("#GuestName").val();
    var comment = $("#newNote #txtContentNote").val();
    y = $("#newNote").position().top;
    x = $("#newNote").position().left;
    h = $("#newNote").height();
    w = $("#newNote").width();

    $.post("/farmservice/photoservice.asmx/addPhotoNotes", { x: x, y: y, w: w, h: h, comment: comment, ownerID: ownerID, imageID: $("#CurrImgID").val() }, function(data) {
        newNoteID = $("int", data).text();
        $("#abcxyz").addNote(x, y, w, h, newNoteID, ownerName, comment);
        $("#newNote").fadeOut("fast");
        processBor(-1);
    });
}
function fncEditNote(id) {
    var comment = $("#note_"+id+" #txtContentNote").val();
    y = $("#note_"+id).position().top;
    x = $("#note_"+id).position().left;
    h = $("#note_"+id).height();
    w = $("#note_"+id).width();

    $.post("/farmservice/photoservice.asmx/editPhotoNotes", { noteID: id, x: x, y: y, w: w, h: h, comment: comment});
    $(".editNote").hide();
    $("#note_" + id).css({ "top": y + "px", "left": x + "px", "width": w + "px", "height": h + "px" }).show();
    $("#note_" + id + " .noteCom").text(comment);
}
function fncDelNote(id) {
    $("#note_"+id).fadeTo(1000, 1).fadeTo(1000, 0.2, function() {
    $.post("/farmservice/photoservice.asmx/deletePhotoNotes", { noteID: id });
        $(this).remove();
    })
}
function fncCancelNote(id) {
    if (parseInt(id) > 0) {
        $("#abcxyz div[id^=note]").remove();
        showNote();
    }
    else {
        $("#newNote").hide();
    }
    $("#abcxyz .editNote").hide();
}
function processBor(exNoteID) {
    $("div[id^=note] #dummy1,#newNote #dummy1").addClass("borW").removeClass("borY");
    if (exNoteID != "-1") {
        $("#" + exNoteID + " #dummy1").addClass("borY").removeClass("borW");
    }
}

jQuery.fn.addNote = function(x, y, w, h, nodeID, username, comment) {
    //addNote
    template = $("#templateNote").html();
    template = template.replace(/{noteID}|%7BnoteID%7D/g, nodeID);
    template = template.replace(/{username}|%7Busername%7D/g, username);
    template = template.replace(/{note_comment}|%7Bnote_comment%7D/g, comment);
    template = template.replace(/{x}|%7Bx%7D/g, x);
    template = template.replace(/{y}|%7By%7D/g, y);
    template = template.replace(/{w}|%7Bw%7D/g, w);
    template = template.replace(/{h}|%7Bh%7D/g, h);
    $(this).append(template);
    $("#note_" + nodeID).css({ "top": y + "px", "left": x + "px", "width": w + "px", "height": h + "px" });
    
};

jQuery.fn.initContainerNote = function(x, y, w, h) {
    var guestID = $("#GuestID").val();
    if (guestID != "-1") {
        width = $(this).width();
        height = $(this).height();

        var dragresize = new DragResize('dragresize', { minWidth: 50, minHeight: 50, minLeft: 0, minTop: 0, maxLeft: width, maxTop: height });
        dragresize.isElement = function(elm) {
            if (elm.className && elm.className.indexOf('drsElement') > -1) return true;
        };
        dragresize.isHandle = function(elm) {
            if (elm.className && elm.className.indexOf('drsMoveHandle') > -1) return true;
        };
        dragresize.apply(document);
    }
}


