var interval;
var rows = 0;
var currentRow = 0;
var count = 2;
var thumbsVisible = false;
var infoStartPos;
var desc1Start;
var infoRange = 399;
var infoPos = 0;
var infoPositions;

var position;
var timer;

$(document).ready
(
	function()
	{
		var ie6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false;
		
		//set the content to not display oddly in IE6 and Safari
		$('#content').css({overflow:'hidden'});
		
		//add controls to move the text up and down
		$('.info-up').click(moveInfoUp);
		$('.info-down').click(moveInfoDown);
		//set up the 'mask' for the info
		$('#home-news-container').css({top:"-450px",overflow:'hidden',width:'1020px',height:'450px',marginLeft:'-10px'});
		$('#home-news-info').css({marginTop:"410px",marginLeft:'20px'});
		$('#home-news-bg').css({marginTop:"410px",marginLeft:'0px'});
		//IE 6 fix
		if(ie6)
		{
			
		}
		$(".featured-thumb").each
		(
		 	function(i, e)
			{
				$(e).remove();
			}
		);
		$("#feature-image").html('<img src="' + arrImages[0] + '"/>');
		position = 1;
		timer = setTimeout("nextSlide()", 5000);
		
		
		$("#content").fadeIn('slow');//we need to show to calculate height
		
		//find the starting position
		$('#desc1').css({height:'83px'});
		infoStartPos = Number($('#home-news-info').css('top').split("px").join(""));
		desc1Start = 10 + $('#desc1').height();
		infoPositions = [15, -desc1Start, -infoRange];
		//set the default position
		moveInfoUp();
		//find the actual height of the project text, if it requires, allow for scrollin
		var objHeight;
		var orgH = Number($('#home-news-info-content').css('height').split("px").join(""));
		$('#home-news-info-content').css({overflow:'',height:'auto'});
		objHeight = $('#home-news-info-content').height();
		$('#home-news-info-content').css({overflow:'hidden',height:orgH});
		if(objHeight > orgH)
		{
			$("#info-content").css({overflow:'hidden'});
			$(".scrollUp").mousedown(function(){moveUpStart();});
			$(".scrollUp").mouseup(function(){moveUpStop();});
			$(".scrollDown").mousedown(function(){moveDownStart();});
			$(".scrollDown").mouseup(function(){moveDownStop();});
		}
		else
		{
			$("#hp-scrollers").hide();
		}
		
	}
);
function nextSlide()
{
	showSlide(arrImages[position++]);
	if(position >= arrImages.length)
	{
		position = 0;
	}
}
function showSlide(url)
{
	loadSlide(url);	
}
function loadSlide(path)
{
	clearTimeout(timer);
	var i = new Image(); 
	var images;
	$(i).hide();//hide first - IE is too quick when the image is cached
	i.onload = function()
	{//once loaded, we fade. Once faded, we remove the image faded onto, and set the new image's position to 'relative' for IE (otherwise opening thumbs jogs display).
		$(i).fadeIn
		(1000, 
			function()
			{
				$('#feature-image').empty();
				$('#feature-image').prepend(i);
				$('#feature-image img').css({position:'relative'});
				timer = setTimeout("nextSlide()", 5000);
			}
		);
	};
	i.src = path;
	$(i).css({position:'absolute',zIndex:3,verticalAlign:'bottom'});
	$('#feature-image').prepend(i);
	$('#feature-image img:last').css({zIndex:2});	
}
function moveInfoUp()
{
	var currentPos = infoStartPos - Number($('#home-news-info').css('top').split("px").join(""));
	if(infoPos < infoPositions.length - 1)
	{
		infoPos ++;
		if(infoPositions[infoPos] + infoStartPos <= currentPos && currentPos + infoPositions[infoPos] <= infoRange && thumbsVisible)
		{
			toggleThumbs();
		}
		if(currentPos + infoPositions[infoPos]  <= infoRange)
		{
			$('#home-news-info').animate( {top:infoPositions[infoPos]+'px' }, 750, 'easeInOutCubic');
			$('#home-news-bg').animate( {top:infoPositions[infoPos]+'px' }, 750, 'easeInOutCubic');
		}
	}
}
function moveInfoDown()
{
	var currentPos = infoStartPos - Number($('#home-news-info').css('top').split("px").join(""));
	if(infoPos > 0)
	{
		infoPos --;
		if(currentPos - infoPositions[infoPos] >= 0)
		{
			$('#home-news-info').animate( {top:infoPositions[infoPos]+'px' }, 750, 'easeInOutCubic');
			$('#home-news-bg').animate( {top:infoPositions[infoPos]+'px' }, 750, 'easeInOutCubic');
		}
	}
}
function moveDownStart()
{
	clearInterval(interval);
	interval = setInterval("moveDown()", 50);
}
function moveDownStop()
{
	clearInterval(interval);
	$('#home-news-info-content').stop();
	$('#home-news-info-content').scrollTo( {top:'+=60px' }, 500, { easing:'easeOutSine' } );
}
function moveDown()
{
	$('#home-news-info-content').scrollTo( {top:'+=10px' }, 50, { easing:'easeNone' } );
}
function moveUpStart()
{
	clearInterval(interval);
	interval = setInterval("moveUp()", 50);
}
function moveUpStop()
{
	clearInterval(interval);
	$('#home-news-info-content').stop();
	$('#home-news-info-content').scrollTo( {top:'-=60px' }, 500, { easing:'easeOutSine' } );
}
function moveUp()
{
	$('#home-news-info-content').scrollTo( {top:'-=10px' }, 50, { easing:'easeNone' } );
}
