/*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1. 插件名称：tagswitch
 2. 插件描述：tag形式的复选框
 3. 版本：1.0
 4.  对其他插件的依赖：无
 5. 作者：zhaohuagang@guanaihui.com
 6. 未尽事宜：
 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
(function($) {
    $.fn.tagswitch = function(options) {
        /*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
        tagswitch加上对应的配置属性
         -----------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
        var opts = $.extend({}, $.fn.tagswitch.defaults, options);
        /*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
         执行事件添加后返回
         -----------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
        return this.each(function() {
            var tagContainer = this ;            
            $(this).find(".tag-switch").each(function(){
                var tag = $.fn.tagswitch.createTag(this) ;
                $(this).replaceWith(tag) ;
                $(tag).click(function(){
                    $.fn.tagswitch.tagClick(this) ;
                    $.fn.tagswitch.total(tagContainer, opts) ;
                }) ;
            }) ;                  
            
        });
    } ;
    /*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    点击tag的时候的处理
     -----------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
    $.fn.tagswitch.tagClick = function(tag) {
        $.fn.tagswitch.switchTag(tag, $(tag).find(".sign"), ! $(tag).hasClass("on")) ;
        var dataRole = $(tag).attr("data-role") ;       
        if(dataRole === "captain") {                    
            $(tag).siblings(".tag-switch").each(function(){
                $.fn.tagswitch.switchTag(this, $(this).find(".sign"), $(tag).hasClass("on")) ;
            }) ;
        }
        else {
            var memberTags = $(tag).parent().children(".tag-switch[data-role!='captain']") ;
            var checkedMemberAmount = $(memberTags).filter(".on").size() ;                    
            var captain = $(tag).siblings(".tag-switch[data-role='captain']") ;
            $.fn.tagswitch.switchTag(captain, $(captain).find(".sign"), checkedMemberAmount === $(memberTags).size()) ;                     
        }
    } ;
    /*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    创建tag节点
     -----------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
    $.fn.tagswitch.createTag = function(basicCheckbox) {
        var tag = $(document.createElement("A")).addClass("tag-switch").attr("href", "javascript:void(0);").attr("data-value", $(basicCheckbox).attr("value")) ;            
        var sign = $(document.createElement("SPAN")).addClass("sign").append($(document.createElement("I")).addClass("iconfont icon-gouxuan")) ;
        $(tag).append($(document.createElement("SPAN")).addClass("description").html($(basicCheckbox).attr("data-label"))).append(sign) ;
        if($(basicCheckbox).attr("data-role") === "captain") $(tag).attr("data-role", "captain") ;
        $.fn.tagswitch.switchTag(tag, sign, $(basicCheckbox).is(":checked")) ;
        return tag ;
    } ;
    /*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
     选择/取消选择某个tag
     -----------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
    $.fn.tagswitch.switchTag = function(tag, sign, status) {
        if(status) {
            $(tag).addClass("on") ;
            $(sign).css({ "visibility" : "visible" }) ;
        }
        else {
            $(tag).removeClass("on") ;
            $(sign).css({ "visibility" : "hidden" }) ;
        }
    } ;
    /*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    汇总选中的cbx
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
    $.fn.tagswitch.total = function(tagContainer, opts) {
        var valueArray = new Array ;
        $(tagContainer).find(".tag-switch").each(function(){
            if($(this).hasClass("on") && $(this).attr("data-role") !== "captain") valueArray.push($(this).attr("data-value")) ;
        }) ;
        $(opts.dataSaveInto).val(valueArray.join(opts.separator)) ;
    } ;
    /*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
     tagswitch默认配置
     -----------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
    $.fn.tagswitch.defaults = {       
        /*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
        选中的值存放在哪个隐藏域里面
        -----------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
        dataSaveInto : null ,
        /*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
        选中的值保存的时候的分隔符
        -----------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
        separator : ","
        
    } ;
})(jQuery) ;