  /*       Javascript document created by Rahul for the PLME Senate.       */
 /* If you need to contact me, my email is "rb1248" att "gmail" dot 'com' */
/*                     Last updated: 30 December 2008                    */ 


/////////////////////////////////// NEWSFEED FUNCTIONS //////////////////////////////////////////////
addOnload(activateNewsfeed);
function activateNewsfeed() { //make the newsfeed entries clickable
	var newsfeed_ul = document.getElementById('newsfeed_container').getElementsByTagName('ul')[0];
	var spans = newsfeed_ul.getElementsByTagName('span');
	for(var i=0; i < spans.length; i++) {
		spans[i].onclick = function() {
			if(this.className != 'header') return; //this isn't the header span
			var info = this.parentNode.getElementsByTagName('span')[1]; //[0] is the header span itself
			if(info.style.display == 'block') info.style.display = 'none';
			else info.style.display = 'block';
		}	
	}
}


//////////////////////////////////////// CALENDAR FUNCTIONS ///////////////////////////////////
function calendar(month,year) { //fetch calendar months via ajax and load event descriptions
	titles = null;
	descriptions = null;
	var findDomainName = document.URL.match(/^(.+senate\/).+$/); //either www.brown.edu or just brown.edu
	var URL = findDomainName[1] + 'includes/calendar.php?';
	URL += 'month=' + month + '&year=' + year;
	var div = document.getElementById('calendar_container');
	var request = new ajax();
	request.onreadystatechange = function() {
		if(request.readyState == 4) {
			div.innerHTML = request.responseText;
			var script = document.createElement('script');
			script.setAttribute('type','text/javascript');
			script.setAttribute('src',URL + '&javascript=yes'); //an array of event titles/descriptions
			
			//does another script like this already exist?  If so, remove it...
			var currentScripts = document.getElementsByTagName('head')[0].getElementsByTagName('script');
			var lastScript = currentScripts[currentScripts.length - 1];
			var replaceOldScript = (lastScript.getAttribute('src').indexOf('calendar') > -1) ? true: false;
			if(replaceOldScript) lastScript.parentNode.removeChild(lastScript);
			document.getElementsByTagName('head')[0].appendChild(script);
		}
	}
	request.open('GET',URL,true);
	request.send(null);
	
	return false;
}
function showEvent(num) { //load the event description when clicked on by a link
	if(typeof titles != 'object' || typeof descriptions != 'object') return false;
	var message = '<p id="message_top">' + titles[num] + '</p>';
	message += '<p id="message_bottom">' + descriptions[num] + '</p>';
	document.getElementById('calendar_description').innerHTML = message;
	return false;
}

////////////////////////////////////// PHOTO FUNCTIONS ////////////////////////////////////////////
//will return true if the browser is Internet Exploder, so no opacity effects will happen
var stupid_browser = (navigator.appName.indexOf('Microsoft') > -1) ? true: false;

addOnload(loadFirstPicture);
function loadFirstPicture() { //setup the global div, load new picture, fade after 5s
	self.scrollTo(0,0);
	div = document.getElementById('photos_container');
	var firstPhoto = div.getElementsByTagName('img')[0];
	if(!stupid_browser) firstPhoto.style.opacity = 1;
	var x = function() {incomingPhoto = generateNewImage();} //create the incoming photo, this is a global
	window.setTimeout(x,2000); //generate the next image in 2 seconds
	window.setTimeout(fadeoutPhoto,8000); //load the next picture in 8 seconds
}
function generateNewImage() { //get a new image ready and return it
	if(typeof listOfPhotos == 'undefined') return; //the list of available images
	if(typeof photoDirectory == 'undefined') return; //the directory to the images
	if(typeof currentPhotoKey == 'undefined') return; //the current key to the listOfPhotos array
	var newPhotoKey; //should not equal the currentPhotoKey
	do newPhotoKey = Math.floor(Math.random() * listOfPhotos.length);
	while (typeof newPhotoKey == 'undefined' || newPhotoKey == currentPhotoKey);
	currentPhotoKey = newPhotoKey;
	var newPhoto = document.createElement('img');
	newPhoto.setAttribute('alt','PLME Class Photo Slideshow');
	newPhoto.setAttribute('src',photoDirectory + listOfPhotos[newPhotoKey]);
	if(!stupid_browser) newPhoto.style.opacity = 1;
	return newPhoto;
}
function updateClassYears() { //update the h3 with the name of the class
	if(typeof listOfPhotos == 'undefined') return; //the list of available images
	if(typeof currentPhotoKey == 'undefined') return; //the current key to the listOfPhotos array
	var num = Number(listOfPhotos[currentPhotoKey].replace(/\D/g,'')); //only numbers are left
	var startYear = (num%100 < 10) ? "'0" : "'";
	startYear += String(num%100);
	var endYear = ((num+4)%100 < 10) ? "'0" : "'";
	endYear += String((num+4)%100);
	div.getElementsByTagName('h2')[0].innerHTML = 'PLME Class Photo Slideshow (Class of ' + startYear + '/' + endYear + ')';
}
function fadeoutPhoto() { //fade out the photo defined in the triggerFadout function
	if(!div || div.getElementsByTagName('img').length == 0) return; //there is no image there
	var photo = div.getElementsByTagName('img')[0];
	if(!stupid_browser) {
		if(photo.style.opacity > 0.01) {
			photo.style.opacity -= 0.04;
			window.setTimeout(fadeoutPhoto,10);
			return;
		}
	}
	var incomingPhotoCopy = incomingPhoto.cloneNode(true); //clone the node so it doesn't get deleted
	if(stupid_browser) incomingPhotoCopy.style.height = String(photo.height) + 'px';
	updateClassYears();
	photo.parentNode.replaceChild(incomingPhotoCopy,photo); //replace the photos
	incomingPhoto = generateNewImage(); //generate a new image
	window.setTimeout(fadeoutPhoto,8000); //call it again after 8 seconds	
}