function writeNewsLoader() {
// Writes a loading gif in the placeholder element, to be removed by moveNews.

	var newhome = document.getElementById('home_headlines_placeholder');
	var img = document.createElement('img');
	img.setAttribute('src','images/homepage/ajax-loader.gif');
	img.setAttribute('border','0');
	var div = document.createElement('div');
	div.setAttribute('id','home_headlines_loading');
	div.style.textAlign='center';
	div.style.padding='8em 0 12em';
	div.appendChild(img);
	newhome.appendChild(div);
}

function moveNews() {
// Moves the content returned from the RSS reader to its location on the page. The reader is
// executed at the end of the markup to avoid lag in rendering the page while it executes.

	var content = document.getElementById('home_headlines_content');
	var newhome = document.getElementById('home_headlines_placeholder');
	var loadimg = document.getElementById('home_headlines_loading');
	newhome.removeChild(loadimg);
	$(newhome).html($(content).html());
	content.parentNode.removeChild(content);
}

$(document).ready(function() {
	var arr = [];
	var timerID;
	var curr;
	var len;
	var dir = "up";
	$.ajax({type:"GET",url:"/images/homepage/banners/banners.xml",dataType:"xml",success:parseXml});
	
	function parseXml(xml) {
		var i = 0;
		$(xml).find("item").each(function() {
			arr[i] = [];
			arr[i] = [$(this).attr("title"),$(this).attr("url"),$(this).attr("name"),$(this).text()];
			i++;
		});
		len = i;
		initialize();
	}
	function initialize() {
		curr = Math.floor(Math.random()*len);
		var nextcurr = (curr+1)%len;
		$("div#rightside").prepend("<img class='current' src='/images/homepage/banners/full/"+arr[curr][2]+"'>");//current banner, top
		$("div#rightside").prepend("<img class='next' src='/images/homepage/banners/full/"+arr[nextcurr][2]+"'>");//next banner, bottom
		if(arr[curr][3] == "") {
			$("div#infobox").attr("class","hide");
		} else {
			$("div#infobox").html("<h3>"+arr[curr][0]+"</h3><p>"+arr[curr][3]+"</p>");
		}
		var i = (curr+1)%len;
		for(var lcv=0; lcv<3; lcv++) {
			$("div#thumbs").append("<a><img src='/images/homepage/banners/thumb/"+arr[i][2]+"'/></a>");
			$("div#thumbs a:last").click(function() {thumbClickHandler(this);});
			i = (i+1)%len;
		}
		$("a#up").click(function() {
			clearInterval(timerID);
			if($("div#thumbs a").size() < 4) { //finished moving, or it's ok to rotate.
				rotatePhotosUp(0);
			}
		});
		$("a#down").click(function() {
			clearInterval(timerID);
			if($("div#thumbs a").size() < 4) { //finished moving, or it's ok to rotate.
				rotatePhotosDown(1);
			}
		});
		timerID = setInterval(rotatePhotosUp,12000);
	}
	function rotatePhotosUp(tempdir,index) {//tempdir is for changes between up and down buttons
		if(!index) {var index = 0;}
		if(!tempdir) {var tempdir = 1;}
		curr = (curr+index+1)%len;
		//alert("newcurr: "+curr+" clicked on index: "+index);
		if((tempdir == 1 && dir == "down") || index>0) {
			$("div#rightside img.next").attr("src","/images/homepage/banners/full/"+arr[curr][2]);
			dir = "up";
		}
		$("div#rightside img.current").removeClass().addClass("fading");
		$("div#rightside img.next").removeClass().addClass("current");
		$("div#rightside img.fading").fadeOut(300,function() {
			var nextcurr = (curr+1)%len;
			$(this).remove();
			$("div#rightside").prepend("<img class='next' src='/images/homepage/banners/full/"+arr[nextcurr][2]+"'>");//next banner, bottom
		});
		if(arr[curr][3] == "") {
			$("div#infobox").attr("class","hide");
		} else {
			$("div#infobox").attr("class","visible").html("<h3>"+arr[curr][0]+"</h3><p>"+arr[curr][3]+"</p>");
		}
		if(index>0) {
			$("div#thumbs a:lt("+(index+1)+")").slideUp(325,function() {$(this).remove();});
			for(var i=3-index; i<=3; i++) {
				//alert("curr:"+curr+" i:"+i+" adding thumb: "+((curr+i)%len));
				$("div#thumbs a:last").after("<a><img src='/images/homepage/banners/thumb/"+arr[(curr+i)%len][2]+"'/></a>");
				$("div#thumbs a:last").click(function() {thumbClickHandler(this);});
			}
		} else {
			$("div#thumbs a:first").slideUp(325,function() {$(this).remove();});
			var newthird = (curr+3)%len;
			$("div#thumbs a:last").after("<a><img src='/images/homepage/banners/thumb/"+arr[newthird][2]+"'/></a>");
			$("div#thumbs a:last").click(function() {thumbClickHandler(this);});
		}
	}
	function rotatePhotosDown(tempdir) {
		if(!tempdir) {var tempdir = 1}
		curr = (curr==0) ? len-1 : curr-1;
		if(tempdir == 1 && dir == "up") {
			$("div#rightside img.next").attr("src","/images/homepage/banners/full/"+arr[curr][2]);
			dir = "down";
		}
		$("div#rightside img.current").removeClass().addClass("fading");
		$("div#rightside img.next").removeClass().addClass("current");
		$("div#rightside img.fading").fadeOut(300,function() {
			var nextcurr = (curr==0) ? len-1 : curr-1;
			$(this).remove();
			$("div#rightside").prepend("<img class='next' src='/images/homepage/banners/full/"+arr[nextcurr][2]+"'>");//next banner, bottom
		});
		if(arr[curr][3] == "") {
			$("div#infobox").attr("class","hide");
		} else {
			$("div#infobox").attr("class","visible").html("<h3>"+arr[curr][0]+"</h3><p>"+arr[curr][3]+"</p>");
		}
		var newtop = (curr+1)%len;
		$("div#thumbs a:first").before("<a><img src='/images/homepage/banners/thumb/"+arr[newtop][2]+"'/></a>");
		$("div#thumbs a:first").hide().slideDown(325,function() {$("div#thumbs a:last").remove();});
		$("div#thumbs a:first").click(function() {thumbClickHandler(this);});
	}
	function thumbClickHandler(a) {
		clearInterval(timerID);
		if($("div#thumbs a").size() < 4) { //finished moving, or it's ok to rotate.
			rotatePhotosUp(0,$(a).index());
		}
	}
});
