$(function()
{
    $('div.comboWrapp').hover(function()
    {
        $( this ).find( 'a' ).addClass('hover');
    }, function()
    {
        $( this ).find( 'a' ).removeClass('hover');
    });

    $('div.comboWrapp > a').click(function()
    {
        var width = $( this ).width() + 50;

        $( this ).next().toggleClass('hidden').width( width ).find( 'a' ).width( width - 10 );

        return false;
    });

    $('div.comboWrapp div.comboCat ul li a').click(function()
    {
        $( this ).parent().parent().find( 'li.hidden' ).removeClass( 'hidden' );
        $( this ).parent().addClass( 'hidden' );
        $( this ).parents( 'div.comboCat' ).toggleClass( 'hidden' ).prev().find('span').text( $( this ).text() );
    });
    
    $("body").click(function(e) {
         $('div.comboCat').addClass('hidden');
    });
});

$(document).ready(function() {
	$('div.tables').addClass('cat');
	if($('#main > h3').text().search(/Sire/) != -1) {
		$('div.cat > table').css('opacity','0');
		$('div.cat > table tbody tr').each(function() {
			$(this).children('td:eq(1)').append('<table><tbody></tbody></table>')
			
			var theTable = $(this).children('td:eq(1)').children('table').children('tbody');
			
			$(this).children('td:eq(1)').contents().filter(function() {
				return this.nodeType == 3;
			}).each(function() {
				if(this.nodeValue.replace(/^\s+|\s+$/g,"") != "" && this.nodeValue.replace(/^\s+|\s+$/g,"") != ",") {
					$(this).wrap('<td class="h-type"/>');
				}
			});
			
			$(this).find('.h-type').each(function() {
				var hIndex = theTable.children('tr').length;
				theTable.append('<tr><td></td></tr>');
				if($(this).prevAll('a').length != 0)
					$(this).prevAll('a').appendTo(theTable.children('tr:eq('+(hIndex-1)+')').children('td:eq(1)'));
				if($(this).nextAll('.h-type').length == 0)
					$(this).nextAll('a').appendTo(theTable.children('tr:eq('+hIndex+')').children('td:eq(0)'));
				$(this).prependTo(theTable.children('tr').eq(hIndex));
			});
			$(this).parents('table').css('opacity','1');
		});
	}
});
