/**
 * Unobtrusive notification messages v1.0
 * http://www.code-masters.org
 *
 * Copyright 2011, Strong
 * 
 * Under GPL license Version 2.
 * http://www.gnu.org/licenses/gpl-2.0.html
 * 
 * Date: 31/08/2011
 *
 * @uses jQuery
 * @author Strong
 * @version 1.0
 */

(function( $ ) {
  
    $.extend({
    
        notifier : {
        
            options : {
		placeholder : 'notifications',
		box_class : 'notify-message',
		notice_class : 'notify-notice',
		error_class : 'notify-error',
		success_class : 'notify-success',
		info_class : 'notify-info',
		box_title_class : 'notify-title',
		box_message_class : 'notify-text',
		duration : 4000,
		speed : 300
	    },
	    
	    notices : {},
	    
	    setNoticesPlaceholder : function() {
		var placeholder = this.options.placeholder;
		return ($('#' + placeholder).size() == 0)
		       ? $('body').prepend('<div id="' + placeholder + '"></div>')
		       : $('#' + placeholder);
	    },
	    
	    broadcast : function(title, message, type) {
	        this.setNoticesPlaceholder();
	        
	        // Cleanup current notices
	        if (!$.isEmptyObject(this.notices)) {
	            $.each(this.notices, function() {
	                $.notifier.destroy(this.id, true);
	            });
	        }
	        	        	        
	        var
	            placeholder = $('#' + this.options.placeholder),
	            id = 'notice-' + this.getUnique(),
	            box	= $('<div></div>')
	                .attr('id', id)
		        .addClass(this.options.box_class + ' ' + type)
		        .appendTo(placeholder),
		    title = $('<div></div>')
		        .addClass(this.options.box_title_class)
		        .appendTo(box)
		        .html(title),
		    text = $('<div></div>')
		        .addClass(this.options.box_message_class)
		        .insertAfter(title)
		        .html(message);
		        		        
		box.css({
		    top : -(box.outerHeight())
		});
		
		box.stop(true,true).animate({top:0}, this.options.speed);
		        		        
		// set notices object
		this.notices[id] = {id: id};             			
		this.life(box, id);
		this.events(box, id);	
				
		return this;
	    },
            
            error : function(title, message){
		this.broadcast(title, message, this.options.error_class);
	    },
	    
	    success : function(title, message) {
	        this.broadcast(title, message, this.options.success_class);
	    },		
		
	    notice : function(title, message){
		this.broadcast(title, message, this.options.notice_class);
	    },
			    
	    info : function(title, message) {
	        this.broadcast(title, message, this.options.info_class);
	    },
		    
	    events : function(box, seed){
		$(box).bind('click', function() {
		    var seed = $(this).attr('id');
		    $.notifier.destroy(seed, true);
		});
		/*.bind('mouseover', function() {
		    if ($.notifier.notices[$(this).attr('id')].interval) {
		        var seed = $(this).attr('id');
			$.notifier.destroy(seed)
		    }
		})
		.bind('mouseout', function() {
		    $.notifier.life(this, $(this).attr('id'));
		})*/
	    },
		
	    life : function(box, seed) {
	        if (!this.notices[seed].duration) { 
	            this.notices[seed].duration = this.options.duration;
	        }
	        this.notices[seed].interval = {};
	        this.notices[seed].interval = setInterval(function() {
	            (function(seed) {
	                $.notifier.destroy(seed, true);
		    })(seed);
		}, this.notices[seed].duration)
	    },
		
            destroy : function(seed, remove) {
                clearInterval($.notifier.notices[seed].interval);
                delete $.notifier.notices[seed].interval;
                
                if (remove == true) {
                    var notice = $('#' + seed), speed = this.options.speed || 300;
                    notice.stop(true,true).animate({top:-(notice.outerHeight())}, speed, function() {
                        $(this).unbind('click');
                        $(this).remove();
                        // Cleanup notices object
                        delete $.notifier.notices[seed];
                    });
                }
	    },
		
            getUnique : function() {
                return (new Date()).getTime();
	    }
	
        }
    
    });
    
})( jQuery );

