//Change this if the number of ads change.
var number_of_ads = 2;

function addLoadEvent(func) //allows multiple events to occur when page is loaded
{
	var oldonload = window.onload;
	if(typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

function createAjaxRequest()
{
	var request;
	
	try
	{
		request = new XMLHttpRequest();
	} 
	catch(trymicrosoft)
	{
		try
		{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(othermicrosoft)
		{
			try
			{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(failed) 
			{
				request = false;
			}
		 }
	}
	
	return request;
}

var bannerRotation = true;
var storyRotation = false;
var stories = new Array();
var banner = new Array(number_of_ads);
var bannercounter = 0;
var request = false;
var requestIsBusy = false;
var thumbs = new Array();
var current_story = 0; //used by setRotation

//If the "noajax" URL variable doesn't exist
if(window.location.search.search("noajax") == -1)
{
	request = createAjaxRequest();
}

if(request)
{
	addLoadEvent(getThumbs);
	addLoadEvent(setTabLinks);
	addLoadEvent(setThumbnailLinks);
    addLoadEvent(initializeBanner);
}

//Disable all AJAX-related code
function resetPage()
{
	if(storyRotation)
	{
		clearInterval(storyRotation);
	}
	
	if(bannerRotation)
	{
		clearInterval(bannerRotation);
	}
	
	var tabs = document.getElementById('tabs').getElementsByTagName('a');
	for(var i = 0; i < tabs.length; i++)
	{
		tabs[i].onclick = '';
		tabs[i].href += '&noajax=true';
	}

	if(document.getElementById('thumbnails'))
	{
		var thumbs = document.getElementById('thumbnails').getElementsByTagName('a');
		for(var i = 0; i < thumbs.length; i++)
		{
			thumbs[i].onclick = '';
			thumbs[i].href += '&noajax=true';
		}
	}
}

//-----Tab functions-----

function setTabLinks()
{
	var tabs = document.getElementById('tabs').getElementsByTagName('a');
	
	for(var i = 0; i < tabs.length; i++)
	{
		var tabname = tabs[i].href;
		tabname = tabname.replace(/.*?tab=/,""); //Remove everything before the tab name
		tabname = tabname.replace(/&.*/,""); //Remove everything after the tab name
		setTab(tabname, tabs[i]);
	}
}

function setTab(tabname, tab)
{
	if(tabname == "look")
	{
		tab.onclick = function() {setContent('look'); return false; };
	}
	else
	{
		tab.onclick = function() {setContent('listen'); return false; };
	}
}

function setContent(current_tab)
{
	if(storyRotation)
	{
		clearInterval(storyRotation);
	}
	
	var url = current_tab + ".cfm";
	
	request = createAjaxRequest();
	
	//If request is made while another request is being processed, kill the old request
	if(requestIsBusy)
	{
		request.onreadystatechange = function () {};
		request.abort();
	}
	
	request.open("GET", url, true);
	requestIsBusy = true;
	
	request.onreadystatechange = function()
	{
		if(request.readyState == 4)
		{
			if(request.status == 200)
			{
				var content = request.responseText;
				requestIsBusy = false;
	
				if(content)
				{
					document.getElementById("content").innerHTML = content;
					if(current_tab == 'look')
					{
						getThumbs();
						setThumbnailLinks();
					}
					//Make tab selected
					var tabs = document.getElementById('tabs').getElementsByTagName('a');

					for(var i = 0; i < tabs.length; i++)
					{
						var tabname = tabs[i].href;
						tabname = tabname.replace(/.*?tab=/,""); //Remove everything before the tab name
						tabname = tabname.replace(/&.*/,""); //Remove everything after the tab name
						if(tabname == current_tab)
						{
							tabs[i].className = "selected";
						}
						else
						{
							tabs[i].className = "";
						}
					}
				}
			}
		}
	};
	
	request.send(null);
}

//-----Functions used by the "look" and "learn" tabs-----
function getThumbs()
{
	thumbs = new Array(); //clear thumbs
	
	for(var i = 0; i < 8; i++)
	{
		getThumbsAjax(i);
	}
}

function getThumbsAjax(i)
{
	var url = 'get_thumbs.cfm?thumbnumber=' + i;
		
	var thumbrequest = createAjaxRequest();
	thumbrequest.open("GET", url, true);
	
	thumbrequest.onreadystatechange = function()
	{
		if(thumbrequest.readyState == 4)
		{
			if(thumbrequest.status == 200)
			{
				var thumb_info = thumbrequest.responseText;
				if(thumb_info)
				{
					thumb_info = eval("(" + thumb_info + ")");

					thumbs[i] = new Array();
					thumbs[i]['ID'] = thumb_info['ID'];
					thumbs[i]['title'] = thumb_info['title'];
					thumbs[i]['thumb'] = thumb_info['thumb'];
					getStory(thumbs[i]['ID']);
				}
			}
		}
	};
	
	thumbrequest.send(null);
}

function setThumbnailLinks()
{
	if(document.getElementById('thumbnails'))
	{	
		var thumbs = document.getElementById('thumbnails').getElementsByTagName('ul')[0].getElementsByTagName('a');
		
		//There will always be 4 thumbnails, plus "previous" and "next" links 
		for(var i = 1; i < 5; i++)
		{	
			var storyID = thumbs[i].href;
			storyID = storyID.replace(/.*?id=/,""); //Remove everything before the ID number
			storyID = storyID.replace(/&.*/,""); //Remove everything after the ID number
			setThumb(storyID, thumbs[i]);
		}
		
		thumbs[0].onclick = function () { 
			if(storyRotation)
			{
				clearInterval(storyRotation);
			}
			nextSetOfThumbs();
			return false;
		};
		thumbs[5].onclick = function () { 
			if(storyRotation)
			{
				clearInterval(storyRotation);
			}
			nextSetOfThumbs();
			return false;
		};
		
		storyRotation = setInterval(setRotation,10000);
	}
}

//Determine whether the thumbnails being displayed are 1-4 or 5-8
function determineFirst()
{
	var thumb_a = document.getElementById('thumbnails').getElementsByTagName('ul')[0].getElementsByTagName('a');
	var first = thumb_a[1].href;
	first = first.replace(/.*&first=/,"");
	
	return first;
}

//Change the thumbnails from 1 - 4 to 5 - 8 or vice versa.
//The thumbnails are elements 1 - 4 in the li list (0 and 5 are the arrows) and either 0-3 or 4-7 in the thumbs array.
function nextSetOfThumbs()
{
	var first = determineFirst();
	var thumb_a = document.getElementById('thumbnails').getElementsByTagName('a');
	
	if(first == 1)
	{
		for(var i = 1; i <= 4; i++)
		{
			thumb_a[i].href = 'index.cfm?id=' + thumbs[i+3].ID + '&first=5';
			thumb_a[i].getElementsByTagName('img')[0].src = thumbs[i+3].thumb;
			thumb_a[i].getElementsByTagName('img')[0].alt = thumbs[i+3].title;
			thumb_a[i].getElementsByTagName('img')[0].className = '';
			
			setThumb(thumbs[i+3].ID, thumb_a[i]);
		}
	}
	else
	{
		for(var i = 1; i <= 4; i++)
		{
			thumb_a[i].href = 'index.cfm?id=' + thumbs[i-1].ID + '&first=1';
			thumb_a[i].getElementsByTagName('img')[0].src = thumbs[i-1].thumb;
			thumb_a[i].getElementsByTagName('img')[0].alt = thumbs[i-1].title;
			thumb_a[i].getElementsByTagName('img')[0].className = '';
			
			setThumb(thumbs[i-1].ID, thumb_a[i]);
		}
	}
}

function setThumb(storyID, thumb)
{
	thumb.onclick = function() {
		if(storyRotation)
		{
			clearInterval(storyRotation);
		}
		changeStory(storyID, false); 	
		return false; 
	};
}

function setThumbClass(id)
{
	var thumb_a = document.getElementById('thumbnails').getElementsByTagName('a');
		
	for(var i = 1; i < 5; i++)
	{
		thumb_a[i].getElementsByTagName('img')[0].className = "";
		var storyID = thumb_a[i].href;
		storyID = storyID.replace(/.*?id=/,""); //Remove everything before the ID number
		storyID = storyID.replace(/&.*/,""); //Remove everything after the ID number
		if(storyID == id)
		{
			thumb_a[i].getElementsByTagName('img')[0].className = "selectedthumb";
		}
	}
}

//Use Ajax to get a story
function getStory(storyID)
{
	if(!stories['story' + storyID])
	{
		var url = "get_stories.cfm?id=" + storyID;
		
		var storyrequest = createAjaxRequest();
		storyrequest.open("GET", url, true);
		
		storyrequest.onreadystatechange = function()
		{
			if(storyrequest.readyState == 4)
			{
				if(storyrequest.status == 200)
				{
					var story = storyrequest.responseText;

					if(story)
					{		
						story = eval("(" + story + ")");
						
						stories['story' + storyID] = new Array();
						stories['story' + storyID]['title'] = story.TITLE;
						stories['story' + storyID]['story_url'] = story.STORY_URL;
						stories['story' + storyID]['summary'] = story.SUMMARY;
						
						var newimg = document.createElement('img');
						newimg.src = story.IMAGE;
						newimg.alt = '';
						newimg.id = 'story' + storyID + 'image';
						document.getElementById('storyimages').appendChild(newimg); //Load image in hidden div so that the image is already in browser's cache when it's needed by the story rotation
					}
				}
			}
		};
		
		storyrequest.send(null);
	}
}

//Retrieve the new story and replace the current story
function changeStory(storyID, fade)
{
	if(stories['story' + storyID])
	{
		var content = document.getElementById('tabMainContent');
		
		var fadeimage = new Fx.Style($('tabMainImage'),'opacity', {duration:2000});
		var fadecontent = new Fx.Style($('tabMainContent'),'opacity', {duration:2000});
	
		if(fade) //fade out
		{
			fadeimage.start(1,0).chain(function () {this.start(0,1);});
			fadecontent.start(1,0).chain(function () {replaceContent(content,'story' + storyID); this.start(0,1); 		setThumbClass(storyID);});
		}
		else
		{
			replaceContent(content,'story' + storyID);
			setThumbClass(storyID);
		}
	}
}

function replaceContent(content,ID)
{
	//Set title
	content.getElementsByTagName('h1')[0].childNodes[0].nodeValue = stories[ID]['title'];
	//Set image
	document.getElementById('tabMainImage').getElementsByTagName('a')[0].href = stories[ID]['story_url'];
	document.getElementById('tabMainImage').getElementsByTagName('img')[0].src = document.getElementById(ID + 'image').src;
	document.getElementById('tabMainImage').getElementsByTagName('img')[0].alt = stories[ID]['title'];
	//Set link
	content.getElementsByTagName('p')[1].getElementsByTagName('a')[0].href = stories[ID]['story_url'];
	//Set lead-in
	content.getElementsByTagName('p')[0].innerHTML = stories[ID]['summary'];
}

function setRotation()
{
	current_story++;
	if(current_story >= 8)
	{
		current_story = 0;	
	}
	
	if(current_story == 0 || current_story == 4)
	{
		
		nextSetOfThumbs();
	}
	
	changeStory(thumbs[current_story]['ID'], true);
}

//-------Banner functions-----

function initializeBanner()
{
	loadBanner();
	bannerRotation = setInterval(setBanner,4000);
}

function loadBanner()
{
	for(var i = 0; i < number_of_ads; i++)
	{
		bannerAjax(i);
	}
}

function bannerAjax(adNumber)
{
	var url = "cfads.cfm?json=true&rotating_image=" + adNumber;
	
	var request = createAjaxRequest();
	request.open("GET", url, true);
	
	request.onreadystatechange = function()
	{
		if(request.readyState == 4)
		{
			if(request.status == 200)
			{
				var ad = request.responseText;
		
				if(ad)
				{				
					ad = eval("(" + ad + ")");
					banner[adNumber] = new Array();
					banner[adNumber]['link'] = ad.LINK;
					banner[adNumber]['img'] = ad.IMAGE;
					banner[adNumber]['alt'] = ad.ALT;
				}
			}
		}
	};		
	request.send(null);
}

function setBanner()
{
	if(document.getElementById("tabSubAdlisten"))
	{
		var bannerdiv = document.getElementById("tabSubAdlisten");
	}
	else
	{
		var bannerdiv = document.getElementById("tabSubAd");
	}

	bannerdiv.getElementsByTagName('a')[0].href = banner[bannercounter]['link'];
	bannerdiv.getElementsByTagName('a')[0].onclick = function() {return true;};
	bannerdiv.getElementsByTagName('img')[0].src = banner[bannercounter]['img'];
	bannerdiv.getElementsByTagName('img')[0].alt = banner[bannercounter]['alt'];
	
	if(bannercounter == number_of_ads - 1)
	{
		bannercounter = 0;
	}
	else
	{
		bannercounter++;
	}
}

//-----Mouseover sets the mouseover and mouseout properties for the RowanToday links-----
addLoadEvent(mouseover);

function mouseover()
{
	var images = document.getElementById('rowanToday').getElementsByTagName('img');
	
	for(var i = 0; i < images.length; i++)
	{
		if(images[i].src == "http://www.rowan.edu/images/news_off6.gif")
		{
			setImage(images[i],"news_on6.gif","news_off6.gif");
		}
		if(images[i].src == "http://www.rowan.edu/images/news_off1.gif")
		{
			setImage(images[i],"news_on1.gif","news_off1.gif");
		}
		if(images[i].src == "http://www.rowan.edu/images/news_off2.gif")
		{
			setImage(images[i],"news_on2.gif","news_off2.gif");
		}
		if(images[i].src == "http://www.rowan.edu/images/news_off3.gif")
		{
			setImage(images[i],"news_on3.gif","news_off3.gif");
		}
		if(images[i].src == "http://www.rowan.edu/images/news_off4.gif")
		{
			setImage(images[i],"news_on4.gif","news_off4.gif");
		}
		if(images[i].src == "http://www.rowan.edu/images/news_off5.gif")
		{
			setImage(images[i],"news_on5.gif","news_off5.gif");
		}
	}
}

function setImage(image,onsource,offsource)
{
	image.onmouseover = function(){image.src = "http://www.rowan.edu/images/" + onsource; return false;};
	image.onmouseout = function(){image.src = "http://www.rowan.edu/images/" + offsource; return false;};
}
