var siteCookie = {'language':'bs','category':'','search':'','related':''};
var latestTimer = setTimeout(function() { loadLatest(); },30000);
var scrollTimeout = setTimeout(function() { scroll(0); },10000);
$(document).ready( function() {

	$(".tip-south").tipsy({
		delayIn: 0,
		delayOut: 0,
		gravity: 's',
		fade: false,
		title: 'title',
		offset: 10,
		opacity: 1
	});
	
	$(window).resize( function() {
		resize();
	});
	
	$(".arrow_down").live('click', function() {
		$(this).parent().children("ol").slideToggle();
	});
	
	$("#lastNews").slimScroll({
		 width: '350px',
		 height: $(window).height()-290,
		 size: '7px',
		 position: 'right',
		 color: '#282828',
		 alwaysVisible: false,
		 start: 'top'
	});
	
	$(".topnews .article").each( function() {
		var id = $(this).attr('rel');
		$(".center .article[rel="+id+"]").css({'display':'none'});
	});
	
	$("ul.topnavigation li").live('click',function() {
		clearTimeout(scrollTimeout);
		scroll($(this).attr('rel'));
	});
	
	updateBread();
	sendGoogleStats();
	resize();
	
	$("#lastNews img").lazyload({
		container: $("#lastNews")
	});
	
	$("#content img").lazyload();
});

function scroll(target) {
	clearTimeout(scrollTimeout);
	if (target>0) target = (target-1)*190;
	else target = $('.topnews').scrollTop()+190;
	if (target>4*190) target=0;
	$("ul.topnavigation li.active").removeClass('active');
	$("ul.topnavigation li[rel="+(Math.floor(target/190)+1)+"]").addClass('active');
	$(".topnews").animate({scrollTop : target},'normal');
	scrollTimeout = setTimeout(function() { scroll(0); },10000);
}

function loadLatest(resetAll) {
	var lastID = 0;
	clearTimeout(latestTimer);
	if ($('#lastNews span.latestArticle').length>0) lastID = $("#lastNews span.latestArticle").attr('rel');
	$.ajax({
		url: 'ajax/latest.php?lang='+siteLang+'&dummy='+new Date().getTime(),
		type: 'get',
		success: function(html) {
			if ($("#lastNews span.latestArticle").length==0 || resetAll==true) {
				$("#lastNews").html(html);
			} else {
				var el = $(html).filter("span.latestArticle").filter(function(index) { return parseInt($(this).attr('rel'))>lastID })
				if (el.length>0) {
					el.each( function() {
						$(this).css('display','none');
					});
					$("#lastNews").prepend(el);
					$("#lastNews span.latestArticle:hidden").fadeIn();
				} 
			}
		},
		complete: function() {
			latestTimer = setTimeout( function() {
				loadLatest(false);
			},30000);
		}
	});
}

function sendGoogleStats() {
	/* Google Tracking */
	var newLocation = '#language='+siteCookie.language+'&category='+siteCookie.category+'&search='+siteCookie.search+'&related='+siteCookie.related;
	var bread = new Array();
	if (typeof(siteCookie.language)=="string") {
		language = $("a[rel="+siteCookie.language+"]").attr('original-title');
		if (typeof(language)=="undefined") language = $("a[rel="+siteCookie.language+"]").attr('title');
		bread.push(language);
	}
	if (typeof(siteCookie.category)=="string" && new String(siteCookie.category).length>0) bread.push($("#categoriesMenu a[rel="+siteCookie.category+"]").text());
	if (typeof(siteCookie.search)=="string" && new String(siteCookie.search).length>0) bread.push(siteCookie.search);
	if (typeof(siteCookie.related)=="string" && new String(siteCookie.related).length>0) bread.push($("div[rel=related"+siteCookie.related+"] h1 a").text());
	_gaq.push(['_trackPageview', '/'+newLocation]);
	_gaq.push(['_trackEvent', bread[0], bread[1], ((typeof(bread[3])=='undefined')?bread[2]:bread[3])]);
	/* */
}

function updateBread() {
	var bread = Array();
	if (typeof(siteCookie.language)=="string") {
		language = $("a[rel="+siteCookie.language+"]").attr('original-title');
		if (typeof(language)=="undefined") language = $("a[rel="+siteCookie.language+"]").attr('title');
		bread.push('<a href="/'+siteCookie.language+'">'+language+'</a>');
	}
	if (typeof(siteCookie.category)=="string" && new String(siteCookie.category).length>0) bread.push('<a href="/'+siteCookie.language+'/!'+siteCookie.category+'">'+$("#categoriesMenu a[rel="+siteCookie.category+"]").text()+'</a>');
	if (typeof(siteCookie.search)=="string" && new String(siteCookie.search).length>0) bread.push('<a href="/'+siteCookie.language+'/!'+siteCookie.category+'/!'+siteCookie.search+'">search: '+siteCookie.search+'</a>');
	if (typeof(siteCookie.related)=="string" && new String(siteCookie.related).length>0) bread.push($("div[rel=related"+siteCookie.related+"] h1 a").text());
	$(".bread").html(bread.join(' &raquo; '));
}

function resize() {
	var searchWidth = $(document).width()-850;
	$("div.search input.text").css({'width':searchWidth+'px'});
	$("#lastNews").height($(window).height()-290);
}

