// jQuery overlay plugin
(function( $ ){

    $.fn.closeOverlay = function(delay) {
        if(delay) {
            setTimeout(close_overlay(), delay)
        } else {
            close_overlay()
        }

    }
  $.fn.createOverlay = function(title, content, width, notClosable) {
    
    var width_stl = "";
    
    if (width != undefined) width_stl = "style='width:"+width+"px;'";
    
    // Set bind to global event
    $("body").bind("close_overlay", close_overlay);

    var html = $('<div id="bbb" class="p-container"  > \
                  <div class="p-content" '+width_stl+'> \
                  <div class="p-title"> \
                  <span class="title">'+title+'</span> \
                  <a href="#" class="close">&times;</a> \
                  </div> \
                  <div id="p-container">' +
            '<div class="nbr"></div></div> \
                  </div> \
                  </div>');

    // Show overlay layer and append content to body element

//    console.log (html);
    $("body").append(html);
//      html.css({position: 'absolute', top: '20px'})

    // Add content to form
    $("#p-container").prepend(content);

    // Set delegate for close event
      if(!notClosable) {

          $(".p-title a.close").click(close_overlay);
          $("body").keydown(function(e) {
              if (e.keyCode == 27) {
                  close_overlay();
              }
          });
      } else {
          $('.p-title').find('.close').hide();
      }

      show_overlay();
  };

  // Make gray overlay
  function show_overlay()
  {
    var div = $("<div class='overlay' style='height=100%;'></div>");
    $("body").prepend(div)
    div.height($(document).height()).width($(document).width());
      
    div.fadeIn(300, function(){

//        $('.overlay').html($('#bbb'));
//      $('#bbb').show()
    });
      $(window).resize(function(){
          div.height($(document).height())
          div.width($(document).width())
      });


//      div.html('Ghjdthrf')

  }

  // Hide overlay
  function close_overlay()
  {
    $(".overlay").fadeOut('fast',function(){
        $(".p-container").remove(); $(this).remove();

    });
    return false;
  }

})(jQuery);
