/**
* @author : Anthony Cashaw
* @name : Form Note Pad
* @use: Notes for a form validator.  When all notes are moved from a note object instance
* @example: 
*
*	//creating a note repository for your object
*		this.notes = new app.notes({ 
*			listClass : 'class_name',	
			title : 'title_text',
			titlePos : ('top' | 'bottom') [ defaults to top ]
			padID : 'unique_name'[defalut will add pad_X where X is the number of pad objects on the stage],
*			showAlways : 0 [default is false. Set at namespace level]
*		});
*
*	//adding a note
*		this.notes.add({
*			id: 'note_id', //[required]
*			message : 'what will be displayed in the note'  //[required]
*			targ : '#dom_ref' [optional]
*		});
*
*	//removeing a note
*		this.notes.remove('note_id');
*
*/

app.notes = function(args){
	
	//adds the config of this 
	sb.objects.importProperties(args, this);
	
	//pass the parent node reference
	this.parent =  app.noteBin;

	//get the note title & position
	this.title = args.title;	
	
	this.titlePos = (args.titlePos)?args.titlePos:'top';
	
	this.showAlways = (this.showAlways)?this.showAlways:app.notes.showAlways;	 
	if(this.showAlways == 1){this.makeList()};
};

app.notes.pads = 0;

app.notes.init = function(){	
	// add event listener for all instances
	app.extra.event('click', function(e){
		var that = sb.events.target(e);	
		
		if(that.nodeName == 'A' && that.className == 'note'){
			sb.events.stopAndPrevent(e);	
			if(that.targ){that.targ.focus();}			
		}	
	});
}

app.notes.prototype = {
	
	makeList : function(){	
		
		//create DOM structure
		this.pad.appendTo(this.parent);
		
		var top = new sb.element({nodeName : 'div', id : this.pad.id + '_top', className: 'note_top'}).appendTo(this.pad);
		
				
		if(this.titlePos = 'top' && this.title.trim() !== ''){
			this.pad.innerHTML = "<h4>" + this.title + "</h4>";
		}
		
		//attach note list
		this.list = new sb.element({
		nodeName : 'ol',
		id : this.id
		}).appendTo(this.pad);
		
		if(this.titlePos = 'bottom' && this.title.trim() !== ''){
			this.pad.innerHTML = "<h4>" + this.title + "</h4>";
		}
		
		var bottom = new sb.element({nodeName : 'div', id : this.pad.id + '_bottom', className : 'note_bottm'}).appendTo(this.pad);
			
		
	},

	add : function(args){	
		
		if(!args.id || !args.message){
			return;
		}else{	
					
			//check for list
			if(!this.list){this.makeList();}		
			
			var prenote = $('#' + args.id);				
			
			args.targ = s$(args.targ) || null;
			
			var a = new sb.element({
					nodeName: 'a',
					href : '#',
					className : 'note',
					innerHTML: args.message,
					targ : args.targ
			});
			
			
			//add note to document	
			if(!prenote){	
				//make a new note							
				var li = new sb.element({ nodeName: 'li', id : args.id, className : this.listClass }).appendTo(this.list);							li.append(a);
			}else{	
				//replace an existing note message
				 a.replace(prenote.firstChild);
			}	
			
			this.pad.show();	
	
		}
	}, 
	
	remove : function(id){
				
		//add flashy removal script here...
		
		var prenote = $('#' + id);
		if(prenote){sb.dom.remove(prenote);}
		
		if(this.list && !this.showAlways){			
			if(this.list.childNodes.length < 1 && this.list.parentNode){
				this.pad.hide();
			}
		}
	}
}