var NoteElementID="";
var NoteLinkType="";
var NoteLinkID="";

function initNotes(ElementID,LinkType,LinkID){
	var NoteBox=document.createElement("DIV");
	NoteBox.setAttribute('id','NoteWindow');
	NoteBox.setAttribute('class','WindowDiv');
	$('#Overall').append(NoteBox);
	$('#NoteWindow').draggable();
	NoteElementID=ElementID;
	NoteLinkType=LinkType;
	NoteLinkID=LinkID;
	$(document).ready(function(){
		loadNotes(ElementID,LinkType,LinkID);
	});
}

function loadNotes(){
	var URL="/index.php?Realm=Note&Action=ObjectNotes&LinkType=" + NoteLinkType + "&LinkID=" + NoteLinkID;
	$.get(URL,'',function(data){
		var Notes=data.getElementsByTagName("NoteDetail");
		if(Notes.length==0){
			$("#ReloadNotesButton").attr("src","/assets/images/action-refresh.png");
			return false;
		}
		$('#ItemNoteCount').text('(' + Notes.length + ')');
//		$('#' + ElementID).empty();
		var Result=RT_TransformXMLFragment(data, '/XSL/note/note.xsl');
		$("#" + NoteElementID).append(Result);
		$("#ReloadNotesButton").attr("src","/assets/images/action-refresh.png");
	});

}

function drawNoteBox(){
	$("#" + NoteElementID).fadeIn("SLOW");	
}

function showNoteDetail(NoteID){
	var URL="/index.php?Realm=Task&Action=showTaskDetail&TaskID=" + TaskID;
	$.get(URL,'',function(data){
		var Result=RT_TransformXMLFragment(data, '/XSL/task/task.xsl');
		_clearTaskWindow();
		$('#TaskDetailViewer').append(Result);
		$('#TaskDetailViewer').fadeIn('slow');
		$('#TaskDetailViewer').focus();
	});
}

function showAllNotes(Object){
	Notes=$('#' + NoteElementID).find('.NoteDetail');
	for(var i=0;i<Notes.length;i++){
		toggleDesc(Notes[i].id);
	}
}

function reloadNoteList(){
	$("#ReloadNotesButton").attr("src","/assets/images/loading.gif");
	$('#' + NoteElementID).find('#NoteListContainer').remove();
	loadNotes();
}

function closeNoteWindow(){
	$('#NoteWindow').hide('fast');
	$('#NoteWindow').empty();
}

function _drawNoteWindow(){
	$('#NoteWindow').empty();
	$('#NoteWindow').html('<img src="/assets/images/loading.gif" style="align: center; margin: 20px;"/>');
	$('#NoteWindow').fadeIn('slow');
}

function newNote(ElementID,LinkType,LinkID){
	_drawNoteWindow();
	createNote(ElementID,LinkType,LinkID,0);
}

function replyNote(ElementID,LinkType,LinkID,ParentID){
	_drawNoteWindow();
	createNote(ElementID,LinkType,LinkID,ParentID);
}

function createNote(ElementID,LinkType,LinkID,ParentID){
	var URL="/index.php?Realm=Note&Action=New&LinkType=" + LinkType + "&LinkID=" + LinkID + '&ParentID=' + ParentID;
	$.get(URL,'',function(data){
		var Result=RT_TransformXMLFragment(data, '/XSL/note/note.xsl');
		
		$('#NoteWindow').empty();
		$('#NoteWindow').append(Result);
	});
}

function saveNote(Form){
	if(document.getElementById('Note_Title').value.length==0){
		alert('Your note MUST have a title!');
		$('#NoteEditorButton').attr("value","Try Again");
	}else{
		var URL="/index.php?Realm=Note&Action=SaveNote"
				+ "&Note[Title]=" + document.getElementById('Note_Title').value
				+ "&Note[Description]=" + escape(document.getElementById('Note_Description').value)
				+ "&Note[LinkType]=" + document.getElementById('Note_LinkType').value
				+ "&Note[LinkID]=" + document.getElementById('Note_LinkID').value
				+ "&ParentID=" + document.getElementById('Note_ParentID').value
				+ "&NoteID=" + document.getElementById('NoteID').value;
				var LinkType=document.getElementById('Note_LinkType').value;
				var LinkID=document.getElementById('Note_LinkID').value;
		$.get(URL,'',function(data){
			var Status=data.getElementsByTagName('Success')[0].firstChild.data;
			if(Status=="Yes"){
				$('#NoteEditorButton').attr("value","Saved!");
				reloadNoteList();
				closeNoteWindow();
			}else{
				alert(data.getElementsByTagName('Message')[0].firstChild.data);
			}
		});
	}
}

function closeTaskWindow(){
	_clearTaskWindow();
	$('#TaskDetailViewer').fadeOut('slow');
}

