var Farm = 
{
	loop : false,
	url : '',
	Page : function(title, url)
	{
		var e = $('#layer');
		e.find('form').attr('action', url);
		if (title) {
			e.find('#layer_title').html(title);
		}
		$.get(url, function(t){
			e.find('#layer_content').html(t);
			e.show()
		})
	}

	,Delete : function(obj, id, url)
	{
		var self = this;
		this.Confirm("确定要删除吗?" , function() {
			data = {};
			data.id = id;
			self.Request('delete', url, data, function(msg)
			{
				$(obj).parents("dl").remove();
				//self.Notice('已删除');
			})
		});
	}

	,Cancel : function(obj, id, url)
	{
		var self = this;
		this.Confirm("确定要进行此项操作吗?" , function() {
			data = {};
			data.id = id;
			self.Request('delete', url, data, function(msg)
			{
				location.reload();
			})
		});
	}

	,Recovery : function(obj, id, url)
	{
		var self = this;
		this.Confirm("确定要恢复吗?" , function() {
			data = {};
			data.id = id;
			data.state = true;
			self.Request('delete', url, data, function(msg)
			{
				location.reload();
			})
		});
	}

	,Switch : function(obj, url, id)
	{
		var self = this;
		var e = $(obj);
		var c = e.attr('class');
		if ($('.loading').length) {
			return;
		}
		if (e.attr('class').indexOf('loading') > -1) {
			return;
		}
		if (c.indexOf('disabled') != -1) {
			return;
		}
		var value = e.attr('data-status');
		data = {};
		data.id = id;
		data.value = value;
		if (value == 1) {
			e.attr('data-status', 2);
		} else {
			e.attr('data-status', 1);
		}
		self.ClearLoop();
		e.addClass('loading');
		self.Request('post', url, data, function(msg)
		{
			self.Load();
			self.CreateLoop();
			/*
			e.removeClass('loading');
			msg = eval('(' + msg + ')');
			if (msg.status == 2) {
				alert(msg.msg);
				//this.Alert(msg.msg);
				return;
			}
			
			if (value == 1) {
				//开启
				e.addClass('open');
			} else {
				//关闭
				e.removeClass('open');
			}
			*/
		})
	}

	,Button : function(obj, url, id, value, disabledId)
	{
		var self = this;
		var e = $(obj);
		var c = e.attr('class');
		var p = e.parent();
		if ($('.loading').length) {
			return;
		}
		if (p.find('.loading').length) {
			return;
		}
		if (c.indexOf('disabled') != -1) {
			return;
		}
		/*
		var name = e.html();
		var old = p.find('.open')
		var oldDisabled = p.find('.disabled')
		old.removeClass('open');
		e.addClass('open');
		if (disabledId) {
			$(disabledId).addClass('disabled');
		} else {
			oldDisabled.removeClass('disabled');
		}
		*/
		data = {};
		data.id = id;
		data.value = value;
		self.ClearLoop();
		e.addClass('loading');
		self.Request('post', url, data, function(msg)
		{	
			self.Load();
			self.CreateLoop();
			/*
			e.removeClass('loading');
			self.CreateLoop();
			msg = eval('(' + msg + ')');
			if (msg.status == 2) {
				alert(msg.msg);
				//this.Alert(msg.msg);
				old.addClass('open');
				e.removeClass('open');
				if (disabledId) {
					$(disabledId).removeClass('disabled');
				} else {
					oldDisabled.addClass('disabled');
				}
				return;
			}
			*/
		})
	}

	,Request : function(type, url, data, callback)
	{
		data._xsrf = this.Cookie("_xsrf");
		$.ajax({
			type:type,
			url:url,
			data:data,
			success:function(msg){
				callback(msg)
			}
		});
	}

	,Confirm : function(msg, func) {
		var e = $('#msg');
		e.find('#msg_content').html(msg);
		e.find('.controls a').eq(0).unbind('click').bind('click', function()
		{
			func();
		})
		e.show();
	}

	,Alert : function(msg, title) {
		var e = $('#msg');
		if (title) {
			e.find('#msg_title').html(title);
		}
		e.find('#msg_content').html(msg);
		e.find('.controls a').eq(0).unbind('click');
		e.show();
	}

	,Msg : function(value) {
		if (!value.data) {
			value.data = value.msg;
		}
		var url = '';
		if (value.data != 'reload' && value.status == 2) {
			this.Alert(value.msg);
			return;
		} else if(value.data == 'reload') {
			this.Jump('');
		} else {
			if($("#url").length) {
				url = $("#url").val();
			} else {
				location.reload()
			}

			if (url) {
				this.Jump(url);
			} else {
				this.Jump('');
			}
		}
		
		return;
	}

	,Jump : function(url) {
		if (url) {
			location.href = url
		} else {
			location.reload()
		}
	}

	,Cookie : function(name) {
		var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
		return r ? r[1] : undefined;
	}

	,Load : function() {
		var self = this;
		$.ajax({
	        url:self.url,
	        type:'get',
	        data:{},
	        timeout:5000,
	        success:function(data, textStatus){
	            if (data) {
	            	
	                if (data.indexOf('||') != -1) {
	                    var html = data.split('||');
	                    for(var i = 0;i < html.length; i++) {
	                        $("#loadMain_" + i).html(html[i]);
	                    }
	                } else{
	                    $("#loadMain").html(data);
	                }
					if ($('.video-js video').length && $('.box img').length) {
						var height = $('.video-js video').height();
						$('.box img').height(height).css('display','block');
					}
	                self.CreateLoop();
	            }
	        }
	    });
	}

	,ShowChart : function(url) {
		this.url = url;
		this.ClearLoop();
		this.Load();
		document.getElementById('chart').scrollIntoView();
	}

	,CreateLoop : function() {
		if (!this.loop) {
			this.loop = setInterval("Farm.Load()", 1000);
		}
	}

	,ClearLoop : function() {
		if (this.loop) {
			clearInterval(this.loop);
			this.loop = false;
		}
	}

	,Upload : function(id1, id2) {
		layui.use('upload', function() {
			var xsrf = Farm.Cookie("_xsrf");
		    var uploadUrl = '/upload?_xsrf='+xsrf;
		    var upload = layui.upload; //得到 upload 对象
			var config = {
		      elem: id1
		      ,url: uploadUrl
		      ,done: function(res, index, upload){
		      	var id = $(id1).attr('id');
		        $('#upload_' + id + '_show').attr('src',res.data.src + '?v' + Date.parse(new Date())).show();
		        $('#update_' + id).val(res.data.src);
		      } 
		      ,accept: 'images' //允许上传的文件类型
		      ,size: 5000 //最大允许上传的文件大小
		    }
		    upload.render(config);

		    if (id2) {
		    	var config = {
			      elem: id2
			      ,url: uploadUrl
			      ,done: function(res, index, upload){
			      	var id = $(id2).attr('id');
			        $('#upload_' + id + '_show').attr('src',res.data.src + '?v' + Date.parse(new Date())).show();
			        $('#update_' + id).val(res.data.src);
			      } 
			      ,accept: 'images' //允许上传的文件类型
			      ,size: 5000 //最大允许上传的文件大小
			    }
			    upload.render(config);
		    }
		});
	}
}

function msg(value) {
	Farm.Msg(value);
}

//瀑布流分页
var _Dever_Page = 
{
    name : '.paging',
    Init : function()
    {
        if ($(this.name).length) {
            var self = this;
            $(window).scroll(function() {  
                if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
                    self.Start(self.name);
                }
            });
        }
    }

    ,Start : function(page)
    {
        page = page ? page : this.name;
        if ($(page).length) {
            $(page).hide();
            var url = $(page + ':last').attr('link');
            if (url) {
            	$(".loading-more").show();
                var key = $(page).attr('dever-list');
                $.get(url, function(t) {
                    t = '<div>' + t + '</div>';
                    var c = $(t).find(key).html();
                    if (c) {
                        $(key).append(c);
                        $(".loading-more").hide();
                        var l = $(t).find(page).attr('link');
                        $(page).attr('link', l);
                    }
                });
            }
        }
    }
    
    ,Ajax : function(e)
    {
		var self = this;
		e.find('a').each(function() {
			var url = $(this).attr('href');
			
			$(this).attr('href', 'javascript:;');
			
			$(this).unbind('click').bind('click', function() {
				$.get(url.replace('callback', ''), function(t) {
					e.html(t);
					self.Ajax(e);
				});
			});
		});
	}
}