/**
 * @author Sebastian
 */

// base variables
var intCount = 0;
var intIteration = 0;
var intLastIndex = -1;
var intRotationTime = 6000;
var intRotationSpeed = 400;
var rotateTime;
var bolIsBusy = false;

// on document ready
$(document).ready(function(){
	// read variables
	intCount = $('.news_flash').size();
	
	// start carousel
	startCarousel();
	
	// set up hover effects
	$('#btn_blog').mouseenter(function(){ showScriptInfo('script_blog', true); });
	$('#btn_blog').mouseleave(function(){ showScriptInfo('script_blog', false); });
	$('#btn_archive').mouseenter(function(){ showScriptInfo('script_archive', true); });
	$('#btn_archive').mouseleave(function(){ showScriptInfo('script_archive', false); });
	$('#btn_material').mouseenter(function(){ showScriptInfo('script_material', true); });
	$('#btn_material').mouseleave(function(){ showScriptInfo('script_material', false); });
	$('#btn_blogroll').mouseenter(function(){ showScriptInfo('script_blogroll', true); });
	$('#btn_blogroll').mouseleave(function(){ showScriptInfo('script_blogroll', false); });
	$('#btn_search').mouseenter(function(){ showScriptInfo('script_search', true); });
	$('#btn_search').mouseleave(function(){ showScriptInfo('script_search', false); });
	
	// set up thumbnail click listeners
	for(var a = 0; a < intCount; a++) {
		$('#news_thumbs ul li:eq(' + a + ') a').attr('rel', a);
		$('#news_thumbs ul li:eq(' + a + ') a').click(function(){ forceNewsItem($(this).attr('rel')); });
	}
	
	// update date and time, set time out to 1 sec
	setInterval(function(){
			// padding function
			var pad = function (val, len) {
				val = String(val);
				len = len || 2;
				while (val.length < len) val = "0" + val;
				return val;
			};
		
			// get time names
			var bolShowSecs = false;
			var now = new Date();
			var weekday = new Array('So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa');
			var month = new Array('Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez')
			var showSecs = bolShowSecs ? ':' + pad(now.getSeconds()) : '';
			
			// print time stamp
			$('#btn_time').html(weekday[now.getDay()] + ', ' + now.getDate().toString() + '. ' + month[now.getMonth()] + ' ' + now.getFullYear() + ' ' + pad(now.getHours()) + ':' + pad(now.getMinutes()) + showSecs);
		}, 1000);
	
	// start twitter ticker
	ticker_start();
});

function startCarousel() {
	// show first news item
	$('.news_flash:eq(0)').css('display','block');
	$('#news_thumbs li a:eq(0)').addClass('focus');
	
	// get second item in position
	rotateNews_getInPosition(1);
	
	// start rotating items
	rotateTime = window.setTimeout('rotateNewsItem(1)', intRotationTime);
}

function rotateNewsItem(intIndex) {
	// set busy
	bolIsBusy = true;
	
	// get next and previous flash items
	var intNextIndex = (intIndex + 1) % intCount;
	var intPrevIndex = Math.max(intLastIndex, 0);

	// get next flash item in position and display
	rotateNews_getInPosition(intNextIndex);
	
	// move items to the left
	$('div.news_flash:eq(' + intPrevIndex  + '), div.news_flash:eq(' + intIndex  + ')').animate({
		left: '-=600' }, intRotationSpeed, function() { bolIsBusy = false; });

	// (un)focus thumb links
	$('#news_thumbs li a:eq(' + intPrevIndex + ')').removeClass('focus');
	$('#news_thumbs li a:eq(' + intIndex + ')').addClass('focus');
	
	// set last index
	intLastIndex = intIndex;
	
	// call again
	rotateTime = window.setTimeout('rotateNewsItem(' + intNextIndex + ')', intRotationTime);
}

// force a specific news item to pop up
function forceNewsItem(intIndex) {
	// if busy or index already displayed, cancel
	if(bolIsBusy || intIndex == intLastIndex) return;
	
	// set busy
	bolIsBusy = true;
	
	// clear timeout
	if(rotateTime != null) { window.clearTimeout(rotateTime); };
	
	// get flash item in position and display
	rotateNews_getInPosition(intIndex);
	
	// get last index
	var intPrevIndex = Math.max(intLastIndex, 0);
	
	// move items to the left
	$('div.news_flash:eq(' + intPrevIndex  + '), div.news_flash:eq(' + intIndex  + ')').animate({
		left: '-=600' }, intRotationSpeed, function() { bolIsBusy = false; });
	
	// (un)focus thumb links
	$('#news_thumbs li a:eq(' + intPrevIndex + ')').removeClass('focus');
	$('#news_thumbs li a:eq(' + intIndex + ')').addClass('focus');

	// set last index
	intLastIndex = intIndex;
	
	// prevent default behavior
	return false;
}

//get next flash item in position and display
function rotateNews_getInPosition(intIndex) {
	$('.news_flash:eq(' + intIndex + ')').css('left','600px');
	$('.news_flash:eq(' + intIndex + ')').css('display','block');
}

// show script
function showScriptInfo(strClass, bolShow) {
	// hide info by removing all classes
	$('#script_info').removeClass();
	
	// show new info?
	if(bolShow) {
		// show by assigning new class
		$('#script_info').addClass(strClass);
	}
}