/////////////////////////
// TABLES
/////////////////////////
$(function(){

	var myclass ='';
	$(myclass+' .standard colgroup').prepend('<col style="width:8px;"></col>');
	$(myclass+' .standard colgroup').append('<col style="width:8px; "></col>');
	$(myclass+' .standard tbody tr:not(.head)').prepend('<td class="first">&nbsp;</td>');
	$(myclass+' .standard tbody tr:not(.head)').append('<td class="last">&nbsp;</td>');
	$(myclass+' .standard tbody tr.head th').removeClass('first');
	$(myclass+' .standard tbody tr.head').prepend('<th class="first" >&nbsp;</th>');
	$(myclass+' .standard tbody tr.head').append('<th class="last txtM">&nbsp;</th>');
	$(myclass+' .collapsible th.last').html( '');
	$(myclass+' .collapsible th.last').append('<a href="#" class="switch"><span>Open/Close</span></a>');

	if( $( 'table:not(.ajaxed) td').size() > 0  ){
		$( myclass+' td').each(function(i , val){
			var cell_v = $( myclass+' td').eq(i).html();
			if( jQuery.trim(cell_v) == '' )
				$( myclass+' td').eq(i).prepend(' &nbsp; ');
		});
	}
	if( $( 'table:not(.ajaxed) th').size() > 0  ){
		$( myclass+' th').each(function(i , val){
			var cell_v = $( myclass+' th').eq(i).html();
			if( jQuery.trim(cell_v) == '' )
				$( myclass+' th').eq(i).prepend('&nbsp;');
		});
	}

  // РўР°Р±Р»РёС†Рё - СЃР»Р°РіР° РєР»Р°СЃ РЅР° РїСЉСЂРІРё Рё РїРѕСЃР»РµРґРµРЅ С‡Р°Р№Р»Рґ
  //$('table.standard td:first-child, table.standard th:first-child').addClass('first');
  //$('table.standard td:last-child, table.standard th:last-child, table.standard tr:last-child').addClass('last');
  //$('table.standard tr:last-child').addClass('last');
  //$('.last .bubble').addClass( 'bubbleAlt' );
});


/////////////////////////
// COLLAPSIBLE TABLES
/////////////////////////
$(function(){

	$('.clickable').live( 'click' , function(ev) {
		var ix = $('.clickable').index(this);
		ev.preventDefault();
		$('.clickable').eq(ix).toggleClass('closed');
	});

	$('table tr:last-child').addClass('last');

	var cSpan = $('table tbody:first tr:first-child').children().size();



	$('tbody .switch').closest('tr').addClass('clickable');

	$('tbody .switch').closest('tr').click(function(ev) {
		ev.preventDefault();
		$(this).closest('tbody').toggleClass('closed');
	});

	// patch for preventing closed harmonic to absolutly closed ?!
	if( $('.collapsible tbody').hasClass('closed') ){
		$('.collapsible .closed').append('<tr class="vSpace"><td colspan="'+cSpan+'"></td></tr>');
	}else{
		$('.collapsible .switch').not(':first').closest('tbody').addClass('closed');
		size =$('.closed tr').eq(0).children().size();
		cSpan = 0;
		for( var k =0; k<size; k++){
			cSpan +=$('.closed tr').eq(0).children().eq(k).attr('colspan');
		}
		$('.closed').append('<tr class="vSpace"><td colspan="'+cSpan+'"></td></tr>');
	}

	$('tbody:not(.closed) .switch').closest('tr').toggle(function() {		
			$(this).closest('tbody').append('<tr class="vSpace"><td colspan="'+cSpan+'"></td></tr>');
		},function() {
			$(this).closest('tbody').children('.vSpace').remove();
	});

	$('tbody.closed .switch').closest('tr').toggle(function() {
			$(this).closest('tbody').children('.vSpace').remove();
		},function() {
			$(this).closest('tbody').append('<tr class="vSpace"><td colspan="'+cSpan+'"></td></tr>');
	});


});
