/**
 * 初始化selector
 * @author poly 89490364a@gmail.com
 */
(function($){
	$.fn.initSelector=function()
	{
		this.each(function(){
			var that=$(this);
			if (!that.data("initComplete")) {
				var popwin = $(this).bind("onchange", triggerEvent).parents(".popwin").bind("confirm", save);
				$(".vs", this).bind("click", chageSelect);
				$(".eListener", this).bind("selectorChanged", refresh);
				$("#filter :text", this).bind("keyup", filtKeyword).bind("focus", function(){
					$(this).val("").trigger("keyup")
				});
				$("#result", this).bind("filt", dofilt);
				$("button", this).bind("click", function(){
					$(this).trigger("confirm")
				});
				that.data("initComplete", true);
			}
		});
		return this;
	}
	
	/**
	 * 分发事件到监听器。
	 */
	function triggerEvent(e)
	{
		var that=$(this);
		var event=new jQuery.Event(e.eventype);
		event.param=e.param;
		event.href=that.attr("href")
		$(".eListener",this).trigger(event);
		e.stopPropagation();
	}
	
	/**
	 * 选择器onchange处理。
	 */
	function chageSelect(e)
	{
		var that=$(this);
		var target=$(e.target);
		if(!target.hasClass("current") && target.attr("value"))
		{
			$(".current",this).removeClass("current");
			target.addClass("current");
			var event=new jQuery.Event("onchange");
			event.param={type:that.attr("id"),id:target.attr("value")};
			event.eventype=that.attr("eventype");
			that.trigger(event);
		}
		e.stopPropagation();
	}
	
	/**
	 * 更新数据。
	 */
	function refresh(e)
	{
		var that=$(this);
		if(e.target==this && e.param.type==that.attr("capture"))
		{
			e.param.self=that.attr("id");
			that.load(e.href,e.param);
		}
		e.stopPropagation();
	}
	
	/**
	 * 根据关键字过滤
	 */
	function filtKeyword(e)
	{
		var that=$(this);
		if(that.data("timer"))
		{clearTimeout(that.data("timer"));}
		setTimeout(function(){
			var event=new jQuery.Event("onchange");
			event.eventype="filt";
			event.param={kw:that.val().replace(/^\s+|\s+$/g,"")};
			that.trigger(event);
		},400);
		e.stopPropagation();
	}
	
	/**
	 * 过滤结果
	 */
	function dofilt(e)
	{
		var that=$(this);
		var labels=$("label",this);
		if(e.param.id)
		{
			var temp=labels.filter("[filtId="+e.param.id+"]")
			if(temp.length)
			{
				labels.hide();
				temp.show();
			}
			else
			{
				labels.show();
			}
		}
		else if(e.param.kw)
		{
			labels.hide().filter(function(){
				var that=$(this);
				return that.contents().not("[nodeType=1]")[0].nodeValue.indexOf(e.param.kw)>-1;
			}).show();
		}
		else
		{
			labels.show();
		}
		e.stopPropagation();
	}
	
	/**
	 * 保存选择结果
	 */
	function save(e)
	{
		var that=$(this);
		var names=[];
		var ids=[];
		var labels=$("label:visible",this).filter(function(){
			return $(":checkbox",this).attr("checked");
		});
		labels.each(function(){
			var that=$(this);
			names.push(that.contents().not("[nodeType=1]")[0].nodeValue);
			ids.push($(":checkbox",this).val());
		});
		that.data("relation").val(names.join(" "));
		that.data("relation").next().val(ids);
		that.trigger("popdown");
		e.stopPropagation();
	}
})(jQuery);

	$(function(){

	});

