/*
 * jQuery Simple Greybox plugin
 * Version 1.0.0 (03/03/2009)
 * @requires jQuery v1.2.1 or later
 *
 * Copyright (c) 2009 Aleksandar Pavic for majlab.com
 * http://acosonic.com/jquery_simple_overlay/
 
 *EDITED BY MEW
 -works with fixed style
 -height and width can be set dynamically

USAGE:
	
	$( #id ).gbxInit(); 	
	$( #id ).gbxShow(); //-usually assigned to click events	
	$( #id ).gbxHide(); // same as above
EXAMPLE:
$("#gbx_create_album").gbxInit({width:451,height:317,top:100});
		
	$("#gbx_create_album_close").click(function () { //close button inside greybox
		$("#gbx_create_album").gbxHide();
		return false;
	});
			
	$("#create_album_link").click(function() {
		$("#gbx_create_album").gbxShow();
		return false;
	});
*/
(function($) {
  
  $.fn.gbxInit = function(options) {
	var opts = $.extend({}, $.fn.gbxInit.defaults, options);
    return this.each(function() {
      $this = $(this);
/*	
		grab the settings if any
*/
      var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
      // only create the overlay if it does not exist
	  if ($('#gbx_overlay').length == 0) $('body').append('<div id="gbx_overlay"></div>');
	  
      $this.css(o);
	  var left = document.body.scrollLeft+ ($('html,body').width()-o.width)/2;
	 // var top = document.body.scrollTop;
	  var top = document.body.scrollTop;
	  /* mew: scroll top fix for IE and JS*/
	  
	  if (top == 0)
		{
			if (window.pageYOffset)
				top = window.pageYOffset;
			else
				top = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}
		
	 top = top + ($(window).height() / 2) - (options.height / 2);
	 if ((options.height +20) >= $(window).height()) top = top + 50;
	 if (top <= 20) top = 50;
     // $this.css("left",left);
	 $this.css("top",top);
	 // $this.css("left",left);
     $this.css("z-index",3000);
     $this.css("position","relative");
	  // $this.css("margin", "0 auto");
	  
	  /*
      $this.bind('drag',function( event ){
        $(this).css({
  			 top: event.offsetY,
  			 left: event.offsetX
  			 });
		  });
	  
	  
	  
  	  $this.append('<div class="gbx_drag"></div>');
       $(".gbx_drag").css ({
          "cursor":"move",
          "height":"20px",
          "z-index":"-1",
          "width":o.width,
          "position": "fixed",
          "top":0,
          "float":"left"
        });
		
		*/
		
		return false;
		
		
    });
  };
  
  $.fn.gbxInit.defaults = {
     /* background: 'yellow'*/
  };
  
  $.fn.gbxShow = function() {
	return this.each( function() {
		//if($.browser.mozilla) {
			$("#gbx_overlay").fadeIn(0);
			$(this).fadeIn(500);
		// }
		//  else {
		//	$(this).show();
		//	$("#gbx_overlay").show();
		//  }
		  return false;
	});
  }
  
  $.fn.gbxHide = function(id) {
    return this.each( function() {
		//if($.browser.mozilla) {
			  $("#gbx_overlay").fadeOut(300);
			 $(this).fadeOut(300);
			//} else {
			//  $("#gbx_overlay").hide();
			// $(this).hide();
			//}
			return false;
	});
   }
  
})(jQuery);
