$(function(){
//return;    
    // skaracanie nazwy dni
    $('.articles-info .articles-info-inner, .comm span.date').each(function(i, val){
        data = $(this).html().replace('Niedziela', 'Nd.')
            .replace('Poniedziałek', 'Pn.')
            .replace('Wtorek', 'Wt.')
            .replace('Środa', 'Śr.')
            .replace('Czwartek', 'Cz.')
            .replace('Piątek', 'Pt.')
            .replace('Sobota', 'Sb.')
            .replace(' stycznia ', '.01.')
            .replace(' lutego ', '.02.')
            .replace(' marca ', '.03.')
            .replace(' kwietnia ', '.04.')
            .replace(' maja ', '.05.')
            .replace(' czerwca ', '.06.')
            .replace(' lipca ', '.07.')
            .replace(' sierpnia ', '.08.')
            .replace(' września ', '.09.')
            .replace(' października ', '.10.')
            .replace(' listopada ', '.11.')
            .replace(' grudnia ', '.12.');
        $(this).html(data);
    });
	
	// archiwum
	var archiwumList = '';
	$.each($('#archiwum').html().split('<br>'), function(i, val){
		val = $.trim(val);
		if (val == '') {return;}
		if (matches = val.match(/href="(.+)".*>([^,]+), ([^<]+)<\/a> (\(\d+\))/)) {
			archiwumList += '<li><a href="'+matches[1]+'">'+matches[3]+' '+matches[2]+'<span> '+matches[4]+'</span></a></li>';
		} else {
			matches = val.match(/<span[^>]*>([^,]+), ([^<]+)<\/span> (\(\d+\))/);
			archiwumList += '<li class="current"><a href="'+location.pathname+location.search+'">'+matches[2]+' '+matches[1]+'<span> '+matches[3]+'</span></a></li>';
		}
	});
	$('#archiwum').replaceWith($('<ul class="menu">'+archiwumList+'</ul>'));
	
    // pierwszy idzie na top-story
    if (!location.href.match(/\?\S+=/) && !location.pathname.match(/\/?\d+(,.+\.html)?/)) {
//    if (location.href.match(/\?\S+=/)) {
        $('#topstory h1').html($('.articles-desc h2:first').html());
        $('#topstory .tag strong').html($('.articles-desc .articles-info-inner strong:first').html());
        $('#topstory .tag span').html($('.articles-desc .articles-info-inner').html());
        $('#topstory .tag span strong:first').remove();
        $('#topstory .tag span span:first').remove();

		tophtml = $('.articles-desc:first').clone();
		tophtml.find('h2').remove();
		tophtml.find('.trip-data').remove();
		tophtml.find('.articles-info').remove();
		tophtml.find('.deleteoper').parent().remove();
		$('#topstory p').html(tophtml);
		$('#topstory p:first').prepend($('.articles-desc .deleteoper:first').parent());
        $('#topstory p.trip-data').html($('.articles-desc p.trip-data:first').html());
        $('#topstory').show();
        $('.articles li.box:first').remove();
    }
    
    // pokaz more-stats
    if (location.href.match(/did=\d+/) || location.pathname.match(/\/?\d+(,.+\.html)?/)) {
//    if (!location.href.match(/did=\d+/)) {
        $('.more-stats').show();
        
        $('.main-title h1').text($('.articles-desc h2').eq(0).text());
        $('.articles li').css({padding:'0px'});
        $('.articles-desc h2').eq(0).remove();
        $('.main-title').show();
        
    } else {
        $('p.trip-data').each(function(){
            url = $(this).prevAll('h1,h2').find('a').attr('href');
            $(this).append($(' <span class="pipe">|</span> <a href="'+url+'">komentarze &raquo;</a>'))
        });
    }
    
    // ustawianie klas (first, current, last)
	$('ul.menu li:first-child').addClass('first');
	$('ul.menu li:last-child').addClass('last');
    
	// breadcrumb navigation
	if (location.search == '') {
		breadcrumb = '<strong>Start</strong>';
		$('#nav ul li:first-child').addClass('current');
	} else if (location.href.match(/did=\d+/)) {
		date = $('.articles-info-inner strong').text().match(/.+ \d{1,2}\.(\d{2})\.(\d{4})/);

		breadcrumb = '<a href="/">Start</a> <span>/</span> '
				   + '<a href="/index.php?m='+parseInt(date[1])+'&y='+date[2]+'">'+getBreadcrumbDate()+'</a> <span>/</span> '
				   + '<strong>'+$('.main-title h1').text()+'</strong>';
	
	} else if (location.href.match(/[?&]m=\d+/)) {
		breadcrumb = '<a href="/">Start</a> <span>/</span> '
				   + '<strong>Archiwum: '+getBreadcrumbDate()+'</strong>';
	
	} else if (catId = location.href.match(/[?&]category=(\d+)/)) {
		$('#nav a[href*="category='+catId[1]+'"]').parent().addClass('current');
		$('ul.menu a[href*="category='+catId[1]+'"]').parent().addClass('current');
		catName = $('ul.menu a[href*="category='+catId[1]+'"]').text();
		breadcrumb = '<a href="/">Start</a> <span>/</span> '
				   + '<strong>Kategoria: '+catName+'</strong>';
	}
	$('#tray p.f-left').html(breadcrumb);
    
    // css (lista i form komentarza)
	$('form.addcommform br:eq(0)').remove();
	$('form.addcommform input.button').removeClass('button').addClass('buttonSend');
	
	// kategorie wpisu
	$('span.itemCategories').each(function() {
		$(this).replaceWith($(this).html());
	});
	
	// archiwa
	if ($('.categoryArchHeader').length > 0) {
		$('.categoryArchHeader').html('<h2>'+$('#tray strong').text()+'</h2>');
		$('.categoryArchSummary tr:last-child td').addClass('t-right').css({borderBottom:'none'});
		$('.categoryArchSummary tr').each(function(i){
			$(this).css({backgroundColor:(i%2 ? '#eee' : '#fff')});
		});
		$('table.categoryArchSummary td.label:last').css({border:'none'}).parent().css({backgroundColor:'transparent'});
	}
   
	function getBreadcrumbDate() {
		date = $('.articles-info-inner strong').text().match(/.+ \d{1,2}\.(\d{2})\.(\d{4})/);
		switch(date[1]) {
			case "01": dateText = 'Styczeń'; break;
			case "02": dateText = 'Luty'; break;
			case "03": dateText = 'Marzec'; break;
			case "04": dateText = 'Kwiecień'; break;
			case "05": dateText = 'Maj'; break;
			case "06": dateText = 'Czerwiec'; break;
			case "07": dateText = 'Lipiec'; break;
			case "08": dateText = 'Sierpień'; break;
			case "09": dateText = 'Wrzesień'; break;
			case "10": dateText = 'Październik'; break;
			case "11": dateText = 'Listopad'; break;
			case "12": dateText = 'Grudzień'; break;
		}
		dateText += ' ' + date[2];
		return dateText;
	}
	
	// 2012 goals progress
	$.get('http://www.bikestats.pl/statystyki/rowerowe/webit', function(resp) { 
	    var total = $(resp).find('table.tblspec tr:first td:eq(1)').text();
	    total = Math.ceil(parseFloat(total, 10) / 30);
	    $( "#yearGoalProgress" ).progressbar({value: total}).css({height:'12px'}).after('<br/>').attr('title', total + '% z planowanych 3000km');
	});
});
