//This script actively loads content from external sources
//and displays them within <div id="top"></div> tags.
//The content is rotated on a regular interval defined by
//var changeRate. The files to be loaded are defined as
//strings in array externalLinksTop.
//externalLinksTop[4] = "/static/breaking_news.htm";
//*/

var changeRate = 17000; //changeRate is set in milliseconds
var defaultTeamsList = "football/nfl/49ers_score.txt|football/nfl/raiders_score.txt";
var loading = "<img src=\"/static/images/ajax-loader.gif\" />";
var linkNumberTop = 4;
var linkNumberBottom = 1;
var linkNumber = 0;
var bustcachevar=0
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""
var externalLinks = new Array()
var externalLinksTop = new Array(5)
	externalLinksTop[0] = "/263/v-rotate/index.html";
	externalLinksTop[1] = "/sports/v-rotate/index.html";
	externalLinksTop[2] = "/entertainment/v-rotate/index.html";
	externalLinksTop[3] = "/402/v-rotate/index.html";
	externalLinksTop[4] = "/static/includes/headlines/breaking_news_headlines_new.html";
var externalLinksBottom = new Array(6)
	externalLinksBottom[0] = "/static/includes/hp/tools_bureau.php";
//	externalLinksBottom[1] = "/static/lower/weather.htm";
	externalLinksBottom[1] = "/static/includes/weather/weatherTab.html";
	externalLinksBottom[2] = "/330/v-toolbox/index.html";
	externalLinksBottom[3] = "/172/v-rotate/index.html";
	externalLinksBottom[4] = "/static/lower/shopping.htm";
	externalLinksBottom[5] = "/static/lower/press_club.htm";
var clickedTop=0;
var clickedBottom=0;
var init = 1;

//autoRotate() automatically moves the index variable forward by one
//and calls the insert function for the dirty work. It calls itself
//recursively after a the preset delay in variable changeRate.

function autoRotateTop() {
		if (clickedTop == 1)
			return;
		wrapAround('forward', 'top');
		tabOver(linkNumberTop, 'top', 0);
}

//controlRotation() is the manual control switch for this script. It
//provides 3 functions, forward, backward and pause. If no

function controlRotation(command, containerid) {
	wrapAround(command, containerid);
	tabOver(linkNumberTop, containerid, 1);
}

//This function should prove reusable, it simply wraps around
//an index variable to count a position within an array.
//it makes sure that if the counter is increased beyond the length
//of the array, it wraps back around to 1 and vice versa for 
//the other direction. The counter is a member of the natural
//numbers starting with 1, not 0.

function tabOver(tabSelection, containerid, clicked) {
	clickedTop=clicked;
	insert(tabSelection, containerid);
	linkNumberTop = tabSelection;
	document.getElementById('current').removeAttribute('id');
	var counter = 1;
	var newTab = document.getElementById('listoftabs');
	var numberOfTabs = newTab.childNodes.length;
	var newPos;
	for (i = 0; i < numberOfTabs; i++) {
		if (newTab.childNodes[i].nodeType == 1) {
			if (counter == tabSelection)
			newPos = newTab.childNodes[i];
			counter++;
		}
	}
	newPos.setAttribute("id", "current");
}

function tabOverBottom(tabSelection) {
	linkNumberBottom = tabSelection;
	document.cookie="lowerTabSelection="+tabSelection;
	document.getElementById("currentBottom").removeAttribute('id');
	var counter = 1;
	var newTab = document.getElementById("navlist");
	var numberOfTabs = newTab.childNodes.length;
	var newPos;
	for (i = 0; i < numberOfTabs; i++) {
		if (newTab.childNodes[i].nodeType == 1) {
			if (counter == tabSelection)
					newPos = newTab.childNodes[i].childNodes[0];
			counter++;
		}
	}
	newPos.setAttribute("id", "currentBottom");
	var bottom = document.getElementById('bottom');
	while (bottom.firstChild) {
		bottom.removeChild(bottom.firstChild);
	}
	insert(tabSelection, 'bottom');
}

