var WEBROOT = '/';

function replaceStyles()
{

	
}

function replaceStylesDiv(elementSelector) {

	/* BUTTONS */
		$('.ie '+elementSelector+' a.button')
			.addClass('replaced')
			.wrapInner('<span class="button-m" />')
			.prepend('<span class="button-l">&nbsp;</span>')
			.append('<span class="button-r">&nbsp;</span>');

}

function changeAttendance()
{
	var paddingWithFooter = 81;
	//window.alert('hi!');
	var destination = document.attendGig.postDestination.value;
	$.post(destination, $("#attendGig").serialize(), function(data)
	{
		$(".attending-members-list").html(data);
		replaceStylesDiv('.attendance-holder');
		$(".extend").css(
		{
			'min-height': $(".col3").height()-(
				($(".col12-span .panel").height()-$(".extend").height())+paddingWithFooter
			)		
		});
	});
	return false;
}

function loadContent(elementSelector, sourceURL)
{
	$("#"+elementSelector).load(sourceURL, '', function()
	{
		replaceStylesDiv(elementSelector)
	});
}

function checkEmail(emailAddress)
{
	var str = emailAddress;
	if ((str.indexOf(".") > 0) && (str.indexOf("@") > 0) == true)
	{
		return true;
	} else {
		//window.alert('Please enter a valid email address');
		return false;
	}
}


$(document).ready(function()
{
	// apply some effects immediately
	replaceStyles();

	// balance cols
	var paddingWithFooter = 81;
	var paddingWithoutFooter = 49;
	
	var parallax = 0;
	
	equalHeight($('#galleries ul li .caption'));
	
	/*$('#page-contents').animate({'min-height': $(window).height()-$('#silhouettes').height()}, 500);*/

	$('#topOfPage').click(function(e) {
		e.preventDefault();
		$('html,body').stop(true).animate({scrollTop:0},500);
	});
	fadeMessages();
	/*$(document).scroll(function() {
		parallax = Math.round(($('#menu').offset().top)/5);
		$('#silhouettes').css({top: parallax+'px'});
		$('body').css({backgroundPosition: '6px '+parallax+'px'});
	});*/
});

function fadeMessages()
{
	$('.message').wrap('<div class="messageHolder" />');
	$('.message').delay(3000).animate({opacity:0}, 500, function()
	{
		$('.messageHolder').slideUp();
	});
}

function checkLogInForm()
{
	var errors = '';
	if (checkEmail(document.logInForm.email.value)==false)
		errors += '- Email\n';
	if (document.logInForm.password.value.length == 0 || document.logInForm.password.value=='Password' || document.logInForm.password.value=='PASSWORD')
		errors += '- Password\n';
	if (errors != '')
	{
		errors = 'Please complete the following:\n\n'+errors;
		window.alert(errors);
		return false;
	} else {
		return true;
	}
}

function checkBigLogInForm()
{
	var errors = '';
	if (checkEmail(document.bigLogInForm.email.value)==false)
		errors += '- Email\n';
	if (document.bigLogInForm.password.value.length == 0 || document.bigLogInForm.password.value == 'Password' || document.bigLogInForm.password.value == 'PASSWORD')
		errors += '- Password\n';
	if (errors != '')
	{
		errors = 'Please complete the following:\n\n'+errors;
		window.alert(errors);
		return false;
	} else {
		return true;
	}
}

function checkImageUploadForm()
{
	var errors = '';
	if (document.getElementById('image-form').imageFile.value.length == 0)
		errors += '- Image (in JPEG format)\n';
	if (document.getElementById('image-form').imageFile.value.length == 0)
		errors += '- Caption\n';
	if (errors != '')
	{
		errors = 'Please complete the following:\n\n'+errors;
		window.alert(errors);
		return false;
	} else {
		/*var destination = document.imageForm.postDestination.value;
		//$(".new-image").css({opacity:0});
		//$(".image-form-holder").height($(".image-form-holder").height());
		$(".image-form").animate({opacity:1}, 500, function() {
			$.post(destination, $("#imageForm").serialize(), 	function(data) {
				$(".new-image").html(data);
				//$(".new-image").animate({opacity:1}, 500);
				//$("#add-image-button").animate({opacity:0}, 500);
				//$(".image-form-holder").animate({height: $(".new-image").height()}, 500);
				fadeMessages();
			});
		});*/
		return true;
	}
}

