(function($) {
	$.fn.cssTable = function(options) {
		var defaults = {
			setClass	 		: 'set',
			setTitleClass		: 'set-title',
			setTitleArrowClass  : 'set-title-arrow',
			setContentClass		: 'set-content',
			rowClass			: 'row',
			columnClass			: 'col',
			thContentClass		: 'th-content',
			thClosureClass		: 'th-closure',
			tdContentClass		: 'td-content',
			arrowDown			: '&#x25BC;',
			arrowRight			: '&#x25BA;',
			printHeaderClosures : true
		};
		
	    var options = $.extend(defaults, options);
	    
	    $('.'+options.setClass).each(function(){
	    	if($(this).hasClass('collapsible')) {
	    		$('.'+options.setTitleClass,$(this)).css('cursor','pointer');
	    		var arrow = "<span class='"+options.setTitleArrowClass+"'>"+options.arrowDown+"</span>"; 
	    		
	    		$('.'+options.setTitleClass,$(this)).html(arrow+$('.'+options.setTitleClass,$(this)).html());
	    		$('.'+options.setTitleClass,$(this)).click(function(){
	    			if($('.'+options.setContentClass,$(this).parent()).css('display')=='block') {
	    				$('.'+options.setTitleArrowClass,$(this)).html(options.arrowRight);	
	    			} else {
	    				$('.'+options.setTitleArrowClass,$(this)).html(options.arrowDown);
	    			}
	    			$('.'+options.setContentClass,$(this).parent()).slideToggle();
	    	    });
	    	}
	    })
    	
	    this.each(function() {  
	    	var table = $(this);
	    	var rowCount = 1;
	    	var colCount = 1;
	    	var widths = new Array();
	    	var heights = new Array();
	    	var set = new Array();
	    	// Larghezza massima del contenuto
	    	var maxWidth = $(this).width();
	    	//maxWidth = maxWidth - 10;
	    	/*
	    	maxColumns = 0;
	    	$('.'+options.rowClass,$(this)).each(function() {
	    		var rowColumns = $('.'+options.columnClass,$(this)).size();
	    		maxColumns = maxColumns > rowColumns ? maxColumns : rowColumns;
	    	})
	    	maxWidth = maxWidth-maxColumns*4;
	    	*/
	    	$('.'+options.columnClass,table).css('display','block');
	    	$('.'+options.columnClass,table).css('float','left');
	    	$('.'+options.columnClass,table).css('vertical-align','middle');
	    	
	    	$('.'+options.rowClass,$(this)).each(function() {
	    		$(this).append('<div style="clear:both"></div>')
	    		$(this).attr('class',$(this).attr('class')+' row-'+rowCount);

	    		colCount = 1;
	    		$('.'+options.columnClass,$(this)).each(function(){
			    	$(this).attr('class',$(this).attr('class')+' col-'+colCount);
	    			colWidth  = parseInt($(this).css('width'));
	    			colHeight = parseInt($(this).css('height'));

	    			if($(this).hasClass('th')) {
			    		var thContent = $(this).html();
			    		$(this).html('');
			    		$(this).append('<div class="'+options.thContentClass+'">'+thContent+'</div>');
			    		if(options.printHeaderClosures) {
			    			$(this).append('<div class="'+options.thClosureClass+'">&nbsp;</div>');
			    		}
			    		$(this).append('<div style="clear:both;"></div>');
			    		colWidth = colWidth+parseInt($('.'+options.thClosureClass,$(this)).css('width'));
			    	}
		    		if($(this).hasClass('td')) {
			    		var tdContent = $(this).html();
			    		$(this).html('');
			    		$(this).append('<div class="'+options.tdContentClass+'">'+tdContent+'</div>');
			    		colWidth = colWidth+9;
			    	}    		
		    		widths[colCount] = widths[colCount] > colWidth ? widths[colCount] : colWidth;
		    		heights[rowCount] = heights[rowCount] > colHeight ? heights[rowCount] : colHeight;

		    		var className = $(this).attr('class');
	    			var s = className + '';
		    		var regex = /span-(\d+)/i;
		    		
		    		if(s.match(regex)) {
		    			var span = s.match(regex)[1];
		    			colCount = parseInt(colCount)+parseInt(span);
		    		} else {
			    	 	colCount++;
		    		}
		    	});
	    		rowCount++;
	    	});
	    	
	    	maxWidth = maxWidth - (2*colCount);
	    	// Verifico se la larghezza di tutte le colonne è minore della larghezza massima 
	    	var totWidth = 0;
	    	$(widths).each(function(index, value) {
	    		if(value) {
	    			totWidth = totWidth + value;
	    		}
	    	});

		    
		    var maxColumns = 0;
	    	$('.'+options.rowClass,$(this)).each(function() {
	    		var rowColumns = $('.'+options.columnClass,$(this)).size();
	    		maxColumns = maxColumns > rowColumns ? maxColumns : rowColumns;
	    	})
	    	
	    	
	    	// Se la somma delle larghezze delle colonne è maggiore della larghezza totale
	    	// ridefinisco le larghezze delle colonne in percentuale
    		$(widths).each(function(index, value) {
    			if(value) {
    				// Determino la larghezza in pixel
    				var columnWidth = 0;
    				columnWidth = (maxWidth / totWidth) * value;
    				widths[index] = Math.round(columnWidth*Math.pow(10,0))/Math.pow(10,0);
    			}
    		});
	    		
	    	$(widths).each(function(index, value) {
	    		var ind = index;
	    		var val = value;
	    		
	    		$('.col-'+index,table).each(function(){
	    			var value = 0;
	    			var className = $(this).attr('class');
	    			var s = className + '';
		    		var regex = /span-(\d+)/i;
		    		
		    		if(s.match(regex)) {
		    			var span = s.match(regex)[1];
		    			for(var i=0; i<=span-1; i++) {
			    			if(widths[ind+i]) {
			    				value = value + widths[ind+i];
			    			}
			    		}
		    		} else {
		    			value = val;
		    		}
		    		
		    		var borderSize = 0;
		    		if(!isNaN(parseInt($(this).css('border-left-width'))))
		    			borderSize = borderSize + parseInt($(this).css('border-left-width'));
		    		if(!isNaN(parseInt($(this).css('border-right-width'))))
		    			borderSize = borderSize + parseInt($(this).css('border-right-width'));
	    			 
		    		value = value-borderSize;
		    		
		    		var thAdjust = parseInt($('.'+options.thContentClass,$(this)).css('padding-left')) + 
		    				   	   parseInt($('.'+options.thClosureClass,$(this)).css('width'));
		    		
		    		$('.'+options.thContentClass,$(this)).css('width',(value-thAdjust)+'px');
		    		$(this).css('width',(value)+'px');
	    		})
	    	});
	     
	    	$('.'+options.setClass).each(function() {
	    		
	    		var setPadding = 0;
	    		if(!isNaN(parseInt($('.'+options.setContentClass,this).css('padding-left'))))
	    			setPadding = setPadding + parseInt($('.'+options.setContentClass,this).css('padding-left'));

	    		if(!isNaN(parseInt($('.'+options.setContentClass,this).css('padding-right'))))
	    			setPadding = setPadding + parseInt($('.'+options.setContentClass,this).css('padding-right'));
	    					   
	    		if(!isNaN(parseInt($('.'+options.setContentClass,this).css('border-left-width'))))
	    			setPadding = setPadding + parseInt($('.'+options.setContentClass,this).css('border-left-width'));

	    		if(!isNaN(parseInt($('.'+options.setContentClass,this).css('border-right-width'))))
	    			setPadding = setPadding + parseInt($('.'+options.setContentClass,this).css('border-right-width'));
	    			
	    		if(!isNaN(parseInt($(this).css('padding-left'))))
	    			setPadding = setPadding + parseInt($(this).css('padding-left'));
	    	
	    		if(!isNaN(parseInt($(this).css('padding-right'))))
	    			setPadding = setPadding + parseInt($(this).css('padding-right'));
	    			
	    		if(!isNaN(parseInt($(this).css('border-left-width'))))
		    		setPadding = setPadding + parseInt($(this).css('border-left-width'));
		    	    	
			    if(!isNaN(parseInt($(this).css('border-right-width'))))
			    	setPadding = setPadding + parseInt($(this).css('border-right-width'));
			    
				$('.col-1',$(this)).each(function(){
		    		$(this).css('width',parseInt($(this).css('width'))-setPadding+'px');
					$('.'+options.thContentClass,this).css('width',(parseInt($('.'+options.thContentClass,this).css('width'))-setPadding)+'px'); 
				});
			    
			    /*
			    var adjust = Math.floor(setPadding/maxColumns);
		    	$('.'+options.rowClass,$(this)).each(function() {
		    		$('.'+options.columnClass,$(this)).each(function() {
			    		//$(this).css('width',(parseInt($(this).css('width'))-adjust)+'px'); 
			    		//$('.'+options.thContentClass,this).css('width',(parseInt($('.'+options.thContentClass,this).css('width'))-adjust)+'px'); 
			    	
			    		$('.'+options.thContentClass,this).css('width',(parseInt($('.'+options.thContentClass,this).css('width'))-setPadding)+'px'); 
			    		$('.'+options.thContentClass,this).parent().css('width',parseInt($('.'+options.thContentClass,this).parent().css('width'))-setPadding+'px');
			    	})
		    	})
		    	*/
	    	})

	    	$('.'+options.thContentClass,table).css('float','left');
	    	$('.'+options.thContentClass,table).css('white-space','nowrap');
	    	$('.'+options.thClosureClass,table).css('float','right');
		});  
	    
	    $('.'+options.setClass).each(function() {
	    	if($(this).hasClass('collapsed')) {
	    		$('.'+options.setContentClass,$(this)).hide();
	    		$('.'+options.setTitleArrowClass,$(this)).html(options.arrowRight);	
	    	}
	    })
	};
})(jQuery);
