function noteObject(){
	var _self = this;
	this.properties = new Object();
	_self.properties['self'] = _self;
	_self.properties['type'] = "note";
	_self.properties['bars'] ="noDisplay";
	_self.properties['notes'] = new Object();
	
	this.select 	   = function()				{_noteManager.selectedNote = _self;} 
	this.unSelect 	   = function()				{}
	this.appendTo      = function(parentObject)	{parentObject.appendChild(_self.properties["holderDiv"]);}
	this.changeName	   = function(newName)		{_self.properties['name'] = newName};
	this.startResize   = function()				{}
	this.resizeUp      = function(differenceY)	{}
	this.resizeDown    = function(differenceY)	{}
	this.resizeRight   = function(differenceX)	{}
	this.resizeLeft    = function(differenceX)	{}
	this.endResize     = function()				{}
	this.setPosition   = function(topPos,leftPos){_self.properties['holderDiv'].style.top = topPos;_self.properties['holderDiv'].style.left = leftPos;}	
	this.copyElement   = function()				{}		
	this.toXML		   = function()				{_noteManager.selectedNote = _self;return _noteManager.toXML()}
	this.setSize	   = function(height,width)	{ }
	this.changeNoteColor = function(noteColor) {_noteManager.selectedNote = _self;return _noteManager.changeNoteColor(noteColor)}
	this.changeNoteImg = function(noteImg) {_noteManager.selectedNote = _self;return _noteManager.changeNoteImg(noteImg)}
	this.deleteElement  = function()	{_noteManager.deleteElement(_self)};
}

