document.addEvent('domready',function() {
	registerFormValidation();
	handleError();
	handleFlashes();
	handlePostClicks();
	handleNavigationExpandedClicks();
	$('gsLogo').setStyle('cursor','pointer');
	$('gsLogo').addEvent('click',function() {
		window.location = "/";
	});
});

function handleNavigationExpandedClicks() {
	$$('div.gsLink').setStyle('cursor','pointer');
	$$('div.gsLink').addEvent('click',function() {
		window.location = $(this).getFirst().href;
	});
}

function registerFormValidation() {
	$$('.gsForm').each(function(element) {
		new FormCheck(element, {
			fieldErrorClass: 'gsInputError',
			display: {
				addClassErrorToField: 1
			}
		});
	});
	$$('.gsFormNoStyle').each(function(element) {
		new FormCheck(element, {
			fieldErrorClass: 'gsInputError',
			display: {
				addClassErrorToField: 1
			}
		});
	});
}

function handlePostClicks() {
	$$('div.gsPost').addEvent('click', function() {
		var postID = $(this).get('rel');
		window.location = "viewpost.php?id=" + postID;
	});
}

function handleFlashes() {
	$$('div.gsPost.overdue').each(function(ele) {
		$(ele).setStyle('borderColor','#f00');
		var original = $(ele).getStyle('backgroundColor');
		var red = function() {
			$(ele).tween('backgroundColor','#fea3a3');
			red.delay(1500);
		}
		var orig = function() {
			$(ele).tween('backgroundColor',original);
			orig.delay(1500);
		}
		red();
		orig.delay(750);
	});
}

function handleError() {
	if ($('page_errors')) {
		var height = $('page_errors').getStyle('height');
		$('page_errors').setStyle('height',0);
		$('page_errors').setStyle('overflow','hidden');
		$('page_errors').set('tween', {
			link: 'chain',
			onComplete: function() {
				removePageErrors.delay(3000);
			}
		}).tween('height',height);
	}
}

function removePageErrors() {
	$('page_errors').set('tween', {
		onComplete: function() {
			$('page_errors').destroy();
		}
	});
	$('page_errors').tween('height',0);
}
