;(function($){
	$.fn.adaptableGrid2 = function(options){
		var opts = $.extend({
			'getCount': function(size){
				if (size < 1300) {
					if (size < 1150) {
						return 3;
					}else{
						return 4;
					}
				}
				
				if (size >= 1300 && size < 1600) {
					if (size < 1450) {
						return 5;
					}else{
						return 6;
					}
				}
				
				if(size >= 1600){
					return 7;
				}
			}
		},options);
		
		var child1Tag = 'tr';
		var child2Tag = 'td';
		
		
		
		var reorder = function(container, items, count){
			var current = null;
			var newRows = [];
			
			for(var i in items){
				if(i % count == 0){
					current = {
						imgRow: $(document.createElement(child1Tag)).addClass('imgRow'),
						dataRow: $(document.createElement(child1Tag)).addClass('infoRow'),
						dataRow2: $(document.createElement(child1Tag)).addClass('infoRow2')
					};
					newRows.push(current);
				}
				if(items.length > 2){
					items[i].img.css('width',  parseInt(100 / count) + '%');
					items[i].data.css('width', parseInt(100 / count) + '%');
					items[i].data2.css('width', parseInt(100 / count) + '%');
				}
				current.imgRow.append(items[i].img);
				current.dataRow.append(items[i].data);
				current.dataRow2.append(items[i].data2);
			}
			
			container.empty();
			for(var i in newRows){
				container.append(newRows[i].imgRow);
				container.append(newRows[i].dataRow);
				container.append(newRows[i].dataRow2);
			}
		};
		return $(this).each(function(){
			var me = $(this);
			if(me.is('table')){
				me = me.find('tbody');
			}
			
			me.attr('currentcount', 0);
			var onResize = function(){
                            var items = [];
                            var imgItems = me.find(child1Tag + '.imgRow').children(child2Tag);
                            var infoItems = me.find(child1Tag + '.infoRow').children(child2Tag);
                            var infoItems2 = me.find(child1Tag + '.infoRow2').children(child2Tag);

                            var itemCount = imgItems.size();
                            for(var i = 0; i < itemCount; i++){
                                    items.push({
                                            img: imgItems.eq(i),
                                            data: infoItems.eq(i),
                                            data2: infoItems2.eq(i)
                                    });
                            }

                            var containerWidth = $(window).width();
				var count = opts.getCount(containerWidth);
				//$('#left').text(count + " min: " + containerWidth + " | current: " + me.attr('currentcount'));
				if (parseInt(me.attr('currentcount')) != count || $.adaptableGrid2.needUpdate == true) {
					me.attr('currentcount', count);
					reorder(me, items, count);
				}
			}
			
			$(window).resize(onResize);
			onResize();
		});
	};

        $.adaptableGrid2 = {};
        $.adaptableGrid2.update = function(){
            $.adaptableGrid2.needUpdate = true;
            $(window).trigger('resize');
            $.adaptableGrid2.needUpdate = false;
        }

}(jQuery));
