/**
*@Name: tc.widget.notes
*@Dependancies: sb.js, tc.js, sb.effects, sb.colors
*@Example:
<code>
	var myNotes = new tc.widget.notes({
		element : '#myid',
		events : {
		
			click : function(e){
				alert("I'm clicking");
			},
			
			moveover : function(e){
				alert("I'm rolling");
			}
		}
	});
</code>
*/
tc.widget.notes = function(ele, config){
					
	
	if((typeof(ele.myType) == 'function') && ele.myType() == 'tc.widget.notes'){
		return ele;
	}
	
	ele = $(ele);


	if(ele.nodeName && ( (ele.nodeName == 'UL') || (ele.nodeName == 'OL') )){	

		var i;
			
		tc.importProperties(this, ele);								
		tc.widget.notes.bin[ele.id] = ele; 	
				
		ele.makeVisible(0);
		
		//add events
		if(config.events){
			for(i in config.events){
				if(typeof(config.events[i]) == 'function'){
					ele.event(i,config.events[i]);
				}
			}
		}		
		
		//configure object -- speed?
		for(i in config){
			if(ele.publicProps.inArray(i)){
				ele[i] = config[i];
			}
		}
		
		
		
		return ele;
	}else{
		throw("you tried to make a tc.note with something other than a OL or UL");
	}
};

tc.widget.notes.bin = [];

tc.widget.notes.removeAll = function(){
	for(var i in tc.widget.notes.bin){
		if(sb.typeOf(tc.widget.notes.bin[i]) == 'tc.widget.notes'){
			tc.widget.notes.bin[i].removeAll();
		}
	}
	
	tc.widget.notes.bin = [];
};

tc.widget.notes.prototype = {
	
	/**
	*@Name td.notes.add
	*@Example: 
	<code>
		myNotes.add({ 
			id : 'forThis',
			message : 'You need to fill forThis in man!'
		});
	</code>
	*@Params: object args {id, message}
	*/
	
	//START HERE
	// name space conflitcts
	add : function(args, visual){ 
		
		visual = visual || 1;
		
		if(args.id && args.message){					
													
			if(this.list.inArray(args.id)){ 
				this.removeOne(args.id);							
			}			
			
			var mess = new sb.element({ 
				nodeName : 'LI',
				tag : 'LI',
				innerHTML :  args.message,
				id : 'note_' + args.id
			});
			
			try{
				mess.appendTo(this);		
			}catch(e){
				sb.consol.log(e);
				sb.consol.log(this.appendChild);
				sb.consol.log(this.nodeName);
			}
			
			
			this.list.push(args.id);	
			
			delete args.id;
			delete args.message;			
			tc.importProperties(args, mess);
														
			if(visual){this.attention();}
			this.makeVisible(1);						
		}else{
			//DECIDE
			throw("The argument passed to tc.widget.notes.add must have both a 'id' and message 'property'");
		}
		
	},

	attention : function(){					
		var down = this.cssTransition([	
			{
				prop : 'color',
				begin : 'FF0000',
				end : '000000',
				onEnd : function(){}							
			}], 60);
				
		var up = this.cssTransition([	
			{
				prop : 'color',
				begin : '000000', 
				end : 'FF0000',
				onEnd : function(){ down.start();}
			}], 120).start();	
			
	},
	
	debug : 1,
	
	list : [],				
	
	publicProps : ['debug'],
	
	removeOne : function(id){						
	var node;
									
		if(this.list.inArray(id)){
			this.list = this.list.removeByValue(id);	
			node = 	$('#note_' + id);		
			if(node){node.remove();}
		}
		
		if(this.list.length < 1){
			this.makeVisible(0);
		}
		
	},
	
	removeAll : function(){
							
		this.list.forEach(function(v, b, c){						
			sb.dom.remove('#note_' + v);
		});
		this.list = [];	
			
	},
	
	
					
	makeVisible : function(state){
		this.css('display', (state)?'block':'none');
	},
	
	myType : function(){
		return 'tc.widget.notes';
	},
	
	toString : function(){
		return this.typeOf();	
	}
};