jQuery.lbox = {
	build : function(options) {
		settings = jQuery.extend({
			 width: "500px",
			 height: "500px",
			 background: "#fff",
			 overlaybg: "#000",
			 button: "blue"
		  }, options);
		  
		var cssSrc = '/css/lbox.css';
		
		if ($.browser.msie) {
			cssSrc = '/css/lbox.ie.css';
		}
		
		$('<link>').appendTo('head').attr({
			rel: 'stylesheet',
			type: 'text/css',
			href: cssSrc
		});
		
		$(this).each(function() {
			var theID = $(this).attr('id');
			
			var theLightbox = $('<div id="lightbox-'+theID+'" class="lightbox"/>');
			var theShadow = $('<div id="lightbox-shadow-'+theID+'" class="lightbox-shadow"/>');
			$(theShadow).click(function(e){
				$.lbox.close();
			});
			$('body').append(theShadow);
			$('body').append(theLightbox);
		
		
			$(theLightbox).css('width',settings.width);
			$(theLightbox).css('height',settings.height);
			$(theLightbox).css('background',settings.background);
			$(theShadow).css('background',settings.overlaybg);
			
			
			
			
			$(theLightbox).append('<div style="text-align:right"><a href="javascript:$.lbox.close()"><img src="/images/close_'+settings.button+'.gif" height="19" width="19"/></a></div>');
			
			$(theLightbox).append($(this));
			
			//$(this).css('display','none');
		});
		
		$(window).bind("resize", function(){
				$.lbox.center();
			});

	},
	
	open : function(obj) {
		/*
		settings = jQuery.extend({
			 background: "#fff",
			 overlaybg: "#000"
		  }, options); */
		// remove any previously added content
		//$('#lightbox-'+obj).empty();
		
		// make it center :
		$.lbox.center(obj);
		
		if ($.browser.msie) {
				if(typeof curvyCorners == 'function')  {
					 var settings_curvyCorners = {
						  tl: { radius: 11 },
						  tr: { radius: 11 },
						  bl: { radius: 11 },
						  br: { radius: 11 },
						  antiAlias: true
						}
						curvyCorners(settings_curvyCorners, "#lightbox-"+obj);
				} else {
					$.getScript('/js/curvycorners.js', function() {
						 var settings_curvyCorners = {
						  tl: { radius: 11 },
						  tr: { radius: 11 },
						  bl: { radius: 11 },
						  br: { radius: 11 },
						  antiAlias: true
						}
						curvyCorners(settings_curvyCorners, "#lightbox-"+obj);
					});
				}
		}
				
		// display the lightbox
		$('#lightbox-shadow-'+obj).show();
		$('#lightbox-'+obj).show();
	},
	close : function() {
		// hide lightbox/shadow <div/>'s
		$('.lightbox').hide();
		$('.lightbox-shadow').hide();
		
		// remove contents of lightbox in case a video or other content is actively playing
		//$('#lightbox').empty();
	},
	
	center : function(obj) {
		var element = $("#lightbox-"+obj);
		var imageHeight = $(element).height();
		var imageWidth = $(element).width();
		var windowWidth = $(window).width();
		var windowHeight = $(window).height();
		var boxTop = windowHeight /2 - imageHeight / 2 ;
		if ($.browser.msie) {
			boxTop = boxTop + $(window).scrollTop();
		}
		$(element).css({
			"left" : windowWidth / 2 - imageWidth / 2,
			"top" : boxTop
		});
	}
};
jQuery.fn.extend(
	{
		lbox : jQuery.lbox.build
	}
);