function checkPasswordReminder()
{
	errors = '';
	if (checkEmail(document.membersLogInForm.email.value)==false)
	{
		errors += '- Email Address\n';
	}
	if (errors == '')
	{
		return true;
	} else {
		window.alert('Please complete the following:\n\n'+errors);
		document.membersLogInForm.email.focus();
		return false;
	}
}

//

function checkRegisterForm()
{
	errors = '';
	if (document.registerForm.firstName.value.length == 0)
		errors += '- First Name\n';
	if (document.registerForm.lastName.value.length == 0)
		errors += '- Last Name\n';
	/*var sexcheck = '';
	for (i=0; i<document.registerForm.sex.length; i++) {
 		if (document.registerForm.sex[i].checked) {
			sexcheck = document.registerForm.sex[i].value;
		}
	}
	if (sexcheck == '')
		errors += '- Sex\n';
	*/
	if (document.registerForm.username.value.length == 0)
		errors += '- Username\n';
	if (checkEmail(document.registerForm.email.value)==false)
		errors += '- Email Address\n';
	if (document.registerForm.confirmEmail.value != document.registerForm.email.value)
	{
		errors += '- Email Addresses did not match\n';
		document.registerForm.checkEmail.value = '';	
	} else if (document.registerForm.confirmEmail.value.length == 0)
		errors += '- Confirmation Email Address\n';

	if (document.registerForm.password.value.length < 6)
		errors += '- Password\n';

	if (document.registerForm.confirmPassword.value.length < 6 || document.registerForm.confirmPassword.value != document.registerForm.password.value)
		errors += '- Confirm Password\n';

	if (errors == '')
	{
		return true;
	} else {
		window.alert('Please complete the following:\n\n'+errors);
		return false;
	}
}

function checkProfileForm()
{
	errors = '';

	if (checkEmail(document.profileForm.email.value)==false)
		errors += '- Email address\n';

	if (document.profileForm.password.value.length > 0 && (document.profileForm.password.value.length < 6 || document.profileForm.password.value.length > 30))
		errors += '- Password must be between 6 and 30 characters';
	else if (document.profileForm.password.value.length > 0 && document.profileForm.password.value != document.profileForm.confirmPassword.value)
		errors += '- Passwords do not match\n';

	if (errors=='')
	{
		return true;
	} else {
		window.alert('Please complete the following:\n\n'+errors);
		return false;
	}
}

function checkCommentForm()
{
	errors = '';
	
	if (document.commentForm.commentTitle.value.length == 0)
	{
		errors += '- Comment Title\n';
		document.commentForm.commentTitle.focus();
	}
	if (document.commentForm.commentText.value.length == 0)
	{
		errors += '- Comment Text\n';
	}

	if (errors == '')
	{
		var destination = document.commentForm.postDestination.value;
		$(".new-comment").css({opacity:0});	$(".comments-form-holder").height($(".comments-form-holder").height());
		$(".comments-form").animate({opacity:0}, 500, function() {
			$.post(destination, $("#commentForm").serialize(), 	function(data) {
				$(".new-comment").html(data);
				$(".new-comment").animate({opacity:1}, 500);
				$("#add-comment-button").animate({opacity:0}, 500);
				$(".comments-form-holder").animate(
					{height: $(".new-comment").height()}, 500);
				loadContent("comments-title", commentsTitleURI); 
				fadeMessages();
			});
		});
		return false;
	} else {
		window.alert('Please complete the following:\n\n'+errors);
		return false;
	}
}

function fadePanels()
{
	// fade panels up
	$('.panelWrapper').css({'display':'block','visibility':'visible','opacity':0});
	$('.galleries li').css({'display':'block','visibility':'visible','opacity':0});
	var delayMS = 0;
	for(i=0; i<=$('.panelWrapper').length; i++)
	{
		delayMS = (i*75);
		$('.panelWrapper').eq(i).delay(delayMS).animate({opacity:1}, 250);
	}
	for(i=0; i<=$('.galleries li').length; i++)
	{
		delayMS = (i*75);
		$('.galleries li').eq(i).delay(delayMS).animate({opacity:1}, 250);
	}
}

function equalHeight(group)
{
	var tallest = 0;
	group.each(function()
	{
		var thisHeight = $(this).height();
		if(thisHeight > tallest)
		{
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

//------------------------------------------------------------------------------

