/**
 * Скрытие блока с установкой ссылки для показа/скрытия
 * @author Pavel Alexandrov <pavel@vmt.ru>
 * @copyright Copyright (c) 2010, Pavel Alexandrov
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU Public License (GPL) Version 2, June 1991
 */
(function($){$.fn.cutter=function( options ){
	/**
	 * options.selector - седектор скрываемого блока. Строка.
	 * options.txt - отображаемые тексты ссылки. Массив.
	 */
	var opt=$.extend({selector:'.cut',txt:['читать далее...','скрыть','смотреть']},options)
	$(this).each(function(){
		$(opt.selector,this).each(function(){
			var block,tr,count=0,title
			title=$(this).attr('title')
			title=title?opt.txt[2]+' &quot;'+title+'&quot;':opt.txt[0]
			if( this.tagName=='TR' || this.tagName=='TBODY' ){
				// считаем столбцы
				tr=$(this)
				if( this.tagName=='TBODY' ){
					tr=$('tr',this).first()
				}
				$('td',tr).each(function(){
					count+=$(this).attr('colspan')?$(this).attr('colspan'):1
				})
				count=count>1?' colspan="'+count+'"':''
				block='<tr><td'+count+' class="cutter"><a href="#">'+title+'</a></td></tr>'
				if( this.tagName=='TBODY' ){
					block='<tbody>'+block+'</tbody>'
				}
			}else{
				block='<div class="cutter"><a href="#">'+title+'</a></div>'
			}
			$(this).hide().after(block)
		})
	})
	$('.cutter a').click(function(e){
		var obj=$(this).parent()
		if( obj[0].tagName=='TD' ){
			obj=obj.parent().parent()
		}
		obj=obj.prev()
		obj.toggle()
		$(this).html(obj.css('display')=='none'?(obj.attr('title')?opt.txt[2]+' &quot;'+obj.attr('title')+'&quot;':opt.txt[0]):opt.txt[1])
		e.preventDefault()
		e.stopPropagation()
		return false
	});
}})(jQuery)

