/*
 *  Project: 
 *  Description: 
 *  Author: 
 *  License: 
 */

;(function ( $, window, undefined ) {

  var pluginName = 'CheckboxRelated',  // checkall.CheckboxRelated(checks);
      document = window.document,
      methodHandler = ["destroy", "refresh"];

  function Plugin( element, checkboxes ) {
    this.element = $(element);
    this.checkboxes = checkboxes;
    this._clone = null;

    this._name = pluginName;

    this.init();
  }

  Plugin.prototype.init = function () {
    if(!this.checkboxes) return this;
    var that = this;
    that.element.prop("checked", that.checkboxes.filter(":checked").length > 0 && that.checkboxes.filter(":checked").length === that.checkboxes.filter(":enabled").length);
		that.clone(that.element);
    that.element.bind("change", function(){
      var checked = this.checked;
      that._clone && that._clone.prop("checked", checked);
      that.checkboxes.filter(":enabled").prop("checked", checked);
      if($.fn.CheckboxUI){
        that._clone.CheckboxUI("check") && that._clone.CheckboxUI("refresh");
        that.checkboxes.each(function(){
          $(this).CheckboxUI("check") && $(this).CheckboxUI("refresh");
        });
      }
    });
    that.checkboxes.bind("change", function(){
      var checked = that.checkboxes.filter(":checked:enabled").length === that.checkboxes.filter(":enabled").length;
      that.element.prop("checked", checked);
      that._clone && that._clone.prop("checked", checked);
      if($.fn.CheckboxUI){
        that._clone.CheckboxUI("check") && that._clone.CheckboxUI("refresh");
        that.element.CheckboxUI("check") && that.element.CheckboxUI("refresh");
      }
    });
  };

  Plugin.prototype.clone = function(checkbox) {
    var that = this;
    if(typeof checkbox === "string") checkbox = $(checkbox);
    that._clone = checkbox;
    checkbox.bind("change", function(){
      var checked = this.checked, _checked = that.element.is(":checked");
      checkbox.not(this).prop("checked", this.checked);
      checked !== _checked && that.element.prop("checked", checked).trigger("change");
    });
  };

  $.fn[pluginName] = function ( options ) {
    if (typeof options == 'string') {
      var args = arguments, method = options, isHandler = function() {
        for(var i=0; i<methodHandler.length; i++) {
          if(methodHandler[i] === method) return true;
        }
        return false;
      };
      Array.prototype.shift.call(args);
      if(method == 'check') {
        return !!this.data('plugin_' + pluginName);
      } else if (isHandler()) {
        return this.each(function() {
          var _plugin = $(this).data('plugin_' + pluginName);
          if (_plugin && _plugin[method]) _plugin[method].apply(_plugin, args);
        });
      } else {
        var _plugin = this.first().data('plugin_' + pluginName);
        if (_plugin && _plugin[method]) {
          return _plugin[method].apply(_plugin, args);
        } else {
          throw new TypeError(pluginName + ' has no method "'+ method +'"');
        }
      }
    } else {
      return this.each(function() {
        var _plugin = $(this).data('plugin_' + pluginName);
        if (!_plugin) {
          $(this).data('plugin_' + pluginName, new Plugin( this, options ));
        }
      });
    }
  };

}(jQuery, window));