// build an array of slide IDs
var site_array = [
							'ffm',
							'itex',
							'fhc',
							'ks',
							'coade',
							'kb',
							'wbn',
							'wpm',
							'wdp',
							'fsc',
							'nea',
							'ddp',
							'cffs',
							'ecms'
							];

// load loading graphic
var limage = new Image();
limage.src = '/i/style/1.gif';

var autoplay_timer;

// preload screenshot images
for ( var i = 0; i <= site_array.length-1; i++) {
	for (var j = 1; j <= 3; j++) {
		var ss_file = '/i/screenshots/' + site_array[i] + j + '.jpg';
		var ss_img = new Image();
		ss_img.src = ss_file;
	}
}

// clear background style for all slideshow links
function resetSlideShowLinks() {
	for (var i = 1; i <= 3; i++) {
		var ss_link = 'sc' + i;
		$(ss_link).setStyle({'background': 'transparent'});
	}
}

// determine slide number by getting left position value of slideshow div
function getCurrentNum() {
	var ss_left;
	if ($('bborder').getStyle('display') == 'none') {
		ss_left = -980;
	} else {
		ss_left = parseFloat($('slideshow').getStyle('left'));
	}
	var site_num = (ss_left/980 * -1) - 1;
	return site_num;
}

function getProjectNum(project) {
	var project_num = 0;
	for (var i = 0; i < site_array.length; i++) {
		if (site_array[i] == project) {
			project_num = i;	
		}
	}
	return project_num;
}

// transition between different slides
function slideShow(slide_num, site) {
	resetSlideShowLinks();
	// get site name value if not present
	if (site == '' || site == undefined) {
		var site_num = getCurrentNum();
		var site = site_array[site_num];
	}
	// set background style for active link
	var ss_link = 'sc' + slide_num;
	$(ss_link).setStyle({'backgroundImage': 'url(/i/style/cbg.png)'});
	// build screenshot IDs 
	var ss = site + '_ss';
	var opacity_ss1 = $(ss).getStyle('display');
	var ss2 = site + '_ss2';
	// build screenshot image file name
	var ss_file = '/i/screenshots/' + site + slide_num + '.jpg';
	// transition between visible and hidden images
	if (opacity_ss1 == 'none') {
		$(ss).setAttribute('src', ss_file);
		$(ss2).setStyle({'z-index': 10});
		$(ss).setStyle({'z-index': 1});
		new Effect.Fade($(ss2), {duration: 1});
		new Effect.Appear($(ss), {duration: 1});
	} else {
		$(ss2).setAttribute('src', ss_file);
		$(ss).setStyle({'z-index': 10});
		$(ss2).setStyle({'z-index': 1});
		new Effect.Fade($(ss), {duration: 1});
		new Effect.Appear($(ss2), {duration: 1});
	}
}

function nextSite(direction) {
	var site_num = getCurrentNum();
	var next_site_num;
	var slideshow_width = parseFloat($('slideshow').getStyle('width'));
	var slideshow_left = parseFloat($('slideshow').getStyle('left'));
	var x_factor = slideshow_width - 1960;
	if (direction == 'forward') {
		next_site_num = site_num + 1;
		// loop the slideshow if we've reached the end
		if (slideshow_left + slideshow_width === 1960) {
			new Effect.Move($('slideshow'), {x: -980, y: 0, duration: 1, afterFinish: function() {new Effect.Move($('slideshow'), {x: x_factor, y: 0, duration: 0});}});
			next_site_num = 0;
		} else {
			new Effect.Move($('slideshow'), {x: -980, y: 0, duration: 1});
		}
	} else {
		// loop the slideshow if we've reached the beginning
		if (site_num === 0) {
			new Effect.Move($('slideshow'), {x: 980, y: 0, duration: 1, afterFinish: function() {new Effect.Move($('slideshow'), {x: -x_factor, y: 0, duration: 0});}});
			next_site_num = site_array.length - 1;
		} else {
			new Effect.Move($('slideshow'), {x: 980, y: 0, duration: 1});
			next_site_num = site_num - 1;
		}
	}
	resetSlideShowLinks();
	// build screenshot IDs
	var ss = site_array[next_site_num] + '_ss';
	var ss2 = site_array[next_site_num] + '_ss2';
	// build screenshot image file name
	var ss_file = '/i/screenshots/' + site_array[next_site_num] + '1.jpg';
	// reset site image to first image in the series
	$(ss).setAttribute('src', ss_file);
	$(ss).setStyle({'z-index': 10});
	$(ss2).setStyle({'z-index': 1});
	new Effect.Appear($(ss), {duration: 0});
	new Effect.Fade($(ss2), {duration: 0});
	// reset background style of links to initial setup
	for (var i = 1; i <= 3; i++) {
		var ss_link = 'sc' + i;
		if (i == 1) {
			$(ss_link).setStyle({'backgroundImage': 'url(/i/style/cbg.png)'});
		}
	}
}

