/**
 * @desc 兼容不同的浏览器居中scrollCenter
 * @author Zhang Huihua
 */
(function($){
	$.fn.extend({

		getWindowSize: function(){
			if ($.browser.opera) { return { width: window.innerWidth, height: window.innerHeight }; }
			return { width: $(window).width(), height: $(window).height() };
		},
		/**
		 * @param options
		 */		
		scrollCenter: function(options){
			// 扩展参数
			var op = $.extend({ z: 1000000, mode:"WH", top: -1, left:-1, scroll:true}, options);
			
			// 追加到 document.body 并设置其样式
			var windowSize = this.getWindowSize();
			var iTop = op.top >= 0 ? op.top : (windowSize.height - this.height()) / 2;
			var iLeft = op.left >= 0 ? op.left : (windowSize.width - this.width()) / 2;

			if (op.mode == "W") {
				this.appendTo(document.body).css({
					'left': iLeft + $(window).scrollLeft() + 'px'
				});
			} else if (op.model == "H"){
				this.appendTo(document.body).css({
					'top': iTop + $(window).scrollTop() + 'px'
				});	
			} else {
				this.appendTo(document.body).css({
				//	'position': 'absolute',
				//	'z-index': op.z,
					'top': iTop + $(window).scrollTop() + 'px',
					'left': iLeft + $(window).scrollLeft() + 'px'
				});
			}

			
			// 保存当前位置参数
			var bodyScrollTop = $(document).scrollTop();
			var bodyScrollLeft = $(document).scrollLeft();
			var movedivTop = this.offset().top;
			var movedivLeft = this.offset().left;
			
			var thisjQuery = this;
			
			// 滚动事件
			if (op.scroll){
				$(window).scroll(function(e){
					var windowSize = thisjQuery.getWindowSize();
					var tmpBodyScrollTop = $(document).scrollTop();
					var tmpBodyScrollLeft = $(document).scrollLeft();
					
					movedivTop += tmpBodyScrollTop - bodyScrollTop;
					movedivLeft += tmpBodyScrollLeft - bodyScrollLeft;
					bodyScrollTop = tmpBodyScrollTop;
					bodyScrollLeft = tmpBodyScrollLeft;
	
					// 以动画方式进行移动
					if (op.mode == "W") {
						thisjQuery.stop().animate({
							'left': movedivLeft + 'px'
						});
					} else if (op.mode == "H") {
						thisjQuery.stop().animate({
							'top': movedivTop + 'px'
						});
					} else {
						thisjQuery.stop().animate({
							'top': movedivTop + 'px',
							'left': movedivLeft + 'px'
						});
					}
				});
							
				// 窗口大小重设事件
				$(window).resize(function(){
					var windowSize = thisjQuery.getWindowSize();
					var iTop = op.top >= 0 ? op.top : (windowSize.height - thisjQuery.height()) / 2;
					var iLeft = op.left >= 0 ? op.left : (windowSize.width - thisjQuery.width()) / 2;
					movedivTop = iTop + $(document).scrollTop();
					movedivLeft = iLeft + $(document).scrollLeft();
					
					if (op.mode == "W") {
						thisjQuery.stop().animate({
							'left': movedivLeft + 'px'
						});
					} else if (op.mode == "H") {
						thisjQuery.stop().animate({
							'top': movedivTop + 'px'
						});
					} else {
						thisjQuery.stop().animate({
							'top': movedivTop + 'px',
							'left': movedivLeft + 'px'
						});
					}
					
				});
			}

			
			return this;
		}
	});
})(jQuery);