// function which add design to tables
$(document).ready(function() {


        // Вешаем обработчик перед печатью (добавление ссылок)
        window.onbeforeprint = ShowLinks;



var tables = $('.wrapper #middle table').get();

for (var i = 0; i < tables.length; i++) {
	if (tables[i].className != 'simpletable' && tables[i].className != 'borderedtable' && tables[i].className != 'not_nice_table' && tables[i].className != 'ui-datepicker-calendar' && tables[i].className != 'bookmarks-table' && tables[i].className != 'search_table' && tables[i].className != 'contact_group' && tables[i].className != 'contact_form' && tables[i].className != 'contact_form_sendmail' && !$(tables[i]).hasClass('photo-photo-item-default') && !$(tables[i]).hasClass('photo-photo-item') ) {
		console.log(tables[i].className);
		//alert(tables[i].className);
		
		var id = 'nice_table' +i;
		
		// add id and class
		$(tables[i]).removeAttr('style');
		$(tables[i]).attr('id', id);
		$(tables[i]).attr('class', 'nice_table');
		$(tables[i]).attr('cellpadding', '1');
		$(tables[i]).attr('cellspacing', '0');
		$(tables[i]).attr('border', '0');
				
		// clear all td
		var td = $(tables[i]).find('td');
		for (var k = 0; k < td.length; k++) {
			$(td[k]).removeAttr('style');
			
			//TODO: add strip tags here
			//var tags = $(td[k]).get(0).innerHTML;
			//console.log(tags);
			//alert(tags);
		}
		
		var rowspan = 1;
		
		// add first row class
		var tr = $(tables[i]).find('tr')[0];
		$(tr).attr('class', 'head_row');
		
		// add first row td / th classes
		var head_cell = $(tr).find('th');
		if (head_cell.length <= 0) head_cell = $(tr).find('td');
		
		// check head cells for rowspan
		for (var l = 0; l < head_cell.length; l++) {
			if ($(head_cell[l]).attr('rowspan') > 1) { rowspan = $(head_cell[l]).attr('rowspan'); break; }
		}
		
		// take the right count of tr (if rowspan > 1)
		var table_trs = $(tables[i]).find('tr');
		//alert(table_trs.length);
		
		for (var x = 0; x < rowspan; x++) {
			// add first row class
			var tr = $(tables[i]).find('tr')[x];
			$(tr).attr('class', 'head_row');
			
			// add first row td / th classes
			var head_cell = $(tr).find('th');
			if (head_cell.length <= 0) head_cell = $(tr).find('td');

			for (var j = 0; j < head_cell.length; j++) {
				if (j == 0) {			
					var html = $(head_cell[j]).html();
					$(head_cell[j]).empty();
					$(head_cell[j]).removeAttr('style');
					$(head_cell[j]).attr('class', (x > 0 ? 'th' : 'th_first'));
					if (x == 0) $(head_cell[j]).append('<div class="th_first_bg" style="position: absolute; top: ' + $(head_cell[j]).position().top + 'px; left: ' + $(head_cell[j]).position().left + 
		'px;"></div>');
					$(head_cell[j]).append(html);
				}
				else if (j == head_cell.length - 1) {
					$(head_cell[j]).attr('class', (x > 0 ? 'th' : 'th_last'));
					if (x == 0) $(head_cell[j]).append('<div class="th_last_bg" style="position: absolute; top: ' + $(head_cell[j]).position().top + 'px; left: ' + Number($(head_cell[j]).position().left + $(head_cell[j]).outerWidth() - 2) + 'px;"></div>');
				}
				else $(head_cell[j]).attr('class', 'th');
			}
		}
	}
}


    $(".search-button").hover(function(){
$(this).toggleClass("hover_button");
        });


 $(".search-button").mousedown(function(){
$(this).removeClass("hover_button");
        });

        
    $(".search-button").click(function(){
$(this).toggleClass("click_button");

        });

 function ShowLinks () {
        window.location=window.location+"?print=Y";
        }
        
        
    
$(document).keydown(function(event) {

 if ((event.which == 80 && event.ctrlKey)){

        ///window.location=window.location+"?print=Y";
        //return false;
  }
});


});

