/**
 * @desc jquery²å¼þ µ¯³ö²ãwin¿ò
 * @date 2010/01/14
 * @author DingFeng.Li (li_dingfeng@hotmail.com)
 */
(function($) {
	$.lightbox_confim = function(htmlId, options) {
		var ie6		= ($.browser.msie && $.browser.version < 7);
		var $body	= $(document.body);
		var $window	= $(window);
		var options = $.extend({}, $.lightbox_confim.defaults, options);

		if( $("#lightbox_confirmbox") ){
			$("#lightbox_confirmbox").remove();
		}
		
		//build the box and fade
		var msgbox = '<div id="lightbox_confirmbox">';
		if( options.usefade ){
			 msgbox += ie6 ? '<div id="lightbox_confimfade" style="width:100%"><iframe frameborder="0"></iframe></div>' : '<div id="lightbox_confimfade" style="width:100%"></div>';
		}
		msgbox +='<div id="lightbox_confimmain" style="display:none"></div>';
		msgbox +='</div>';

		var $cbox	= $(msgbox).appendTo($body);
		var $cmain	= $cbox.children("#lightbox_confimmain");
		var $cfade	= $cbox.children("#lightbox_confimfade");
		$cmain.html($("#"+htmlId).html());
		$cmain.children("div").show();
		
		var positionConfim = function(){
			$cbox.css({
				position: (ie6) ? "absolute" : "fixed",
				height: $window.height(),
				width: "100%",
				top: (ie6)? $window.scrollTop() : 0,
				left: 0,
				right: 0,
				bottom: 0
			});
			$cfade.css({
				position: (ie6) ? "absolute":"fixed",
				height: $window.height(),
				width: "100%",
				background:"#000000",
				opacity:0.4,
				top: 0,
				left: 0,
				right: 0,
				bottom: 0
			});
			if(ie6) {
				$cfade.find("iframe").css({
					width:"100%",
					height:"100%",
					opacity:0
				})
			};
			$cmain.css({
				position: "absolute",
				top: "50%",
				left: "50%",
				marginLeft: (($cmain.outerWidth()/2)*-1),
				marginTop: (($cmain.outerHeight()/2)*-1)
			});
		};
		
		var styleConfim = function(){
			$cfade.css({
				zIndex: options.zIndex,
				display: "none",
				opacity: options.opacity
			});
			$cmain.css({
				zIndex: options.zIndex+1,
				display: "none"
			});
			$cbox.css({
				zIndex: options.zIndex
			});
		};

		var removeConfim = function(callCallback){
			//ie6, remove the scroll event
			if(ie6) {
				$body.unbind('scroll',ie6scroll);
			}
			$cmain.hide(100)
			$window.unbind('resize',positionConfim);
			$cbox.hide(100,function(){
				$cfade.unbind();
				if(callCallback){
					options.callback();
				}
				$cbox.remove();
			});
		};

		//Events
		var ie6scroll = function(){
			$cbox.css({top: $window.scrollTop()});
		};

		var getKeyCode = function(e){
			var key = (window.event) ? event.keyCode : e.which;
			return key;
		};
		
		//ie6, add a scroll event to fix position:fixed
		if(ie6) {
			$window.scroll(ie6scroll);
		}

		$window.resize(positionConfim);
		$cmain.find('#lightbox_confimclose').click(function(){removeConfim(0)});
		$(document).keydown(function(event) {
			if(getKeyCode(event) == 27){removeConfim()};
		});

		//Show it
		positionConfim();
		styleConfim();
		$cfade.show();
		$cmain.show();
	};

	$.lightbox_confim.defaults = {
		callback: function(){},
		opacity: 0.4,
		zIndex: 999,
		usefade:true
	};
	
	$.lightbox_confim.close = function() {
		$('#lightbox_confimbox').fadeOut(100,function(){
			$(this).remove();
		});
	};
})(jQuery);