// JavaScript Document, created by Rahul Banerjee '10 for Brown SASA.

/* FILMSTRIP FUNCTIONS */
var filmstrip = null; //will become the filmstrip div
var description = null; //will become the description td
addOnload(filmstripFxns);
function filmstripFxns() {
	filmstrip = document.getElementById('filmstrip');
	description = filmstrip.getElementsByTagName('td')[5];
	var photos = filmstrip.getElementsByTagName('img');
	for(var i=0; i<5; i++) photos[i].onmouseover = showDescription;
	var originalHTML = filmstrip.getElementsByTagName('td')[5].innerHTML;
	filmstrip.getElementsByTagName('td')[5].onmouseover = function() {this.innerHTML = originalHTML;}
}
function showDescription() { //onmouseover
	if(this.parentNode.parentNode.className == 'next') { //the link to more entries
		description.innerHTML = '<h4>Announcement &amp; Events Archives</h4><p>Click to view archives for the ' + CURRENT_YEAR + ' school year.<br /><em>(other years\' archives can be accessed here as well)</em>';
		return;
	}

	var id = Number(this.getAttribute('id').replace(/\D/g,''));	
	var title = document.createElement('h4');
	title.appendChild(document.createTextNode(photos[id]['title']));
	
	var text = document.createElement('p');
	if(photos[id]['tagline']) text.innerHTML = '<span>' + photos[id]['date'] + ':</span> ' + photos[id]['tagline'];
	else text.innerHTML = 'Posted on: <span>' + photos[id]['date'] + '</span>';
	text.innerHTML += '<br /><em id="clickForMoreInfo">(click on the photo for more information)</em>';

	description.innerHTML = '';
	description.appendChild(title);
	description.appendChild(text);
}


/* MAILING LIST FUNCTIONS */
var button = null; //will become the button
var buttonText = null; //will become the button's text
addOnload(setupMailingList);
function setupMailingList() {
	var div = document.getElementById('subscribe');
	div.getElementsByTagName('input')[0].onkeydown = checkIfReturnKeyPressed;
	button = div.getElementsByTagName('button')[0];
	buttonText = button.innerHTML;
	button.onclick = submitMailingList;
}
function submitMailingList() {
	var resetButton = function() {button.className = 'normal'; button.innerHTML = buttonText;}
	var validEmail = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	var yourEmail = button.previousSibling.value;
	if(!validEmail.test(yourEmail)) {
		button.className = 'error';
		button.innerHTML = 'Invalid email address';
		window.setTimeout(resetButton,2000);
		return false;
	}
	var request = ajax();
	var href = WEB_ROOT + 'includes/processes/mailing_list.php';
	request.onreadystatechange = function() {
		if(this.readyState != 4 || this.status != 200) return;
		if(this.responseText.length > 0) {
			button.className = 'error';
			button.innerHTML = this.responseText;
			window.setTimeout(resetButton,2000);
			return;
		}
		button.innerHTML = 'Success! ' + yourEmail + ' has been added.';
	}
	request.open('GET', href + '?subscribe=' + yourEmail,true);
	request.send(null);
}
function checkIfReturnKeyPressed(e) {e = e || window.event; if(e.keyCode == 13) submitMailingList();}
function ajax(mime){var http_request=false;if(window.XMLHttpRequest){http_request=new XMLHttpRequest();if(http_request.overrideMimeType&&typeof mime=='undefined')http_request.overrideMimeType('text/html')}else if(window.ActiveXObject){try{http_request=new ActiveXObject("Msxml2.XMLHTTP")}catch(e){try{http_request=new ActiveXObject("Microsoft.XMLHTTP")}catch(e){alert('Please upgrade to a better browser or disable Javascript altogether.')}}}return http_request}


/* SLIDESHOW FUNCTIONS */
var slideshow = null;
var slideshowIndex = 0;
var slideshowNextImg = null;
var slideshowInterval = null;

addOnload(setupSlideshow);
function setupSlideshow() {
	slideshow = document.getElementById('slideshow');
	resumeSlideshow();
	slideshow.firstChild.onmouseover = pauseSlideshow;
	slideshow.firstChild.onmouseout = resumeSlideshow;
}
function resumeSlideshow() {loadNextImg(); slideshowInterval = setInterval(countdownSlideshow,1000);}
function pauseSlideshow() {if(slideshowInterval) clearInterval(slideshowInterval);}
function loadNextImg() {
	slideshowIndex = (slideshowIndex < slideshowArray.length - 1) ? slideshowIndex + 1 : 0;
	slideshowNextImg = new Image();
	slideshowNextImg.src = document.URL.replace(/^(.+SASA\/).*$/,'$1' + 'assets/placeholders/' + slideshowArray[slideshowIndex]);
}

function countdownSlideshow() {
	var numberOfSeconds = Number(slideshow.lastChild.innerHTML) - 1;
	slideshow.lastChild.innerHTML = (numberOfSeconds > 0) ? String(numberOfSeconds) : '5'
	if(numberOfSeconds == 4) loadNextImg();
	if(numberOfSeconds < 1) slideshow.firstChild.setAttribute('src',slideshowNextImg.src);
}