/*
  Simple Growl Alert
*/

;(function($){
  $.growl = {};
  /* default options */
  $.growlDefaults = {
    css: {},
    cls: "ui-growl",
    titleCls: "ui-growl-title",
    messageCls: "ui-growl-message",
    iconCls: "ui-growl-icon",
    timeout: 2000,
    duration: 200,
    closeOnClick: true,
    autoClose: true
  }
  
  $.growl = function(title, message, options){
    var $body, $container, $growl, $message, $title;
    var settings;
    var animations = {};
    var init = function(){
      //merge settings
      settings = $.extend(true, $.growlDefaults, options);      
      //initialize UI Elements
      initUI();
      return show();
    }
    
    var initUI = function(){
      //get UI elements
      $body = $("body");
     
      $container = $.growl.container = $.growl.container || $("<div>", {
        css: {
          top: 0,
          right: 0,
          position: 'absolute'
        },
        "class": [settings.cls, "container"].join("-")
      });
      $body.append($container);
      $growl = $("<div>",{
        "class": settings.cls
      }).hide();
      $title = $("<h1>", {
        html: title,
        "class": settings.titleCls   
      });
      $message = $("<p>", {
        html: message,
        "class": settings.messageCls 
      });
      $growl.append($title);
      $growl.append($message);      
      $container.append($growl);
      if(settings.closeOnClick) $growl.click(hide);
    }
    
    var show = function(){
      return $growl.fadeIn(settings.duration, function(){
        if(settings.autoClose) setTimeout(hide, settings.timeout);
      });      
    }
    
    var hide = function(){
      $growl.fadeOut(settings.duration, function(){
        $growl.remove();
      });
    }
    
    return init();
  }
  
})(jQuery);
