var is_app = 1;
var imgUrl = 'http://img.zgqlg.com.cn/';
// var webUrl = 'http://www.zgqlg.com.cn/';
// var webUrl1 = 'http://www.zgqlg.com.cn/';
// var imgUrl = 'http://qlg.tsgz.moe:233/';
var webUrl = 'http://qlg.tsgz.moe:233/';
var webUrl1 = 'http://qlg.tsgz.moe:233/';
var cssUrl = localStorage.getItem("cssUrl");
var jsUrl = localStorage.getItem("jsUrl");
var version = localStorage.getItem("version");
function qlgUrl(url) {
	return webUrl + url;
}

function hyhUrl(url) {
	return webUrl + url;
}

function llUrl(url) {
	return webUrl + url;
}

function hgUrl(url) {
	return webUrl + url;
}

function kxUrl(url) {
	return webUrl + url;
}

function hyhImgUrl(url) {
	return imgUrl + url;
}

function ectUrl(url) {
	return webUrl + url;
}

function ectImgUrl(url) {
	return imgUrl + url;
}

function cssUrl(url) {
	return '../css/' + url;
}

function formatDate(date) {
	var d = new Date(date),
	
		month = '' + (d.getMonth() + 1),
		day = '' + d.getDate(),
		year = d.getFullYear(),
		hour = d.getHours(),
		minute =d.getMinutes(),
		second = d.getSeconds();
		console.log(hour );
	if (month.length < 2) month = '0' + month;
	if (day.length < 2) day = '0' + day;
	if (hour< 10) hour = '0' + hour;
	if (minute< 10) minute = '0' + minute;
	if (second < 10) second = '0' + second;
	// return [year, month, day].join('-')+" "+[hour,minute,second].join(':');
	return year+"年"+month +"月"+day+"日";
}

function backTop() {
	mui('.mui-scroll-wrapper').scroll().scrollTo(0, 0, 0);
}
function bMapTransQQMap(lng, lat) {
	var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
	var x = lng - 0.0065;
	var y = lat - 0.006;
	var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
	var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
	var lngs = z * Math.cos(theta);
	var lats = z * Math.sin(theta);

	return {
		lng: lngs,
		lat: lats
	}
}
function toJson(str, notLimit) {
	var json = {};
	if (str) {
		try {
			if (typeof(str) == "object") {
				json = str;
			} else {
				json = eval("(" + str + ")");
			}
			if(json.status && (json.status == '-555' || json.status == '-666')){
				//topingzheng 
				var type = 0;
				if(json.status == '-666') type = 1;
				JZL.openWindow("paymentVoucher.html",json.data.shopId,{'type':type});
			}
			if (!notLimit) {
				if (json.status && json.status == '-999') {
					//console.log(str.msg)
					inLogin();

				}
			}
		} catch (e) {
			mui.alert("系统发生错误:" + e.getMessage);
			json = {};
		}
		return json;
	} else {
		return;
	}
}
var JZL = JZL || {};
JZL.js_init = function(cdn) {
	var typeName = 'script';
	var type = 'text/javascript';
	var element, i = 0,
		file, parent = document.body;

	for (i; i < cdn.length; i++) {
		file = cdn[i];
		element = document.createElement(typeName);
		element.type = type;
		element.src = file;
		parent.appendChild(element);
		//free file's value
		file = null;
	}
	/*
	 * Cleanup at end of function, which isn't necessary unless lots of memory is being         
	 * used up.
	 * No point nulling a Number, however. In some ECMAScript implementations
	 * (ActionScript 3), this throws a warning.
	 */
	//empty array
	cdn.splice(0, cdn.length);
	//clear variable values
	element = null;
	parent = null;
	cdn = null;
}
JZL.css_init = function(cdn) {
	var typeName = 'style';
	var type = 'text/css';
	var rel = 'stylesheet';
	var element, i = 0,
		file, head, parent = document.head || document.getElementsByTagName('head')[0];
	for (i; i < cdn.length; i++) {
		file = cdn[i];
		element = document.createElement('link');
		element.rel = rel;
		element.type = type;
		element.href = file;
		parent.appendChild(element);
		//free file's value
		file = null;
	}
	cdn.splice(0, cdn.length);
	element = null;
	parent = null;
	cdn = null;
}
JZL.getToken = function() {
	return localStorage.getItem('token');
}
JZL.getCssUrl = function(cssName) {
	return cssUrl + cssName + '?ver=' + version
}
JZL.getJsUrl = function(jsName) {
	return jsUrl + jsName + '?ver=' + version
}
JZL.openWindow = function(openUrl, openId, extras, targetUrl) {
	if (1 == is_app) {
		if (typeof(extras) == "undefined") extras = {};
		mui.openWindow({
			url: openUrl,
			id: openId,
			styles: {
				top: '0px', //新页面顶部位置
				bottom: '0px', //新页面底部位置
				width: '100%', //新页面宽度，默认为100%
				height: '100%' //新页面高度，默认为100%
			},
			extras: extras,
			createNew: false, //是否重复创建同样id的webview，默认为false:不重复创建，直接显示
			show: {},
			waiting: {
				autoShow: true, //自动显示等待框，默认为true
				title: '正在加载...', //等待对话框上显示的提示内容
				options: {
					//						width: waiting - dialog - widht, //等待框背景区域宽度，默认根据内容自动计算合适宽度
					//						height: waiting - dialog - height, //等待框背景区域高度，默认根据内容自动计算合适高度
					//						......
				}
			}
		})
	} else {
		location.href = targetUrl;
	}

}

