(function($){
	$.fn.extend({
		b5280Modal: function() {
			var w = $(window);
			var underlay = $("#underlay");
			underlay.css({"opacity": 0.05});
			var modalWindow = null;
			var modalClose = "<div class=\"close\"></div>";
			var modalBottom = "<div class=\"overlay-bottom\"></div>";
			$('.overlay').hide();
			$(this).live("click", function(e){
				$('.overlay').hide();
				modalWindow = ($(this.rel));
				if(modalWindow.find(".product-details select").length > 0) {
					modalWindow.find(".item-options select").get(0).selectedIndex = 0; 
					modalWindow.find(".quantity").val("1");
					modalWindow.find(".item-action").html("<img src=\"images/addToBagOff.gif\" alt=\"Disabled Add to Bag\" class=\"item-no-select\" />");
				}
				//modalWindow.find(".quantity").val("1");
				if(modalWindow.find(".overlay-bottom").length < 1)
					modalWindow.append(modalBottom);
			 	// find the height of the page.
				if (typeof document.body.style.maxHeight === "undefined") { //if IE 6
					$("body","html").css({height: "100%", width: "100%"});
					$("html").css("overflow","hidden");
				}
				underlay.fadeIn(1000);
				e.preventDefault();
				$(document).keydown(handleEscape);	
				var top = (w.scrollTop() < 120) ? w.scrollTop() + (120-w.scrollTop()) : w.scrollTop()+10;
			   	var left = ((($('#product-wrapper').outerWidth()/2 + $('#product-wrapper').offset().left)-modalWindow.outerWidth()/2)+1);
				modalWindow.css({'top': top, 'left':left}).fadeIn(500);
				if(modalWindow.find(".close").length < 1)
					modalWindow.prepend(modalClose);
				$('.overlay .close, .overlay .alt-close, #underlay').click(function(e) {e.preventDefault(); modalHide();});
			});
			
			//Our function for hiding the modalbox
			function modalHide() {
				$(document).unbind("keydown", handleEscape)
				underlay.fadeOut(100);
				modalWindow.fadeOut(300);
			}
			
			//Our function that listens for escape key.
			function handleEscape(e) {
				if (e.keyCode == 27) {
					modalHide();
				}
			}
		}
	});
})(jQuery);
