// Jtables

function Jtables(header, myClassID)
{
$( myClassID +" TR:lt("+ (header) +")").children("TD").css({ 'text-align':'center', 'padding':'6px', 'font-size':'12px', 'font-weight':'bold' });

$( myClassID +" TR:eq("+ (header) +") TD").each(
					function(index)
					{
						$(this).attr({ 'h':'0' }).css({ 'cursor':'pointer' }).bind(
					{
							dblclick: function()
							{
									
									if( $(this).attr('h') != '1' )
									{
									$( myClassID +" TR:gt("+ (header-1) +")").each(
														  
									function()
												{
													$("TD:eq("+ index +")", this).attr({ 'h':'1' }).css({ 'background-color':'#FFFFEE' });	
												}														  
														  );
								}
								else
								{
									$( myClassID +" TR:gt("+ (header-1) +")").each(
														  
									function()
												{
													$("TD:eq("+ index +")", this).attr({ 'h':'0' }).css({ 'background-color':'' });	
												}														  
														  );
								}
							}							
						}
						);
					}
);

$(myClassID +" TR:gt("+ (header-1) +")").each(
	function(index) {
		if( (index%2)==0 )
		{
			$(this).css({ 'background':'#F4F4F4' });	
		}
		$(this).attr({ 'c':$(this).css('background-color'), 't':'0' });
		
		$(this).hover(
					  function() { if( $(this).attr('t') != '1') { $(this).css({ 'background':'#CDE0F1' }); } },
					  function() { if( $(this).attr('t') != '1') { $(this).css({ 'background':$(this).attr('c') }); } }
					  );
		
		$(this).toggle(
					 function()
					 {
							$(this).css({ 'background':'#CDE0F1' }).attr({ 't':'1' });
					 },
					 function()
					 {
							$(this).css({ 'background':$(this).attr('c') }).attr({ 't':'0' });
					 }
		);
		
		$("TD", this).each(
							function()
			{
					if($(this).html() == '<p>-</p>' || $(this).html() == '-' || $(this).html() == '<P>-</P>')
					{
						$(this).css({ 'text-align':'center' });
					}
			}
		);
	}
					  
);
$(myClassID).append("<div class='jtable_tools'><div><a href='#' class='jtable_tools_link' inverttoolWith='"+ myClassID +"'>Инвертировать выделение</a></div><div>|</div><div><a href='#' class='jtable_tools_link' printtoolWith='"+ myClassID +"'>Отправить выбранное на печать</a></div><div>|</div><div><a href='#' class='jtable_tools_link' pdftoolWith='"+ myClassID +"'>Скачать PDF</a></div><div style='clear:left; float:none;'>&nbsp;</div></div>");

$("A[inverttoolWith='"+ myClassID +"']").bind({ click: function() {
			
			$( myClassID +" TR:gt("+ (header-1) +")").each( function() { $(this).click(); });
			return false;								  
						} });

$("A[printtoolWith='"+ myClassID +"'], A[pdftoolWith='"+ myClassID +"']").bind({ click: function() {
			$(".jtable_print_container").remove();
			$(".jtable_pdf_container").remove();
			$("BODY").append("<div class='jtable_print_container'><table border='1' cellpadding='5'></table></div>");
			
			$(myClassID +" TR:lt("+ (header) +")").each( function() { var jtable_printTable = $(this).html(); $(".jtable_print_container TABLE").append("<tr>"+ jtable_printTable +"</tr>"); } );
			
			$(myClassID +" TR:gt("+ (header-1) +")").each( function() { 
						
						if($(this).attr('t') == '1')
						{
						 var jtable_printTable = $(this).html();
						$(".jtable_print_container TABLE").append("<tr>"+ jtable_printTable +"</tr>");
						}
																	 
																	 });
			if($(this).attr('printtoolWith'))
			{
			open_jtable_printWindow = open("", "displayWindow", "status=no, resizable=yes, scrollbars=yes, toolbar=no, menubar=no, width="+($(myClassID + " TABLE").width()+30));
			open_jtable_printWindow.document.open();
			open_jtable_printWindow.document.write("<html><head><title>Печать выбранного из таблицы</title><style>TABLE { font-family:Arial; font-size:11px; border:1px solid #000; border-collapse:collapse; } TD { border:1px solid #000; border-collapse:collapse; }</style></head>");
			open_jtable_printWindow.document.write("<body><h1>"+ ($(myClassID +" H2").html() ? $(myClassID +" H2").html() : '') + "</h1>" + $(".jtable_print_container").html() +"<br><input type='button' value='Печатать' onClick='print();'></html>");
			open_jtable_printWindow.document.close();
			}
			
			if($(this).attr('pdftoolWith'))
			{
				$("BODY").append("<div class='jtable_pdf_container'><form action='/pdf/' method='POST' enctype='multipart/form-data' target='_blank'><textarea name='jtable_pdftool_container'><h1>"+ ($(myClassID +" H2").html() ? $(myClassID +" H2").html() : '') + "</h1>" + $(".jtable_print_container").html() +"</textarea><input id='printpdf_toolform' type='submit'></form></div>");
				$("#printpdf_toolform").click();
				$(".jtable_pdf_container").remove();
				}
			
			return false;								  
						} });



}
