// smClipboardCopy [PLUGIN: Aller Grid data ]
// Author - Igor Szyporyn
// Owner - System & Metode A/S

(function($) {

    var parentPrefix = 'smClipboardCopy',
        prefix = 'allergrids',
        parent = $.fn[parentPrefix];

    // namespace taken - exit
    if (parent[prefix]) { return false; }

    var methods = {

        init    : function( options ) {

            var settings =  $.extend(true, {}, {
                                beforeCopy  : $.noop,
                                afterCopy   : $.noop   
                            }, options);

            return {

                name            : prefix,
                getItems        : function( element ) {

                    var newArray = [];

                    $.each( $(element).find('.x-grid3-row'), function ( i, v ) {
                        
                        var newObj = parent( 'createPluginBase', v );

                        newObj.onHighlight = function( isSelected ) {
                            var row = $(this.element),
                                clsName = 'x-grid3-row-selected';
                            if ( isSelected ) {
                                row.addClass( clsName );
                            } else {
                                row.removeClass( clsName );
                            }                            
                        };

                        newObj.getValue = function( isSelected ) {
                            if ( isSelected ) {
                                var vals = $(this.element).find('.x-grid3-cell-inner'),
                                    o;

                                vals.each(function( i, v ) {
                                  if (i === 0) {
                                    o = $(v).text();
                                  } else {
                                    o += '\t' + $(v).text();
                                  }
                                });

                                o += '\n'
                                
                            }
                            return isSelected ? o : false;
                        };

                        newArray.push(newObj);

                    });
                    return newArray;
                },
                onHighlight     : true
            }

        }

    };

    parent.allergrids = function( method ) {

        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.' + prefix );
        }

    }


  

})(jQuery);