$(function() {
	var Expression = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
	var patrn = new RegExp(Expression);
	mui.plusReady(function() {
		if($.getlocalUrl()){
			$("#neiwang").val($.getlocalUrl());
		}
		if($.getremoteUrl()){
			$("#waiwang").val($.getremoteUrl());
		}
		$("body").css("height", plus.screen.resolutionHeight + "px");
		$("html").css("height", plus.screen.resolutionHeight + "px");
		$("#bindingBtn2").on("tap", function() {
			if(yanzheng()) {
				isRemoteUrl(remoteShuruBind, localShuruBind);
			};

		});
		$("#neiwangs").on("tap", function() {
			if(plus.os.name == "Android") {
				var Ipstrs = copyToClip1();
				if(patrn.exec(Ipstrs)) {

					var Ipstr = Ipstrs.split(patrn.exec(Ipstrs)[0]);
					var strport = "";
					var xieGangArr = [];
					for(var i = 0; i < Ipstr.length; i++) {
						if(Ipstr[i].indexOf("/") != -1) {
							var str = Ipstr[i].split("");
							for(var j in str) {
								if(str[j] == "/") {
									xieGangArr.push(j)
								}
								if(xieGangArr.length == 2) {
									break
								}
							}
							//patrn.exec(Ipstrs)[0]是ip,Ipstr[i].substring(0,xieGangArr[1])端口号到edei的字符串
							strport = patrn.exec(Ipstrs)[0] + Ipstr[i].substring(0, xieGangArr[1]);

							break;

						}
						//strport = Ipstr[i].slice(0, Ipstr[i].indexOf("edei") + 4);

					}

					$("#neiwang").val(strport);
				} else {
					mui.alert("请输入正确的ip地址")
				}

			} else {
				var Ipstrs = iosCopToClip1();
				if(patrn.exec(Ipstrs)) {

					var Ipstr = Ipstrs.split(patrn.exec(Ipstrs)[0]);
					var strport = "";
					var xieGangArr = [];
					for(var i = 0; i < Ipstr.length; i++) {
						if(Ipstr[i].indexOf("/") != -1) {
							var str = Ipstr[i].split("");
							//console.log(str)
							for(var j in str) {
								if(str[j] == "/") {
									xieGangArr.push(j)
								}
								if(xieGangArr.length == 2) {
									break
								}
							}
							//patrn.exec(Ipstrs)[0]是ip,Ipstr[i].substring(0,xieGangArr[1])端口号到edei的字符串
							strport = patrn.exec(Ipstrs)[0] + Ipstr[i].substring(0, xieGangArr[1]);

							break;

						}
						//strport = Ipstr[i].slice(0, Ipstr[i].indexOf("edei") + 4);

					}

					$("#neiwang").val(strport);
				} else {
					mui.alert("请输入正确的ip地址")
				}

			}

		})
		$(".icon-shanchu").on("tap",function(){
			$(this).siblings("input").val("");
		})
		$("#waiwangs").on("tap", function() {
			if(plus.os.name == "Android") {
				var Ipstrs = copyToClip1();
				if(patrn.exec(Ipstrs)) {

					var Ipstr = Ipstrs.split(patrn.exec(Ipstrs)[0]);
					var strport = "";
					var xieGangArr = [];
					for(var i = 0; i < Ipstr.length; i++) {
						if(Ipstr[i].indexOf("/") != -1) {
							var str = Ipstr[i].split("");
							//console.log(str)
							for(var j in str) {
								if(str[j] == "/") {
									xieGangArr.push(j)
								}
								if(xieGangArr.length == 2) {
									break
								}
							}
							//patrn.exec(Ipstrs)[0]是ip,Ipstr[i].substring(0,xieGangArr[1])端口号到edei的字符串
							strport = patrn.exec(Ipstrs)[0] + Ipstr[i].substring(0, xieGangArr[1]);

							break;

						}
						//strport = Ipstr[i].slice(0, Ipstr[i].indexOf("edei") + 4);

					}

					$("#waiwang").val(strport);
				} else {
					mui.alert("请输入正确的ip地址")
				}

			} else {
				var Ipstrs = iosCopToClip1();
				if(patrn.exec(Ipstrs)) {

					var Ipstr = Ipstrs.split(patrn.exec(Ipstrs)[0]);
					var strport = "";
					var xieGangArr = [];
					for(var i = 0; i < Ipstr.length; i++) {
						if(Ipstr[i].indexOf("/") != -1) {
							var str = Ipstr[i].split("");
							//console.log(str)
							for(var j in str) {
								if(str[j] == "/") {
									xieGangArr.push(j)
								}
								if(xieGangArr.length == 2) {
									break
								}
							}
							//patrn.exec(Ipstrs)[0]是ip,Ipstr[i].substring(0,xieGangArr[1])端口号到edei的字符串
							strport = patrn.exec(Ipstrs)[0] + Ipstr[i].substring(0, xieGangArr[1]);

							break;

						}
						//strport = Ipstr[i].slice(0, Ipstr[i].indexOf("edei") + 4);

					}

					$("#waiwang").val(strport);
				} else {
					mui.alert("请输入正确的ip地址")
				}

			}

		})
		//ios设置剪切板的内容
		function iosCopyToClip(value) {
			var UIPasteboard = plus.ios.importClass("UIPasteboard");
			var generalPasteboard = UIPasteboard.generalPasteboard();
			// 设置/获取文本内容:  
			generalPasteboard.setValueforPasteboardType(value, "public.utf8-plain-text");

		}
		//ios获取剪切板的内容
		function iosCopToClip1() {
			var UIPasteboard = plus.ios.importClass("UIPasteboard");
			var generalPasteboard = UIPasteboard.generalPasteboard();
			var value = generalPasteboard.valueForPasteboardType("public.utf8-plain-text");
			return value;
		}

		//Android设置剪切板的内容
		function copyToClip(value) {
			var Context = plus.android.importClass("android.content.Context");
			var main = plus.android.runtimeMainActivity();
			var clip = main.getSystemService(Context.CLIPBOARD_SERVICE);
			plus.android.invoke(clip, "setText", value);
		}
		//Android获取剪切板的内容
		function copyToClip1() {
			var Context = plus.android.importClass("android.content.Context");
			var main = plus.android.runtimeMainActivity();
			var clip = main.getSystemService(Context.CLIPBOARD_SERVICE);
			return plus.android.invoke(clip, "getText");
		}

		function remoteSuccessCheckEdei() { //监测外网edei存在,进行跳转
			mui.openWindow({
				url:"../..//index.html",
				id: 'appindex'
			});
		}

		function remoteFailCheckEdei() { //监测外网edei不存在,不进行跳转
			mui.alert("服务器未打开或者已关闭！");
		}

		function remoteShuruBind() { //外网绑定执行函数

			$.setLocal({
				"url": $.getremoteUrl()
			})
			checkEdei(remoteSuccessCheckEdei, remoteFailCheckEdei)

		}

		function localSuccessCheckEdei() { //监测内网edei存在,进行跳转
			mui.openWindow({
				url: "../../index.html",
				id: 'appindex'
			});
		}

		function localFailCheckEdei() { //监测内网edei不存在,不进行跳转
			mui.alert("服务器未打开或者已关闭！");
		}

		function localShuruBind() { //内网执行绑定函数

			$.setLocal({
				"url": $.getlocalUrl()
			})
			checkEdei(localSuccessCheckEdei, localFailCheckEdei)
		}
	})

	function yanzheng() {
		var neiwang = $("#neiwang").val();
		var waiwang = $("#waiwang").val();

		if(!neiwang && !waiwang) {
			mui.alert("请至少填写一个地址");
			return false;
		}
		if(!neiwang) {

		} else {
			if(patrn.exec(neiwang)) {
				$.setLocal({
					"localUrl": neiwang,
				});

			} else {
				mui.alert("请填写正确的IP地址格式");
				return false;
			}
		}

		if(!waiwang) {

		} else {
			if(patrn.exec(waiwang)) {
				$.setLocal({
					"remoteUrl": waiwang
				});
			} else {
				mui.alert("请填写正确的IP地址格式");
				return false;
			}
		}
		return true;
	}
})