/*
+---------------------------------------------------------------------------
| 满世界都是弹窗
+---------------------------------------------------------------------------
*/
$.extend({
	// 弹窗
	alert: function(str,v,t) {
		if (typeof(str) == 'object' && typeof(v) != 'object') {
			v = str;
		} else {
			if (typeof(v) != 'object') {
				if (v > 1000) {
					v = {time: v};
				} else if (v == 1) {
					v = {btn: 1};
				} else {
					v = {};
				}
			}
			if (!v.str) {
				v.str = str;
			}
		}
		t = {
			a: function() {
				v = $.extend({
					resize: 1,
					close: 1,
					title: '',
					time: 0,
					css: {},
					zIndex: 999,
					xy: [-1, -1],
					wh: [450, 120],
					type: 'default'
				}, v);
				if (v.tv) {
					v.ratio = v.tv == 1 ? 9/16 : v.tv;
				}
				v.isloading = (typeof(v.str) == 'string' && v.str && v.str.indexOf('loading') == 0);
				v.into = 0;
				t.m();
				$(window).resize(function() {
					t.s()
				});
				t.s();
				t.s();
				if (!$.mobile() && !v.max) {
					t.r();
				}
				t.div.attr('jextalert', t.bd.width()+'-'+t.bd.height()+'-'+parseInt(t.bd.offset().left)+'-'+parseInt(t.bd.offset().top));
				// t.go_to_reset_time = setInterval(t.go_to_reset, 300);
				return this;
			},
			m: function() {
				t.div = $('<div>' + jextalert + '</div>');
				t.bd = t.div.find('#jextalert');
				t.bg = t.div.find('#jextalert-mask');
				t.move = t.bd.children('[move][move1]');
				t.title = t.bd.children('[top]');
				t.close = t.title.children('[close]');
				t.contents = t.bd.children('[contents]');
				t.my = t.bd.find('[contents] [contentschild]');
				t.btn = t.bottom = t.bd.children('[bottom]');
				t.not = t.cancel = t.btn.find('[not]');
				t.yes = t.confirm = t.btn.find('[yes]');
				t.tip = t.btn.find('[tip]');
				t.not.html(jLang.cancel ? jLang.cancel : '取消');
				t.yes.html(jLang.ok ? jLang.ok : '确定');
				t.contents.children().html(v.str).find('[exec]').removeAttr('exec').end().find('[zzmap][absolute][max]').remove();
				t.bd.css({zIndex: v.zIndex});
				t.bg.css({zIndex: v.zIndex});
				t.div.find('[yes]').click(function() {
					if ($(this).hasClass('not')) {
						return;
					}
					var r = 1;
					if (typeof(v.btn) == 'function') {
						r = v.btn(t);
					}
					if (typeof(v.yes) == 'function') {
						v.yes(t);
					}
					r = parseInt(r);
					t.closes(r);
					if (r != 2) {
						t.remove();
					}
				});
				t.div.find('[not]').click(function() {
					if (typeof(v.not) == 'function') {
						v.not(t);
					}
					t.remove();
				});
				t.title.children().eq(0).html(v.title);
				if (!v.title || v.tv || v.time > 0 || v.isloading) {
					t.title.height(0).css('padding', 0).css({border: 0});
					t.close.css({top: -14});
				}
				if (typeof(v.btn) != 'function') {
					t.not.addClass('hide');
				}
				if (v.iframe) {
					t.iframe = $('<iframe></iframe>').attr({
						src: v.iframe,
						name: 'thisiframe',
						width: '100%',
						height: '100%',
						scroll: 'no',
						frameborder: 0,
						allowfullscreen: 'allowfullscreen'
					});
					t.contents.html(t.iframe);
				}
				if (v.max) {
					// 非自定义
					if (v.type != 'null') {
						v.type = 'title';
					}
					t.bg.css({background: 'none'});
					t.bd.css({borderRadius: 0});
				}
				t.div.attr('menu-null', '').find('[close]').click(function() {
					t.remove()
				});
				t.bg.on({
					contextmenu: function() {
						return false
					},
					mousedown: function(e) {
						e.stopPropagation()
					}
				});
				if (v.type == 'default') {
					t.btn.css({border: 0});
					t.title.css({border: 0});
					if (!v.title) {
						t.close.hide();
					}
				} else if (v.type == 'title') {
					v.btn=0;
				}
				if (v.isloading) {
					t.bg.css({background: 'rgba(225,225,225,.4)'}).off('click');
					t.bd.addClass('null');
					t.contents.html('<div loadings text-center><span>loading...</span></div>');
					if (v.event) {
						t.bg.css({background: 'rgba(225,225,225,0)'});
						t.contents.find('[loadings]').hide();
						// t.contents.find('[loadings] span').css({background:'none',color:'#999'});
					}
				}
				if (v.time > 0) {
					t.bg.hide();
					t.bd.addClass('null');
					t.contents.html('<div loadings text-center><span w>' + v.str + '</span></div>');
					v.xy[1] = $(window).height() * .24;
					setTimeout(function() {
						t.remove()
					}, v.time);
				}
				if (v.tv) {
					t.bd.css({background:'none',overflow:'visible',padding:0});
				}
				if (!(v.iframe || v.isloading || v.time>0)) {
					t.contents.attr('mcscroll', '');
				}
				t.bd.addClass(v.type);
				$('body').append(t.div);
				if (typeof(v.init) == 'function') {
					v.init(t);
				}
				if (!v.btn) {
					t.btn.css({height: 0, padding: 0, margin: 0}).hide();
				}
			},
			r: function() {
				t.contentsbg = t.contentsbg || $('<div absolute zzmap max></div>').css({zIndex: 999});
				$(t.div.find('[move]')).move(t.bd,{
					down: function(x,y) {
						t.contents.append(t.contentsbg);
					},
					up: function(x,y) {
						t.contentsbg.remove();
					}
				});
			},
			c: function(fn) {
				// clearInterval(t.go_to_reset_time);
				setTimeout(function() {
					t.bg.removeClass('janimated').fadeOut(300, function() {
						if (!v.max) t.bg.remove();
					});
					t.bd.removeClass('janimated').fadeOut(300, function() {
						if (!v.max) {
							if (typeof(v.end) == 'function') {
								v.end(t);
							}
							t.div.remove();
						}
						if (typeof(fn) == 'function') {
							fn();
						}
					});
				}, v.isloading ? 600 : 0);
			},
			/*go_to_reset: function() {
				var init=set=0;
				if (t.str && t.str!=v.str) {
					init=1;
					set=1;
					v.str=t.str;
					t.contents.children().html(t.str).find('[exec]').removeAttr('exec').end().find('[zzmap][absolute][max]').remove();
				}
				if (init && typeof(v.init)=='function') v.init(t);
				if (set) t.s();
			},*/
			str: function(str) {
				if (str && str != v.str) {
					v.str = str;
					t.contents.children().html(str).
					find('[exec]').removeAttr('exec').end().
					find('[zzmap][absolute][max]').remove();
					if (typeof(v.init) == 'function') {
						v.init(t);
					}
				}
			},
			remove: function(fn) {
				if (t.bd.attr('close') == '0') {
					return;
				}
				if (v.max) {
					history.back();
				} else {
					t.c(fn);
				}
			},
			show: function(fn) {
				t.bg.show().addClass('janimated');
				t.bd.show().addClass('janimated');
			},
			closes: function(s) {
				t.bd.attr('close', s);
				setTimeout(function() {
					t.bd.attr('close', v.close);
				}, 30);
				return this;
			},
			s: function() {
				ww = $(window).width();
				w0 = ww * (v.tv ? .96 : .9);
				w1 = t.bd.find('[contentschild]').outerWidth();
				w2 = parseInt(t.bd.css('padding-left') || 0) + parseInt(t.bd.css('padding-right') || 0);
				w3 = v.wh[0]-w2;
				if (w3 <= 0) {
					w3 = w1;
				}
				if (w3 > w0-w2) {
					w3 = w0-w2;
				}
				if (v.ratio) {
					v.wh[1] = v.ratio * w3;
				}
				hh = $(window).height();
				h4 = t.btn.outerHeight() + t.title.outerHeight();
				h0 = hh * .9;
				h1 = t.bd.find('[contentschild]').outerHeight() + h4 + 3;
				h2 = parseInt(t.bd.css('padding-top') || 0) + parseInt(t.bd.css('padding-bottom') || 0);
				h3 = v.wh[1] || 120;
				if (h3 <= 120 && h1 > 120) {
					h3 = h1;
				}
				if (h3 > h0-h2) {
					h3 = h0-h2;
				}
				if (v.max) {
					h3 = hh-h2;
					w3 = ww-w2;
				}

				// 识别鼠标位置，加强体验
				/*if (v.event && v.xy[0]==-1 && v.xy[1]==-1) {
					v.xy[0]=(v.event.pageX?v.event.pageX:v.event.originalEvent.changedTouches[0].pageX);
					v.xy[1]=(v.event.pageY?v.event.pageY:v.event.originalEvent.changedTouches[0].pageY);
					var pyl,pxl;
					if (v.xy[0]<ww*.3) pxl=.1;
					else if (v.xy[0]>ww*.3&&v.xy[0]<ww*.4) pxl=.3;
					else if (v.xy[0]>ww*.4&&v.xy[0]<ww*.7) pxl=.5;
					else if (v.xy[0]>ww*.7&&v.xy[0]<ww*.8) pxl=.6;
					else pxl=.7;
					if (v.xy[1]<hh*.33) pyl=.1;
					else if (v.xy[1]>hh*.3&&v.xy[1]<hh*.4) pyl=.3;
					else if (v.xy[1]>hh*.4&&v.xy[1]<hh*.7) pyl=.57;
					else if (v.xy[1]>hh*.7&&v.xy[1]<hh*.8) pyl=.66;
					else pyl=.82;
					v.xy[0]-=w3*pxl;
					v.xy[1]-=h3*pyl;
				}*/

				if (v.into <= 2) {
					var left = v.xy[0] != -1 ? v.xy[0] : (ww-w3-w2) / 2;
					var top = v.xy[1] != -1 ? v.xy[1] : (hh-h3-h2) / (h3 < 400 ? 3 : 2);
					var jianxi = v.event ? 30 : 0;
					left = left < jianxi ? jianxi : (left>ww-w3 ? ww-w3 : left);
					top = top < jianxi ? jianxi : (top > hh-h3 ? hh-h3 : top);
					t.bd.css({left:left,top:top});
				}

				t.contents.css({height: h3 - h4});
				t.bd.css({width: w3, height: h3});
				v.into++;
				return this;
			}
		}
		return t.a();
	},
	// hash 改变
	hash: function(fn, hash) {
		if (typeof(window.hash_ary) != 'object') {
			window.hash_ary = {};
		}
		window.hash_ary[hash] = window.hash_ary[hash] || '';
		location.hash = hash;
		setTimeout(function() {
			if (("onhashchange" in window) && ((typeof document.documentMode === "undefined") || document.documentMode == 8)) {
			    window.onhashchange = fn;
			} else {
				clearInterval(window.hash_ary[hash]);
				window.hash_ary[hash] = setInterval(function() {
					if (hash != location.hash) fn();
				}, 150);
			}
		}, 60);
	},
	// 满屏弹窗
	max: function(str, v, lo, hash, a, b, c) {
		if (typeof(str) == 'object') {
			v = str;
		} else {
			if (typeof(v) != 'object') v = {};
			if (!v.str) v.str = str;
		}
		window.maxalert = typeof(window.maxalert) == 'object' ? window.maxalert : {};
		v = $.extend({
			max:1
		}, v);
		hash = 'max' + md5(v.str);
		if (typeof(window.maxalert[hash]) != 'object') {
			window.maxalert[hash] = $.alert(v);
		} else {
			window.maxalert[hash].show();
		}
		$.hash(function(h, m) {
			h = location.hash;
			h = h.substring(1, h.length);
			m = window.maxalert;
			for (var i in m) {
				if (h == i) {
					m[i].show();
				} else {
					m[i].c();
				}
			}
		}, hash);
		return window.maxalert[hash];
	}
});

var jextalert =
	"<div id='jextalert-mask' class='alertop janimated' fixed max close mine-scroll></div>" +
	"<div id='jextalert' class='alertbn janimated'>" +
	    "<div move move1 absolute max></div>" +
	    "<div top move clean relative>" +
	        "<font>标题</font><div close absolute>×</div>" +
	    "</div>" +
	    "<div contents contentsxxxx relative over mine-scroll><div contentschild clean></div></div>" +
	    "<div bottom over clean>" +
	    	"<div fl tip></div>" +
	        "<div fr aaaa yes>已完成</div>" +
	        "<div fr bbbb not>取消</div>" +
	    "</div>" +
	"</div>"
;