/**

 layui官网

 */

layui.define(['layer', 'laydate', 'toastr'], function(exports) {
	window.$ = layui.jquery;
	var layer = layui.layer;
	var _hmt = _hmt || [];
	(function() {
		var hm = document.createElement("script");
		hm.src = "//hm.baidu.com/hm.js?2a935166b0c9b73fef3c8bae58b95fe4";
		var s = document.getElementsByTagName("script")[0];
		s.parentNode.insertBefore(hm, s);
	})();

	var exp = {
		winopen : function(url, w, h) {
			url = fix_url(url);
			var index = layer.open({
				type : 2,
				title : false,
				area : [w + 'px', h + 'px'],
				shade : 0.6,
				closeBtn : 0,
				shadeClose : false,
				scrollbar : false,
				content : [url, 'no']
			});
			if (is_mobile()) {
				layer.full(index);
			}
		},
		send_form : function(from_id, post_url, return_url, callback) {
			check_form(from_id, function(ret) {
				if (ret.status) {
					if ($("#ajax").val() == 1) {
						var vars = $("#" + from_id).serialize();
						$.ajax({
							type : "POST",
							url : post_url,
							data : vars,
							dataType : "json",
							success : function(data) {
								if ( typeof (callback) == 'function') {
									callback(data);
								} else {
									if (data.status) {
										layer.msg(data.info, {
											time : 1200
										}, function() {
											if (return_url) {
												location.href = return_url;
											} else {
												location.reload(true);
											}
										});
									} else {
										layer.msg(data.info);
										return false;
									};
								}
							}
						});
					} else {
						//取消beforeunload事件
						if (return_url) {
							set_return_url(return_url);
						}
						$(window).unbind('beforeunload', null);
						$("#" + from_id).attr("action", post_url);
						$("#" + from_id).submit();
					}
				} else {
					layer.msg(ret.info);
					ret.dom.focus();
					return false;
				}
			});
		},
		/* ajax提交*/
		send_ajax : function(url, vars, callback) {
			return $.ajax({
				type : "POST",
				url : url,
				data : vars + "&ajax=1",
				dataType : "json",
				success : callback
			});
		},
		winprint : function() {
			setTimeout(function() {
				window.print();
			}, 300);
		}
	};
	win_exp(exp);
	//窗口scroll
	$(document).on('click touchstart', '.btn-preview', function() {
		var file_list = $(this).attr('file_list');
		var file_id = $(this).attr('file_id');
		if ($(this).parents('body').hasClass('popup')) {
			location.href = "index.php?m=&c=popup&a=preview&file_list=" + file_list + '&file_id=' + file_id;
		} else {
			winopen("/index.php?m=&c=popup&a=preview&file_list=" + file_list + '&file_id=' + file_id, 560, 470);
		}
	});
	;! function() {
		var main = $('.site-tree').parent(),
		    scroll = function() {
			var stop = $(window).scrollTop();
			if ($(window).width() <= 750)
				return;
			var bottom = 0;
			if (stop > 61 && stop < bottom) {
				if (!main.hasClass('site-fix')) {
					main.addClass('site-fix');
				}
				if (main.hasClass('site-fix-footer')) {
					main.removeClass('site-fix-footer');
				}
			} else if (stop >= bottom) {
				if (!main.hasClass('site-fix-footer')) {
					main.addClass('site-fix site-fix-footer');
				}
			} else {
				if (main.hasClass('site-fix')) {
					main.removeClass('site-fix').removeClass('site-fix-footer');
				}
			}
			stop = null;
		};
		scroll();
		$(window).on('scroll', scroll);
	}();

	//手机设备的简单适配
	var treeMobile = $('.site-tree-mobile'),
	    shadeMobile = $('.site-mobile-shade');

	treeMobile.on('click', function() {
		$('body').addClass('site-mobile');
	});

	shadeMobile.on('click', function() {
		$('body').removeClass('site-mobile');
	});

	treeMobile.on('click', function() {
		$('body').addClass('site-mobile');
	});

	var $top_menu_link = $('a.top_menu_link');
	$top_menu_link.on('click touchstart', function() {
		url = $(this).attr("url");
		node = $(this).attr("node");
		set_cookie("top_menu", node);
		set_cookie("left_menu", "");
		set_cookie("current_node", "");
		("http://" === url.substr(0, 7) || "#" === url.substr(0, 1)) ? window.open(url) : location.href = url;
	});

	top_menu = get_cookie("top_menu");
	$(".x-nav .x-nav-item a.top_menu_link[node=" + top_menu + "]").addClass("active");

	var $sidebar_link = $('.x-sidebar a');
	$sidebar_link.on('click', function() {
		var $this = $(this);
		var url = $this.attr("href");
		if (url.length > 0 && (url != "#")) {
			node = $this.attr("node");
			set_cookie("current_node", node);
		}
	});
	$(".x-checkbox,.x-radio").each(function() {
		var label = document.createElement("label");
		label.innerHTML = "&nbsp;";
		this.appendChild(label);
	});

	current_node = get_cookie("current_node");
	$(".x-sidebar .nav a[node='" + current_node + "']").closest("li").addClass("current");
	$(".x-sidebar .nav a[node='" + current_node + "']").parents("li").each(function() {
		$(this).addClass("active open");
		//alert($(this).html());
		//$(this).find('> ul').addClass("in");
		//breadcrumb = '<li>' + $(this).find("a:first .menu-text").text() + '</li>' + breadcrumb;
	});
	//$(".breadcrumb").append(breadcrumb);

	var $toogle_tom_menu = $('.toogle_tom_menu');
	$toogle_tom_menu.on('click', function() {
		if ($('.header .x-nav').hasClass('mobile-hidden')) {

			$('.header .x-nav').removeClass('mobile-hidden');
			$('.header').addClass('mobile_fixed');
		} else {
			$('.header .x-nav').addClass('mobile-hidden');
			$('.header').removeClass('mobile_fixed');
		}
	});

	$('#submit_search').on('click', function() {
		$('#form_search').submit();
	});

	$('#form_search #keyword').on('keydown', function(event) {
		if (event.keyCode == 13) {
			$('#form_search').submit();
		}
	});

	$('#toggle_adv_search').on('click', function() {
		toggle_adv_search();
	});

	$('#close_adv_search').on('click', function() {
		toggle_adv_search();
	});

	$('#submit_adv_search').on('click', function() {
		$('#form_adv_search').submit();
	});

	var start = {
		choose : function(datas) {
			end.min = datas;
			//开始日选好后，重置结束日的最小日期
			end.start = datas;
			//将结束日的初始值设定为开始日
		}
	};

	$('.input-start-date').on('click', function() {
		start.elem = this;
		laydate(start);
	});

	var end = {
		choose : function(datas) {
			start.max = datas;
			//结束日选好后，重置开始日的最大日期
		}
	};

	$('.input-end-date').on('click', function() {
		end.elem = this;
		laydate(end);
	});

	$('.input-date').on('click', function() {
		laydate({
			elem : this,
			fixed : false
		});
	});

	$('.input-date-time').on('click', function() {
		laydate({
			elem : this,
			istime : true,
			format : 'YYYY-MM-DD hh:mm'
		});
	});

	$('.toggle-select-all').on('click', function() {
		$this = $(this);
		var count = 0;
		var checkbox_name = $this.attr('data');
		is_checked = $this.attr('checked');
		if (is_checked) {
			$("input[type=checkbox][name='" + checkbox_name + "']").each(function() {
				this.checked = false;
				$(this).closest('.tbody').removeClass('selected');
			});
			$this.attr('checked', false);
		} else {
			$("input[type=checkbox][name='" + checkbox_name + "']").each(function() {
				this.checked = true;
				$(this).closest('.tbody').addClass('selected');
			});
			$this.attr('checked', true);
		}
	});

	function toggle_adv_search() {
		if ($("#adv_search").attr("class").indexOf("hidden") < 0) {
			$("#adv_search").addClass("hidden");
			$("#toggle_adv_search_icon").addClass("fa-chevron-down");
			$("#toggle_adv_search_icon").removeClass("fa-chevron-up");
		} else {
			$("#adv_search").removeClass("hidden");
			$("#toggle_adv_search_icon").addClass("fa-chevron-up");
			$("#toggle_adv_search_icon").removeClass("fa-chevron-down");
		}
	};

	$(".dropdown-toggle").on('click', function() {
		$next = $(this).next().toggle();
	});

	$(".dropdown-menu").on('mouseleave', function() {
		$(this).toggle();
	});

	$(document).on('click', '.input-box .address_list a.del', function() {
		$(this).parent().parent().remove();
	});
	exports('global', {});
});
