/*
 * jQuery popbox plugin 1.0
 *
 * Copyright (c) 2009 Hiro Zhang
 *
 * E-mail: hiro.zhd@gmail.com
 *
 * http://www.ihiro.org/
 *
 * Usage:
 *
 * @options: 设置效果的参数，是一个json对象
 *	options内部参数介绍：	@timer 设置动画的运行时间， 非必选参数
 *						@fadeTimer 设置提示盒子的消失时间， 非必选参数
 *						@content 设置你提示的信息内容， 必选参数
 *						@top, @left, @right, @bottom, @top_left, @top_right, @bottom_left, @bottom_right 八个参数为布尔型，设置提示框出现的位置（八个方位）， 非必选参数，默认底部弹出
 *	options参数格式实例： {
 *							'timer': 500,
 *							'fadeTimer': 1000,
 *							'content': '提示信息',
 *							'top_left': 'true'
 *						} 
 *
 * @style: 是附加参数，非必选参数，是追加新的css样式(这里的样式和css文件中的写法一样，只是无需加id、class而已)
 *	style参数格式实例: {
 *						'color': '#000',
 *						'border-color': '#f00'
 *					 }
 *
 * 
 */

;(function() {
	$.extend({
		popbox: function(options, style, aniHeight) {	
			var top, left;
			var timer = 1000, fadeTimer = 500, content;	//默认的速度，消失时间，填充的内容
			var location = {};		//存储出现方位的对象，共八个方位
			var aniParams = '';			//记录动画需要到达的位置参数，json数据
			
			//若自定义属性
			if(options != 'undefined' &&　typeof options == 'object') {
				content = options.content ? options.content: '';
				if(content == '') {
					alert('你没有输入任何的提示信息，content为必选参数！');
					return false;
				};
				timer = options.timer ? options.timer : timer;
				fadeTimer = options.fadeTimer ? options.fadeTimer : fadeTimer;
				location.t_c = options.top ? options.top : false;							//上--中
				location.b_c = options.bottom ? options.bottom : false;						//底--中
				location.l_c = options.left ? options.left : false;							//左--中
				location.r_c = options.right ? options.right : false;						//右--中
				location.t_l = options.top_left ? options.top_left : false;					//上--左
				location.b_l = options.bottom_left ? options.bottom_left : false;			//底--左
				location.t_r = options.top_right ? options.top_right : false;				//上--右
				location.b_r = options.bottom_right ? options.bottom_right : false;			//底--右
			};
			//样式
			style = $.extend({	//默认的样式 结合 用户自定义
				 'position': 'absolute',
				 'z-index': '50',
				 'display': 'none'
			}, style);
			
			//追加盒子到html中
			$('<div id="popbox">' +
			  	'<div class="p_t_l "></div><div class="p_t_c"></div><div class="p_t_r"></div>' +
				'<div class="p_l_c"></div><div class="con">' + content + '</div><div class="p_r_c"></div>' +
				'<div class="p_b_l"></div><div class="p_b_c"></div><div class="p_b_r"></div>' +
			'</div>').css(style).appendTo('body');
			var box = $('#popbox');
			
			//计算浏览器的宽、高，盒子的宽、高
			var pa = new $.PopAction();
			top = pa.getPageHeight() + pa.getPageScroll()[1];
			left = pa.getPageWidth() + pa.getPageScroll()[0];
			boxWidth =  box.outerWidth();
			boxHeight =  box.outerHeight();
			
			//若没传入自定义方位，默认从底部出现
			if(!(location.t_c || location.b_c || location.l_c || location.r_c || location.t_l || location.b_l || location.t_r || location.b_r)) {
				location.b_c = true;
			};
			
			//根据浏览器的大小和盒子的大小计算出现位置和到达的位置
			if(location.t_c) {	//上--中
				aniParams = {'top': (top - boxHeight)/2 + 'px'};
				top = boxHeight*0.1;
				left = (left - boxWidth)/2;
			};
			if(location.b_c) {	//底--中
				aniParams = {'top': (top - boxHeight)/2 + 'px'};
				top = top - boxHeight*(1+0.1);
				left = (left - boxWidth)/2;
			};
			if(location.l_c) {	//左--中
				aniParams = {'left': (left - boxWidth)/2 + 'px'};
				top = (top - boxHeight)/2;
				left = boxWidth*0.05;
			};
			if(location.r_c) {	//右--中
				aniParams = {'left': (left - boxWidth)/2 + 'px'};
				top = (top - boxHeight)/2;
				left = left - boxWidth*(1+0.05);
			};
			if(location.t_l) {	//上--左
				aniParams = {'top': boxHeight*5/4 + 'px'};
				top = boxHeight*0.1;
				left = boxWidth*0.05;
			};
			if(location.b_l) {	//底--左
				aniParams = {'top': (top - boxHeight*2) + 'px'};
				top = top - boxHeight*(1+0.1);
				left = boxWidth*0.05;
			};
			if(location.t_r) {	//上--右
				aniParams = {'top': boxHeight*5/4 + 'px'};
				top = boxHeight*0.1;
				left = left - boxWidth*(1+0.05);
			};
			if(location.b_r) {	//底--右
				aniParams = {'top': (top - boxHeight*2) + 'px'};
				top = top - boxHeight*(1+0.1);
				left = left - boxWidth*(1+0.05);
			};
			
			//根据浏览器界面设置初始位置
			pa.resetPos(box, top, left);
			
			//设置自适应段度、高度
			box.find('.p_t_c, .p_b_c').css('width', boxWidth - 24 + 'px').end()
			   .find('.p_l_c, .p_r_c').css('height', boxHeight - 23 + 'px').end();
			
			//调用fixPNG()方法	
			if($.browser.msie) {
				this.fixPNG($('.p_t_l, .p_t_r, .p_b_l, .p_b_r, .p_t_c, .p_l_c, .p_r_c, .p_b_c', box));
			};
			
			
			if(aniHeight && location.b_c) {
				aniParams = {'top': (box.css('top').replace('px', '') - aniHeight) + 'px'};
			};
			
			//扩展json数据
			aniParams = $.extend({'opacity': '0.95'}, aniParams);
			
			//弹出盒子
			box.show().animate(aniParams, timer, function() {
				//消失提示信息
				setTimeout(function() {
					box.fadeOut(fadeTimer, function() {
						$(this).remove();
					});
				}, fadeTimer);
			});
		},//popbox结束
		
		//修复IE6下png透明问题以及IE下背景加opacity属性变黑的bug
		fixPNG: function(domObj) {
			domObj.each(function() {
				var image = $(this).css('background-image');
				if (image.match(/^url\(["']?(.*\.png)["']?\)$/i)) {
					image = RegExp.$1;
					$(this).css({
						'background-image': 'none',
						'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled="true", sizingMethod="crop", src="' + image + '")'
					});
				};
			});
		}
	});
	
	$.PopAction = function() {
		//设置初始位置
		this.resetPos = function(box, top, left) {
			box.css({
				'top': top,
				'left': left,
				'opacity': 0
			});
		};
	
		//获得滚动条的宽和高
		this.getPageScroll = function() {
			var xScroll, yScroll;
			if (self.pageYOffset) {
			  yScroll = self.pageYOffset;
			  xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 
			  yScroll = document.documentElement.scrollTop;
			  xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {					// all other Explorers
			  yScroll = document.body.scrollTop;
			  xScroll = document.body.scrollLeft;	
			};
			return new Array(xScroll,yScroll);
		};
		
		//获得浏览器的页面宽度
		this.getPageWidth = function() {
			var windowWidth;
			if (self.innerWidth) {	//标准
			  windowWidth = self.innerWidth;
			} else if (document.documentElement && document.documentElement.clientWidth) { 	//IE6
			  windowWidth = document.documentElement.clientWidth;
			} else if (document.body) { //其他
			  windowWidth = document.body.clientWidth;
			};
			return windowWidth;
		};
		
		//获得浏览器的页面高度
		this.getPageHeight = function() {
			var windowHeight;
			if (self.innerHeight) {	
			  windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) {
			  windowHeight = document.documentElement.clientHeight;
			} else if (document.body) {
			  windowHeight = document.body.clientHeight;
			};
			return windowHeight;
		};
	};
	
})(jQuery);
