$(document).ready(function()
{
	$('#menu ul.user li:last-child').addClass('last');
	
	$('#main div.large li:last-child').addClass('last');
	
	$('#main div.large div.calltoaction a').wrapInner('<span />');
	
	$('#main div.large div.header a').wrapInner('<span />');
	
	$('#news li:last-child').addClass('last');
	
	$('#blogs li:last-child').addClass('last');
	
	$('#groups li:last-child').addClass('last');
	
	$('#sitemap ul li:eq(0)').addClass('first');
	$('#sitemap ul li:last-child').addClass('last');
	
	$('#search-results div:eq(0)').addClass('first');
	$('#search-results div:last-child').addClass('last');
	$('#search-results a.toggle').click(function(event)
	{
		event.preventDefault();
		
		if($(this).parent().find('table:visible').length || $(this).parent().find('ul:visible').length)
		{
			$(this).text('Pokaż szczegóły');
			$(this).parent().find('h2').css('border-bottom', 0);
			$(this).parent().find('table').hide();
			$(this).parent().find('ul').hide();
		}
		else
		{
			$(this).text('Schowaj szczegóły');
			$(this).parent().find('h2').css('border-bottom', '1px solid #cecddb');
			$(this).parent().find('table').show();
			$(this).parent().find('ul').show();
		}
	});
        $('#archive-polls div:eq(0)').addClass('first');
        $('#archive-polls a.toggle').click(function(event)
	{
		event.preventDefault();

		if($(this).parent().find('table:visible').length || $(this).parent().find('ul:visible').length)
		{
			$(this).text('Pokaż szczegóły');
			$(this).parent().find('h2').css('border-bottom', 0);
			$(this).parent().find('table').hide();
			$(this).parent().find('ul').hide();
		}
		else
		{
			$(this).text('Schowaj szczegóły');
			$(this).parent().find('h2').css('border-bottom', '1px solid #cecddb');
			$(this).parent().find('table').show();
			$(this).parent().find('ul').show();
		}
	});
	
	if($('#article').length)
	{
		$('#article a[href$=".jpg"]').fancybox();
		$('#article a[href$=".gif"]').fancybox();
		$('#article a[href$=".png"]').fancybox();
		$('#article a[href*="attachment"]').fancybox({'type' : 'image'}); 
		$('#article div:last-child').addClass('close'); 
	}

	height = 0;
	
	$('#card #benefits li:eq(0), #card #benefits li:eq(1), #card #benefits li:eq(2)').each(function()
	{
		if($(this).height() > height)
		{
			height = $(this).height();
		}
	});
	
	$('#card #benefits li:eq(0), #card #benefits li:eq(1), #card #benefits li:eq(2)').height(height);
	
	height = 0;
	
	$('#card #benefits li:eq(3), #card #benefits li:eq(4), #card #benefits li:eq(5)').each(function()
	{
		if($(this).height() > height)
		{
			height = $(this).height();
		}
	});
	
	$('#card #benefits li:eq(3), #card #benefits li:eq(4), #card #benefits li:eq(5)').height(height);
	
	height = 0;
	
	$('#card #benefits li:eq(6), #card #benefits li:eq(7), #card #benefits li:eq(8)').each(function()
	{
		if($(this).height() > height)
		{
			height = $(this).height();
		}
	});
	
	$('#card #benefits li:eq(7), #card #benefits li:eq(8), #card #benefits li:eq(9)').height(height);
	
	$('#card #benefits li').click(function()
	{
		$.fancybox([$(this).find('div.details').html()], {
			'padding' : 20
		});
	});
	
	$('#card #benefits li a').click(function(event)
	{
		event.preventDefault();
	});
	
	$('#card #benefits li').mouseover(function() {
		$(this).addClass('active');
	});
	
	$('#card #benefits li').mouseout(function() {
		$(this).removeAttr('class');
	});
	
	$('#content #application li.card table img').click(function()
	{
		$(this).parent().find('input').attr('checked', 'checked');
	});
	
	$('#content input[type=text], #content input[type=password]').addClass('text');
	$('#content input[type=checkbox]').addClass('checkbox');
	$('#content input[type=submit]').addClass('submit');
	
	$('#partners h2').html('<strong>'+$('#partners h2').text()+':</strong><span>'+$('#partners h2').text()+':</span>');

	refreshGlobalMessage();
	refreshPagination();
	refreshAdminTable();
});

function refreshGlobalMessage()
{
    $('li.success').wrapInner('<div class="lt" />');
    $('li.success').wrapInner('<div class="lb" />');
    $('li.success').wrapInner('<div class="rt" />');
    $('li.success').wrapInner('<div class="rb" />');
    $('li.failure').wrapInner('<div class="lt" />');
    $('li.failure').wrapInner('<div class="lb" />');
    $('li.failure').wrapInner('<div class="rt" />');
    $('li.failure').wrapInner('<div class="rb" />');

    if($('#content div.long').length && $('#content div.form').length && ($('#content ul li.success').length || $('#content ul li.failure').length))
    {
            $('li.failure').addClass('hack');
            $('li.success').addClass('hack');
    }
}

function refreshPagination()
{
    if(!$('#main div.large div.calltoaction a span').length)
    {
            $('#main div.large div.calltoaction a').wrapInner('<span />');
    }
    $('div.pages ul').css('padding', '0 '+Math.round(($('div.pages').width()-$('div.pages ul').width())/2)+'px 0 '+Math.floor(($('div.pages').width()-$('div.pages ul').width())/2)+'px');

}

function refreshAdminTable()
{
    if(!$('#content #admin table.elementDetails').length && $('#content #admin thead tr').length > 1)
    {
            $('#content #admin thead tr:eq(0)').addClass('pages');
            $('#content #admin thead tr:eq(1)').addClass('labels');
            $('#content #admin table.cpi-result tbody tr:nth-child(even)').addClass('even');
    }
    else if(!$('#content #admin table.elementDetails').length && $('#content #admin thead tr').length == 1)
    {
            $('#content #admin thead tr:eq(0)').addClass('labels');
            $('#content #admin table').css('margin-top', '5px');
            $('#content #admin tbody td input[type=checkbox]').parent().css('text-align', 'center');
            $('#content #admin tbody td input[type=submit]').parent().css('text-align', 'center');
            $('#content #admin tbody td input[type=submit]').parent().addClass('submits');
            $('#content #admin table.cpi-result tbody tr:nth-child(even)').addClass('odd');
            $('#content #admin table.cpi-result tbody tr tr').removeAttr('class');
    }

    $('#content #admin table.elementDetails tr td:eq(0)').addClass('label');
    $('#content #admin table.meta tr td:eq(0)').addClass('label');
}

function refreshEditableTable() {
    refreshAdminTable();
    refreshGlobalMessage();
}

function refreshPaginationAndGlobalMessage() {
    refreshPagination();
    refreshGlobalMessage();
}
