//blue tears
var prodSelects;
var prodRemoves;
var prodAdds;
var prodCount = -1;
function preloadImg(image) {
  var img = new Image();
  img.src = image;
}

$(document).ready(function(){	
			
$("#seeTips").overlay({
	effect: 'apple',
	expose: {
		color: '#333',
		opacity: 0.7, 
		closeSpeed: 1250
	},
	onStart: function(event) { 
		if (jQuery.browser.msie) {
			if (jQuery.browser.version==6) {
				$('select').css("display","none");
			}
		}
	},
	onClose: function(event) { 
		if (jQuery.browser.msie) {
			if (jQuery.browser.version==6) {
				$('select').css("display","block");
			}
		}
	}
});
	
prodSelects = $('.prodSelect select');
prodRemoves = $('.removeProd');
prodAdds = $('.addProd');
prodSelects.parent().fadeOut(0);
prodRemoves.fadeOut(0);
prodAdds.fadeOut(0);
prodSelects.eq(0)
	.parent()
		.fadeIn(0)
			.find('.addProd,.removeProd')
				.fadeIn(0);
				
prodSelects.each(function () {	
	if ($(this).val()!="") {
		$(this)
			.parent()
			.fadeIn(0)
				.find('.addProd,.removeProd')
					.fadeIn(200);
	}
});
	
prodRemoves.click(function (event) {
	$(this).fadeOut(200);
	currentProd = $(prodRemoves).index($(this));
	$(this).parent().stop().animate({backgroundColor: '#ccddff'}, 200);
	$(this).parent().fadeOut(200);
	$(prodSelects).eq(currentProd)
		.val("")
			.parent()
				.find('.addProd,.removeProd')
					.fadeIn(200);
});

prodAdds.click(function (event) {
	currentProd = $(prodAdds).index($(this));
	 if (currentProd>0) {
	 	$(prodRemoves).eq(currentProd-1).fadeOut(200);
	 }
	$(this).fadeOut(200);
	$(prodSelects).eq(currentProd+1)
		.val("")
			.parent()
				.stop().animate({backgroundColor: '#ccddff'}, 200,
					function () 
					{
						$(this).animate({backgroundColor: '#ccddff'}, 800,
							function () 
							{
								$(this).animate({backgroundColor: '#fff'}, 150);
							}
						);
					}
				)
				.find('.addProd,.removeProd').fadeIn(200);
					
	$(prodSelects).eq(currentProd+1).parent().fadeIn(300);
});

var submitOverlay = $("#submitOverlay").overlay({ 
	effect: 'apple',
	expose: {
		color: '#333',
		opacity: 0.7, 
		closeSpeed: 1250
	},
    closeOnClick: false, 
    api: true,
	onStart: function(event) { 
		if (jQuery.browser.msie) {
			if (jQuery.browser.version==6) {
				$('select').css("display","none");
			}
		}
	},
	onClose: function(event) { 
		if (jQuery.browser.msie) {
			if (jQuery.browser.version==6) {
				$('select').css("display","block");
			}
		}
	}
}); // end submit overlay

preloadImg('/images/overlay-ui/ajax-loader.gif');

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);
	var result= pattern.test(emailAddress);
	return pattern.test(emailAddress);
}

//  ---------- FORM VALIDATION
$('#uploadForm').submit(function (e) {
	$("#uploadForm input, #uploadForm label, #uploadForm textarea, #uploadForm select").removeClass("error");
	$('.errors').hide();
	$(this).find('.errors').remove();
	var emailVal = $('#email').val();
	var emailConfirmVal = $('#confirm-email').val();
	var errors = $(this)
		.find('.required, .check')
			.find('input, select, textarea, #terms')
				.map(function () {	
					if ($(this).val() == '') {
						if ($(this).attr('id') == 'uploadA') {
							$(this).addClass("error");
							return ('A photo has not been selected');
						}
						else if ($(this).attr('id') == 'entryTitle') {
						  	$(this).parent().find('label').addClass("error");
							return ('Please choose a project type');
						}
						else
						{
							var fixedLabel = $(this).parent().find('label').html().substring(1,27);
							$(this).addClass("error");
							return (fixedLabel + ' is blank.');
						}
					}
					else if ($(this).attr('id') == 'email') {						
						if ((emailVal != '' && emailConfirmVal != '') 
								&& (emailVal != emailConfirmVal)) {
									$('#email, #confirm-email').addClass("error");
									return ('Email does not match confirmation.');
						}
						else
						{
							if (isValidEmailAddress(emailVal)==false) {
								$('#email').addClass("error");
								return ('Email is not a valid address.');
							}
						}
					}
					else if ($(this).attr('id') == 'confirm-email') {
						if (isValidEmailAddress(emailConfirmVal)==false) {
							$('#confirm-email').addClass("error");
							return ('Confirm Email is not a valid address.');
						}
					}
					else if ($(this).attr('id') == 'terms') {
						if ($('#terms:checked').val() == null) {
						  	$(this).parent().find('label').addClass("error");
							return ('Terms have not been accepted');
						}
					}
					else if ($(this).attr('id') == 'entryState') {
						if ($(this).val() == '-1') {
						  	$(this).parent().find('label').addClass("error");
							return ('State is blank');
						}
					}
				});
				
				
	
	if (errors.length > 0) {
		$(this).prepend('<div class="errors" id="flash-message"><a name="errorsdiv"></a><em>Please correct the following errors.</em><ul></ul></div>').end()
			.find('.errors').hide().fadeIn();
		var errorList = $('.errors ul');
		errors.each(function () { errorList.append('<li>' +  this + '</li>'); });
		if(window.location.hash) window.location.hash='#errorsdiv';
		e.preventDefault();
	}
	else
	{
		submitOverlay.load();
	}
});

}); //doc ready
