$(document).ready(function()
{
  slideShow(10000);
  newsScroller(8000);
});

var prevID = 0;
var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;
function slideShow(speed) 
{
	$('ul.slideshow').append('<li id="slideshow-caption" class="caption"><div class="slideshow-caption-container"><h3></h3><p></p></div></li>');
	$('ul.slideshow li').css({opacity: 0.0});
	$('ul.slideshow li:first').css({opacity: 1.0});
	$('#slideshow-caption h3').html($('ul.slideshow a:first').find('img').attr('title'));
	$('#slideshow-caption p').html($('ul.slideshow a:first').find('img').attr('alt'));
	var id = $('ul.slideshow a:first').find('img').attr('name');
	document.getElementById("tr" + id).style.backgroundColor = "#ffffff";
	document.getElementById("spn" + id).style.color = "#000000";
	prevID = id;
	$('#slideshow-caption').css({opacity: 0.6, bottom:0});
	var timer = setInterval('gallery()',speed);
//	$('ul.slideshow').hover(
//		function () {
//			clearInterval(timer);	
//		}, 	
//		function () {
//			timer = setInterval('gallery()',speed);			
//		}
//	);
}

function gallery() {
	var current = ($('ul.slideshow li.show')?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));
	var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption')? $('ul.slideshow li:first') :current.next()) : $('ul.slideshow li:first'));
	var title = next.find('img').attr('title');	
	var desc = next.find('img').attr('alt');	
	if (prevID > 0)
	{
		document.getElementById("tr" + prevID).style.backgroundColor = "#444444";
		document.getElementById("spn" + prevID).style.color = "#ffffff";
	}
	var id = next.find('img').attr('name');
	
	document.getElementById("tr" + id).style.backgroundColor = "#ffffff";
	document.getElementById("spn" + id).style.color = "#000000";
	prevID = id;
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
	$('#slideshow-caption').animate({opacity:0}, 500, function () {
			$('#slideshow-caption h3').html(title);
			$('#slideshow-caption p').html(desc);				
			$('#slideshow-caption').animate({opacity:0.6}, 1000);	
	});		
	current.animate({opacity: 0.0}, 1000).removeClass('show');

}

function newsScroller(time)
{
  headline_count = $("div.headline").size();
  $("div.headline:eq(" + current_headline + ")").css('top', '5px');
  headline_interval = setInterval(headlineRotate, time);
  $('#scrollup').hover(function()
  {
    clearInterval(headline_interval);
  }, function()
  {
    headline_interval = setInterval(headlineRotate, time); 
    headline_rotate();
  });
}

function headlineRotate() {
  current_headline = (old_headline + 1) % headline_count;
  $("div.headline:eq(" + old_headline + ")").css("display", "none");
  $("div.headline:eq(" + current_headline + ")").fadeIn(1000);
  old_headline = current_headline;
}
