/*
 * Luvbox (for jQuery)
 * Version: Beta (09 Jun 2009)
 * Author: Nick Lovibond
*/
 
 (function($) {
			  
  $.fn.luvbox = function(settings) { // create plugin
	init(settings)
	function clickHandler() {
		luvbox(this.href)
		return false
    }
    return this.click(clickHandler)
  }
  
  function luvbox(href) { // determine element to reveal using id specified in href, and clone it OR grab HTML
	  if (href.match(/#/)) {
		  var url = window.location.href.split('#')[0]
		  var target = href.replace(url,'')
		  reveal($(target).clone().show())
	  } else {
		  $.get(href, function(html) { reveal(html) })  
	  }
  }
  
  function reveal(element) { // create and position the scene
  	  winH = ( typeof( window.innerHeight ) == 'number' ) ? window.innerHeight : document.documentElement.clientHeight; // Cross browser (not quirks!)
	  $("#luvbox .e").append(element)
	  $("#luvbox").css({
		 "position" : "absolute",
		 "top" : $(window).scrollTop() + winH/2 - ($('#luvbox').height()/2),
		 "left" : $(window).width()/2 - ($('#luvbox').width()/2),
		 "z-index" : "100",
		 "display" : "none"
	  })

	  $("#overlay").fadeIn(200, function() {
        $("#luvbox").fadeIn(350);
      })
  }
  
  function close() { // fade out and delete content
	  $("#luvbox").fadeOut(250, function() {
        $("#overlay").fadeOut(200, function() {
			$("#luvbox .e").empty();							   
		})
      })
  }

  function init(settings) {
	  var luvbox = '<div id="overlay"></div>\
	  <table id="luvbox">\
	    <tr>\
		  <td class="a"></td><td class="b"></td><td class="c"></td>\
		</tr>\
	    <tr>\
		  <td class="d"></td><td class="e"></td><td class="f"></td>\
		</tr>\
	    <tr>\
		  <td class="g"></td><td class="h"></td><td class="i"></td>\
		</tr>\
	  </table>';
	  $('body').append(luvbox);
	  
	  $("#overlay").css({
		 "height" : $(document).height(),
		 "width" : "100%",
		 "position" : "absolute",
		 "cursor" : "pointer",
		 "left" : "0",
		 "top" : "0",
		 "z-index" : "99",
		 "display" : "none"
	  }).click(function() { close() });

  }
  
})(jQuery);