function buildSportsToolbox() {

	var scoreboard = document.getElementById("sportsScoreboard");
	var cookieString = getCookie('selection0');
	if (!cookieString)
		cookieString = defaultTeamsList;
	var selectionsArray = cookieString.split("|");
	
	for (i = 0; i < selectionsArray.length; i++) {
		var container = document.createElement('div');
		container.setAttribute('id', 'toolboxContainer' + i);
		scoreboard.appendChild(container);
		insert('/static/includes/sports/' + selectionsArray[i], 'toolboxContainer' + i);
	}
}

function wrapAround(direction, containerid) {
		if (direction == 'forward') {
			if (linkNumberTop == externalLinksTop.length) {
				linkNumberTop = 0;
			}
			linkNumberTop++;
		}
		if (direction == 'back') {
			if (linkNumberTop == 1) {
				linkNumberTop = externalLinksTop.length+1;
			}
			linkNumberTop--;
		}
}

function apvDwd(pid,title,pl,t,rf,fg) {
	 if(!pl) {pl='';}
	  var rf = document.URL;
		 var w = window.open("http://video.ap.org/vws/search/aspx/ap.aspx?f="+pid+"&g="+title+"&p="+pl+"&t="+t+"&rf="+rf+"&fg=tool","apvDwd","width=788,height=598,status=1,scrollbars=1,resizable=1");
}

function insert(link_num, containerid) {
	document.getElementById(containerid).innerHTML=loading;
	if (containerid == 'top')
		var url = externalLinksTop[link_num-1];
	else if (containerid == 'bottom') {
		var url = externalLinksBottom[link_num-1];
		if (link_num == 4)
			loadobjs('/static/calendar/calendar_popup.js','/static/calendar/calendar_search.js','/static/calendar/validate_date.js');
		if (link_num == 3)
			loadobjs('http://ibd.morningstar.com/AP/graph.js');
	}
	if (isNaN(link_num))
		var url = link_num;
	var page_request = false
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		page_request = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
		} 
		catch (e){}
		try{
			page_request = new ActiveXObject("Microsoft.XMLHTTP")
		}
		catch (e){}
	}
	else
	return false
	page_request.onreadystatechange=function(){
	loadpage(page_request, containerid, link_num)
	}
	if (bustcachevar) //if bust caching of external page
		bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
	page_request.open('GET', url+bustcacheparameter, true)
	page_request.send(null)
}

function loadpage(page_request, containerid, link_num){
	if (page_request.readyState == 4) {// && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {
		document.getElementById(containerid).innerHTML=page_request.responseText;
		if (containerid == 'top') {
			var imageExists = page_request.responseText.indexOf('autoRotateTop()');
			if (imageExists > 0)
				return;
			else
				setTimeout('autoRotateTop();', changeRate);
		}
		if (containerid == 'bottom' && link_num == 2) {
			buildSportsToolbox();
		}
	}
}

function loadobjs() {
	if (!document.getElementById)
	return
	for (i=0; i<arguments.length; i++){
		var file=arguments[i]
		var fileref=""
		if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
			if (file.indexOf(".js")!=-1){ //If object is a js file
			
				fileref=document.createElement('script')
				fileref.setAttribute("type","text/javascript");
				fileref.setAttribute("src", file);
			}
			else if (file.indexOf(".css")!=-1){ //If object is a css file
				fileref=document.createElement("link")
				fileref.setAttribute("rel", "stylesheet");
				fileref.setAttribute("type", "text/css");
				fileref.setAttribute("href", file);
			}
		}
		if (fileref!=""){
			document.getElementsByTagName("head").item(0).appendChild(fileref)
			loadedobjects+=file+" " //Remember this object as being already added to page
		}
	}
}

function playMovie(id) {
	var videoPath = "http://www.fresnobee.com/static/2006/"+id;
	window.open(videoPath, 'preview', 'width=590,height=470,resizable=0,scrollbars=0').focus();
}
