﻿

(function($) {

	$.fn.jSquared = function(settings) {
		var settings = $.extend({}, $.fn.jSquared.defaults, settings);
	
		this.each(function() {
			var $t = $(this);
			init($(this), settings);
		});
		
		function init(k, o) {
			var $t = k;
		    
			$t.find('tr').find('td:eq(1)').each( function() {
				var $h = $(this);
				var text = $h.html();
				
				var newH =  renderRowHeader(text,o);
				$h.empty();
				$h.append(newH)
				//$h.attr('display','none');
				registerRowCellHandlers($h,o);
			});
		
			
			
			$('#addSanpham').parents('td:first').find('input:two').keydown( function(e) { if (e.keyCode==46) {onAddRow( $(this).parents('td:first'), o); return false;} });;
		}
		
		function onRemoveCol(cell, o) {
			var $t = cell.parents('table.jSquaredTable:first');
			var idx = cell.parents('tr:first').find('th').index(cell);
			o.onRowRemoving(cell);
			$t.find('tr:first').find('th:eq('+idx+')').remove();
			$t.find('tr:not(:last)').find('td:eq('+idx+')').remove();
			fixLastRow($t);
		}
		
		function renderColumnHeader(text, o) {
			return '<th class="jSquaredColumnText"><span class="text">'+text+'</span><a class="jSquaredColumnRemove" href="javascript:void(0)">'+o.removeRowText+'</a></th>';
		}
		function fixLastRow(t) {
			// fix cell for last row
			var colCount = t.find('tr th').length;
			t.find('tr:last td.jSquaredColspan').attr('colspan', colCount-1);
		}
		
		function registerColCellHandlers(cell,o) {
			cell.find('a.jSquaredColumnRemove').click( function() { onRemoveCol( $(this).parents('th:first'), o);	});
		}
		
		function onAddCol(cell, o) {
			var $t = cell.parents('table.jSquaredTable:first');
			var $addTh = cell;
			var $input = $addTh.find('input');
			var text = $input.val();
			if (text=='')
				return;			
			var thIndex = $addTh.parents('tr:first').find('th').index($addTh);
			// render col header
			var $newTh = $(renderColumnHeader(text,o));
			$newTh.insertBefore( $addTh.parents('tr:first').find('th:eq('+thIndex+')') );
			registerColCellHandlers($newTh,o);
			// render cells
			$t.find('tr:not(:first):not(:last)').each( function() {
				$(this).find('td:eq('+thIndex+')').before('<td>'+o.loadingContent+'</td>');
			});
			fixLastRow($t);
			
			if (o.clearInputAfterAdd)
				$input.val('').focus();
				
			o.onColumnAdded($newTh, text, $t.find('tr:not(:last)').find('td:eq('+thIndex+')') );
		}
		function registerRowCellHandlers(cell,o) {
			cell.find('a.jSquaredRowRemove').click( function() { onRemoveRow( $(this).parents('td'), o); });
		}		
		
		function renderRowHeader(text, o) {
			return '<span class="text" style="float:left; padding-left:5px;">'+text+'</span><a class="jSquaredRowRemove" style="float:right;width:20px;"href="javascript:void(0)">'+o.removeColumnText+'</a>';
		}
		
		function updateRowNumber(){
		    jQuery('#tbl_Main_XN tr').find('td:eq(0)').each(function(i){
		        jQuery(this).find('div').html(i);
		    }) ;
		
		}
		function onAddRow(cell, o){
			var $t = jQuery('#tbl_Main_XN') ;
			var $addTr = $t.find('tr:first');
			var $addTow = $t.find('tr:eq(1)');
			var clas = $addTow.attr('class');
			
			if(clas=='odd_gradeA')
			{
			    clas ='even_gradeA' ;
			}
			else
			{
			    clas ='odd_gradeA' ;
			}
			var $input = cell.find('input');
			var text = $input.val();
			var id = $input.attr('ma');
			if (text=='')
				return;
			// render row
			var colCnt = $t.find('tr:first td').length;
			var cells = '';
			for (var cellIdx=0; cellIdx<(colCnt-2); cellIdx++)
				cells += '<td>'+(cellIdx==(colCnt-2) ? '' : o.loadingContent) +'</td>';
			var $newRow = $('<tr class="'+clas+'"><td style="background-color:#4297D7"><div class="ui-widget-header">1</div></td><td>'+renderRowHeader(text,o)+'</td>'+cells+'</tr>').attr('id',id);
			
			
			registerRowCellHandlers($newRow.find('td:eq(1)'),o);
			$newRow.insertAfter($addTr);
			
			updateRowNumber() ;
			if (o.clearInputAfterAdd)
				$input.val('').focus();
			
			o.onRowAdded($newRow.find('td:first'), text, $newRow.find('td:not(:first)') );
		}
		function onRemoveRow(cell, o) {
			var $t = cell.parents('table.jSquaredTable:first');
			o.onColumnRemoving(cell);
			cell.parents('tr:first').remove();
			updateRowNumber() ;
			tinhtien(); 
		}
		
		
	};
	
	//Default configuration:
	$.fn.jSquared.defaults = {
		addColumnText : 'Add',
		addRowText : 'Add',
		removeColumnText : '[x]',
		removeRowText : '[x]',
		topLeftCellText : '',
		clearInputAfterAdd : true,
		onRowAdded  : function(textCell, text, dataCells) { } ,
		onColumnAdded  : function(textCell, text, dataCells) { },
		onColumnRemoving : function(textCell) { },
		onRowRemoving : function(textCell) { },
		loadingContent : 'loading...'
	};
	
	//Current version:
	$.fn.jSquared.version = 0.1;

})(jQuery);
