;(function($) {
   $.pocketbox = {
     init: {
       wrapper: $('<div></div>'),
       layer: $('<div style="display:none; background-color:black; top:0px; left:0px; opacity:0; position:fixed;z-index:50000;"><div style="width: 100%; height: 100%;"></div></div>'),
       box: $('<div id="pocketbox"><div class="head"><div><span></span><a></a></div></div><div class="body"><div class="content"></div></div><div class="foot"><div></div></div></div>').hide(),    
       title: "",
       active: false,       
       initailized: false
     },
     
     defaults: {
       width: 700,
       opacity: 0.85,
       title: "",
       href: "",
       wrapper: "#wrapper",
       live: false
     },    
     
     show: function(settings)
     {
      if($.pocketbox.init.active)
        return false;
      else
        $.pocketbox.init.active=true;
         
				if(!$.pocketbox.init.initailized)
				{
				  $.pocketbox.init.initailized=true;
				  
				  if(settings.wrapper)
				  {
				    $.pocketbox.init.wrapper = $(settings.wrapper);
				  }
				  else
				  {
				    jQuery('body').children(':visible').appendTo($.pocketbox.init.wrapper);
				    $.pocketbox.init.wrapper.appendTo('body');
				  }
				  $.pocketbox.init.layer.hide();
				  
				  $.pocketbox.init.layer.appendTo('body');
				 // $.pocketbox.init.layer.css({'background-color':'black', 'top':'0px', 'left':'0px', 'opacity': '0', 'position':'fixed'});
				  $.pocketbox.init.box.appendTo('body');
				  $.pocketbox.init.box.find('.head a').click(function(){
				    $.pocketbox.hide();
				  });
				  $.pocketbox.init.layer.find('div').mousedown(function(){
				    $.pocketbox.hide();
				  });        
				}				     
       
       var offscreen, loading; 
       var win = $(window);
       var content = $.pocketbox.init.box.find('.content');  
       
//      offscreen.appendTo(content);               
      $.pocketbox.init.scroll = $(document).scrollTop();

      $.pocketbox.init.wrapper.width(win.width()).height(win.height()+$.pocketbox.init.scroll);    
      $.pocketbox.init.wrapper.css({'margin-top': -$.pocketbox.init.scroll, 'overflow':'hidden', 'position':'fixed'});                      
      
      if($.pocketbox.init.wrapper.width() <= win.width())
        jQuery('body').css('overflow-y', 'scroll');

      $.pocketbox.init.box.width(settings.width);      
      $.pocketbox.init.box.css({'left':-settings.width, 'top':0, 'position':'fixed'});
      $.pocketbox.init.box.find('.head div span').text(settings.title);
      $.pocketbox.init.box.show();

      $.pocketbox.init.layer.height(win.height());
      $.pocketbox.init.layer.width(win.width());
      if($.pocketbox.init.layer.bgiframe)
        $.pocketbox.init.layer.bgiframe();
      
      $.pocketbox.init.layer.show();
      
      offscreen = content.clone();
      
      offscreen.css({'left':-settings.width, 'width':content.width(), 'top':0, 'position':'fixed'});
      offscreen.appendTo(content.parent());
      offscreen.empty();     
      offscreen.append('<div class="loading"></div>');
      
      $.pocketbox.init.layer.animate({'opacity': '0.85'}, 000, function(){
                                  
        content.height( offscreen.height());                        
        content.empty().append(offscreen.html());
        
        $.pocketbox.init.box.css({'left':(win.width()-settings.width)/2, 'top':(win.height()/10)});          
                  
        offscreen.empty()
        
        offscreen.load(settings.href, null,function(responseText, textStatus, XMLHttpRequest){                 
          var h = offscreen.height(),
              w = content.width(),
              max_h = win.height()/10 * 8;
          
          if(h > max_h)
          {
            h = max_h;
            content.css('overflow-y', 'scroll');
          } 
          else
          {
            content.css('overflow-y', 'auto');
          }
          
          if($.trim($.pocketbox.init.box.find('.head div span').text()) == '')
          {
            $.pocketbox.init.box.find('.head div span').text(offscreen.find('h1,h2,h3').eq(0).text());
          }          
          
          content.animate( { height:h }, 1000 );          
          content.empty().append(offscreen.html()); 
          content.width(w);     
          offscreen.remove();
        });
      });
      return false;
     },
     
     hide: function()
     {
       if(!$.pocketbox.init.active)
         return;
       else
         $.pocketbox.init.active=false;
         
       //var offscreen = $('<div id="offscreen"></div>');
       var content = $.pocketbox.init.box.find('.content');
       while(content.size() > 1)
       {
         content.eq(0).remove();
         content = $.pocketbox.init.box.find('.content');
       } 
       //offscreen.appendTo(content);

       $.pocketbox.init.box.hide();  
       content.replaceWith('<div class="content"></div>');    
       
       $.pocketbox.init.layer.animate({'opacity': '0'}, 000, function(){
         $.pocketbox.init.layer.hide();         
         $.pocketbox.init.wrapper.css({'margin-top': 0, overflow:'auto', height:'auto', width:'auto', 'position':'static'});
         $(document).scrollTop($.pocketbox.init.scroll);                 
       });  
     }
   };
   
  $.fn.extend({
    pocketbox: function(settings){
      var settings = $.extend({}, $.pocketbox.defaults, settings);
            
      if(settings.live)
      {
        this.bind('click', function(event){
          event.preventDefault(); 
          var localsettings = $.extend({}, $.pocketbox.defaults, settings);
          o = $(this);
          if(!o.is('a'))
            o = o.find('a:first');
          if(localsettings.href == "")         
            localsettings.href = o.attr('href');
          if(localsettings.title == "")
            localsettings.title = o.attr('title');
          return $.pocketbox.show(localsettings);
        });
      }
      else
      {
	      return this.each(function() {        
	        o = $(this);
	        if(settings.href == "")         
	          settings.href = o.attr('href');
	        if(settings.title == "")
	          settings.title = o.attr('title');
	        o.click(function(){
	          return $.pocketbox.show(settings);          
	        })
	      });
      }
    }
  });       
})(jQuery);

