
var novosti_interval = 8;		// sekundi
var novosti_speed = 'normal';	// milisekundi || 'slow' || 'normal' || 'fast'
var novosti_refresh = 1;		// minuta

$(function()
{
	var novosti_list = $('.novosti-list ul')[0];
	var novosti_timer = null;
	var novosti_id = 0;

	if (novosti_list)
	{
		novosti_id = $('.news-id').html();

		function novosti_start()
		{
			if (novosti_list.listItems.length > 3)
			{
				novosti_stop();

				$.each(novosti_list.listItems, function(index, item)
				{
					$(item).css
					({
						position: 'absolute',
						top: 90 * (index + 1)
					})
					.removeClass('current')
					[0].lastItem = index == (novosti_list.listItems.length - 1);
				});

				$(novosti_list.listItems[novosti_list.listItems.length - 1]).css({ top: 0 });

				$(novosti_list.listItems[novosti_list.currentItem = 0]).addClass('current');

				novosti_list.running = true;
				novosti_timer = setInterval(novosti_move, novosti_interval * 1000);
			}
		}

		function novosti_move(prev)
		{
			var prev = typeof(prev) == 'boolean';
			var next = !prev;

			if (prev)
			{
				var prevItem = novosti_list.currentItem - 2;

				$(novosti_list.listItems[prevItem < 0 ? novosti_list.listItems.length + prevItem: prevItem]).css({top: -90});
			}

			if (next)
			{
				novosti_list.currentItem =	++novosti_list.currentItem == novosti_list.listItems.length ?
											0 : novosti_list.currentItem;
			}
			else
			{
				novosti_list.currentItem =	--novosti_list.currentItem == -1 ?
											novosti_list.listItems.length - 1 : novosti_list.currentItem;
			}

			$.each(novosti_list.listItems, function(index, item)
			{
				$(item).animate({top: (next ? '-' : '+') + '=90'}, novosti_speed, 'backout', function()
				{
					if (next)
					{
						if (parseInt($(this).css('top')) < 0)
						{
							$(this).css('top', (novosti_list.listItems.length - 1) * 90);
						}
					}

					if ($(this).hasClass('current'))
					{
						$('#big_image').attr
						({
							src: $('.attr-bigimage', this).text(),
							alt: $('h4 a', this).html()
						});

						$('#big_text').html($('.attr-fulltext', this).html());
						$('#big_href').attr('href', $('h4 a', this).attr('href'));
						$('#big_title').html($('h4 a', this).html());
					}
				})
				[index == novosti_list.currentItem ? 'addClass' : 'removeClass']('current');
			});
		}

		function novosti_stop()
		{
			if (novosti_timer)
			{
				novosti_list.running = false;
				clearInterval(novosti_timer);
			}
		}

		function novosti_resume()
		{
			novosti_list.running = true;
			novosti_timer = setInterval(novosti_move, novosti_interval * 1000);
		}

		novosti_list.listItems = $('li', novosti_list).get();
		novosti_list.running = false;

		novosti_start();

		$('.btn-pause').click(function()
		{
			if (novosti_list.running)
			{
				novosti_stop();
			}
			else
			{
				novosti_resume();
			}
		});

		$('.btn-prev').click(function()
		{
			var running = novosti_list.running;
			novosti_stop();
			novosti_move(true);
			if (running) novosti_resume();
		});

		$('.btn-next').click(function()
		{
			var running = novosti_list.running;
			novosti_stop();
			novosti_move();
			if (running) novosti_resume();
		});

		function novosti_update()
		{
			$.getJSON('./news?output=json&id=' + novosti_id, function(result)
			{
				if (result.id != novosti_id)
				{
					novosti_id = result.id;

					novosti_stop();

					for (var i = result.items.length - 1; i >= 0; i--)
					{
						$('.novosti-list ul').prepend
						(
							'<li><img src="' + result.items[i].thumb + '" width="69" height="69" alt="' + result.items[i].title +
							'" /><h4><a href="' + result.items[i].href + '">' + result.items[i].title +
							'</a></h4><span class="attr-fulltext">' + result.items[i].summary +
							'</span><span class="attr-bigimage">' + result.items[i].image + '</span><p>' +
							result.items[i].short_summary + '</p></li>'
						);
					}

					novosti_start();
				}
			});
		}

		setInterval(novosti_update, novosti_refresh * 60000);
	}
});

$.extend($.easing,
{
	backout: function(x, t, b, c, d) { var s=1.70158; return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b; }
});