var timer;



/**
 * @desc auto volání řidiče carouselu
 */
function timedCarousel()
{
	if (timer_ids.length > 1)
	{
		var actual_id = jQuery.inArray(timer_pointer, timer_ids);

		var next_id = actual_id + 1;
		if (next_id >= timer_ids.length)
		{
			next_id = 0;
		}

		setCarouselPointer(timer_ids[next_id]);

		controlCarousel(timer_ids[next_id]);
	}
}

/**
 * @desc nastaví control carouselu na aktivní
 */
function setCarouselPointer(id)
{
	$("#article-" + id + " input:radio").attr("checked", true);
	$("#carouselControl span.radio").css("background-position", "0 0");
	$("#article-" + id + " span.radio").css("background-position", "0 -38px");
}

/**
 * @desc řidič carouselu
 */
function controlCarousel(article_id)
{
	$("#carouselOuter").height($("#carouselInner").height());
	$("#carouselOuter").width($("#carouselInner").width());
	$("#carouselOuter").css("background-color", $("#carousel").css("background-color"));
	$("#carouselOuter").fadeIn(carousel_speed, function() {

		// získání nových dat podle id
		var data = getArticleById(article_id);

		// nasypání nových dat
		setNewArticle(data);

		$("#carouselInner .image").waitForImages(function() {
			$("#carouselOuter").fadeOut(carousel_speed);
		});

		timer = setTimeout("timedCarousel()", timer_speed);
	});
}


/**
 * @desc získá article data podle id
 */
function getArticleById(item_id)
{
	var data = $.ajax({
		url: "ajax/getarticlebyid/" + item_id,
		global: false,
		type: "GET",
		data: "",
		dataType: "json",
		async: false
	}).responseText;

	// úprava pro json
	return data = eval( '(' + data + ')' );
}


/**
 * @desc získá event data podle id
 */
function getDataById(item_id)
{
	var data = $.ajax({
		url: "ajax/geteventbyid/" + item_id,
		global: false,
		type: "GET",
		data: "",
		dataType: "json",
		async: false
	}).responseText;

	// úprava pro json
	return data = eval( '(' + data + ')' );
}


/**
 * @desc nastaví nové data article
 */
function setNewArticle(data)
{
	$("#carouselInner .image img").attr("src", "/images/article/size-385x270/" + data.image);
	$("#carouselInner .image img").attr("alt", "/images/article/size-385x270/" + data.title);
	$("#carouselInner .image img").attr("title", "/images/article/size-385x270/" + data.title);
	$("#carouselInner .desc .date").text(data.date);
	$("#carouselInner .desc h2.recommended").text(data.title);
	$("#carouselInner .desc .annotation").text(data.perex);
	$("#carouselInner .desc a").attr("href", data.seo_uri);

	timer_pointer = parseInt(data.id);

	Cufon.replace("h2.recommended", {
		fontFamily: "Gill Sans MT"
	});
}


/**
 * @desc nastaví nové data
 */
function setNewData(data)
{
	$("#description #data p.date").text(data.date);
	$("#description #data h4").text(data.name);
	$("#description #data p.place").text(data.address);

	$("#description #arrowLeft a").attr("id", "left-" + data.prev_event_id);
	$("#description #arrowRight a").attr("id", "right-" + data.next_event_id);
}



$(document).ready(function() {

	// run it like forever!
	clearTimeout(timer);
	timer = setTimeout("timedCarousel()", timer_speed);

	// carousel
	$("#carouselControlInner div").click(function(event)
	{
		clearTimeout(timer);

		var split = this.id.split("-");

		controlCarousel(split[1]);

		event.preventDefault();
	});

	// přehled akcí
	$("#arrowLeft a, #arrowRight a").click(function(event)
	{
		var split = this.id.split("-");
		var item_id = split[1];

		$("#dataOuter").height($("#description").outerHeight());
		$("#dataOuter").width($("#description").width());
		$("#dataOuter").css("background-color", $("#description").css("background-color"));
		$("#dataOuter").fadeIn(event_speed, function() {

			// získání nových dat podle id
			var data = getDataById(item_id);

			// nasypání nových dat
			setNewData(data);

			$("#dataOuter").fadeOut(event_speed);

		});
  
		event.preventDefault();
	});

});
