var changeRate = 10000; //changeRate is set in milliseconds
var loading = "<img src=\"http://www.mainecommonsense.org/images/design/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(4)
	externalLinksTop[0] = "rotate/from_mmpi.html";
	externalLinksTop[1] = "rotate/national_news.html";
	externalLinksTop[2] = "rotate/campaigns.html";
	externalLinksTop[3] = "rotate/welcome.html";
var externalLinksBottom = new Array(6)
	externalLinksBottom[0] = "rotate/a.html";
	externalLinksBottom[1] = "rotate/b.html";
	externalLinksBottom[2] = "rotate/c.html";
	externalLinksBottom[3] = "rotate/d.html";
	externalLinksBottom[4] = "rotate/e.html";
	externalLinksBottom[5] = "rotate/f.html";
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 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 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 (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);
		}
	}
}

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
		}
	}
}