$(function() {
		//moves arrow under carousel
		$("div.trendscroller").mousemove( function(e) {
			compareLeft = (window.innerWidth / 2) - 175;
			compareRight = (window.innerWidth / 2) + 150;
			if(e.pageX < compareLeft) {
				xx = e.pageX + (compareLeft - e.pageX) * 0.25;
			} else if(e.pageX > compareRight) {
				xx = e.pageX - (e.pageX - compareRight) * 0.6;
			} else {
				xx = e.pageX - 8;
			}
			$("#balloon-arrow").css({'top': ($(this).position().top + 283), 'left': xx});
		});
		
// 		$("div.trendscroller").mouseout(function(){$("#debug_zone").hide()});

		$("div.trendscroller").hover( function() {
			$("#balloon-arrow").show("fast");
		}, function() {
			$("#balloon-arrow").hide("fast");
		});
		
		//shows title in baloon
		$("div.trendscroller ul.galerycontainer li a").hover(
			function() 
			{
				$(".carousel-balloon #balloon-text").stop().animate({"opacity": "1"}, "slow");		
				$(".carousel-balloon #balloon-text").html(this.title);
			},
			function() 
			{
				$(".carousel-balloon #balloon-text").stop().animate({"opacity": "0"}, "slow");
				$(".carousel-balloon #balloon-text").html($(".jcarousel-item-active a").attr("title"));
				$(".carousel-balloon #balloon-text").stop().animate({"opacity": "1"}, "slow");
		});
		
		var relatedCount = $("div.trendscroller ul.galerycontainer li").length;
		var nI = ~~$("#first_index").val();
		//autoscroll popular trends
		if(typeof($("#first_index").val()) != 'undefined' && relatedCount > 1) {
			$('.galerycontainer').jcarousel({
				initCallback: mycarousel_initCallback,
				scroll: 1
			});
			setTimeout(function(){autoScrollTrends(relatedCount, nI)}, 7000);
		} else {
			$('#galerycontainer, .galerycontainer').jcarousel();
		}
		
});

function autoScrollTrends(trendC, nextIndex) {
	nextIndex = nextIndex < (trendC - 1) ? (nextIndex + 1) : 0;
	slug = $("div.trendscroller ul.galerycontainer li").eq(nextIndex).find("a").eq(0).attr("href");
	$(".jcarousel-item-active").removeClass("jcarousel-item-active");
	$("div.trendscroller ul.galerycontainer li").eq(nextIndex).addClass("jcarousel-item-active");
	$(".carousel-balloon #balloon-text").stop().animate({"opacity": "1"}, "slow");		
	$(".carousel-balloon #balloon-text").html($("div.trendscroller ul.galerycontainer li.jcarousel-item-active a").attr("title"));
	getTrendData(slug);
	setTimeout(function(){autoScrollTrends(trendC, nextIndex)}, 7000);
}

function mycarousel_initCallback(carousel) {
	$("body").ajaxStart( function() {
		var cl=/jcarousel-item-active/gi;
		$("div.trendscroller ul.galerycontainer li").each(function() {
			if($(this).attr("class").match(cl)) {
				carousel.scroll($.jcarousel.intval(~~($("div.trendscroller ul.galerycontainer li").index(this)) + 1));
			}
		});
    return false;
	});
};

function getTrendData(slug) {
	slug = slug.replace($("#base_url").val() + "tendence/", "").split("/");
	slug = slug[0];
	$.get(
		$("#base_url").val() + "ajax-article/" + slug,
		function(data) {
			data = data.split("::--:::--::");
			comCountLabel = data[3] != 1 ? 'komentāri' : 'komentārs';
			postCountLabel = data[8] != 1 ? 'tendences' : 'tendence';
			baseUrl = $("#base_url").val();
			$("#start-img-container div").hide();
			$("#start-img-container div").html('<a href="'+ baseUrl + 'tendence/' + slug +'"><img src="' + baseUrl + 'uploads/gallery/' + data[9] + '_popular.jpg" /></a>').fadeIn(1000);
// 			alert($("#start-img-container div").html());
			
			$(".trend-main").eq(0).hide().html('<h1><a href="'+ baseUrl + "tendence/" + slug +'">' + data[0] + '</a></h1>' +
																	'<table class="hunterlist"><tr><td class="spacer-block" colspan="4"></td></tr><tr><td class="meta">' +
																	'<p><img src="' + baseUrl + 'theme/img/icons/positive_icon.png" alt="" />&nbsp;&nbsp;&nbsp;' +
																	'<strong>' + data[1] + '</strong> saka: <b>Ir tendence</b></p>' +
																	'<p><img src="' + baseUrl + 'theme/img/icons/negative_icon.png" alt="" />&nbsp;&nbsp;&nbsp;' +
																	'<strong>' + data[2] + '</strong> saka: <b>Nav tendence</b></p>' +
																	'<p><img src="' + baseUrl + 'theme/img/icons/comments_icon.png" alt="" />&nbsp;&nbsp;&nbsp;' +
																	'<strong>' + data[3] + '</strong>&nbsp;' + comCountLabel + '</p>' +
																	'</td><td class="profile-img mid"><a href="' + baseUrl + 'lietotajs/' + data[4] + '">' + data[5] + '</a>' +
																	'</td><td class="authorbox"><p>Pievienoja: <a href="' + baseUrl + 'lietotajs/' + data[4] + '">' +
																	'<span class="plain-text">' + data[6] + '</span></a></p>' +
																	data[7] + '<br />' + data[8] + '&nbsp;' + postCountLabel + '</td></tr></table>'
																	).fadeIn(1000);
		}
	);
}