JZL.closeWindow = function(closeId) {
	if (1 == is_app) {
		plus.webview.getWebviewById(closeId).close();
	}
}

JZL.ajax = function(url, data, fnDeal, sendType, sendToken) {
	if (typeof(sendType) == "undefined") sendType = 'POST';
	if (typeof(sendToken) == "undefined") sendToken = 1;
	if (1 == sendToken)
		headers = {
			"HYH-Token": JZL.getToken()
		};
	else
		headers = {};
	//	//console.log(JSON.stringify(headers))
	if (1 == is_app) {
		
		mui.ajax(url, {
			headers: headers,
			data: data,
			dataType: 'json', //服务器返回json格式数据  
			type: sendType, //HTTP请求类型  
			timeout: 10000, //超时时间设置为10秒；  
			success: function(result) { //服务器返回响应，根据响应结果，分析是否登录成功；  
				
				if (typeof(fnDeal) != "undefined") {
					fnDeal(result);
				}
			},
			error: function(xhr, type, errorThrown) { //异常处理；  
				//alert(xhr+type+errorThrown);  
				console.log(JSON.stringify(data));
				console.log(url);
				//mui.alert(xhr);
			}
		});
	} else {
		
		$.ajax(url, {
			headers: headers,
			data: data,
			dataType: 'jsonp', //服务器返回json格式数据  
			type: sendType, //HTTP请求类型  
			timeout: 10000, //超时时间设置为10秒；  
			success: function(result) { //服务器返回响应，根据响应结果，分析是否登录成功；  
				if (typeof(fnDeal) != "undefined") {
					fnDeal(result);
				}
			},
			error: function(xhr, type, errorThrown) { //异常处理；  
				//alert(xhr+type+errorThrown);      
				alert(type);
			}
		});
	}

}

function inLogin() {
	JZL.openWindow('login.html', 'login.html');
}

var scroll = mui('.mui-scroll-wrapper').scroll();

var ua = navigator.userAgent.toLowerCase();
if (/iphone|ipad|ipod/.test(ua)) { //苹果手机            
	if (document.getElementsByClassName('zhe')[0]) {
		document.getElementsByClassName('zhe')[0].style.display = 'none';
	}
	//	document.getElementsByClassName('zhe')[0].style.display='none';
} else if (/android/.test(ua)) {
	//	document.getElementsByClassName('zhe')[0].style.display='block';
	if (document.getElementsByClassName('zhe')[0]) {
		document.getElementsByClassName('zhe')[0].style.display = 'none';
	}
}

var hSize = 64 + (+localStorage.getItem('ipxSizeTop')) + 'px';
var hPadding = 22 + (+localStorage.getItem('ipxSizeTop')) + 'px';
$('.header').css('height', hSize);
$('.header').css('padding-top', hPadding);
$('.scroll_out_tb').css('top', hSize);
$('.scroll_out_t').css('top', hSize);
mui.plusReady(function() {
	plus.navigator.setStatusBarStyle('dark');
})

mui('.nav').on('tap', '.nav_block', function() {
	$(this).addClass('on').siblings().removeClass('on');
})

//打开广告
function openAds(item) {
	if (item.attr('data-adURL') == '' || item.attr('data-targetType') == 0) {
		return;
	}
	var adURL = item.attr('data-adURL');
	var targetType = '';
	var data_set = {};
	if (item.attr('data-targetType') == 1) {
		//商品
		targetType = 'details.html';
		data_set = {
			data_id: adURL
		}
	} else if (item.attr('data-targetType') == 2) {
		//商家
		targetType = 'storeout.html';
		if (adURL == 1) {
			//			targetType = 'self_shop.html'
		}
		data_set = {
			shopId: adURL
		}
	} else if (item.attr('data-targetType') == 3) {
		//活动
		targetType = adURL;
		data_set = {
			adURLId: adURL.split('?')[1]
		}
	}
	////console.log(item.attr('data-adURL'));
	JZL.openWindow(targetType, targetType + adURL, data_set);
}

