window.rotatingStoryBehavior = new function() {
		this.intStoryChangeInterval = 5000;
		this.oStoryRotationTimer = null;
		this.rotatingStoryBehavior = 0;
		this.intNumberOfStories = 0;
		this.intCurrentStoryNumber = 0;
		this.oStatusBar = null;
		this.arrStories = [];
		this.oControlNext = null;
		
		eval(estrada.namespace);
		
	tilt.attachEvent(document, "layout", function() {
		
		////////////////////////////////////////////
		// create layout
		////////////////////////////////////////////
		var layout = window.commonLayout.layout
						
		layout.bind("rotating-stories").to(document);

	});
	this.rotateStory = function() {
		$("div.l-status-bar div.l-ctrl-next").click();
		rotatingStoryBehavior.startStoryRotation();
	}
	this.nextStory = function() {
		rotatingStoryBehavior.adjustStoryNumber(1);
	}
	this.prevStory = function() {
		rotatingStoryBehavior.adjustStoryNumber(-1);
	}
	this.lastStory = function() {
		rotatingStoryBehavior.adjustStoryNumber(2);
	}
	this.firstStory = function() {
		rotatingStoryBehavior.adjustStoryNumber(0);
	}
	this.adjustStoryNumber = function(intAdjustment) {
		rotatingStoryBehavior.stopStoryRotation();
		rotatingStoryBehavior.hideStories();
		switch (intAdjustment) {
			case 0:
				rotatingStoryBehavior.intCurrentStoryNumber = 0;
				break;
			case -1:
				if (--rotatingStoryBehavior.intCurrentStoryNumber < 0) {
					rotatingStoryBehavior.intCurrentStoryNumber =
						rotatingStoryBehavior.intNumberOfStories - 1;
				}
				break;
			case 1:
				if (++rotatingStoryBehavior.intCurrentStoryNumber >=
						rotatingStoryBehavior.intNumberOfStories) {
					rotatingStoryBehavior.intCurrentStoryNumber = 0;
				}
				break;
			default:
				rotatingStoryBehavior.intCurrentStoryNumber =
					rotatingStoryBehavior.intNumberOfStories - 1;
				break;
		}
		rotatingStoryBehavior.showStory(
			rotatingStoryBehavior.intCurrentStoryNumber);
	}
	
	this.hideStories = function() {
		foreach(map(rotatingStoryBehavior.arrStories), function(oStory) {
			oStory.style.display="none";
		});
	}
	this.startStoryRotation = function() {
		if (!rotatingStoryBehavior.oStoryRotationTimer) {
			rotatingStoryBehavior.oStoryRotationTimer =
				setInterval(rotatingStoryBehavior.rotateStory,
				rotatingStoryBehavior.intStoryChangeInterval);
		}
	}
	this.stopStoryRotation = function() {
		if (rotatingStoryBehavior.oStoryRotationTimer) {
			clearInterval(rotatingStoryBehavior.oStoryRotationTimer);
			rotatingStoryBehavior.oStoryRotationTimer = null;	
		}
	}
	this.showStory = function(intStoryNumber) {
		var oStory = rotatingStoryBehavior.arrStories[intStoryNumber];
		if (oStory) {
			oStoryDate = first(getElements(oStory, "span", "story-date"));
			if (oStoryDate) {
				rotatingStoryBehavior.updateStatus(intStoryNumber);
				oStoryDate.parentNode.insertBefore(rotatingStoryBehavior.oStatusBar,
					oStoryDate);
			}
			oStory.style.display = "block";
		}
	}
	this.updateStatus = function(intStoryNumber) {
		var oStatusInfo = first(getElements(rotatingStoryBehavior.oStatusBar,
			"div", "l-status-info"));
		if (oStatusInfo) {
			oStatusInfo.innerHTML = (intStoryNumber+1) + " of " +
				rotatingStoryBehavior.intNumberOfStories;
		}
	}
}
///////////////////////////////////////////////////////////////////////
// check for and implement rotating stories
///////////////////////////////////////////////////////////////////////
estrada.behavior("rotating-stories").is({
	bind: function(node) {
		var oRotatingStories = first(getElements(
			node, "div", "rotating-stories"));
		if (oRotatingStories) {
			var oDate, oLink, oMore, oNewLink, oStatusInfo, oPicture;
			var oDescription, oNextNode, oTitleDiv;
			var strDateTime, oDate = new Date();
			foreach(map(getElements(oRotatingStories, "li", "story")),
				function(oStory) {
				oStory.style.display ="none";
				oStoryDate = first(getElements(oStory, "span", "story-date"));
				if (oStoryDate) {
					oRotationDate = first(getElements(
						oStory, "abbr", "rotation-date"));
					if (oRotationDate) {
						strDateTime = oRotationDate.title;
						oRdate = new Date();
						oRdate.setFullYear(strDateTime.substring(0,4));
						oRdate.setMonth(strDateTime.substring(5,7)-1);
						oRdate.setDate(strDateTime.substring(8,10));
						if (oDate > oRdate) {
							oStory.parentNode.removeChild(oStory);
						} else {
							if (!rotatingStoryBehavior.oStatusBar) {
								rotatingStoryBehavior.oStatusBar =
									document.createElement("div");
								rotatingStoryBehavior.oStatusBar.className =
									"l-status-bar";
								rotatingStoryBehavior.oStatusBar.innerHTML =
									"<div class=\"l-status-info\"></div>" +
									"<div class=\"l-player-controls\">" +
									"<div class=\"l-control l-ctrl-last\"></div>" +
									"<div class=\"l-control l-ctrl-next\"></div>" +
									"<div class=\"l-control l-ctrl-play\"></div>" +
									"<div class=\"l-control l-ctrl-stop\"></div>" +
									"<div class=\"l-control l-ctrl-prev\"></div>" +
									"<div class=\"l-control l-ctrl-first\"></div>" +
									"</div>"
								oStoryDate.parentNode.insertBefore(
									rotatingStoryBehavior.oStatusBar, oStoryDate);
								oStatusInfo = first(getElements(
									oStory, "div", "l-status-info"));
								setControlAction("last",
									rotatingStoryBehavior.lastStory);
								setControlAction("first",
									rotatingStoryBehavior.firstStory);
								setControlAction("next",	
									rotatingStoryBehavior.nextStory);
								setControlAction("prev",
									rotatingStoryBehavior.prevStory);
								setControlAction("play",
									rotatingStoryBehavior.rotateStory);
								setControlAction("stop",
									rotatingStoryBehavior.stopStoryRotation);
								this.oControlNext = $(".l-ctrl-next");
							}
						}
						oRotationDate.parentNode.className =
							oRotationDate.className + "-div";
						oLink = first(getElements(oStory, "a", "url"));				
						if (oLink) {
							oDescription = first(getElements(
								oStory, "p", "description"));
							if (oDescription) {
								oTitleDiv = document.createElement("div");
								oTitleDiv.className = "l-story-title";
								oTitleDiv.innerHTML = oLink.innerHTML;
								oDescription.parentNode.insertBefore(oTitleDiv, oDescription);
								oDescription.parentNode.removeChild(oLink);
								oNewLink = oLink.cloneNode(true);
								oNewLink.innerHTML = "MORE";
								oNewLink.className = "l-more";
								oDescription.appendChild(oNewLink);
							}
						}
					}
				}	
			});
			rotatingStoryBehavior.arrStories = getElements(oRotatingStories,
				"li", "story");
			if (rotatingStoryBehavior.arrStories.length > 0) {
				rotatingStoryBehavior.intNumberOfStories =
					rotatingStoryBehavior.arrStories.length;
				rotatingStoryBehavior.intCurrentStoryNumber = 0;
				rotatingStoryBehavior.updateStatus(
					rotatingStoryBehavior.intCurrentStoryNumber);
				rotatingStoryBehavior.arrStories[0].style.display = "block";
				if (rotatingStoryBehavior.intNumberOfStories > 1) {
					rotatingStoryBehavior.startStoryRotation();
				}
			} else {
				$("div.rotating-stories").remove();
			}
		}
		function setControlAction(strAction, oCallBack) {
			$("div.l-status-bar div.l-ctrl-"+strAction).click(oCallBack);
		}
	}
});