/*kanu was it
 */
(function($) {
var container;
var uiInformantClasses =
	'ui-informant ' +
	'ui-widget ' +
	'ui-widget-content ' +
	'ui-corner-all ';

var InfotypeClasses = {
    'error' : { 
        informant : 'error-msg',
        titlebar:'error-msg-titlebar',
        content: '' 
    },
    'notice' : { 
        informant : '',
        titlebar:'',
        content: '' 
    },
    'info' : {
        informant : 'info-msg', 
        titlebar:'info-msg-titlebar',
        content: '' 
    },
    'news' : {
        informant : 'news-msg',
        titlebar:'news-msg-titlebar',
        content: '' 
    }
}

$.widget("ui.informant", {
    options: {
		closeText: 'close',
		default_type : 'info',
        fxshow : {},
        fxhide : {}
	},
    _create: function() {
        var self = this,
            ele = self.element,
            options = this.options,
            title = options.title || ele.attr('title') || '&#160;',
            uid = $.ui.informant.getUID(),
            titleId = 'title_'+uid,
            infotype = options.type || ele.attr('class').match(/informant-(\w+)/) || options.default_type;
            infotype = (typeof infotype == 'object')? infotype[1] : infotype;
            infoclasses = InfotypeClasses[infotype] || InfotypeClasses.info

        var uiInformant = (this.uiInformant = $('<div></div>'))
            .hide()
            .prependTo(container)
            .addClass(uiInformantClasses + ' ' + infoclasses.informant),
            
            uiInformantContent = $('<div></div>')
				.removeAttr('title')
				.addClass(
					'ui-informant-content ' +
					'ui-widget-content ' +
					infoclasses.content)
				.appendTo(uiInformant)
				.append(ele.attr('title','')),
				
			uiInformantTitlebar = (self.uiInformantTitlebar = $('<div></div>'))
				.addClass(
					'ui-informant-titlebar ' +
					'ui-widget-header ' +
					'ui-corner-all ' +
					'ui-helper-clearfix ' +
					infoclasses.titlebar
				)            
				.addClass(InfotypeClasses[infotype].titlebar)
				.prependTo(uiInformant),
				
			uiInformantTitlebarClose = $('<a href="#"></a>')
    				.addClass(
    					'ui-informant-titlebar-close ' +
    					'ui-corner-all'
    				)
    				.attr('role', 'button')
    				.hover(
    					function() {
    						uiInformantTitlebarClose.addClass('ui-state-hover');
    					},
    					function() {
    						uiInformantTitlebarClose.removeClass('ui-state-hover');
    					}
    				)
    				.focus(function() {
    					uiInformantTitlebarClose.addClass('ui-state-focus');
    				})
    				.blur(function() {
    					uiInformantTitlebarClose.removeClass('ui-state-focus');
    				})
    				.click(function(event) {
    					self.close(event);
    					return false;
    				})
    				.appendTo(uiInformantTitlebar),

			uiInformantTitlebarCloseText = (self.uiInformantTitlebarCloseText = $('<span></span>'))
				.addClass(
					'ui-icon ' +
					'ui-icon-closethick'
				)
				.text(options.closeText)
				.appendTo(uiInformantTitlebarClose),	
				
			uiInformantTitle = $('<span></span>')
				.addClass('ui-informant-title')
				.attr('id', titleId)
				.html(title)
				.prependTo(uiInformantTitlebar);


                
        container.filter(':hidden').show();
        
        var coffset = container.offset().top
        if ( coffset < $('html,body').scrollTop()){
            $('body,html').animate({scrollTop:coffset},100)
        }

        return ele
    },
    _init: function(){
        this.uiInformant.show('blind',function(){})
        container.trigger('update.informants')
        
    },
    close : function(e){
        var self = this;

        this.uiInformant.hide('blind',function(){self.destroy()});
        
        // self.uiInformant.hide('clip',function(e){self.destroy()});
    },
    destroy : function(){
        $.Widget.prototype.destroy.apply(this, arguments);
        var removed = this.uiInformant.remove();
        container.trigger('update.informants');
        return this.element;
    }
});


$.extend($.ui.informant,{
    uuid: 0,

    getUID : function(){
        return 'infochunk_'+this.uuid++;
    },
    register : function(node,opts){
        // register the container where informants appear
        container = $(node).hide()
        
        // .before(render_toolbar().hide()); 
        if (opts) $.extend(this.prototype.options,opts);
        
        // container.bind('update.informants',function(){
        //     if ($(this).children().length > 0) {
        //         $('#informant_toolbar:hidden').show('drop')
        //     } else {
        //         $('#informant_toolbar:visible').hide('drop');
        //     }
        // })
        container.children().informant();
        return container.trigger('registered.informant');
        
    },
    create : function(msgobj) {
        $(msgobj).each(function(i,v){
            $('<div></div>').html(v.content||v.text||'').informant(v);
        })
        container.trigger('update.informants')
    },
    container : function(){
        return container;
    }
});


}(jQuery));

