$(document).ready(function() {
	$('.modal-layout').click(function(event) {
		var _this = $(this);
		_this.addClass('hide');
		_this.find('.modal-wrapper').removeClass('animated')
		_this.find('.guarantee-modal-list').slideToggle('600');
	});
	// 选择诉讼阶段
	$('.online-letter-step').click(function(event) {
		var $layout = $('.modal-layout[data-target=online-letter-step]'); 
		var $item = $layout.find('.modal-radio-list li');
		$layout.removeClass('hide').find('.modal-wrapper').addClass('animated');
		$item.on('click',function(){
			var _this = $(this);
			var txt = _this.text();
			$layout.addClass('hide');
			$layout.find('.modal-wrapper').removeClass('animated');
			$('.online-letter-step input').val(txt);
			$item.off('click');
		});
	});
	// 选择诉讼类型
	$('.online-letter-form').on('click','.online-letter-type',function(event) {
		var $layout = $('.modal-layout[data-target=online-letter-type]'); 
		var $typeTarget = $(this);
		var $item = $layout.find('.modal-radio-list li');
		$layout.removeClass('hide').find('.modal-wrapper').addClass('animated');
		$item.on('click',function(){
			var _this = $(this);
			var txt = _this.text();
			$layout.addClass('hide');
			$layout.find('.modal-wrapper').removeClass('animated');
			$('.online-letter-type input').val(txt);
			$item.off('click');
		})
	});
	$('.online-letter-price').click(function(){
		var _this = $(this);
		_this.find('input').trigger('focus');
	});
	// 添加标的类型
	$('.online-letter-add-type').click(function(event) {
		var num = $('.online-letter-form .online-letter-type-wrapper').length;
		if (num <= 2) {
			var str = '<div class="pad-tb-20 ub ub-ac ub-pj fc-grey list-item pos-relate br-bottom1 touchact online-letter-type-wrapper">';
			str+='<label class="mar-r-25">选择标的类型</label>';
			str+='<div class="pad-r-25 ub ub-ac ub-f1 online-letter-type"><input type="text" name="type" class="ub-f1 form-control fs-14" readonly="readonly">';
			str+='<span class="icon icon-left5 icon-down-blue"></span></div></div>';
			$('.online-letter-form').append(str);
			var $minus = $('.online-letter-minus-type');
			if($minus.hasClass('hide')){
				$minus.removeClass('hide');
			}
		}
	});
	// 删除标的类型
	$('.online-letter-minus-type').click(function(event) {
		var $wrapper = $('.online-letter-type-wrapper')
		if($wrapper.length>1){
			if ($wrapper.length == 2) {
				$('.online-letter-minus-type').addClass('hide');
			}
			$('.online-letter-type-wrapper').last().remove();
		}

	});
	//查询判断是否必填项已填
	$('.btn-search-org').click(function(){
		var court = $('input[name="court"]').val().trim();
		var stage = $('input[name="stage"]').val().trim();
		var price = $('input[name="price"]').val().trim();
		if (court == '' || court == 'undefined') {
			toast1.alert('请选择法院');
			return false;
		}
		if (stage == '' || stage == 'undefined') {
			toast1.alert('请选择诉讼阶段');
			return false;
		}
		if (price == '' || price == 'undefined') {
			toast1.alert('请填写标的金额');
			return false;
		}
		$('input[name="type"]').each(function(){
			var type = $(this).val().trim();
			if (type == '' || type == 'undefined') {
				toast1.alert('请选择标的类型');
				return false;
			}
		});
	});


	$('.online-letter-court').click(function(event) {
		$('.select-area-layout').removeClass('hide');
	});
	$('.area-back').click(function(event) {
		$('.select-area-layout').addClass('hide');
	});
	$('.select-court-btn').click(function(event) {
		var text = $(this).find('.local-city').text();
		// $('.select-court-layout').removeClass('hide').addClass('pageIn');
		moveIn('select-court-layout');
		$('.current-city').text(text);
	});
	$('.area-recent-list li').click(function(event) {
		var text = $(this).text();
		// $('.select-court-layout').removeClass('hide').addClass('pageIn');
		moveIn('select-court-layout');
		$('.current-city').text(text);
	});
	$('.area-city-list li').click(function(event) {
		var text = $(this).text();
		// $('.select-court-layout').removeClass('hide').addClass('pageIn');
		moveIn('select-court-layout');
		$('.current-city').text(text);
	});
	$('.court-back-btn,.court-back').click(function(event) {
		moveOut($('.select-court-layout'));
		$('.area-search-form input').val('');
		$('.select-area-layout').css('overflow','auto');
		$('.search-city-wrapper').addClass('hide');
	});
	$('.search-area').click(function(){
		$('.select-area-layout').removeClass('hide');
		$('.area-search-form input').val('');
		$('.select-area-layout').css('overflow','auto');
		$('.search-page').addClass('hide');
	});
	function moveback(box){
		var $court = box;
		$court.css({
			'webkitTransform':'translate3d(0,0,0)',
			'transform':'translate3d(0,0,0)'
		});
		$court.removeClass('pageIn').addClass('pageOut');
		$court.on('animationend webkitAnimationEnd',function(){
			$court.removeClass('pageOut').addClass('hide').css({
				'webkitTransform':'translate3d(100%,0,0)',
				'transform':'translate3d(100%,0,0)'
			});
			$court.off('animationend webkitAnimationEnd');
		});
	}
	$('.court-list li').click(function(event) {
		var txt = $(this).text();
		$('.online-letter-court input').val(txt);
		setTimeout(function(){
			// $('.select-court-layout,.select-area-layout').removeClass('pageIn').addClass('hide');
			$('.select-area-layout').addClass('hide');
			$('.search-city-wrapper').addClass('hide');
			moveOut($('.select-court-layout'));
		},300)
	});

	$('.guarantee-order-tab>div').click(function(event) {
		var _this = $(this);
		var target = _this.attr('data-target');
		var $layout = $('.modal-layout[data-target='+target+']'); 
		$layout.siblings('.modal-layout').addClass('hide').find('.guarantee-modal-list').slideUp('600');
		$layout.toggleClass('hide');
		$layout.find('.guarantee-modal-list').slideToggle('600');
	});

	$('#online-letter-money').on('change',function(event){
		var value = $(this).val();
		var reg = new RegExp("^[0-9]*$");
		if(!/^[0-9]*$/.test(value)){  
			$(this).val('').focus();
			$('.online-letter-money-tips').removeClass('hide');
	    }  
	    else{
	    	$('.online-letter-money-tips').addClass('hide');
	    }
		
	});	


	/*clm*/
	//radio多
	$('.radio-box').click(function(){
		var _this = $(this).find('.icon-radio1');
		var radio = _this.attr('data-radio');
		var value = _this.attr('data-value');
		if (!_this.hasClass('on')) {
			_this.addClass('on');
			$('input[data-type='+radio+']').val(value);
			radio_if = false;
		}else{
			_this.removeClass('on');
			$('input[data-type='+radio+']').val('');
			radio_if = true;
		}
	});
	$('.select-box').click(function(){
		var _this = $(this).find('.icon-select');
		var radio = _this.attr('data-radio');
		var value = _this.attr('data-value');
		if (!_this.hasClass('on')) {
			_this.addClass('on');
			$('input[data-type='+radio+']').val(value);
			radio_if = false;
		}else{
			_this.removeClass('on');
			$('input[data-type='+radio+']').val('');
			radio_if = true;
		}
		if (_this.hasClass('on')) {
			$('.delivery-form .form-control').each(function(){
				$(this).val('');
			});
			var data = $(this).attr('data-to');
			moveIn(data);
		}
	});
	//radio一定要有选项
	$('.pay-method').click(function(){
		var _this = $(this).find('.radio-signle');
		var _thispar = _this.parent();
		var radio = _this.attr('data-radio');
		var value = _this.attr('data-value');
		if (!_this.hasClass('on')) {
			_this.addClass('on');
			_thispar.siblings().find('.radio-signle').removeClass('on');
			$('input[data-type='+radio+']').val(value);
			radio_if = false;
		}
	});
	$('.cancel-reason li').click(function(){
		var _this = $(this).find('.radio-signle');
		var _thispar = _this.parent();
		var radio = _this.attr('data-radio');
		var value = _this.attr('data-value');
		if (!_this.hasClass('on')) {
			_this.addClass('on');
			_thispar.siblings().find('.radio-signle').removeClass('on');
			$('input[data-type='+radio+']').val(value);
			radio_if = false;
		}
	});
	//点击空白隐藏弹窗
	$('.mask-page').click(function(event){
		var box = $(this);
		fadeOutBox(box);
	});
	$('.mask-page .mask-confirm').click(function(){
		var box = $(this).parent();
		fadeInBox(box);
		event.stopPropagation();
	});
	$('.mask-page .mask-box').click(function(){
		var box = $(this).parent();
		fadeInBox(box);
		event.stopPropagation();
	});
	$('.mask-page .mask-reason').click(function(){
		var box = $(this).parent();
		fadeInBox(box);
		event.stopPropagation();
	});

	$('.cancel-order').click(function(){
		$('.cancel-page').fadeIn(300).removeClass('hide');
	});
	$('.cancel-page .btn-yes').click(function(event){
		$('.cancel-page').fadeOut(300).removeClass('hide');
		event.stopPropagation();
	});	
	$('.cancel-page .btn-cancel').click(function(event){
		$('.cancel-page').fadeOut(300).removeClass('hide');
		event.stopPropagation();
	});	
	function fadeInBox(box){
		box.fadeIn(200).removeClass('hide');
	}
	function fadeOutBox(box){
		box.fadeOut(200).addClass('hide');
	}

	// 担保机构
	$('.list-sort-item.active').find('.icon').removeClass('icon-top-bottom').addClass('icon-top-blue3');

	$('.list-sort-item').click(function(){
		var active_click = true;
		var _this = $(this);
		var _icon = _this.find('.icon'); 
		_this.addClass('active').siblings().removeClass('active');
		_this.siblings().find('.icon').removeClass('icon-top-blue3 icon-bottom-blue').addClass('icon-top-bottom');
		if (active_click) {
			if (!_icon.hasClass('icon-top-blue3')) {
				_icon.removeClass('icon-top-bottom').addClass('icon-top-blue3');
			}else{
				_icon.removeClass('icon-top-blue3').addClass('icon-bottom-blue');
			}
	    	active_click = false;
		}else{
			if (!_icon.hasClass('icon-bottom-blue')) {
				_icon.removeClass('icon-top-blue3').addClass('icon-top-bottom');
			}else{
				_icon.removeClass('icon-bottom-blue').addClass('icon-top-blue3');
			}
	    	active_click = true;
		}
	});
	//申请
	$('.application-top').click(function(){
		var _this = $(this);
		var data = _this.attr('data-to');
		// $('#'+data).removeClass('hide').addClass('pageIn');
		moveIn(data);
	});
	$('.layout-top-back').click(function(){
		var box = $(this).parent().parent();
		// moveback(box);
		moveOut(box);
	});
	//提交申请
	$('.btn-apply').click(function(){
		var length1 = $('.apply-info1').find('input').length;
		var length2 = $('.apply-info2').find('input').length;
		var length3 = $('.apply-info3').find('input').length;
		var length4 = $('.apply-info4').find('input').length;
		if (length1 < 1) {
			toast1.alert('请上传民事诉讼状');
			return false;
		}
		if (length2 < 1) {
			toast1.alert('请上传身份证明');
			return false;
		}
		if (length3 < 1) {
			toast1.alert('请上传财产保全申请');
			return false;
		}
		if (length4 < 1) {
			toast1.alert('请上传证据清单');
			return false;
		}
	});

	$('.application-material-title').click(function(){
		var _this = $(this);
		var _thispar = _this.parent();
		if (!_thispar.hasClass('active')) {
			_thispar.addClass('active');
			_this.find('.icon').removeClass('icon-down-blue2').addClass('icon-top-blue2');
			_this.next().removeClass('hide');
		}else{
			_thispar.removeClass('active');
			_this.find('.icon').removeClass('icon-top-blue2').addClass('icon-down-blue2');
			_this.next().addClass('hide');
		}
	});
	$('.lawyer-evaluate').click(function(){
		var _this = $(this);
		var data = _this.attr('data-to');
		// $('#'+data).removeClass('hide').addClass('pageIn');
		moveIn(data);
	});
	$('.btn-how').click(function(){
		var _this = $(this);
		var data = _this.attr('data-to');
		// $('#'+data).removeClass('hide').addClass('pageIn');
		// $('body,html').css('overflow','hidden');
		moveIn(data);
	});
	//出函
	$('.check-logistics').click(function(){
		var _this = $(this);
		var data = _this.attr('data-to');
		$('#'+data).fadeIn(400).removeClass('hide');
	});
	$('.delivery-btn .btn-yes').click(function(event){
		$('.delivery-page').fadeOut(400).addClass('hide');
		event.stopPropagation();
	});

	//ios or android
	var u = navigator.userAgent;
	var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
	var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); 
	if (isAndroid) {
		$('.reason-reason').addClass('icon-top3');
	}
	if (isiOS) {
		document.body.addEventListener('touchstart', function () {});  
	}
	//登录
	var btn_click = true;
	$('.btn-password').click(function(){
		var _this = $(this);
		if (btn_click) {
			_this.removeClass('icon-unsee').addClass('icon-see');
			_this.prev().attr('type','text');
			btn_click = false;
		}else{
			_this.removeClass('icon-see').addClass('icon-unsee');
			_this.prev().attr('type','password');
			btn_click = true;
		}
	});
	
	
	//忘记密码
	$('.btn-submit-password').click(function(){
		var phone = $('input[name="phone"]').val();
		var code = $('input[name="code"]').val();
		if (phone == '' || phone == 'undefined') {
			toast1.alert('请填写手机号');
			return false;
		}
		if (code == '' || code == 'undefined') {
			toast1.alert('请填写验证码');
			return false;
		}
	});
	//设置新密码
	$('.btn-set-password').click(function(){
		var password1 = $('input[name="password1"]').val();
		var password2 = $('input[name="password2"]').val();
		var text = /\S{6,16}/;
		if (password1 == '' || password1 == 'undefined') {
			toast1.alert('请填写新密码');
			return false;
		}
		if (!text.test(password1)) {
			toast1.alert('密码应为6-16位');
			return false;
		}
		if (password2 == '' || password2 == 'undefined') {
			toast1.alert('再次输入不能为空');
			return false;
		}
		if (!text.test(password2)) {
			toast1.alert('密码应为6-16位');
			return false;
		}
		if (password1 !== password2) {
			toast1.alert('两次密码输入不一致');
			return false;
		}
		toast1.alert('新密码设置成功！');
	});


//	获取手机验证码
//	var InterValObj;
//	var count = 60;
//	var curCount;
//	function sendMessage(_this) {  
//	    curCount = count;  
//	    var phone = $('input[name=phone]').val();
//	    if(phone != ""){  
//	        if (!_this.attr('data-disabled')) {
//	        	_this.attr('data-disabled','true');
//		        $.ajax({
//					type:"POST",  
//					url: '',  
//					data: {phone:phone},
//					dataType:"json",  
//					success: function(res){  
//						//正式的时候取消掉注释
//						// InterValObj = window.setInterval(SetRemainTime, 1000); 
//						// _this.removeAttr('data-disabled');
//					},
//					error:function(res) {
//			        	InterValObj = window.setInterval(SetRemainTime, 1000); 
//						_this.removeAttr('data-disabled');
//					},
//					complete: function(){
//					}  
//				});
//		    }
//	    }else{  
//	        toast1.alert('请输入手机号');
//	    }  
//	}
//	$('.get-code').click(function() {
//		var _this = $(this);
//		if(!$('.get-code').hasClass('disable')){
//    		sendMessage(_this);
//    	}
//	});
//	function SetRemainTime() {  
//	    if (curCount == 1) {                  
//	        window.clearInterval(InterValObj);//停止计时器  
//	        $(".get-code").removeAttr("data-disabled");//启用按钮  
//	        $(".get-code").text("获取验证码");  
//	        $(".get-code").removeClass('disable');
//	    }  
//	    else {  
//	        curCount--;  
//	        $(".get-code").text(curCount + "s后重新获取"); 
//	        $(".get-code").addClass('disable');
//	    }  
//	}

	//搜索
	$('.btn-search-right').click(function(){
		var _this = $(this);
		var val = _this.prev().val();
		if (val==''||val=='underfined') {
			toast1.alert('请输入搜索内容');
			return false;
		}
		if(!_this.attr('data-disabled')){
			_this.attr('data-disabled','true');
			var tpl = '';
    		$.ajax({
				type:"GET",  
				url: '',  
				data: {},
				dataType:"json",  
				success: function(res){  
					// res = {
					// 	'code' : '200',
					// 	'data' : [
					// 		{'url':'#','city':'广州'},
					// 		{'url':'#','city':'广州2'},
					// 		{'url':'#','city':'广州3'},
					// 		{'url':'#','city':'广州4'},
					// 		{'url':'#','city':'广州5'}
					// 	]
					// }
					// data=res.data;
					// $('.search-city-wrapper').removeClass('hide');
					// if(res.code == 200){
					// 	if(data!=null&&data.length>0){
					// 		for (var i = 0; i < data.length; i++) {	
					// 			tpl += '<li class="pad-l-25 pos-relate br-bottom1"><a href="'+data[i].url+'">'+data[i].city+'</a></li>';
					// 		}
					// 		$('.search-city-list').html(tpl);
					// 		$('.select-area-layout').css('overflow','hidden');
					// 	}
					// }
				},
				error:function(res) {
					//正式写的时候删掉
					res = {
						'code' : '200',
						'data' : [
							{'url':'#','city':'广州'},
							{'url':'#','city':'广州2'},
							{'url':'#','city':'广州3'},
							{'url':'#','city':'广州4'},
							{'url':'#','city':'广州5'}
						]
					}
					data=res.data;
					$('.search-city-wrapper').removeClass('hide');
					if(res.code == 200){
						if(data!=null&&data.length>0){
							for (var i = 0; i < data.length; i++) {	
								tpl += '<li class="pad-l-25 pos-relate br-bottom1 touchact"><a href="'+data[i].url+'">'+data[i].city+'</a></li>';
							}
							$('.search-city-list').html(tpl);
							$('.select-area-layout').css('overflow','hidden');
						}
					}
				},
				complete: function(){
					_this.removeAttr('data-disabled');
				}  
			});
		}
	});
	$('.area-back').click(function() {
		$('.select-area-layout').addClass('hide');
		$('.select-area-layout').css('overflow','auto');
		$('.search-city-list').children('li').remove();
		$('.area-search-form input').val('');
		$('.search-city-wrapper').addClass('hide');
		$('.search-page').removeClass('hide');
	});
	$('.page').on('click', '.search-city-list li', function() {
		var txt = $(this).text();
		moveIn('select-court-layout');
		$('.current-city').text(txt);
	});
	//服务合同邮箱提交
	$('.btn-email').click(function(){
		var email = $('input[name="email"]').val();
		if (email == '' || email == 'undefined') {
			toast1.alert('请输入邮箱');
			return false;
		}
	});
	//快递信息提交
	$('.btn-submit-delivery').click(function(){
		var name = $('input[name=name]').val();
		var phone = $('input[name=phone]').val();
		var addr = $('input[name=addr]').val();
		if (name == '' || name == 'undefined') {
			toast1.alert('请输入收件人姓名');
			return false;
		}
		if (phone == '' || phone == 'undefined') {
			toast1.alert('请输入收件人电话');
			return false;
		}
		if (addr == '' || addr == 'undefined') {
			toast1.alert('请输入收件人地址');
			return false;
		}
	});
	//转账
	$('.btn-transfer').click(function(){
		var data = $(this).attr('data-to');
		// $('#'+data).removeClass('hide').addClass('pageIn');
		// $('body,html').css('overflow','hidden');
		moveIn(data);
	});	
	//评价
	$('.evaluation-star .star').click(function(){
		var _this = $(this);
		if (_this.parent().hasClass('active')) {
			var index = _this.index();
			_this.parent().find('.star').removeClass('active');
			for (var i = 0; i <= index ; i++) {
				_this.parent().find('.star').eq(i).addClass('active');
			}
			_this.parent().next().val(index+1);
		}
	});
	$('.btn-save').click(function(){
		var quality = $('input[name="quality"]').val();
		var speed = $('input[name="speed"]').val();
		var content = $('.evaluation-box textarea').val();
		if (quality == '' || quality == 'undefined') {
			toast1.alert('请评价服务质量');
			return false;
		}
		if (speed == '' || speed == 'undefined') {
			toast1.alert('请评价出函速度');
			return false;
		}
		if (content == '' || content == 'undefined') {
			toast1.alert('请填写评价');
			return false;
		}
	});
	//取消订单原因
	$('.btn-reason-cancel').click(function(event) {
		var reason = $('input[name="reason"]').val();
		if (reason == '' || reason == 'undefined') {
			toast1.alert('请选择原因');
			return false;
		}
		$('.cancel-page').fadeOut(400).addClass('hide');
		event.stopPropagation();
	});
	$('.btn-reason-close').click(function(event){
		$('.cancel-page').fadeOut(400).addClass('hide');
		event.stopPropagation();
	});
	//touch
	$('.page').on('click', '.touchact', function(event) {
		var _this = $(this);
		_this.addClass('touchbg');
		var timer = setTimeout(function(){
			_this.removeClass('touchbg');
			clearTimeout(timer);
		},200);
	});
	//toast
	var toast1 = new toast({
        pos:'center'
    });
	
	function moveIn(box){
		$('#'+box).removeClass('hide');
		$('#'+box).animate({'left': 0},400,function(){
			$('body,html').addClass('sift-move');
		});
	}
	function moveOut(box){
		box.animate({'left': '100%'},400,function(){
			box.addClass('hide');
		});
		$('body,html').removeClass('sift-move');
	}
});
	