/*requires jquery-1.3.2.min.js*/
/*requires Text.js*/

$(function () {
	// init news rotator
	var wrapper = $("#BusinessNewsRotator .Wrapper"),
		items = $("#BusinessNewsRotator li").css({display: "none", width: "180px"}),
		itemCountElement,
		noOfItems = items.length,
		currentItem = 0;
		
	// create navigation
	$(document.createElement("a"))
		.attr({
			id: "RotatorPreviousItem",
			href: "#",
			title: GetText("PreviousLabel")
		})
		.append("<span>&laquo;</span>")
		.appendTo("#BusinessNewsRotator .Header");
	itemCountElement = $(document.createElement("span")).attr("id", "RotatorItemCount").appendTo("#BusinessNewsRotator h3");
	$(document.createElement("a"))
		.attr({
			id: "RotatorNextItem",
			href: "#",
			title: GetText("NextLabel")
		})
		.append("<span>&raquo;</span>")
		.appendTo("#BusinessNewsRotator .Header");
		
	$(items[currentItem]).css("display", "block");
	wrapper.css({
		height: $(items[currentItem]).outerHeight() + "px",
		overflow: "hidden"
	});
	itemCountElement.text((currentItem + 1) + "/" + noOfItems);
	
	// set controller events	
	$("#RotatorNextItem").click(function () {
		if (currentItem < (noOfItems - 1)) {
			currentItem += 1;
		} else {
			currentItem = 0;
		}
		
		$(items).css("display", "none");
			
		$(items[currentItem]).css("display", "block");
		wrapper.css("height", $(items[currentItem]).outerHeight() + "px");
		itemCountElement.text((currentItem + 1) + "/" + noOfItems);
		
		return false;		
	});
	
	$("#RotatorPreviousItem").click(function () {
		if (currentItem > 0) {
			currentItem -= 1;
		} else {
			currentItem = (noOfItems - 1);
		}
		
		$(items).css("display", "none");
		
		$(items[currentItem]).css("display", "block");
		wrapper.css("height", $(items[currentItem]).outerHeight() + "px");
		itemCountElement.text((currentItem + 1) + "/" + noOfItems);
		
		return false;
	});
});
