jQuery.fn.notes = function(content, id, userhash) {
    
    jQuery.fn.notes.created.id = "tipBox";
    $("body").append(jQuery.fn.notes.created);
    
    var tipBox = $(jQuery.fn.notes.created);
    tipBox.css( {
        "position":"absolute","display":"none"
    }
    );
    
    tipBoxShow();
    tipBox.html(content);
    tipBox.removeClass();

    //functions
    
    function tipBoxShow() {
        var pos = $("#res"+id).position();
        tipBox.css( {
            "display":"block","top":(pos.top-43), "left":(pos.left+22)
        }
        );
    }
    
    function tipBoxHide() {
        tipBox.css( {
            "display":"none"
        }
        );
    }
}
;

jQuery.fn.notes.created = document.createElement("div");


function notesopen(id, userhash) {
    msg='<div class="notes">';
    msg+='<img src="/datastore/images/notes/notes_windows_top.gif" alt="" />';
    msg+='<img src="/datastore/images/notes/notes_windows_arrow.gif" alt="" class="arrow" />';
    msg+='<div class="notesbody"><a href="javascript://" class="close" onclick="notesclose()"><img src="/datastore/images/notes/notes_windows_x.gif" alt="" /></a>';
    msg+='Dodanie obiektu do notesu po <br />zalogowaniu da Ci dodatkowe możliwości zarządzania nim.<br /><br>';
    msg+='Będziesz mógł korzystać niego na każdym komputerze, na którym się zalogujesz jako użytkownik PonajMazury.pl.<br /><br>';
    msg+='Założenie konta na naszym serwisie zajmie Ci tylko chwilę.<br /><br>';
    msg+='<a href="/logowanie/"><b>Dodaj do notesu po logowaniem</b></a><br>';
    msg+='<a href="javascript://"';
    
    msg+="onclick=\"addnotes("+id+", '"+userhash+"\')\"";
    msg+='style="margin-top:5px"><b>Dodaj do notesu bez logowania</b></a><br></div>';
    msg+='<img src="/datastore/images/notes/notes_windows_bottom.gif" alt=""/>';
    msg+='</div>';
    
    var wliscie = false;
    if(userhash == '') {
        $("#resb"+id).each(function() {
            wliscie = true
        }
        )
        if(!wliscie)
        $("#res"+id).notes(msg, id, userhash);
        else
        addnotes(id,userhash);
    } else {
        addnotes(id,userhash);
    }
}

function notesclose() {
    $(jQuery.fn.notes.created).css( {
        "display":"none"
    }
    );
}

function addnotes(id, userhash) {
    notesclose();
    $.ajax( {
        type: "POST",
        url: "/Notes/add/",
        data: "id="+id+"&userhash="+userhash,
        beforeSend: 	function(i) {
            var image1= new Image();
            image1.src = "/datastore/images/results/itemmenuaddnoteswait.gif";
            document.images[ 'mimg' + id ].src=image1.src;
        }
        ,	
        success: function(msg) {
            
            if (msg == 2) {
                var image1= new Image();
                image1.src = "/datastore/images/results/itemmenuaddnotes.gif";
                document.images[ 'mimg' + id ].src=image1.src;
                
                $('#resb' + id).remove();
            } else {
                getnotes(id);
            }
        }
    }
    );
}


function getnotes(id) {
	var count = 0
    $.ajax( {
        type: "POST",
        url: '/Notes/notesxml/',
        data: "id="+id,	
        dataType: "xml",
        success: function(xml) {
            var toPrint = '';
            $("date", xml).each(function(i) {
                
                $('#resb' + $('id',this).text()).remove();
					 
                toPrint += "<li id='resb" + $('id',this).text() + "'><a href='/" + $('link',this).text() + "'>" + $('name',this).text() + "<br /><span>" + $('city',this).text() + "</span></a></li>" ;
            }
            );
            
            $("#noteslist").append(toPrint);
        }
        ,
        error: function() {
            alert("Ajax error");
        }
    }
    );
    
    var image1= new Image();
    image1.src = "/datastore/images/results/itemmenuaddnotes2.gif";
    document.images[ 'mimg' + id ].src=image1.src;
}
