//initialization
$(document).ready(function() {	
	Stickynote.initialize();
})

Stickynote = new Object();

Stickynote.initialize = function() {
	$(".stickynote").draggable({
		cursor: 'move', 
		start: function(ev,ui) {
			var id_input = $("input[name='sn_id']",$(ev.target.parentNode));
			if(typeof(id_input[0]) != 'undefined')
				Stickynote.currentDragId= id_input[0].value;
		},
		stop: function(ev,ui) {
			var top = $(ev.target.parentNode).css('top');
			var left = $(ev.target.parentNode).css('left');
			
			if(Stickynote.currentDragId !== false)
				Stickynote.updatePosition(Stickynote.currentDragId,top,left);
			Stickynote.currentDragId = false;
			
		}
	});
	
	return true;
}

Stickynote.deleteNote = function(id)
{
	if(!confirm('Czy napewno chcesz usunąć tą notatkę?'))
		return true;
		
	ajaxLoading(true);
	$.get('?p=plugin&a=stickynote&f=deleteStickynote&id='+id, function(AjaxObj) {
		if(AjaxObj.__RESULT_STATUS == 0)
			msgBox(AjaxObj.errorMsg);
		ajaxLoading(false);
		$("#stickynote_"+id).remove();
	});
	return true;
}

Stickynote.saveNote = function(id) {
	var bodyValue = $('#stickynote_body_'+id).attr('value');
	ajaxLoading(true);
	$.post('?p=plugin&a=stickynote&f=updateStickynote', {Id: id, Body: bodyValue}, function(AjaxObj) {
		eval(AjaxObj);
		//if(AjaxObj.__RESULT_STATUS == 0) 
		ajaxLoading(false);
	});
	return true;

}

Stickynote.updatePosition = function(id,top,left) {
	ajaxLoading(true);
	$.get('?p=plugin&a=stickynote&f=updatePosition&id='+id+'&top='+top+'&left='+left, function(AjaxObj) {
		eval(AjaxObj);
		//if(AjaxObj.__RESULT_STATUS == 0) 
		ajaxLoading(false);
	});
	return true;
}

Stickynote.createNew = function() {
	ajaxLoading(true);	
	$.ajax({
		type: "GET",
		url: '?p=plugin&a=stickynote&f=createStickynote',
		cache: false,
		success: function(html) {
			ajaxLoading(false);
			if(html.match(/^var AjaxObj/)) {
				eval(html)
				alert(AjaxObj.errorMsg);
				return false;
			}
			$(document.body).append(html);
			return Stickynote.initialize();
		}
	});
	return true;
}