(function ($) {

	var o;

	$.fn.extend({
		toksport_schedule: function (options) {
			o = $.extend({}, $.fn.toksport_schedule.defaults, options)
			return this.each(init)
		}
	})

	function init() {
		var $this = $(this)

		$this.find('div.schedule-item').each(function () {
			$(this).data('_height', $(this).height())
			//$('#content > h1').append($(this).data('_height'))
		})

		$this.find('div.schedule-item:gt('+(o.show_items-1)+')').each(function () {
			$(this).stop().animate(
				{'opacity': 'hide', height: 0},
				{'duration': o.hide_duration, easeMethod: 'easeout'}
			)
		})
		//$('#content > h1').append('|')
		$this.interval = setInterval( function () { spin($this) }, o.switch_duration)
	}

	function spin(element) {
		var hour = new Date().getHours()

		if ( ! $(':first', element).is('#schedule-'+hour))
		{

			/*$(':first', element).animate(
				{height: 0, opacity: 'hide'},
				{duration: o.switch_duration, complete: function () {
					$(element).append($(this).remove())
					$(':hidden:first', element).each(function () {
						$(this).animate(
							{'opacity': 'show', height: $(this).data('_height')},
							{duration: o.switch_duration}
						)
					})
				}
			})*/

		}
	}

	$.fn.toksport_schedule.defaults = {
		'show_items': 24,
		'switch_duration': 10,
		'hide_duration': 300
	}

})(jQuery)

function rds() {
	var a_w = $('#rds-content div.wrapper').width()
	var as_w = $('#rds-content div.wrapper span').width()

	if (as_w > a_w) {
		$('#rds-content div.wrapper span').animate(
			{ 'left': '-'+(as_w-a_w)+'px' },
			{ duration: 4000, easing: 'easeOutSine', complete: function () {
				$('#rds-content div.wrapper span').animate(
					{ 'left': 0 },
					{ duration: 1000, easing: 'easeOutBounce', complete: function () {
						setTimeout('rds()', 5000)
					} }
				)
			} }
		)
	}
}

$(function () {

	$('#navbar, #logo a, #intro, a.btn-listen, a.btn-forum, #rds').ifixpng('/static/images/pixel.gif')
	$('input.btn-send').hover(function () { $(this).addClass('btn-send-hover') }, function () { $(this).removeClass('btn-send-hover') })

	$('input.btn-save').hover(function () { $(this).addClass('btn-save-hover') }, function () { $(this).removeClass('btn-save-hover') })

	$('#schedule').toksport_schedule()

	$('ul.program-list li, ul.threads-list li').hover(function () { $(this).addClass('hover') }, function () { $(this).removeClass('hover') })
	//$('ul.threads-list li').hover(function () { $(this).prev().addClass('hide-border') }, function () { $(this).prev().removeClass('hide-border') })

	$('ul.program-list li').hover(
		function () {
			var a_w = $('a.label', this).width()
			var as_w = $('a.label span', this).width()

			if (as_w > a_w) {
				$('a.label span', this).stop().animate({ 'left': '-'+(as_w-a_w)+'px' }, { duration: 4000, easing: 'easeOutSine' })
			}

			$('a.rss', this).eq(0).append('<span>&nbsp;</span>')
			$('a.rss span', this).eq(0).stop().animate({opacity: 'show'})

		},
		function () {
			var as_w = $('a.label span', this).stop().animate({ 'left': 0 }, { duration: 1000, easing: 'easeOutBounce' })
			$('a.rss span', this).eq(0).animate({opacity: 'hide'}, {complete: function () { $(this).remove() }})
		}
	)

	rds()

	$('#rds-play').hover(
		function() {
			if ($('#rds-play span').size() == 0) {
				$('<span>').stop().appendTo(this).animate( { 'opacity': 'show' }, { duration: 200 } )
			} else { $('#rds-play span').remove() }
		},
		function () {
			$('#rds-play span').stop().animate( { 'opacity': 0 }, { duration: 2000, complete: function () {
				$(this).remove()
			}} )
		}
	)

	if(_Uzytkownik_Zalogowany == false) {
		$("a.mustlogon").click(function () {
	      	comm_show('Wymagane jest zalogowanie', 'error');
			return false;
	    });

	}

	$('.q-more').click(function () {
		$(this).toggleClass('q-less')
		$('#'+$(this).attr('id')+'-content').animate({'height': 'toggle'});
		return false
	});

	$('.tab-view-block a').click(function () {
		if ($(this).hasClass('tab-toggle')) {
			$('#'+$(this).parent().parent().attr('id')+ ' a').removeClass('active');
			$(this).addClass('active');
			$('.'+$(this).parent().parent().attr('id')+ '-element').hide();

			$('#'+$(this).attr('id')+'-content').show();
			return false;
		}
	});

	/*
	$('.blog-text').keyup(function () {
		suggestTagsByText(escape($(this).val()), 'blog-tag-suggest');
	});
	*/

	$("#tagi-wszystkie").cumulusTags({ minFont: 7, fontLength: 17 });
	$('.tag-completer').tagSuggest({
	    url: '/tagi/sugestie',
	    delay: 100,
	    separator: ','
	});

	$('.tag-middle-size').cumulusTags({ minFont: 8, fontLength: 4 });
	$('.tag-normal-size').cumulusTags({ minFont: 8, fontLength: 14 });
	$('#hcontrol').val('1');
})



    $.fn.cumulusTags = function(options) {
        var defaults = {
            fontLength: 16,
            minFont: 11,
            regex: /(\d+)/,
            tag: "a"
        };
        var settings = $.extend(defaults, options);
        var min = 0, max = 0, scaleLength = 0;
        var alist = $(this).find(settings.tag);
        alist.each(function() {
            var mct = settings.regex.exec(this.rel);
            if (mct != null && mct.length > 1) {
                $(this).data("count", mct[1]);
                mct = parseInt(mct[1]);
                if (mct < min) {
                    min = mct;
                };
                if (mct > max) {
                    max = mct;
                };
            } else {
                $(this).data("count", -1);
            };
        });
        scaleLength = parseFloat((max - min + 1)) / parseFloat(settings.fontLength);
        alist.each(function() {
            var count = parseInt($(this).data("count"));
            if (count > -1) {
                count = Math.round((parseFloat(count) - min) / scaleLength);
                count += settings.minFont;
                $(this).css("fontSize", count + "pt");
            };
        });
    };