function NoteManager(){
	var _self = this
	var _noteImg = "left.gif";
	var _noteDiv = document.getElementById("note_div");
	var _noteColor = "green";
	var _saveNotes = false;
	var _newMessages = new Object();
	var _pageNotes = new Object();
	this.selectedNote = null;
	
	var _toolkitOptions = new Object();
	//_toolkitOptions['addNote'] = 'addNote';
	
	this.createNewNote = function(noteImg){
		_noteImg = noteImg;
		_elementsManager.addElementToPage("note");
		document.getElementById('elements_holder_div').style.display='block';
		document.getElementById('notes_holder_div').style.display='none';
	}
	
	this.createNote = function(){				
		return createNote(_objectManager.createId("note"), _noteColor, _noteImg);
	}
	
	function createNote(id, noteColor, noteImg){
		_self.selectedNote = new noteObject();
		setProperty("id",id);				
		setProperty("toolkit",_toolkitOptions);
		setProperty("noteColor",noteColor);
		setProperty("noteImg",noteImg);
		createHolderDiv(noteImg,noteColor);
		_pageNotes[id] = _self.selectedNote;
		return _self.selectedNote;	
	}
	
	
	function setProperty(property,value){_self.selectedNote.properties[property] = value;}
	function getProperty(propertyName){	return _self.selectedNote.properties[propertyName];}	
	function createHolderDiv(noteImg,noteColor){		
		var holderDiv = document.createElement('div');
		holderDiv.id = getProperty('id');		
		holderDiv.style.background = "url(work/WebsiteEditor/Images/elementsMenu/notes/"+ noteColor+"/"+noteImg+")";
		holderDiv.title = "Double click to view/enter notes.";
		holderDiv.cancelBubble = true;
		_pm.setStyle(holderDiv,"position:absolute;width:50px; height:70px; top:0; left:0; overflow:hidden; background-repeat: no-repeat;")
		
		var notificationDiv = document.createElement("div");
		_pm.setStyle(notificationDiv,"position:absolute; top:50;background:#ffec8a; border:1px solid black; width:50; height:20;cursor:pointer;overflow:hidden;");
		notificationDiv.innerHTML = '<table width="100%" height="20"><tr><td id="notification.' +getProperty('id') +'" align="center" style=" font-size:12">0 Notes</td></tr></table>'
		notificationDiv.onclick = function(){_self.selectNote();} 
		holderDiv.appendChild(notificationDiv);
		holderDiv.onmousedown = function(e){if (!e) var e = window.event;
											_elementsManager.selectElement(this,e.ctrlKey);
											e.cancelBubble = true;} 
		holderDiv.ondblclick = function(){_self.selectNote();} 
		setProperty("holderDiv", holderDiv);	
	}
	
	this.selectNote = function(){
		_noteDiv.style.display = "block";
		var holderDiv = getProperty("holderDiv");
		var position = _utilities.findPos(holderDiv);
		_noteDiv.style.top = position[1];
		_noteDiv.style.left = position[0] + 50;
		var notes = getProperty("notes")
		if( numMessages()==0) _self.addNewNote();
		else _self.cancelNewNote();
	}
	
	function numMessages(){
		var num = 0;
		var notes = getProperty("notes");
		for(eachNote in notes)
			num++;
			
		return num;
	}
	
	this.toXML = function(){		
		var xml = "";		
		if(numMessages()>0){
				xml = "<note>";
				xml+= xmlTag("id",getProperty('id'));
				xml+= xmlTag("subject",getProperty('subject'));	
				xml+= xmlTag("noteImg",getProperty("noteImg"));
				xml+= xmlTag("noteColor",getProperty("noteColor"));
				xml+= xmlTag("top",getProperty('holderDiv').style.top)
				xml+= xmlTag("left",getProperty('holderDiv').style.left)			
				xml+= xmlTag("parentId",getProperty('parentId'));
				if(getProperty('holderDiv').style.zIndex == "") 
					xml+= xmlTag("zindex","0");
				else
					xml+= xmlTag("zindex",getProperty('holderDiv').style.zIndex);
				
				var notes = getProperty("notes");
				for(eachNote in notes){
					xml+="<message>"
						xml+= xmlTag("by",notes[eachNote]["user"]);
						xml+= xmlTag("time",notes[eachNote]["time"]);
						xml+= xmlTag("text","<![CDATA[" + notes[eachNote]["message"]+"]]>");
					xml+="</message>"
				}
				xml+="</note>"			
		}
		return xml;	
	}
	
	function xmlTag(tagName,tagData){return "<" + tagName +">" + tagData + "</" + tagName + ">"};
	
	this.loadNote = function(id, subject,noteImg,noteColor, topPos, leftPos, parentId,zindex){
		var newElement = createNote(id,noteColor,noteImg);
		newElement.properties['holderDiv'].id = id;
		newElement.properties['holderDiv'].style.zIndex = zindex;
		newElement.setPosition(topPos,leftPos);
		newElement.properties['parentId'] = parentId;
		if(subject!="undefined" && subject!="") newElement.properties['subject'] = subject;
		_objectManager.loadElement(newElement);
	} 
	this.loadMessage = function(id, user, time, text){
		var newNote = new Object();
		newNote["user"] = user;
		newNote["time"] = time;
		newNote["message"] = text;
		_self.selectedNote = _pageNotes[id]
		_self.selectedNote.properties["notes"][makeNoteId()] = newNote;
		if(_pageManager.compareUserDates(time)){
			var numNew = _self.selectedNote.properties["newMessages"]
			if(numNew != undefined)
				 numNew = parseInt(numNew)+1;
			else
				numNew = 1;
			
			setProperty("newMessages",numNew)
		}
		var notificationTd = document.getElementById('notification.' +id); 
		var newMessages = _self.selectedNote.properties["newMessages"];
		if(newMessages != undefined)
			notificationTd.innerHTML = '<span style="color:red; font-size:12;"><b>' +newMessages + " new </b></span>";
		else
			notificationTd.innerHTML = numMessages() + " notes";
	}
	
	this.closeNoteDiv = function(){
		_noteDiv.style.display = "none";
	}
	
	this.cancelNewNote = function(){
		document.getElementById("note_addNote_div").style.display="none";
		document.getElementById("note_messages_div").style.display="block";
		_self.displayNotes();
	}
	
	this.addNewNote = function(){
		
		document.getElementById("note_messages_div").style.display="none";
		document.getElementById("note_addNote_div").style.display="block";
		document.getElementById("note_text_input").value="";
		document.getElementById("note_text_input").focus();
		var subject = getProperty("subject");
		if(subject == undefined){
			document.getElementById("note_subject_div").style.display="block";
			document.getElementById("note_subject_input").value = "";
			document.getElementById("note_subject_input").focus();
		}else{
			document.getElementById("note_subject_div").style.display="none";
		}
		var addNoteTd = document.getElementById("addNote_td");
		addNoteTd.innerHTML = "add note";
		addNoteTd.onclick = function(){_noteManager.addNote()}
			
	}
	
	this.displayNotes = function(){
		var notesHolder = document.getElementById("note_message_holder");
		var notes = getProperty("notes")
		var noteHTML = "";
		var newMessage = "";
		
		var notificationTd = document.getElementById('notification.' +getProperty("id"));
		notificationTd.innerHTML = numMessages() + " notes";
		
		for(eachNote in notes){
			if(_pageManager.compareUserDates(notes[eachNote]["time"]))newMessage = '<span style="color:red">(new) </span>';
			else newMessage = "";
			html = '<div style="border-top: 1px dashed #B8B8B8; margin-top:3;">'+newMessage+'<b>' +notes[eachNote]["user"]+ '</b> says  <span style="font-size:11px;">('+formatTime(notes[eachNote]["time"]) +")</span></div><div>"+ notes[eachNote]["message"]+" </div>";
			if(notes[eachNote]["user"] == _pageInfo["username"]) html+='<table width="100%"><tr><td align = "left"><span style="color:#B8B8B8; cursor:pointer;" onclick="_noteManager.changeNote('+eachNote+')">edit</span></td><td align="right"><span style="color:#B8B8B8; cursor:pointer;" onclick="_noteManager.deleteNote(' +eachNote +')">delete</span></td></tr></table>'
			noteHTML = html + noteHTML;			
		}
		
		if (noteHTML =="") noteHTML = '<div style="border-top:1px dashed #B8B8B8; margin:5px;">There are no messages for this note</div>';
		var subject = getProperty("subject");
		if(subject != undefined && subject != "") noteHTML = '<div align="center" style="margin:3;"><b>' + subject +"</b></div>" + noteHTML;
			
		notesHolder.innerHTML = noteHTML;
	}
	
	this.addNote = function(){
		var subject = getProperty("subject")
		var subjectText = ""
		if(subject== undefined) subjectText = _utilities.trimAll(document.getElementById("note_subject_input").value);
		if (subjectText != "") setProperty("subject", subjectText);
		var noteText = _utilities.trimAll(document.getElementById("note_text_input").value);
		var noteId = makeNoteId();
		if (noteText != ""){
			var stamp = new Date();
			var newNote = new Object();
			newNote["user"] = _pageInfo["username"];
			newNote["time"] = (stamp.getMonth()+1) +"/"+stamp.getDate()+ "/"+stamp.getFullYear() +"/" +stamp.getHours() + "/" + stamp.getMinutes();
			newNote["message"] = noteText;
			
			var notes = getProperty("notes")
			
			notes[noteId] = newNote;
		}
		_newMessages[noteId] = noteId;
		_self.cancelNewNote();
	}
	
	function formatTime(unformattedTime){
		time = unformattedTime.split("/");
		var ampm = "am";
		var stamp = new Date();
		var hour = time[3];
		var minute = time[4];
		var year = time[2];
		var day = time[1];
		var month = "";
		switch(time[0]){
			case "1": month = "Jan."; break;
			case "2": month = "Feb."; break;
			case "3": month = "Mar."; break;
			case "4": month = "Apr."; break;
			case "5": month = "May."; break;
			case "6": month = "Jun."; break;
			case "7": month = "Jul."; break;
			case "8": month = "Aug."; break;
			case "9": month = "Sep."; break;
			case "10": month = "Oct."; break;
			case "11": month = "Nov."; break;
			case "12": month = "Dec."; break;
			default: break;	
		}
		if(parseInt(hour)>=12) ampm = "pm";
		if(parseInt(hour)>12) hour = parseInt(hour) - 12;
		if(year == stamp.getFullYear()) year = "";
		else year = " " +year;
		if (parseInt(minute)<10) minute = "0"+minute;
		
		if(day == stamp.getDate())
			return  hour + ":" +minute + ampm;
		else
			return month+" " + time[1]+ year + " @ " + hour + ":" +minute + ampm;
	}
	this.deleteNote = function(noteId){
		if(confirm("Delete Note?")){deleteNote(noteId)	}
	}
	
	function deleteNote(noteId){
		var notes = getProperty("notes");
		notes[noteId] = "";
		delete notes[noteId]; 
		if(_newMessages[noteId] != undefined) delete _newMessages[noteId];
		else _saveNotes = true;
		
		_self.cancelNewNote();
	}
	
	this.changeNote = function(noteId){
		var notes = getProperty("notes");
		var note = notes[noteId];
		_self.addNewNote();
		document.getElementById("note_text_input").value = note['message'];
		var addNoteTd = document.getElementById("addNote_td");
		addNoteTd.innerHTML = "update note";
		addNoteTd.onclick = function(){_noteManager.updateNote(noteId)}
	}
	
	this.updateNote = function(noteId){
		var notes = getProperty("notes");
		var note = notes[noteId];
		var noteText = _utilities.trimAll(document.getElementById("note_text_input").value);
		if(noteText != note["message"]){
			deleteNote(noteId)
			_self.addNote();
		}else{
			alert("No text was changed.")
		}
	}
	
	function makeNoteId(){
		var id = Math.random() * 5000;
		var notes = getProperty("notes");
		while (notes[id] != undefined){
			id = Math.random() * 5000;
		}
		return id;	
	}
	
	this.changeColors = function(noteColor){
		_noteColor = noteColor;
		document.getElementById("left_note_img").src = "work/WebsiteEditor/Images/elementsMenu/notes/"+_noteColor+"/left.gif";
		document.getElementById("right_note_img").src = "work/WebsiteEditor/Images/elementsMenu/notes/"+_noteColor+"/right.gif";
		document.getElementById("up_note_img").src = "work/WebsiteEditor/Images/elementsMenu/notes/"+_noteColor+"/up.gif";
		document.getElementById("down_note_img").src = "work/WebsiteEditor/Images/elementsMenu/notes/"+_noteColor+"/down.gif";
		document.getElementById("question_note_img").src = "work/WebsiteEditor/Images/elementsMenu/notes/"+_noteColor+"/question.gif";
		document.getElementById("exclamation_note_img").src = "work/WebsiteEditor/Images/elementsMenu/notes/"+_noteColor+"/exclamation.gif";
	}
	this.getPageNotes =function(){
		return _pageNotes;
	}
	this.notesSaved = function(){
		_saveNotes = false;
		delete _newMessages;
		_newMessages = new Object();
	}
	this.saveNotes = function(){
		//only need to save notes if an old note was deleted or a new one has been added.
		//0 = no need to save
		//1 = notes have been deleted - save but don't notify other users
		//2 = notes have been added - notify others
		var saveNotes = 0;
		if(_saveNotes) saveNotes = 1;
		else{
			for(eachMessage in _newMessages) saveNotes = 2;
		}
		return saveNotes
	}
	this.changeNoteImg = function(noteImg){
		getProperty("holderDiv").style.background = "url(work/WebsiteEditor/Images/elementsMenu/notes/"+ getProperty('noteColor')+"/"+noteImg+")";
	}
	this.changeNoteColor = function(noteColor){
		getProperty("holderDiv").style.background = "url(work/WebsiteEditor/Images/elementsMenu/notes/"+ noteColor+"/"+getProperty("noteImg")+")";
	}
	this.deleteElement = function(noteElement){
		_pageNotes[noteElement.properties["id"]] = null;
		delete _pageNotes[noteElement.properties["id"]];
		_saveNotes = true;
	}
}