// scroll project slideshow to requested project
function showProject(project) {
	var curr_site = getCurrentNum();
	var ss_left = curr_site * 980;
	var req_site = getProjectNum(project);
	if (req_site >= curr_site) {
		var left = req_site * 980 - ss_left;
	} else {
		var left = req_site * 980 - ss_left;
	}
	if ($('bborder').getStyle('display') == 'none') {
		new Effect.BlindDown($('bborder'));
	}
	resetSlideShowLinks();
	// reset background style of links to initial setup
	for (var i = 1; i <= 3; i++) {
		var ss_link = 'sc' + i;
		if (i == 1) {
			$(ss_link).setStyle({'backgroundImage': 'url(/i/style/cbg.png)'});
		}
	}
	new Effect.ScrollTo($('page-title'));
	new Effect.Move($('slideshow'), {x:-left, y:0});
	return false;
}

// hide slideshow
function hideSS() {
	new Effect.BlindUp($('bborder'));
	new Effect.ScrollTo($('header'), {afterFinish: function() {$('slideshow').setStyle({'left': '-980px'})}});
}

// show or hide about section div
function showHideAbout() {
	if ($('about').getStyle('display') == 'none') {
		new Effect.BlindDown($('about'), {duration: 1});
	} else {
		new Effect.BlindUp($('about'), {duration: 1});
	}
}

// scroll to contact form
function gotoContactForm() {
	new Effect.Appear($('attention'), {duration: 0.1});
	new Effect.ScrollTo($('contact'), {duration: 1.0, offset: -10, afterFinish: function(){new Effect.Fade($('attention'), {duration: 3.0}); $F('name').focus();}});
}

// contact form submission handler
function submitContactForm() {
	// collect form field values
	var c_name = $F('name');
	var c_email = $F('email');
	var c_message = $F('message');
	var c_ip_addr = $F('ip_addr');

	// check for required data
	var error_alert;
	if (c_name == '') {
		error_alert = alert('Please enter your name before submitting your message.');
		new Effect.Shake($('name'));
		return false;
	}
	if (c_email == '') {
		error_alert = alert('Please enter your email address before submitting your message.');
		new Effect.Shake($('email'));
		return false;
	}
	if (c_message == '') {
		error_alert = alert('Please enter a message.');
		new Effect.Shake($('message'));
		return false;
	}

	// submit data to contact script
	var submit_url = '/s/contact-me.php';
	var params = 'name=' + c_name + '&email=' + c_email + '&message=' + c_message + '&ip_addr=' + c_ip_addr;
	var ajax = new Ajax.Request(
		submit_url, {
					 method: 'get',
					 parameters: params,
					 onSuccess: function(transport) {
									if (transport.responseText == 'success') {
										// on success show thank you message
										$('attention').update('<img src="/i/thanks.png" alt="Thanks" />');
										$('message').value = '';
										new Effect.Appear($('attention'), {afterFinish: function() {new Effect.Fade($('attention'), {duration: 5.0, afterFinish: function() {$('attention').update('<img src="/i/contact-me.png" alt="Use the form to the right to contact me." />'); $('attention').setStyle({'backgroundImage': 'url(/i/ethan-mcelroy.jpg)'});}});}});
									} else {
										// on failure show error message
										$('attention').update('<img src="/i/error.png" alt="Oops..." />'); 
										$('attention').setStyle({'backgroundImage': 'url(/i/ethan-mcelroy-error.jpg)'}); 
										new Effect.Appear($('attention'), {afterFinish: function() {new Effect.Fade($('attention'), {duration: 5.0, afterFinish: function() {$('attention').update('<img src="/i/contact-me.png" alt="Use the form to the right to contact me." />'); $('attention').setStyle({'backgroundImage': 'url(/i/ethan-mcelroy.jpg)'});}});}});
									}
								}
					}
	);
	return false;
}

