// notes.js by ejik
function createNote() {
	jQuery("#dnote").remove();
	jQuery("#main_block").append('<div id="dnote"></div>')
	myNote = jQuery("#dnote");
//	jQuery(myNote).draggable();	
	jQuery(myNote).show();
	
	return myNote;
};
function enableFormSubmit() {
	jQuery("#dform").ajaxSubmit( "index.php?act=notepad&sub=adding", function(xml){
		window.location.reload();
		/*
		window.location.reload();return;
		var id;
		var title;
		var content;
		var article_id;
		jQuery("Note", xml).each(function() {
			id = this.getAttribute("id");
			title = this.getAttribute("title");
			content = this.getAttribute("content");
			article_id = this.getAttribute("article_id");
		});
		jQuery("#notenr_"+id+"_title").text(title);
		jQuery("#notenr_"+id+"_content").text(content);
 		var myLink = '<a href="#" onclick="openNoteView('+id+');" class="button" style="margin-right: 10px;"><span>podgląd</span></a>'
		
		jQuery("#dnote #dtop span").empty().append('<span class="alert">Notatka została dodana!</span>');
		jQuery("#dnote span.inactive").replaceWith(myLink);
		*/
	} ); 
};
function insertNotebyId(id) {
	jQuery.get("index.php?act=notepad&sub=add&id="+id, function(data) {
		   jQuery("#dnote").append(data);
		   	enableFormSubmit();
		});	
};
function openNoteWindow(art_id,pdf_id)
{
	var myNote = createNote();
	var funkcja = function(data) {
		   jQuery(myNote).append(data);
			enableFormSubmit();
		};
	var adres = '';
	if( art_id != undefined ){
		adres = "index.php?act=notepad&sub=add&article_id="+art_id;
	} else if ( pdf_id != undefined ) {
		adres = "index.php?act=notepad&sub=add&pdf_id="+pdf_id;		
	} else {
		adres = "index.php?act=notepad&sub=add";	
	}
	jQuery.get( adres, funkcja );
}
function openNoteWindowById(id, clickedItem)
{
	var myNote = createNote();
	insertNotebyId(id);
}
function openEditView(note_id) {
	jQuery("#dnote").empty();
	insertNotebyId(note_id);	
}
function openNoteView(note_id) {
	jQuery("#dnote").empty();
	jQuery.get("index.php?act=notepad&sub=view&id="+note_id, function(data, dataStatus) {													  
			jQuery("#dnote").append(data);;
	});
};


function closeNote(myItem) {
	jQuery(myItem).parents("#dnote").remove();
	return false;
};

function enableScroll(myDiv) {
	var myNote = jQuery(myDiv).parents("#dnote");
	jQuery(myNote).draggable( "disable" );
	jQuery(myDiv).mouseout(function() {jQuery(myNote).draggable( "enable" )});	
};


// 
function showArtNote() {
	jQuery.get('index.php?act=notepad&sub=get&id='+articleId, function(xml){
		var id;
		var title;
		var content;
		var article_id;
		jQuery("Note", xml).each(function() {
			id = this.getAttribute("id");
			title = this.getAttribute("title");
			content = this.getAttribute("content");
			article_id = this.getAttribute("article_id");
		});
		var myLink = '<div><a href="javascript:void(0)" title="edytuj notatkę" onclick="openNoteWindowById(' + id + ');" class="button"><span>edytuj notatkę</span></a>';
		var myText = '<p>' + content.substr(0,90) + ' &hellip;</p></div>';
		
		jQuery("#art_note").append(myLink + myText);		

	} ); 
};


