YAHOO.namespace("example.container");

var teaserIndex;
var overlayContainers;
var intervalObject;

function teaserObject(newURL, newStoryURL, newHeadline, newInnerText, newDate) {
	this.url = newURL;
	this.storyURL = newStoryURL;
	this.headline = newHeadline;
	this.innerText = newInnerText;
	this.date = newDate;
}

function init(teaserObjects) {
	var containers = new Array(teaserObjects.length);
	teaserIndex = 0;
	
	for (i=0; i < teaserObjects.length; i++) {

		// Generate teaser div from object
		var div = document.createElement("div");
		div.setAttribute("id", "teaser" + i);
		div.style.cssText = "width: 600px; height: 230px; overflow: hidden;";
		
		// hypeStory div
		var hypeStory = document.createElement("div");
		hypeStory.setAttribute("class", "hypeStory");
		
		var img = document.createElement("img");
		img.setAttribute("src", teaserObjects[i].url);
		img.setAttribute("border", "0");
		img.setAttribute("align", "left");
		img.style.cssText = "padding-right: 10px;";
		
		var imgLink = document.createElement("a");
		imgLink.setAttribute("href", teaserObjects[i].storyURL);
		imgLink.appendChild(img);
	
		// headline div
		var headline = document.createElement("div");
		headline.setAttribute("class", "headline");
		headline.style.cssText = "color: #172781; font: bold 16pt 'Georgia', Arial,sans-serif";
		
		var headlineLink = document.createElement("a");
		headlineLink.setAttribute("href", teaserObjects[i].storyURL);
		headlineLink.innerHTML = teaserObjects[i].headline;
		//headlineLink.appendChild(document.createTextNode(teaserObjects[i].headline));
		
		headline.appendChild(headlineLink);
		
		//subhead div
		var subhead = document.createElement("div");
		subhead.setAttribute("class", "subhead");
		subhead.innerHTML = teaserObjects[i].date;
		subhead.style.cssText = "font: bold 12pt 'Helvetica Neue',Arial,sans-serif";
		
		// byline div
		var byline = document.createElement("div");
		byline.setAttribute("class", "byline");
		byline.style.cssText = "font: 12pt Helvetica, sans-serif;color: gray;";
		
		// teaser div
		var teaser = document.createElement("div");
		teaser.innerHTML = teaserObjects[i].innerText;
		//teaser.style.cssText = "font: 12pt Times, serif;";
		teaser.style.cssText = "font: 12px 'Helvetica Neue',Arial,sans-serif";
		
		// build hypeStory div
		hypeStory.appendChild(imgLink);
		hypeStory.appendChild(headline);
		hypeStory.appendChild(subhead);
		hypeStory.appendChild(byline);
		hypeStory.appendChild(teaser);
		
		div.appendChild(hypeStory);

		// Add generated div to hype div
		document.getElementById("hype").appendChild(div);
		
		containers[i] = new YAHOO.widget.Overlay("teaser" + i, {xy:[0,20],
																//fixedcenter:true,
																constraintoviewport:true,
																iframe:false,
																visible:false,
																effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.5} } );
												
//		containers[i].cfg.setProperty("context", ["hypeBox", "tr", "tr"]); 
//		containers[i].render();
	}

	overlayContainers = containers;
}

function nextImage() {
	clearInterval(intervalObject);
	changeImage(0);
}

function prevImage() {
	clearInterval(intervalObject);
	changeImage(1);
}

function changeImage(direction) {
	var tmp = teaserIndex;
	// Hide current hype item

	
	// Change image based on direction parameter 0=next otherwise previous
	if (direction == 0) {
		teaserIndex = teaserIndex + 1;
	
		if (teaserIndex > overlayContainers.length-1) {
			teaserIndex = 0;
		}
	} else {
		teaserIndex = teaserIndex - 1;
		
		if (teaserIndex < 0) {
			teaserIndex = overlayContainers.length-1;
		}
	}
	
	// Show requested hype item
	overlayContainers[teaserIndex].show(overlayContainers[teaserIndex]);
	overlayContainers[tmp].hide(overlayContainers[tmp]);
}

function start() {
	init(teasers);
	overlayContainers[0].show(overlayContainers[0]);
	intervalObject = setInterval("changeImage(0);", 8000);
}