var BillboardLength = 0;
var current = 0;
var tid = 0;
var featureNav = 'featureNav';
var btnDir = '/i/buttons/';
var dotSrcOff = 'feature_dot.gif';
var dotSrcOn = 'feature_dot_on.gif';  
var paused = 0;

function rotateBillboard(offset) {
	clearTimeout(tid);
	var index = current + offset;
	var old_current = current;
	if (index != current) {
		if (index < BillboardLength) {
			if (index < 0) {
				current = BillboardLength - 1;
			} else {
				current = index;
			}
		} else {
			current = 0;
		}
		// rotate Billboard
		switchDivs(old_current, current);
		
		// update cirlces
		switchCircles(current);
	}
	autoRotate();
}

function rotateBillboardJump(item) {
	var old_current = current;
	current = item;

	switchDivs(old_current, current);
	switchCircles(current);
}

function rotateBillboardUpdate(offset) {
	clearTimeout(tid);
	var index = current + offset;
	var old_current = current;
	if (index != current) {
		if (index < BillboardLength) {
			if (index < 0) {
				current = BillboardLength - 1;
			} else {
				current = index;
			}
		} else {
			current = 0;
		}
		// rotate Billboard
		switchDivs(old_current, current);
		switchCircles(current);
	}
	tid = setTimeout('rotateBillboard(1)',10000);
} 

function rotateBillboardPause() {
	clearTimeout(tid);
}

function rotateBillboardResume() {
	tid = setTimeout('rotateBillboard(1)', 10000);
}

function switchDivs(oldnum, newnum) {
	old_id = "story_" + oldnum;
	new_id = "story_" + newnum;
	
	
	
	if (document.getElementById){
		var old_obj = document.getElementById(old_id);
		var new_obj = document.getElementById(new_id);
		if (old_obj && old_obj.style.display == "none"){
			old_obj.style.display = "";
		} else {
			if(old_obj)
				old_obj.style.display = "none";
		}
		if (new_obj)
			new_obj.style.display = "block";
	}
}

function switchCircles(current) {
	clearTimeout(tid);
	var controls = document.getElementById(featureNav); /* get Nav container */
	if(controls)
	{
		var lis = controls.getElementsByTagName("LI"); /* get all LIs */
		if(lis)
		{
			for(var i=0; i<lis.length; i++) {
				var lid = lis[i].id;
				if(lid) {
					if(lid == 'c'+current) {
						lis[i].className = "on";
					}
					else {
						lis[i].className = "";
					}
				}
			}
		}
	}
}

function pauseRestart(id) {
	var pb = document.getElementById(id);
	if(pb)
	{
		var pbLnk = pb.getElementsByTagName("A")[0];
		if(pbLnk)
		{
			pbLnk.className = "running";
		
			pbLnk.onclick = function() {
				if(this.className == "running") {
					this.className = "paused";
					rotateBillboardPause();
					paused = 1;
				}
				else {
					this.className = "running";
					rotateBillboardResume();
					paused = 0;
				}
				return false;
			}
		}
	}
}


function autoRotate() {
	tid = setTimeout('rotateBillboard(1)',10000);
}

function pauseOnHover() {
	var rfPhoto = '';
	var rfStory = '';
	for(var i=0;i<BillboardLength;i++) {
		document.getElementById('story_'+i) ? rfStory = document.getElementById('story_'+i) : rfStory = "";
		if(rfStory && rfStory.getElementsByTagName("h2")[0]) {
			rfHeadline = rfStory.getElementsByTagName("h2")[0];
			rfHeadline.onmouseover = function() {
				rotateBillboardPause();
			}
			rfHeadline.onmouseout = function() {
				if(paused !=1) {
					rotateBillboardResume();
				}
			}
		}
		
		if(rfStory && rfStory.getElementsByTagName("div")[0]) {
			rfPhoto = rfStory.getElementsByTagName("div")[0];
			if(rfPhoto.className == "photo") {
				rfPhoto.onmouseover = function() {
					rotateBillboardPause();
				}
				rfPhoto.onmouseout = function() {
					if(paused != 1) {
						rotateBillboardResume();
					}
				}
			}
		}
	}
}


function startRotator() {
	pauseOnHover();
	autoRotate();
	pauseRestart("pause")
}

YAHOO.util.Event.addListener(window, "load", startRotator);