/* Smallbox implementation.
 * Author: Randy K. Little
 * sevnsevn@gmail.com
 * developed originally for Billy Reid Studios www.billyreid.com
 * under Massive Media Works www.massivemediaworks.com
 *
 * Use under terms of GPL (GNU General Public License)
 * Distributed with this plugin.
 */

$(document).ready(function(){
	$(".opensb").click(function(){
		var _ref = $(".smallbox[title="+$(this).attr("title")+"]");
		var _enc = _ref.parent(".encapsulator");
		var _ovl = $(".overlay",_enc);
		$(_ovl).css("filter","alpha(opacity=25)");
		 $(_enc).show(); $(_ovl).fadeIn(500, function(){setTimeout(function(){$(_ref).fadeIn(600);},400);});
	});
	$(".smallbox").each(function(){
		var _lb = $(this);
		$(_lb).wrap("<div class=\"encapsulator\"></div>");
		var _ref = $(_lb).parent(".encapsulator");
		$(_ref).prepend("<div class=\"overlay\">&nbsp;</div>");
		var _lbheight = $("#sbheight",_lb).attr("value");
		var _lbwidth = $("#sbwidth",_lb).attr("value");
		$(_lb).css({
			"width":_lbwidth  + "px",
			"height":_lbheight  + "px",
			"left":( $(document).width() / 2 ) - ( _lbwidth / 2 ),
			"top":( $(document).height() / 2 ) - ( _lbheight / 2 )
		});
		resize(_ref);
		$(window).resize(function(){
			$(_lb).css({
				"left":( $(document).width() / 2 ) - ( _lbwidth / 2 ),
				"top":( $(document).height() / 2 ) - ( _lbheight / 2 )
			});
		});
		$(_lb).click(function(){
			$(this).fadeOut(600,
				function(){
					setTimeout(function(){
						var _enc = $(_lb).parent(".encapsulator");
						$(".overlay", _enc).fadeOut(500,function(){$(_enc).hide();});
					},400);
				}
			);
		});
	});
	$(window).resize(function(){
		$(".encapsulator").each(function(){resize(this);});
	});
	function resize(ref)
	{
		if ($(window).width() < $(document).width() || $(window).height() < $(document).height() )
		{
			$(ref).css({"width": $(window).width(),"height": $(window).height()});
		}else{
			$(ref).css({"width": $(document).width(),"height": $(document).height()});
		}
	}
});
