/*
| Home Page News
*/
$(document).ready(
	function()
	{
		if (typeof(newsText) != 'undefined')
		{
			/*
			| Start the process off
			*/
			randomiser1 = setTimeout('displayRandomNews()', 10000);
		}
	}
);

var element;
var position;
var clicked = false;

function enlarge(el, text, clickEvent)
{
	if(clicked)
	{
		return;
	}
	
	if(clickEvent != undefined && clickEvent == true)
	{
		clearTimeout(randomiser1);
		clearTimeout(randomiser2);
	}
	
	clicked = true;
	
	//$('#text_container_inner').html(newsText[text]);
	document.getElementById('text_container_inner').innerHTML = newsText[text];
	
	$(el).parent().siblings().css("z-index" , 1);
	$(el).parent().css("z-index" , 100);
	$('#text_container').css("z-index" , 50);
	
	element = $(el).attr('id');
	position = $(el).parent().css('left');
	
	$(el).animate(
		{ width: '138px' , height : '138px' },
		1000, '', 
		function(){
			$(el).parent().animate(
				{ left: '10px', top: '10px' }, 
				400, '', 
				function(){
					$('#text_container').show();
					
					$('#text_container').animate(
						{ left : '148px' , width : '320px' },
						500
					);
				}
			);
		}
	);
}

function shrink(clickEvent)
{
	if(clickEvent != undefined && clickEvent == true)
	{
		clearTimeout(randomiser1);
		clearTimeout(randomiser2);
	}
	
	$('#text_container').animate(
		{left : '10px' , width : '100px'}, 
		500, '', 
		function(){
			$('#text_container').hide();
			
			$('#'+element).animate(
				{ width: '70px' , height : '70px' },
				300, '', 
				function(){
				
					$('#'+element).parent().animate(
						{ left: position, top: '25px' },
						300
					);
					
					clicked = false;
					if(clickEvent != undefined && clickEvent == true)
					{
						resetDefaultNewsDesc();
					}
				}
			);
		}
	);	
}

var setDefaultNews;

function dispNewsDescShort(id)
{
	clearTimeout(setDefaultNews);
	clearTimeout(randomiser1);
	clearTimeout(randomiser2);
	
	if(!clicked)
	{
		$('.news_image').animate(
			{ width: '70px', height: '70px' },
			1, '', 
			function() {
				$('#2'+id).animate(
					{ width: '85px', height: '85px' },
					100
				);
			}
		);
		
				
	}
	
	$('#defultNewsDesc').hide();
	$('#setNewsDesc').show();
	
	var textLong = newsText[id];
	var textWords = textLong.split(" ");
	
	var textShort = '';
	
	if(textWords.length > 11)
	{
		for(var i = 0; i < 11; i++)
		{
			textShort += textWords[i]+" ";
		}
		
		textShort += "...";
	}
	else
	{
		textShort = textLong;
	}
	
	if(textShort.search(/<endOfDesc \/>/) >= 0)
	{
		var textShortLong = textShort.split("<endOfDesc />");
		
		textShort = textShortLong[0];
	}
	
	$('#setNewsDesc').html(textShort);
}

function hideNewsDescShort()
{
	clearTimeout(randomiser1);
	clearTimeout(randomiser2);
	
	if(!clicked)
	{
		$('.news_image').animate(
			{ width: '70px', height: '70px' },
			100
		);
	}
	
	$('#setNewsDesc').html('');
	$('#setNewsDesc').hide();
	
	if(!clicked)
	{
		setDefaultNews = setTimeout("resetDefaultNewsDesc()", 2500);
	}
}

var randomiser1;
var randomiser2;

function resetDefaultNewsDesc()
{
	$('#defultNewsDesc').fadeIn('slow');
	
	randomiser1 = setTimeout('displayRandomNews()', 5000);
}

function displayRandomNews()
{
	$('.news_image').animate(
		{ width: '70px', height: '70px' },
		1, '',
		function() {
			$('#setNewsDesc').html('');
			$('#setNewsDesc').hide();
			$('#defultNewsDesc').fadeIn('slow');
		}
	);
	
	var randNum = get_random_number();
	
	var randEl = $('#2'+randNum);
	
	enlarge(randEl, randNum);
	
	randomiser2 = setTimeout('shrink()', 6000);
	
	randomiser1 = setTimeout('displayRandomNews()', 12000);
}

var randomNumber = 0;

function get_random_number()
{
	var highest = $('.news_image').length;
	
	randomNumber = randomNumber + 1;
	
	if(randomNumber > highest){
		randomNumber = 1;
	}
	
	return randomNumber;
	
	/*
	// Not good for small numbers
	var newRandomNumber = Math.floor(Math.random()*5+1);
	
	if(newRandomNumber == 0){ newRandomNumber = 1; }
	
	if(newRandomNumber == randomNumber)
	{
		return get_random_number();
	}
	
	randomNumber = newRandomNumber;
	
	return newRandomNumber;
	*/
}

/*
| End of Home Page News
*/