(function($){
    var methods={
        init:function(options){
            return this.each(function(){
                var $this=$(this);
                var data=$this.data('shnGrid');
                if(!data){
                    $(this).data('shnGrid',{
                        target:$this,
                        opt:options
                    });
                }
            });
    },
    arrangex:function(){
        return this.each(function(){
            var offy=0;
            var offx=0;
            var maxHeight=0;
            var maxy=new Array();
            var $this=$(this);
            var data=$this.data('shnGrid');
            if(data.opt.offy){
                offy=data.opt.offy;
            }
            if(data.opt.offx){
                offx=data.opt.offx;
            }
            var count=0;
            for(x=0;x<data.opt.columns;x++){
                maxy[x]=0;
            }
            $(data.opt.elements+'',$this).each(function(i){
                count++;
                var pos,cursor;
                cursor=0;
                var el=$(this);
                //e.addClass('elleeeemee');
                for(x=0;x<data.opt.columns;x++){
                    cursor=maxy[x]<maxy[cursor]?x:cursor;
                }
                $(this).css({
                    'position':'absolute',
                    'margin-left':cursor*(el.width())+offx,
                    'margin-top':maxy[cursor]+offy
                    });
                maxy[cursor]+=$(this).outerHeight();
                if(maxy[cursor]>maxHeight){
                    maxHeight=maxy[cursor];
                }
            });
        $this.css({
            'height':maxHeight
        });
        });
}
};

$.fn.shnGrid=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.shnGrid');
    }
};

})(jQuery);
