var itemcount = 0;
var curitem = 1;
var rotateinterval;

$(function() {
	$("#searchquery").val("Search Directory...").focus(function() {
		$(this).val($(this).val().replace("Search Directory...",""));
	});
	$.get("home/getnews", {}, initrotator);
});

function initrotator(xml, textStatus) {
	var maxheight = 0;
	if ($("item", xml).length > 0) {
		$("#newspanel").prev().prepend('<span id="indicators"></span>');
		$("#newsitems").empty();
		$("item", xml).each(function() {
			itemcount++;
			$("<div></div>").attr('id', 'item'+itemcount).html($(this).text()).hide().appendTo("#newsitems");
			$("#indicators").append('<a id="ind'+itemcount+'" href="#" title="'+$("#item"+itemcount+" h1").text()+'">&nbsp;</a>');
			maxheight = Math.max(maxheight, $("#item"+itemcount).height());
		});
		$("#newsitems").height(maxheight).find("div").css("position", "absolute");
		$("#indicators").delegate("a", "click", function(e) {
			e.preventDefault(); rotate($("#indicators a").index(this)+1);
		});
		$("#item1").show();
		$("#ind1").addClass("current");
		rotateinterval = setInterval("rotate(0)", 7000);
	}
}

function rotate(setitem) {
	if (setitem == curitem) {
		return;
	} else if (setitem > 0 && setitem <= itemcount) {
		curitem = setitem;
		clearInterval(rotateinterval);
	} else {
		curitem++;
		if (curitem > itemcount) {
			curitem = 1;
		}
	}
	$("#newsitems div:visible").fadeOut("slow");
	$("#indicators a").removeClass("current");
	$("#item"+curitem).fadeIn("slow");
	$("#ind"+curitem).addClass("current");
}