/**
 * 弹出层
 * @author poly 89490364a@gmail.com
 */
(function($){
	$.fn.popup168=function(o)
	{
		o=o?o:{};
		this.each(initPopup);
		
		function initPopup(){
			var that=$(this);
			if (that.data("initType") != "popup")
			 {
				that.css({
					zoom: 1
				}).bind("click", function(e){
					e.stopPropagation();
					e.preventDefault();
				}).bind("setParam", setParam).data("initType", "popup");
				var layout = that.attr("layout") || "absolute";
				var popwin = $("<div class='popwin'><div class='bger'><span class='close'>x</span><div class='can'></div></div></div>").css({
					position: "absolute",
					display: "none",
					zIndex: 9999
				}).bind("popup", popup).bind("popdown", popdown).bind("click", function(e){
					e.stopPropagation();
				}).bind("ajaxPost", ajaxPost).data("checkParam", function(){
					var paramChanged = that.data("paramChanged");
					that.data("paramChanged", false);
					return paramChanged;
				}).data("getParam", function(){
					return that.data("param") || {};
				}).data("href", function(){
					return that.attr("href")
				}).data("layout", layout).data("relation", that);
				if (layout == "absolute") {
					var close=$(".close",popwin).bind("click",function(){$(this).trigger("popdown");});
					var cover = $("#cover");
					if (!cover.length) {
						cover = $("<div id='cover'><div class='cbger'/><iframe src='/fuckIe6.html' class='fuckIe6'/></div>");
	                    $(".cbger", cover).bind("click", function(e){
	                        if (e.target == this) {
								var popvi=$(".popwin:visible");
								if (popvi.length) {
									popvi.trigger("popdown");
								}
								else
								{cover.hide()}
	                        }
	                    });
					}
					$("body").append(cover);
					cover.append(popwin);
					that.bind("click", function(e){
						cover.show();
						popwin.trigger("popup");
					});
				}
				else {
					that.css({
						overflow: "visible"
					});
					if (that.css("position") == "static") {
						that.css({
							position: "relative"
						})
					}
					eval("popwin.css(" + layout + ")");
					that.append(popwin).hover(function(){
						clearTimeout(this.timer);
						popwin.trigger("popup");
					}, function(){
						this.timer = setTimeout(function(){
							popwin.trigger("popdown")
						}, 100)
					});
				}
			}
		}
		
		function popup(e)
		{
			var that=$(this);
			var href = that.data("href")() + "";
			var f = true;
			if (href.indexOf("uploadItemPic") > 0){
				if (!checkLogin2()){
					return;
				}
			}
			if(that.data("ready") && !that.data("checkParam")())
			{
				$(".popwin:visible").not(that).trigger("popdown");
				if(that.data("layout")=="absolute")
				{
					var left=($(window).width()-that.innerWidth())/2;
					var top=($(window).height()-that.innerHeight())/2//+$(window).scrollTop();
					that.css({left:left+"px",top:top+"px"})//.dequeue();
				}
				that.show()//slideDown();
			}
			else
			{
				var event=new jQuery.Event("ajaxPost")
				event.href=that.data("href")();
				event.param=that.data("getParam")();
				that.trigger(event);
			}
			e.stopPropagation();
		}
		
		function popdown(e)
		{
			var that=$(this);
			that.hide();
			$("#cover").hide();
			e.stopPropagation();
		}
		
		function ajaxPost(e)
		{
			var that=$(this);
			var can=$(".can",that);
			$.ajax({
				dataType:"html",
				url:e.href,
				data:e.param,
				timeout:5000,
				success:function(data){
					can.html(data);
					if(that.data("layout")!="absolute")
					{that.width(that.innerWidth());}
					that.data("ready",true).trigger("popup");
				},
				error:function(XMLHttpRequest,textStatus,errorThrown)
				{
					alert("出错啦!\n错误代码："+(textStatus||errorThrown)+"\n请重试或者检查您的网络连接。");
					that.trigger("popdown")
				}
			});
			e.stopPropagation();
		}
		
		function setParam(e)
		{
			var that=$(this);
			that.data("paramChanged",true);
			that.data("param",e.param||{});
			e.stopPropagation();
		}
		return this;
	}
})(jQuery);

