$(document).ready(function() {
				
	// survey sliding function
	$('h2').click(function() {
		var clicked = $(this);
		$('.step:hidden div').fadeTo(0,0);
		
		// change the font opacity on the banner text
		$('h2.active').removeClass('active');
		clicked.addClass('active');
		
		// slide up/down
		if ($('.step:visible div').length) { // if any are open close them first
				
				$('.step:visible div').fadeTo(500, 0.0, function() { 
					
						$(this).parent('.step').slideUp(300); 
		
						clicked.next('.step').slideDown(300, function() {
						
								$(this).children('div').fadeTo(300,1.0, function() {
								
										if(jQuery.browser.msie) $(this).get(0).style.removeAttribute('filter');
								
								});
						});
				});
		
		} else { // if page loads with all closed
			
			clicked.next('.step').slideDown(300, function() {
			
					$(this).children('div').fadeTo(300,1.0, function() {
					
							if(jQuery.browser.msie) $(this).get(0).style.removeAttribute('filter');
					
					});
			});
		
		}
        
        // update progress indicator
        var currentSection = clicked.attr("id").replace("survey-section-", "");
        for(var i=1; i<=7; i++) {
            if (i <= currentSection) {
                $('#progress-' + i).removeClass('incomplete');
                $('#progress-' + i).addClass('complete');
            } else {
                $('#progress-' + i).removeClass('complete');
                $('#progress-' + i).addClass('incomplete');
            }
        }
	});	
	
	
	// survey filling in
	$('td.fill').click(function() {
		
		//remove checkmarks
		var inputname = $(this).children('input').attr('name');
		$('input[name=' + inputname + ']').each(function() {
			$(this).parent('td').removeClass('checked');
		});
		
		//add checkmark and check hidden input
		$(this).children('input').attr('checked','checked');
		$(this).addClass('checked');
	});
	
	
	
	// close membership q
	$('p.close a').click(function() {
		closeSideText($(this));
		return false;
	});	
	
	// open membership q
	$('a.more').click(function() {
		var clicked = $(this);
		if ($('.gradient div:visible').length) {
			var otherOpen = $('.gradient div:visible').find('.close a');
			closeSideText(otherOpen, function() {
				openSideText(clicked);
			});
		} else {
			openSideText(clicked);
		}
		return false;
	});	
	
	// select fields
	$('form .select_field').click(function() {
				
		var selopts = $(this).children('div');
		var dropdowns = $('form .select_field div');
		var index = $(dropdowns).index(selopts);
		
		// make it appear over other select fields
		$('form .select_field').css('z-index','1');
		$(this).css('z-index','2');
		
		// hide other select fields that are open
		$(dropdowns).each(function() {
			if ($(dropdowns).index(this) != index) $(this).hide();
		});
		
		// drop down or roll up this one
		if ($(selopts).is(':hidden')) {
			selopts.slideDown(50);
		} else {
			selopts.slideUp(50);
		}
		
	});
	
	// choosing from select field
	$('form .select_field span').click(function() {
	
		var newselection = $(this).html();
		var newselectionVal = $(this).attr('class');
		var realfield = $('#' + $(this).parent().attr('class'));
		
		// change appearance of selected
		$(this).parent().children('span').removeClass('selected');
		$(this).addClass('selected');
		
		// put this text into the top
		$(this).parent().parent().children('p').html(newselection);
		
		// secretly select the option
		$(realfield).val(newselectionVal);
		
	});
	
	// pop up tips
	$('#res_tips h4').click(function() {
		var which = $('#res_tips h4').index($(this));
		$('#res_tips div').each(function() {
		 	if ($('#res_tips div').index($(this)) != which) $(this).fadeOut(200);
		 });
		$('#res_tips div:eq(' + which + ')').slideToggle();
		return false;
	});
	
	$('#res_tips a').click(function() {
		var which = $('#res_tips a').index($(this));							
		$('#res_tips h4:eq(' + which + ')').click();
		return false;
	});
	
	function closeSideText(clicked, callback) {	
							
		$(clicked).fadeTo(200, 0.0, function() {
			$(clicked).parent().parent('div').fadeTo(200, 0.0, function() { 
				
				$(this).slideUp(200, function() {
					
					$(this).parent().find('.more').fadeTo(200, 1.0, function() {
							
						if(jQuery.browser.msie) $(this).get(0).style.removeAttribute('filter');

					});
					
					if (callback != null) callback();
					
				}); 
			});
		});
		
		
	}
	
	function openSideText(clicked) {
	
	$(clicked).parent().parent().children('div').children('.close').children('a').fadeTo(0,0);	
		$(clicked).fadeTo(300, 0.0, function() { 
		
			$(clicked).parent().parent().children('div').slideDown(300, function() {
				
				$(this).fadeTo(400, 1.0, function() {
						
					if(jQuery.browser.msie) $(this).get(0).style.removeAttribute('filter');
					
					$(this).children('p.close').children('a').fadeTo(500, 1.0, function() {
					
						if(jQuery.browser.msie) $(this).get(0).style.removeAttribute('filter');
					
					});
				});
			}); 
		});
	}

		
});

