// JavaScript Document

function json_decode(s) {
	if(s == '') {
		return '';
	} else {
		try {
			// 去掉广告代码。这行代码挺无语的，为了照顾国内很多人浏览器中广告病毒的事实。
			s = s.replace(/\}\s*<script[^>]*>[\s\S]*?<\/script>\s*$/ig, '}');
			var json = $.parseJSON(s);
			return json;
		} catch(e) {
			//alert('JSON格式错误：' + s);
			//window.json_error_string = s;	// 记录到全局
			return s + '';
		}
	}
}

function json_error(json) {
	if(typeof json === 'string') {
		if(json == '') {
			return '服务端数据为空，可能PHP碰到什么错误，您可以尝试通过后台“PHP错误日志”了解详情。';
		} else {
			return '服务端返回的 JSON 数据有误:' + json;
		}
	} else if(typeof json != 'object') {
		return json;
	}
	if(json.servererror) {return json.servererror;}
	return '';
}
// 添加参数到 url，去除重复
/*
	兼容以下格式：
	?a-1-b-2.htm
	a-1-b-2.htm
	/?a-1-b-2.htm
	/a-1-b-2.htm
	/?a=1&b=2
	?a=1&b=2
*/
function url_add_arg(url, k, v) {
	if(url.indexOf('/?') != -1) {
		var sep = url.indexOf('/?');
		var prefix = url.substr(0, sep);
		var suffix = url.substr(sep, url.length);
	} else if(url.indexOf('?') != -1) {
		var sep = url.indexOf('?');
		var prefix = url.substr(0, sep);
		var suffix = url.substr(sep, url.length);
	} else {
		var prefix = '';
		var suffix = url;
	}
	if(suffix.substr(suffix.length - 4, 4) == '.htm') {
		suffix = suffix.substr(0, suffix.length - 4) + '-'+k+'-'+v+'.htm';
	} else {
		suffix += (suffix.indexOf('?') == -1 ? '?' : '&') + k + '=' + v;
	}
	return prefix + suffix;
}
function tips(msg,id){
	layer.tips(msg, id, {
					time: 2,
					style: ['background-color:#0FA6D8; color:#fff', '#0FA6D8'],
					maxWidth:150
	});
	$(id).focus();
	
}
function ajaxdialog_click(e){
	var e = e ? e : window.event;// 兼容 event
	var url = $(e).attr('href');
	var options = $(e).attr('ajaxdialog');//获取并判断外部设置参数并转换为对象
	if(options != null && options.length > 0) {
		eval("var options = " + options);//alert(typeof(options));
		options.xcaller = this;
		if(options.type=="confirm"){
			$.layer({
			  shade: [0.5,'#000'],
			  area: ['auto','auto'],
			  dialog: {
				  msg: options.msg,
				  btns: 2,                    
				  type: 4,
				 
				  yes: function(){
					  $.get(url,{ajax: 1}, function(s){
						  var json = json_decode(s);
						  if(json.status==1){
						      layer.msg(json.message, 1,1);
							  if(json.goto!=null && json.goto!=''){
								  window.location.href=json.goto;
							  }else{
							      location.reload();
							  }
						  }else{
							  layer.msg(json.message, 1,3);
						  }
					  });
				  }
			  }
           });
			
			return false;
		}
		if(options.type=="iframe"){
			$.layer({
				type: 2,
				title: options.msg,
				iframe: {src : url},
				area: [options.width,options.height]
			});
			return false;
		}
		if(options.type=="pos"){
			$.get(url, {ajax: 1}, function(s) {
				var json = json_decode(s);
				if((error = json_error(json)) || json.status <= 0) {
					error = error.replace(/\n/ig, '<br />');
					var errstr = error ? '<span class="clear">' + error.replace(/\n/, '<br />') + '</span>' : (json.status <= 0 ? '<div class="error">' + json.message + '</div>' : '');
					var body = errstr + (json && json.status <= 0 ? '' : '<br /><br /><b>URL:<'+'/b> <a href="' + url + '" target="_blank">' + url + '<'+'/a>');
					layer.msg(body,2,{type:3});
					return false;
				}
				
				// 如果为普通信息提示，则没有 body
				if(json.status == 1 && json.message && !json.message.body) {
					json.message = {title: '提示信息', body: json.message};
				}
				var op=json.message;
				var offset = $(e).offset();
				if($(window).width() - offset.left < op.width) {
					var v_left = offset.left - op.width + 4;
				    var v_top = offset.top-$(window).scrollTop();
						
				}else{
				     var v_left = parseInt(offset.left) + 4;
				     var v_top = offset.top-$(window).scrollTop();
				}
				$.layer({
					type: 1,
					shadeClose: true,
					fix: false,
					title:op.title,
					offset: [v_top+'px',v_left+'px'],
					shade: [0],
					area: [op.width, 'auto'],
					page: {html : op.body}
					//yes: function(index){delay_execute(index,recall)}
				});
				
		     })
	     return false;
			
		}
	}
	ajaxdialog_request(url, 0, options);
}

// 检查 cache，如果存在，则先从CACHE中取
function ajaxdialog_request(url, recall, options) {
	
	$.get(url, {ajax: 1}, function(s) {
			var json = json_decode(s);
			if((error = json_error(json)) || json.status <= 0) {
				error = error.replace(/\n/ig, '<br />');
				var errstr = error ? '<span class="clear">' + error.replace(/\n/, '<br />') + '</span>' : (json.status <= 0 ? '<div class="error">' + json.message + '</div>' : '');
				var body = errstr + (json && json.status <= 0 ? '' : '<br /><br /><b>URL:<'+'/b> <a href="' + url + '" target="_blank">' + url + '<'+'/a>');
				//alert(body);
				layer.msg(body,2,{type:3});
				return false;
			}
			
			// 如果为普通信息提示，则没有 body
			if(json.status == 1 && json.message && !json.message.body) {
				json.message = {title: '提示信息', body: json.message};
			}
			var op=json.message;
			$.layer({
				type: 1,
				title:op.title,
				shade: [0.5,'#000'],
				area: [op.width, 'auto'],
				page: {html : op.body},
				yes: function(index){delay_execute(index,recall)}
            });
	})
}
function check_login(){
	if(g_uid==null || g_uid==0){
		ajaxdialog_request(app_url+'?member-login-ajax-1.htm', 0);
        return false;
	}
	return true;	
}