/* --- Happenings --- */

var happening_timer;
var event_pos = 0;
var event_width = 287;
var event_count;
Event.observe(document, 'dom:loaded', function() {
	happening_timer = setInterval('check_happening_position()', 1);
	var event_count = $$('#events .event').length;
	
	$('happenings-left').onclick = function() {
		if (event_pos > 0) event_pos--;
		$('events-slider').morph('left:' + (-1 * event_pos * event_width) + 'px', {
			duration: .5
		});
		return false;
	}
	
	$('happenings-right').onclick = function() {
		if (event_pos < event_count - 1) event_pos++;
		$('events-slider').morph('left:' + (-1 * event_pos * event_width) + 'px', {
			duration: .5
		});
		return false;
	}
	
	$('happenings-button').onclick = function() {
		if ($('happenings-collapse-expand').hasClassName('selected')) {
			$('happenings').morph('height: 45px', { duration: .5 });
			$('happenings-inner').morph('height: 0px', { duration: .5 });
			$('events').morph('height: 0px', { duration: .5 });
		} else {
			$('happenings').morph('height: 250px', { duration: .5 });
			$('happenings-inner').morph('height: 200px', { duration: .5 });
			$('events').morph('height: 200px', { duration: .5 });
		}
		$('happenings-collapse-expand').toggleClassName('selected');
		return false;
	}
	
	// Newsletter Functionality
	$('emma_member_email').onfocus = function() { if (this.value == 'name@example.com') this.value = ''; }
	$('emma_member_email').onblur = function() { if (this.value == '') this.value = 'name@example.com'; }
	
	$('newsletter').down('a').onclick = function() {
		if ($('email-signup').getWidth() == 0) {
			$('email-signup').morph('width: 165px;', { duration: .2 });
			$('newsletter').addClassName('selected');
		} else {
			$('email-signup').morph('width: 0;', { duration: .2 });
			$('newsletter').removeClassName('selected');
		}
		return false;
	}
	
	$('email-signup').onsubmit = function() {
		if ($('emma_member_email').value != '' && $('emma_member_email') != 'name@example.com') {
			$('email-signup').morph('width: 0;', {
				duration: .2,
				afterFinish: function() {
					$('emma_member_email').value = 'name@example.com';
					$('newsletter').removeClassName('selected');
				}
			});
			return true;
		} else {
			alert("Please enter your email address");
			return false;
		}
	}
});

function check_happening_position() {
	if ($('happenings').visible()) {
		var window_height = get_window_height();
		var page_height = document.viewport.getHeight();
		//$('happenings').setStyle({ bottom: window_height - page_height - document.viewport.getScrollOffsets()[1] + 'px' });
		$('events').setStyle({ width: $('happenings').getWidth() - 96 + 'px' });
		$('happenings-button').setStyle({ left: (document.viewport.getWidth() / 2) - ($('happenings-button').getWidth() / 2) + 'px' }).show();
	}
}


function get_window_height() {
	var d = document;
    return Math.max(
        Math.max(d.body.scrollHeight, d.documentElement.scrollHeight),
        Math.max(d.body.offsetHeight, d.documentElement.offsetHeight),
        Math.max(d.body.clientHeight, d.documentElement.clientHeight)
    );
}
