﻿/*
 * Murat Tüfekçi Add Edit Note jQuery Plugin
 *
 */
//Murat Tüfekçi jQuery TagName Extension
$.fn.tagName = function() {
    return this.get(0).tagName;
}

var noteEditFormOpened = false;
var noteOwnerElement = null;
var note = null;
var noteCloseTimer = null;
(function($) {
    $.fn.mtNote = function(options) {
        //Static init
        var defaults = {
			'notes': [],
			noteEkleUyariEnabled: true,
			noteEkleUyariStr: 'Note Ekle',
			noteEkleUyariCssClass: 'ToolContentSingle'
		};
		
        var options = $.extend(defaults, options);

        $("a[id$='aNoteCommandDone']").click(function() {
            $.fn.mtNote.noteDone();
        });
		
        $("a[id$='aNoteCommandCancel']").click(function() {
            $.fn.mtNote.noteCancel();
        });

        $(".aNoteCommandClose").click(function(){
            $.fn.mtNote.noteClose();
        });


        this.each(function(intIndex) {

            $(this).click(function() {
                window.clearTimeout(noteCloseTimer);
                //Note Edit form must be closed.
                //alert("note");
                $('.aNoteCommandClose').click();
				$("#divNotEkleUyari").hide();
                if (!noteEditFormOpened) {
                    // note ref element Object
                    noteOwnerElement = this;
                    var noteTop = $(this).position().top;
                    // Left için referans object
                    //var noteLeft = $("#divBookMarkContainer").offset().left; // - $("#divNoteAdd").width();
                    var noteLeft = $(this).offset().left + $(this).width() - 15; // - $("#divNoteAdd").width();

                    //Yeri Sabit
                    $("#divNoteContainer").css({ "top": noteTop + "px"});

                    //Add note events    
                    $("#divNoteAdd").unbind("click");
                    //                    $("#divNoteAdd").bind("click",
                    //                                         { addNoteOwnerIndex: intIndex, tagName: $(this).tagName() },
                    //                                         addNoteClickEventHandler);
                    $("#divNoteAdd").click(function() {
                        $("#divNoteContainer").css({ "left": (noteLeft + 0) + "px" });
                        $("#divNoteForm").css("display", "block");
                        $("#divNoteAdd").css("display", "none");
                        $("#txtNoteContent").val("");
                        $("#divNoteScroll").css("display", "none");
                        noteEditFormOpened = true;
                        $("#divNotePrevButtons").hide();
                        
                    });

                    //Edit init()
                    $("#divNoteContent").unbind("click");
                    $("#divNoteContent ,#aNoteCommandEdit").click(function() {
                        
                        //$("#divNoteContainer").css({ "left": (noteLeft + 20) + "px" });
                        $("#divNoteForm").css("display", "block");
                        $("#divNoteAdd").css("display", "none");
                        $("#txtNoteContent").val($("#divNoteContent").html());
                        $("#divNoteScroll").css("display", "none");
                        noteEditFormOpened = true;
                        $("#divNotePrevButtons").hide();
                    });

                    

                    note = getElementNote(this.id);

                    $("#divNoteContainer").show();
//                    //Delete init()
                     $("#aNoteCommandDelete").click(function() {
                        deleteSelectedNote(note.DbID);
                        $("#divNoteContainer").hide();
                     });


                    if (note != null) {
                        $("#divNoteContent").html(note.NotIcerik);
                        $("#divHoverPreview").html(note.NotIcerik);
                        
                    }
                    else
                    {
                        $("#divNoteContent").html("");

                        //Acık gelsin eklesin
                        $("#divNoteForm").css("display", "block");
                        $("#divNoteAdd").css("display", "none");
                        $("#txtNoteContent").val("");
                        $("#divNoteScroll").css("display", "none");
                        noteEditFormOpened = true;
                        $("#divNotePrevButtons").hide();
                    }
                   
                    //Not var ise direk not gözüksün.
//                    if ($("#divNoteContent").html().length == 0) {
//                        $("#divNoteAdd").css("display", "block");
//                        $("#divNoteScroll").css("display", "none");
//                    }
//                    else {
//                        $("#divNoteScroll").css("display", "block");
//                        $("#divNoteAdd").css("display", "none");
//                    }
                    
                }
            });

			$(this).hover(function(){
				if (options.noteEkleUyariEnabled) {
					var note = getElementNote(this.id);
                    noteCloseTimer = window.setTimeout("$('.aNoteCommandClose').click();", 500);
                     
					if (note == null) {
						if ($("#divNotEkleUyari").length == 0) {
							var notEkleUyari = $("<div id=\"divNotEkleUyari\" class=\""+options.noteEkleUyariCssClass+"\">"+options.noteEkleUyariStr+"</div>");
							$("#divNotOzetListesi").append(notEkleUyari);
						}
                        //alert($("#divPageContent #" + $(this).attr("id")).position().top);
                        $("#divNotEkleUyari").css("position", "relative");
                        $("#divNotEkleUyari").css("height", $(this).height() +"px");
						var topOffset = $("#divPageContent #" + $(this).attr("id")).position().top + 10;
						$("#divNotEkleUyari").css("top", topOffset + "px");
						$("#divNotEkleUyari").show();
						
					}
				}
			},
			function(){
				if (options.noteEkleUyariEnabled) {
					if ($("#divNotEkleUyari").length > 0) {
						$("#divNotEkleUyari").hide();
					}
				}
				});
            //Mouse Leave Event
            $(this).mouseleave(function() {
//                if (!noteEditFormOpened) {
//                    $("#divNoteContainer").hide();
//                    if ($("#divNoteContent").html().length == 0)
//                        $("#divNoteAdd").show();
//                }
//                else if ($("#txtNoteContent").val().length == 0) {

//                }
            });

            //Note Preview Elements Creation

        });

        //Note Add Element Issues
        $("#divNoteContainer").hover(
            function() {
            },
            function() {
                if (!noteEditFormOpened) {
                    $(this).hide();
                    $("#divNoteScroll").hide();
                    $("#divNoteForm").hide();
                }
            });

        $("#divNoteForm").hover(
        function() {
           $(this).show(); 
        },
        function() {
        $("#divNoteForm").css("display", "none");
        $("#divNotePrevButtons").show();
        $("#divNoteScroll").show();
        noteEditFormOpened = false;
        if( $("#divNoteForm").val().length ==0)
            $("#divNoteContainer").hide();
            
        });
        //Note Content Editable Init
        //        $("#divNoteAdd").click(function() {

        //        });

        return this;
    }

    //Add Note
    $.fn.mtNote.noteDone = function() {
        $("#divNoteContent").html($("#txtNoteContent").val());
        $("#divNoteScroll").show();
        $("#divNoteForm").hide();
        $("#divNotePrevButtons").show();
        noteEditFormOpened = false;
        saveNote(noteOwnerElement.id);
        if(noteScrollObject)
            noteScrollObject.update();
    }

    //Cancel Note Edit
    $.fn.mtNote.noteCancel = function() {
        $("#divNoteForm").css("display", "none");
        $("#divNotePrevButtons").show();
        $("#divNoteScroll").show();
        noteEditFormOpened = false;
        if( $("#divNoteForm").val().length ==0)
            $("#divNoteContainer").hide();
        if(noteScrollObject)
            noteScrollObject.update();
    }

    $.fn.mtNote.noteClose = function(){
        $("#divNoteForm").css("display", "none");
        $("#divNotePrevButtons").show();
        $("#divNoteScroll").show();
        noteEditFormOpened = false;
        if( $("#divNoteForm").val().length ==0)
            $("#divNoteContainer").hide();

    }
    //EOF closure
})(jQuery);
