﻿var cur_news = 0;

$(document).ready(function() {
	$('.carousel-en > .content').each(function() {
		var height = $(this).height()/2;
		$(this).css('margin-top','-'+height+'px');
	});
	
	cur_news = -1;
	nextNews();
});

var t;
//moved to template: dynamic from XML
//var carouselCur = 1;
//var carouselMax = 4;

function nextNews() {
	cur_news++;
	if (typeof tickercontent!= 'undefined') {
	if(cur_news >= tickercontent.length) cur_news = 0;
	$("#newsTicker .content").append("<p class='new'>"+tickercontent[cur_news].title + " <a href='"+tickercontent[cur_news].url+"'>Lue lisää »</a></p>");
	$("#newsTicker .title a[class!='"+tickercontent[cur_news].type+"']").fadeOut(1000);
	$("#newsTicker ."+tickercontent[cur_news].type).fadeIn(1000);
	
	$("#newsTicker .old").animate({top: "-60px"}, 1000, function() {
		$("#newsTicker .old").remove();
	});
	$("#newsTicker .new").animate({top: "12px"}, 1000, function() {
		$("#newsTicker .new").addClass("old").removeClass("new");
	});
	setTimeout("nextNews()", 5000);
	}
}

function showSlide(i) {
	$("#carouselSlide"+carouselCur).fadeOut(1000);
	$("#carouselSlide"+i).fadeIn(1000);
	$("#carousel .links .current").removeClass("current");
	$("#carousel .links .link"+i).addClass("current");
	
	carouselCur = i;
	
	clearTimeout(t);
	t = setTimeout("showNext()", 10000);
}

function showNext() {
	var i = carouselCur+1;
	if(i > carouselMax) i = 1;
	showSlide(i);
}

function showPrev() {
	var i = carouselCur-1;
	if(i < 1) i = carouselMax;
	showSlide(i);
}

$(document).ready(function() {

var found=false;
try
  {
  
  if (carouselCur!=undefined) found=true;
    //Run some code here
  }
catch(err)
  {
  //Handle errors here
  }

  
  
  if (found) {
	$("#carousel .leftArrow").show();
	$("#carousel .rightArrow").show();
	t = setTimeout("showNext()", 10000);
	
	}

	
	
	
});
