/* Annie Kuster javascript/jquery */

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

$(document).ready(function() {
	
	// select the form and wrap it
	//formSelection = $('.salsa form');
	
	if ($('.salsa #questionnaireForm').length) {
		formSelection = $('.salsa input[name|="organization_KEY"]').nextUntil('.salsa #questionnaireQuestions').andSelf();
		formSelection_quest = $('.salsa #questionnaireQuestions');
		formSelection = formSelection.add(formSelection_quest);
	} else {
		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_event = $('#regheader');
		formSelection = formSelection.add(formSelection_event);
	}
	
	if (!$('.salsa #questionnaireForm').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 class="action-sidebar">');
	
	
	// if it's not an event form (which has it's own heading) add in an appropriate take action heading
	if ($('.salsa .event_view').length) {
	
	} else if ($('.salsa #tafForm').length) {
		$('.salsa .action-sidebar').prepend('<h3>SPREAD THE WORD:</h3>');
	} else {
		$('.salsa .action-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('.salsa .supporterInfo').andSelf();
		contentSelection.wrapAll('<div id="post">');
		$('.salsa #salsa-content').after($('.action-sidebar'));
	} else if ($('.salsa #tafForm').length) {
		contentSelection = $('.salsa .action-sidebar').prevUntil('.salsa form');
		contentSelection = contentSelection.reverse().add('.salsa h2');
		contentSelection.wrapAll('<div class="action-content">');
	} else {
		contentSelection = $('.salsa .action-sidebar').prevUntil('.salsa form');
		contentSelection.reverse().wrapAll('<div <div class="action-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');
	}
	
});


