// Javascript for MarkoLiias.com

// function to reverse the order of selection jQuery objects
$.fn.reverse = [].reverse;

jQuery(document).ready(function($) { // special declaration needed, as Wordpress loads jquery in safe mode

	// initialize validation and set default error messages for text inputs to none
		$("#liias-form").each(function() {
			$(this).validate({	
				errorContainer: "div.error-message",
				errorLabelContainer: "div.error-message span",
				messages: {
					Email: "",
					Zip: ""
				}	
			});
	});
	
	// select the form and wrap it
	if (!($('.tellafriend').length || $('.salsa .event_view').length)) {
		formSelection = $('.salsa .supporterInfo').nextUntil('.salsa .formRow').andSelf();
	}
	
	// if it's a questionnaire, do some special form stuff
	if ($('.salsa #questionnaireForm').length) {
		formSelection_quest =  $('.salsa .qHeader').nextUntil('.salsa .qFooter').andSelf();
		formSelection_quest = formSelection_quest.add('.salsa .qFooter');
		formSelection_quest = formSelection_quest.add('.salsa .submit');
		formSelection = formSelection.add(formSelection_quest);
	}
	
	// if it's an event, do some special form stuff
	if ($('.salsa .event_view').length) {
		formSelection = $('.salsa #regheader').nextUntil('.salsa #addGuest').andSelf();
		formSelection = formSelection.add('.salsa #addGuest');
	}
	
	if (!$('.salsa .event_view').length) {
		formSelection =  formSelection.add('.salsa .formRow');
	}
	
	// if it's a tell a friend form, do some special form stuff
	if ($('.salsa #tafForm').length) {
		formSelection_taf = $('.salsa .sendItem').nextUntil('.salsa .supporterInfo').andSelf();
		formSelection_taf = formSelection_taf.add('.salsa #uneditable');
		formSelection_taf = formSelection_taf.add('.salsa #recaptcha_div');
		formSelection_taf = formSelection_taf.add('.salsa #submit');
		formSelection = formSelection.add(formSelection_taf);
	}
	
	formSelection.wrapAll('<div id="salsa-sidebar">');

	
	// if it's not an event form (which has it's own heading) add in an appropriate take action heading
	if (!$('.tellafriend').length) {

	} else if (!$('.salsa .event_view').length) {

	} else if ($('.salsa #tafForm').length) {
		$('.salsa #salsa-sidebar').prepend('<h3>SPREAD THE WORD:</h3>');
	} else {
		$('.salsa #salsa-sidebar').prepend('<h3>JOIN US:</h3>');
	}
	
	// select the content and wrap it
	
	// if it's an event form, do special event content stuff
	if ($('.salsa .event_view').length) {
		contentSelection = $('.salsa .event_view').nextUntil('#regheader').andSelf();
		contentSelection.wrapAll('<div id="salsa-content">');
		$('.salsa #salsa-content').after($('#salsa-sidebar'));
	} else if ($('.salsa #tafForm').length) {
		contentSelection = $('.salsa #salsa-sidebar').prevUntil('.salsa form');
		contentSelection = contentSelection.reverse().add('.salsa h2');
		contentSelection.wrapAll('<div id="salsa-content">');
	} else if (!($('.tellafriend').length)) {
		contentSelection = $('.salsa #salsa-sidebar').prevUntil('.salsa form');
		contentSelection.reverse().wrapAll('<div id="salsa-content">');
	}
	
	// if there are error messages, put them at the top of the page
	if (($('.error').length) && ($('.salsa').length)) {
		$('.salsa').prepend($('.error'));
	}
	
	// add a class to the event location <h2>
	if ($('.salsa .event_view').length) {
		$('h2').filter(function(index) {
			return $(this).text() == "Event Location";
		}).addClass('event-location');
	}
	
});


