
	$(function() {
		_initLoginOverlay();
		_initRegisterOverlay();
		$('a[rel*=external]').live('click', function() {
			window.open(this.href);
			return false;
		});
	});
	
	/*
	function _injectFormAction(form)
	{
		$(form).attr('action', window.location);
	}
	*/
	
	function validateLogin()
	{
		var success = true;
		if(!_isValidEmailAddress($("#email").val())) {
			_flashField('#email');
			success = false;
		}
		if($("#password").val() == "") {
			_flashField('#password');
			success = false;
		}
		return success;
	}
	
	function validateRegister()
	{
		var success = true;
		if($("#name").val() == "") {
			_flashField('#name');
			success = false;
		}
		if(!_isValidEmailAddress($("#email").val())) {
			_flashField('#email');
			success = false;
		} else if(!_isValidEmailAddress($("#emailconfirm").val())) {
			_flashField('#emailconfirm');
			success = false;
		} else if($("#email").val() != $("#emailconfirm").val()) {
			_flashField('#email');
			_flashField('#emailconfirm');
			_showMsg('error', 'Sorry, your email addresses do not seem to match.')
			success = false;
		}
		if($("#username").val() == "") {
			_flashField('#username');
			success = false;
		}
		if($("#password").val() == "") {
			_flashField('#password');
			success = false;
		}
		if($("#exhibition").val() == "") {
			_flashField('#exhibition');
			success = false;
		}
		$.fn.colorbox.resize();
		return success;
	}
	
	function loginSuccess(url)
	{
		// Update link (server side takes over after page refresh)
		$('a.overlay-login').attr('href', url);
		$('a.overlay-login').removeClass('cboxElement').removeClass('overlay-login'); // remove overlay
		$('#exhibition-add').slideDown();
		$('#comment-fields').html('<p><textarea name="add-comment" id="add-comment" rows="8" cols="50">Tell us what you think...</textarea></p><p><input type="submit" name="submit" class="button" value="Add your comment" /></p>');
		$('#add-comment').focus( function() {
			_commentTextFocus($(this)); // see item.js
		})
		.blur( function() {
			_commentTextBlur($(this)); // see item.js
		});
		initAddExhibition();
	}
	
	function _initLoginOverlay()
	{
		$('a.overlay-login, input.overlay-login').colorbox({
			href:"/assets/ajax/login.php",
			scrolling:false,
			width:"279px", height:"250px",
			onComplete:function(){
				$('#login-form').submit( function(){
					if(validateLogin()) {
						_ajaxLogin();
					}
					return false;
				});
				_initRegisterOverlay();
				_initResetOverlay();
			}
		});
	}
	
	function _initRegisterOverlay()
	{
		$('a.overlay-register').colorbox({
			href:"/assets/ajax/register.php",
			scrolling:false,
			width:"279px", height:"440px",
			onComplete:function(){
				$('#register-form').submit( function(){
					if(validateRegister()) {
						_ajaxRegister();
					}
					return false;
				});
			}
		});
	}
	
	function _initResetOverlay()
	{
		$("#overlay-reset").colorbox({
			scrolling:false,
			width:"279px", height:"200px",
			onComplete:function(){
				_injectFormAction('#reset-form');
			}
		});
	}
	
	function _ajaxLogin()
	{
		$.ajax({
			type: "POST",
			url: "/assets/ajax/login.php",
			data: "email="+$("#email").val()+"&password="+$("#password").val(),
			dataType: 'html',
			error: function(results){
				$('#cboxLoadedContent div').html('<p class="error">Sorry, an error occured trying to access this account. Please refresh this page and try again.</p>');
			},
			success: function(results){
				$('#cboxLoadedContent div').html(results);
				$('#login-form').submit( function(){
					if(validateLogin()) {
						_ajaxLogin();
					}
					return false;
				});
				_initRegisterOverlay();
				_initResetOverlay();
				$.fn.colorbox.resize();
			}
		});
	}
	
	function _ajaxRegister()
	{
		var optin = ($('#optin').is(':checked')) ? 1 : 0;
		$.ajax({
			type: "POST",
			url: "/assets/ajax/register.php",
			data: "name="+$("#name").val()+"&email="+$("#email").val()+"&emailconfirm="+$("#emailconfirm").val()+"&username="+$("#username").val()+"&password="+$("#password").val()+"&exhibition="+$("#exhibition").val()+"&optin="+optin,
			dataType: 'html',
			error: function(results){
				$('#cboxLoadedContent div').html('<p class="error">Sorry, an error occured trying to create this account. Please refresh this page and try again.</p>');
			},
			success: function(results){
				$('#cboxLoadedContent div').html(results);
				$('#register-form').submit( function(){
					if(validateRegister()) {
						_ajaxRegister();
					}
					return false;
				});
				$.fn.colorbox.resize();
			}
		});
	}
	
	function _flashField(css)
	{
		$(css)
			.animate({ backgroundColor: "#fd5959" }, 400)
			.animate({ backgroundColor: "#ffffff" }, 400)
		;
	}
	
	function _showMsg(css, msgText)
	{
		$("#msg").addClass(css);
		$("#msg").text(msgText);
	}
	
	function _isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	}
