$(document).ready(function() {
	stopBackground();
	if ($('#submit').length > 0) emailSubscribe();
	if ($('#parade_form').length > 0) parade_form_validation();
	if ($('#txtKeywords').length > 0) clear_text();
	if ($('#step_submit_final').length > 0) pre_paypal();
	
	setupEventSlideshow();
});

function pre_paypal() {	// final step
	$('#step_submit_final').click(function() {
		var error = 1;
		$('input[name=payment]:radio').each(function() {
			if ($(this).attr('checked')) {
				error--;
			}
		});
		
		if (error >= 1) {
			if ($('.error').length <= 0) $('#parade_form').prepend('<div class="error">You must select a payment option.</div>');
			document.getElementById('breadcrumbs').scrollIntoView(true);
			return false;
		} else {
			log_form();
			return false;
		}
	});
}

function clear_text() {
	$('#txtKeywords').click(function() {
		if ($(this).attr('value') == 'Search') $(this).attr('value','');
	});
	$('#txtKeywords').blur(function() {
		if ($(this).attr('value') == '') $(this).attr('value','Search');
	});
}

function parade_form_validation() {
	if ($('#krewe_info_req').length > 0) {
		$('#participate_yes').click(function() {
			$('#krewe_info_req').text('*Krewe Information');
		});
		$('#participate_no').click(function() {
			$('#krewe_info_req').text('Krewe Information');
		});
	}
	
	$('#step_submit').click(function() {
		var error = 0;
		$('.required').each(function() {
			if ($(this).attr('value') == '') {
				error++;
				$(this).addClass('not_valid');
			} else {
				$(this).removeClass('not_valid');
			}
		});
		
		// page two validation
		if ($('#participate_yes').length > 0) {
			if ($('#participate_yes').attr('checked') && ($('textarea#explanation').val() == '' && !$('#same_info').attr('checked'))) {
				error++;
			} else if (!$('#participate_no').attr('checked') && !$('#participate_yes').attr('checked')) {
				error++;
			}
		}
		
		// page three validation
		if ($('#agree').length > 0) {
			if (!$('#agree').attr('checked')) error++;
		}
		
		if (error <= 0) {
			return true;
		} else {
			if ($('.error').length <= 0) $('#parade_form').prepend('<div class="error">Not all required fields have been completed. * Indicates a required field.</div>');
			document.getElementById('breadcrumbs').scrollIntoView(true);
			return false;
		}
	});
}

function stopBackground() {
	resize();
	$(window).bind("resize",function() { 
		// if the window is smaller than 1048, do not center background
		resize();
	});
}

function resize() {
	if ($(window).width() <= 980) {
		$('body').css('background-position','-1128px 23px');
		$('#container').css('background-position','-1039px top');
	} else {
		$('body').css('background-position','center 23px');
		$('#container').css('background-position','center top');
	}
}

function emailSubscribe() {
	$('#submit').click(function() {
		var email = $('#email').attr('value');
		// ajax submit email
		$.ajax({
			type:"GET",
			url:"/library/php/email-signup.php",
			data:"email="+email,
			success:function(result) {
				if (result == 'Success') {
					$('#right_column').html('<h3><img src="/library/images/text-thank-you.gif" alt="Thank You" /></h3>');
				} else {
					$('#right_column').html('<p class="error">Sorry, the following error occured while trying to process your request: '+result+'</p><p><input type="text" name="email" value="" id="email" class="text" /></p><p><input type="image" src="/library/images/button-submit.gif" name="submit" value="submit" id="submit" /></p>');
					emailSubscribe();
				}
			}
		})
		return false;
	});
}

var curSlide = 1;
function setupEventSlideshow(){
	if($("#eventSlides.slideshow").length > 0){
		$.timer(3000, advanceSlideshow);
	}
}

function advanceSlideshow(timer){
	
	$("#eventSlides.slideshow img:nth-child("+curSlide+")").fadeOut("slow");
	curSlide++;
	
	if(!$("#eventSlides.slideshow img")[curSlide - 1]){
		curSlide = 1;
	}
	
	$("#eventSlides.slideshow img:nth-child("+curSlide+")").fadeIn("slow");
}