//返回顶部
//var scrollToTopBox = document.getElementById('scrollToTop'); //返回按钮tap  
//scrollToTopBox.addEventListener('tap', function(e) {
//	e.stopPropagation();
//	mui('.mui-scroll-wrapper').scroll().scrollTo(0, 0, 100); //滚动到顶部  
//});
// Android上监听原生滚动，iOS上监听div滚动，上拉超过一屏后显示按钮，否则隐藏，可自行在条件判断中修改  
//if(mui.os.android) {
//	window.addEventListener('scroll', function(e) {
//		if(window.pageYOffset >= window.innerHeight && scrollToTopBox.classList.contains('hide')) {
//			scrollToTopBox.classList.remove('hide');
//		} else if(window.pageYOffset < window.innerHeight && !scrollToTopBox.classList.contains('hide')) {
//			scrollToTopBox.classList.add('hide');
//		}
//	});
//} else {
//	document.getElementsByClassName('mui-scroll-wrapper')[0].addEventListener('scroll', function() {
//		if(mui('.mui-scroll-wrapper').scroll().y <= window.innerHeight * (-1) && scrollToTopBox.classList.contains('hide')) {
//			scrollToTopBox.classList.remove('hide');
//		} else if(mui('.mui-scroll-wrapper').scroll().y > window.innerHeight * (-1) && !scrollToTopBox.classList.contains('hide')) {
//			scrollToTopBox.classList.add('hide');
//		}
//	});
//}
JZL.saveItems = function(className, itemName) {
	var params = JZL.getParams(className);
	localStorage.setItem(itemName, JSON.stringify(params));
}
JZL.getItems = function(itemName) {
	var itemNames = localStorage.getItem(itemName);
	//if(null != itemNames) 
	JZL.setValues(JSON.parse(itemNames));
   
}
JZL.getParams = function(obj) {
	var params = {};
	var chk = {};
	var s;
	$(obj).each(function() {
		// //console.log($(this)[0]);
		if ($(this)[0].type == 'hidden' || $(this)[0].type == 'number' || $(this)[0].type == 'tel' || $(this)[0].type ==
			'password' || $(this)[0].type == 'select-one' || $(this)[0].type == 'textarea' || $(this)[0].type == 'text') {
			params[$(this).attr('id')] = $.trim($(this).val());
		} else if ($(this).is('img')) {
			// //console.log(1)
			params[$(this).attr('id')] = $.trim($(this).attr('data-src'));
		} else if ($(this)[0].type == 'radio') {
			if ($(this).attr('name')) {
				params[$(this).attr('name')] = $('input[name=' + $(this).attr('name') + ']:checked').val();
			}
		} else if ($(this)[0].type == 'checkbox') {
			if ($(this).attr('name') && !chk[$(this).attr('name')]) {
				s = [];
				chk[$(this).attr('name')] = 1;
				$('input[name=' + $(this).attr('name') + ']:checked').each(function() {
					s.push($(this).val());
				});
				params[$(this).attr('name')] = s.join(',');
			}
		}
	});
	chk = null, s = null;
	return params;
}
JZL.setValue = function(name, value) {

	var first = name.substr(0, 1),
		input, i = 0,
		val;
	if ("#" === first || "." === first) {
		input = $(name);
	} else {
		input = $("[name='" + name + "']");
	}

	if (input.eq(0).is(":radio")) { //单选按钮
		input.filter("[value='" + value + "']").each(function() {
			this.checked = true
		});
	} else if (input.eq(0).is(":checkbox")) { //复选框
		if (!$.isArray(value)) {
			val = new Array();
			val[0] = value;
		} else {
			val = value;
		}
		for (i = 0, len = val.length; i < len; i++) {
			input.filter("[value='" + val[i] + "']").each(function() {
				this.checked = true
			});
		}
	} else if (input.is('img')) { //图片
 
	if (''!= value) {
		input.attr('src',hyhImgUrl(value));
		input.attr('data-src',value);
	}
		
	} else { //其他表单选项直接设置值
		input.val(value);
	}

}
JZL.setValues = function(obj) {
	var input, value, val;
	for (var key in obj) {
		if ($('#' + key)[0]) {
			JZL.setValue('#' + key, obj[key]);
		} else if ($("[name='" + key + "']")[0]) {
			JZL.setValue(key, obj[key]);
		}
	}
}




/**
 * 邮箱格式判断
 * @param str
 */
function checkEmail(str){
    var reg = /^[a-z0-9]([a-z0-9\\.]*[-_]{0,4}?[a-z0-9-_\\.]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+([\.][\w_-]+){1,5}$/i;
    if(reg.test(str)){
        return true;
    }else{
        return false;
    }
}
/**
 * 输入为空检查
 * @param name '#id' '.id'  (name模式直接写名称)
 * @param type 类型  0 默认是id或者class方式 1 name='X'模式
 */
function is_empty(name,type){
    if(type == 1){
        if($('input[name="'+name+'"]').val() == ''){
            return true;
        }
    }else{
        if($(name).val() == ''){
            return true;
        }
    }
    return false;
}


