/**
 * For jExpand : Table row expantion
 */
(function($){
    $.fn.jExpand = function(url, action){
        var element = this;

        $(element).find("tr:odd").addClass("odd");
        $(element).find("tr:not(.odd)").hide();
        $(element).find("tr:first-child").show();

        $(element).find("tr.odd").click(function() {
            if ( $(this).find(".arrow").hasClass("up") ) {
                $(this).next("tr").html("");
                $(this).next("tr").toggle();
                $(this).find(".arrow").toggleClass("up");
            } else {
                $(this).find(".arrow").toggleClass("refresh");
                if ( action == "getPropertyProductRow" ) {
                    var data = {
                        action: action,
                        prop_key: $(this).find("td:eq(1)").text(),
                        catId: $(this).find("td:eq(4)").text()
                    };
                } else if ( action == "getAmazonBatchDetailRow" ) {
                    var data = {
                        action: action,
                        batchNo: $(this).find("td:eq(1)").text(),
                        amzId: $(this).find("td:eq(3)").text()
                    };
                } else if ( action == "getNominalItemDetailRow" ) {
                    var data = {
                        action: action,
                        bType: $(this).find("td:eq(3)").text(),
                        nNo: $(this).find("td:eq(4)").text()
                    };
                } else {
                    var data = {
                        action: action,
                        batchNo: $(this).find("td:eq(1)").text()
                    };
                }
                $(this).next("tr").load(url, data, function() {
                    bindBehaviors(this);
                    $(this).toggle();
                    $(this).prev("tr").find(".arrow").toggleClass("refresh");
                    $(this).prev("tr").find(".arrow").toggleClass("up");
                });
            }            
        });

    }
})(jQuery);


/**
 * mbTableSelector : My table row selector
 *
 */
(function($){

  var defaults = {
    tableClass: "tableselect",
    rowSelectClass: "selected",
    multiSelect: true
  };
  var opts;

  var methods = {
    init : function( options ) {
        opts = $.extend( defaults, options );
        return this.each(function() {
            var tableId = $(this).attr('id');
            // console.log( 'init ' + tableId);
            $("tbody tr", this).live('click', select_row);
            $("tbody tr", this).data("tableId", tableId);
            $("thead tr input", this).bind('click', select_all);
            $("thead tr input", this).data("tableId", tableId);
        });
    },
    getSelectedRows : function(table) {
        return $("tbody tr." + opts.rowSelectClass, this);
    }
  };

  $.fn.mbTableSelector = function( method ) {
    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ) );
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.mbTableSelector' );
    }
  };

  select_row = function() {
    var tableId = $(this).data("tableId");
    // console.log( 'click row function ' + tableId);
    $(this).toggleClass(opts.rowSelectClass);
    if ( $(this).hasClass(opts.rowSelectClass) ) {
        $(this).find('input').attr('checked', 'checked');
    } else {
        $(this).find('input').removeAttr('checked');
    }
    $(document).trigger('rowchange', tableId);
  }
  select_all = function() {
    var tableId = $(this).data("tableId");
    var isChecked = $("#" + tableId + " thead tr input").attr('checked');
    // console.log( 'click all function ' + tableId + " " + isChecked);
    $("#" + tableId + " tbody tr").each(function() {
        if (isChecked) {
          if ( ! $(this).hasClass(opts.rowSelectClass) ) {
              $(this).toggleClass(opts.rowSelectClass);
              $(this).find('input').attr('checked', 'checked');
          }
        } else {
          if ( $(this).hasClass(opts.rowSelectClass) ) {
              $(this).toggleClass(opts.rowSelectClass);
              $(this).find('input').removeAttr('checked');
          }
        }
    });
  }
})( jQuery );
