var arrHomepage = [];
var position;
var totalProjects;
var timer;
var projID;

$(document).ready
(
	function()
	{
		var href = $("#home-news-title").children("a:first").attr("href");
		var text = $("#home-news-title").children("a:first").text();
		$("#home-news-title").text(text);
		
		$("#home-news").css({ cursor:"pointer" });
		$("#home-news").mouseover(function(){$("#home-news-summary").attr("style", "color:#FAFF00;");});
		$("#home-news").mouseout(function(){$("#home-news-summary").attr("style", "");});
		$("#home-news").click(function(){window.location = href});
					
		$(".featured-thumb").each
		(
		 	function(i, e)
			{
				var href = $(e).children(".thumb").children("a").attr("href");
				var title = $(e).children(".thumb-title").text();
				var projectID = getQueryVariable("projectID",href);
				var image = arrImages[projectID];
				arrHomepage.push({title:title, image:image, id:projectID});
			}
		);
		shuffle(arrHomepage);
		projID = arrHomepage[0].id;
		$("#featured-projects").html('<div class="featured-project"> \
    <div id="top-bar" style="margin-left:0px;"> \
      <div id="proj-head"> \
        ' + arrHomepage[0].title + ' \
      </div> \
    </div> \
    <div id="feature-image"><img src="' + arrHomepage[0].image + '"/></div> \
  </div>');
		$("#featured-projects").click(openCurrentProject);
		$("#featured-projects").css({cursor:'pointer'});
		totalProjects = arrHomepage.length;
		position = 1;
		timer = setTimeout("nextSlide()", 5000);
		$("#content").show();
		//
	}
);

function openCurrentProject()
{
	window.location = 'projects/page.cfm?projectID=' + projID;
}

function nextSlide()
{
	showSlide(arrHomepage[position++]);
	if(position >= totalProjects)
	{
		position = 0;
	}
}
function showSlide(obj)
{
	loadSlide(obj.title, obj.image, obj.id);	
}
function loadSlide(title, path, id)
{
	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).
		setSlideData(title, id);
		$(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 setSlideData(title, id)
{
	projID = id;
	$('#proj-head').text(title);
}