// stylesheet switcher
function changeStylesheet(link_id) {
	var styleObj = $$('head link[type=text/css][rel=stylesheet]')[1];
	var stylesheet = styleObj.getAttribute('href');
	if (stylesheet == '/c/nighttime.css') {
		styleObj.setAttribute('href', 'c/daytime.css');
		$('styleswitch').update('daytime mode. To see what the site looks like in the nighttime mode, <a href="#" onclick="changeStylesheet();">view nighttime mode mode</a>');
		setCookie('stylesheet', 'daytime');
	} else {
		styleObj.setAttribute('href', '/c/nighttime.css');
		$('styleswitch').update('nighttime mode. To see what the site looks like in the daytime mode, <a href="#" onclick="changeStylesheet();">view daytime mode mode</a>');
		setCookie('stylesheet', 'nighttime');
	} 
}

// set cookie function
function setCookie(c_name, c_value) {
	document.cookie = c_name + '=' + c_value;
}

// get cookie function
function getCookie(c_name) {
	var results = document.cookie.match('(^|;) ?' + c_name + '=([^;]*)(;|$)');
	if (results) {
		return(unescape(results[2]));
	} else {
    	return null;
	}
}

// autoplay slideshow
function autoplay(slide_num) {
	if (slide_num <= 3 && slide_num > 1) {
		slideShow(slide_num);
		slide_num++;
	} else if (slide_num > 3) {
		nextSite('forward');
		slide_num = 2;
	} else {
		slide_num++;
	}
	autoplay_timer = setTimeout('autoplay(' + slide_num + ')', 5000);
}

// pause autoplay
function pauseAutoplay() {
	var stop_autoplay = clearTimeout(autoplay_timer);	
}

// initialize page
function initPage(is_ready) {
	var loaded = getCookie('loaded');
	if (loaded != 'yes') {
		$('curtain').setStyle({'filter': 'Alpha(opacity=95)', 'opacity': '.95'});
		var init_timer = window.setTimeout("initPage(" + "'yes'" + ")", 500);
	} else {
		is_ready = 'yes';	
	}
	if (is_ready == 'yes') {
		if ($('curtain')) {
			$('loading').setStyle({'display': 'none'});
			new Effect.BlindUp($('curtain'));
		}
		var clear_timer  = clearTimeout(init_timer);
		// open outbound links in new window - this needs some work
		var links_array = document.getElementsByTagName('a');
		var links_array_length = links_array.length;
		for (var i = 0; i < links_array.length; i++) {
			var link_id = links_array[i];
			var link_href = link_id.getAttribute('href');
			if (link_href.match('http://') && !(link_href.match('index.html')) && !(link_href.match('work.html'))) {
				link_id.setAttribute('target', '_blank');
			}
		}
		setCookie('loaded', 'yes');
		var page_url = document.URL;
		if (!(page_url.match('work'))) {
			autoplay(1);
		}
	}
}

window.onload = initPage;
