$(document).ready(function() {
	//fix png
	$(document).pngFix();
	
	
		
		
	// navigation drop downs		
	$("ul.topnav li a").hover(
		function () {
			$(this).parent().find("ul.subnav").stop().slideDown('fast').show('slow', function(){$(this).height("auto");});
		}, 
		function () {
			$(this).parent().hover(function() {	}, 
				function(){	
				$(this).parent().find("ul.subnav").stop().slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
			});
		}
	);

	// --------------------------
	// home page slideshow
	// --------------------------
	$('body').css({opacity: .999999});
	$('.pics').cycle({
		fx: 'fade',
		speed: 1000,
		timeout: 6000,
		delay: -4000
	});
	
	// --------------------------
	// home page slideshow
	// --------------------------
	$('.tour a, a.tour').click(function(){
		$('#tour').modal({
			overlay: 80,
			onShow: function(dialog){
				$('.pics').cycle('pause');
			},
			onClose: function(){
				$('.pics').cycle('resume');
				$.modal.close();
			}
		});
		return false;
	});
	
	// --------------------------
	// Toggle default form values
	// --------------------------
	// Login information
	$('#quick_login #username, #quick_login #password, #your_name, #your_comment').toggleVal();

	// Signup form
	$('#signup #first_name, #signup #last_name').toggleVal();

	// Newsletter Signup
	$('#newsletter #newsletter_email').toggleVal();

	// toggles the slickbox on clicking the noted link 
	// hides the slickbox as soon as the DOM is ready
	// (a little sooner than page load)
	$('#copy-relationship').hide();
	$('#copy-school').hide();
	$('#copy-hangout').hide();
	$('#copy-destination').hide();
	$('a#copy-relationship-button').click(function() {
		$('#copy-relationship').slideToggle(400);
		return false;
	});	
	$('a#copy-school-button').click(function() {
		$('#copy-school').slideToggle(400);
		return false;
	});
	$('a#copy-hangout-button').click(function() {
		$('#copy-hangout').slideToggle(400);
		return false;
	});	
	$('a#copy-destination-button').click(function() {
		$('#copy-destination').slideToggle(400);
		return false;
	});
	// ------------
	// Striped list
	// ------------
	$('.stripe li:odd').addClass('alt');
	$('.stripe tr:even').addClass('alt');

	// ------------------------------------------------------------
	// Highlight current set of fields on signup form and show tips
    // Don't highlight inputs with a class of "button".
	// ------------------------------------------------------------
	$('#primary input, #primary input[@type="radio"], #primary table input, #primary select, #primary textarea').not('.button, .flyer_image, .image-buttons').each(function(index) {
        $(this).focus(function() {
            if ($(this).parent('div').parent('div').hasClass('section')) {
                // If there is a div with class of "section", then it's nested and
                // we need to highlight the level above the normal one.
                $(this).parent('div').parent('div').addClass('here');
                $(this).parent('div').parent('div').children('.tip').show();
            } else if ($(this).parents('div').hasClass('highlight')) {
				$(this).parents('div.highlight').addClass('here');
               	$(this).parents('div.highlight').children('.tip').show();
			} else {
               	// Normal situation.
               	$(this).parent('div').addClass('here');
               	$(this).parent('div').children('.tip').show();
			}
        }).blur(function() {
            if ($(this).parent('div').parent('div').hasClass('section')) {
                // Nested
                $(this).parent('div').parent('div').removeClass('here');
                $(this).parent('div').parent('div').children('.tip').hide();
            } else if ($(this).parents('div').hasClass('highlight')) {
				$(this).parents('div.highlight').removeClass('here');
               	$(this).parents('div.highlight').children('.tip').hide();
			} else {
                // Normal
                $(this).parent('div').removeClass('here');
                $(this).parent('div').children('.tip').hide();
            }
        });
	});
	
	
    // ------------------------------------------
    // Show section names on signup crumb circles
    // ------------------------------------------
    $('#form_crumbs li').hover(function() {
        $(this).children('.tip').show();
    }, function() {
        $(this).children('.tip').hide();
    });

    // --------
    // FAQ page
    // --------
    $('#primary').addClass('js');
    $('#frequently_asked_questions h2').mouseover(function() {
        $(this).addClass('over');
    });
    $('#frequently_asked_questions h2').mouseout(function() {
        $(this).removeClass('over');
    });
    $('#frequently_asked_questions h2').click(function() {
        $(this).siblings('.answer').slideToggle('fast');
        $(this).toggleClass('open');
    });

    // -----------------------------
    // Child Profile > Identity page
    // -----------------------------
	// When Identity form is submitted un-disable field so PHP can read it
	$('#identityForm').submit(function() {
		$('#birth_state').removeAttr('disabled');
		return true;
	});
	
    // Suffix
    if ($('#suffix_other').val() == '') {
        $('#suffix_other_div').hide();
    } else {
        $('#suffix_list_div').hide();
    };
    $('#suffix_list_div a, #suffix_other_div a').click(function() {
        $('#suffix_list_div').toggle();
        $('#suffix_other_div').toggle();
        return false;
    });
    $('#suffix_other_div a').click(function() {
        $('#suffix_other').val('');        
    });
    
    // Team Heard About
	$("#team_heard_about_additional_div").hide();
	
	 $('#team_heard_about_additional_div a').click(function() {
        $('#team_heard_about_additional_div').toggle();
        $('#team_heard_about_list_div').toggle();
        $('#team_heard_about').val("");
        return false;
    });
    $("#team_heard_about").change(function() { 
        var message_index 
        
        message_index = $("#team_heard_about").val(); 
        message_text = $(this).find(':selected').text();
        
        $("#team_heard_about_additional").val("");
        
 		if(message_index == 'referral' || message_index == 'advertisement' || message_index == 'other' || message_index == "cscp") {
        	$("#additional_label").html(message_text);
        	$("#team_heard_about_additional_div").show();
        	$("#team_heard_about_list_div").hide();
        } 
    }); 
    
	// Nicknames
    if ($('#nickname3').val() == '') {
        $('#nickname3_div').hide();
    }
	else {
		$('#nickname2_div a').hide();
	}
    if ($('#nickname2').val() == '') {
        $('#nickname2_div').hide();
    }
	else {
		$('#nickname_div a').hide();
	}

    $('#nickname_div a').click(function() {
		$(this).hide();
        $('#nickname2_div').show();
        return false;
    });

    $('#nickname2_div a').click(function() {
		$(this).hide();
        $('#nickname3_div').show();
        return false;
    });

    // Be Aware Of
    if ($('#be_aware_of_other').val() == '') {
        $('#be_aware_of_other').hide();
    } else {
        $('a.be_aware_of_other').hide();
        $('a.be_aware_of_other').parent('div').html('Other(s):');
    };
    $('a.be_aware_of_other').click(function() {
        $(this).hide();
        $(this).parent('div').html('Other(s):');
        $('#be_aware_of_other').fadeIn();
        return false;
    });

    // Birthmark(s)
    if ($('#birthmark_description').val() == '') {
        if ($('input[@name="birthmark"]').getValue() != 2) {
            $('.birthmark_description').hide();
        }
    };
    $('#birthmark_yes').click(function() {
        $('.birthmark_description').fadeIn();
    });
    $('#birthmark_no').click(function() {
        $('.birthmark_description').fadeOut();
        $('#birthmark_description').val('');
    });


    // Scars/Tattoos
    if ($('#scars_tattoos_description').val() == '') {
        if ($('input[@name="scars_tattoos"]').getValue() != 2) {
            $('.scars_tattoos_description').hide();
        }
    };
    $('#scars_tattoos_yes').click(function() {
        $('.scars_tattoos_description').fadeIn();
    });
    $('#scars_tattoos_no').click(function() {
        $('.scars_tattoos_description').fadeOut();
        $('#scars_tattoos_description').val('');
    });

    // Your Relationship to Child
    if ($('#your_relationship_other').val() == '') {
        if ($('input[@name="your_relationship"]').getValue() != 'other') {
            $('#your_relationship_other').hide();
        };
    };
    $('.your_relationship_other').click(function() {
        $('#your_relationship_other').fadeIn();
    });
    $('#your_relationship_parent').click(function() {
        $('#your_relationship_other').fadeOut();
        $('#your_relationship_other').val('');
    });
    $('#your_relationship_guardian').click(function() {
        $('#your_relationship_other').fadeOut();
        $('#your_relationship_other').val('');
    });
    
    // Race
    if ($('#race_other').val() == '') {
        $('#race_other_div').hide();
    } else {
        $('#race_list_div').hide();
    };
    $('#race_list_div a, #race_other_div a').click(function() {
        $('#race_list_div').toggle();
        $('#race_other_div').toggle();
        return false;
    });
    $('#race_other_div a').click(function() {
        $('#race_other').val('');
    });

    // Hair Color
    if ($('#hair_color_other').val() == '') {
        $('#hair_color_other_div').hide();
    } else {
        $('#hair_color_list_div').hide();
    };
    $('#hair_color_list_div a, #hair_color_other_div a').click(function() {
        $('#hair_color_list_div').toggle();
        $('#hair_color_other_div').toggle();
        return false;
    });
    $('#hair_color_other_div a').click(function() {
        $('#hair_color_other').val('');        
    });
	
	// Hair Length
    if ($('#hair_length_other').val() == '') {
        $('#hair_length_other_div').hide();
    } else {
        $('#hair_length_list_div').hide();
    };
    $('#hair_length_list_div a, #hair_length_other_div a').click(function() {
        $('#hair_length_list_div').toggle();
        $('#hair_length_other_div').toggle();
        return false;
    });
    $('#hair_length_other_div a').click(function() {
        $('#hair_length_other').val('');        
    });
	
	// Hair Type
    if ($('#hair_type_other').val() == '') {
        $('#hair_type_other_div').hide();
    } else {
        $('#hair_type_list_div').hide();
    };
    $('#hair_type_list_div a, #hair_type_other_div a').click(function() {
        $('#hair_type_list_div').toggle();
        $('#hair_type_other_div').toggle();
        return false;
    });
    $('#hair_type_other_div a').click(function() {
        $('#hair_type_other').val('');        
    });

    // Facial Hair
    if ($('#facial_hair_other').val() == '') {
        $('#facial_hair_other_div').hide();
    } else {
        $('#facial_hair_list_div').hide();
    };
    $('#facial_hair_list_div a, #facial_hair_other_div a').click(function() {
        $('#facial_hair_list_div').toggle();
        $('#facial_hair_other_div').toggle();
        return false;
    });
    $('#facial_hair_other_div a').click(function() {
        $('#facial_hair_other').val('');        
    });

    // Skin Tone
    if ($('#skin_tone_other').val() == '') {
        $('#skin_tone_other_div').hide();
    } else {
        $('#skin_tone_list_div').hide();
    };
    $('#skin_tone_list_div a, #skin_tone_other_div a').click(function() {
        $('#skin_tone_list_div').toggle();
        $('#skin_tone_other_div').toggle();
        return false;
    });
    $('#skin_tone_other_div a').click(function() {
        $('#skin_tone_other').val('');        
    });
    
	// Disable 'State' field if country isn't USA
	$('#birth_country').change(function() {
		if ($(this).val()=='United States') {
			if ($('#birth_state').val()=='NA') {
				$('#birth_state').val('--');
			}
			$('#birth_state').removeAttr('disabled');
		} else {
			$('#birth_state').val('NA');
			$('#birth_state').attr('disabled','disabled');
		}
	});

    // Hide and show address fields if "Same as contact" is checked/unchecked.
    if ($('input[@name="address_same"]').getValue() != '') {
        $('.child_address').hide();
    };
    $('#address_same').click(function() {
        if ($('input[@name="address_same"]').getValue() != '') {
            $('#address1').val('');
            $('#address2').val('');
            $('#city').val('');
            $('#state').val('');
            $('#zip').val('');
            $('.child_address').fadeOut();
        } else {
            $('.child_address').fadeIn();
        };
    });
   

    

    // Passport
    if ($('input[@name="passport"]').getValue() != 2) {
        $('.passport_info').hide();
    };
    $('#passport_yes').click(function() {
        $('.passport_info').fadeIn();
    });
    $('#passport_no').click(function() {
        $('#passport_country').val('');
        $('#passport_number').val('');
        $('.passport_info').fadeOut();
    });
    
    // Drive
    if ($('input[@name="drive"]').getValue() != 2) {
        $('.license_info').hide();
    };
    $('#drive_yes').click(function() {
        $('.license_info').fadeIn();
    });
    $('#drive_no').click(function() {
        $('#license_number').val('');
        $('#license_state').val('');
        $('#license_exp_date').val('');
        $('.license_info').fadeOut();
    });
    if ($('input[@name="adopted"]').getValue() != 2) {
        if ($('#adoption_details').val() == '') {
            $('.adoption_details').hide();
        };
    };
    $('#adopted_yes').click(function() {
        $('.adoption_details').fadeIn();
    });
    $('#adopted_no').click(function() {
        $('#adoption_details').val('');
        $('.adoption_details').fadeOut();
    });

    $('#not_online').change(function() {
		if ($('#not_online').attr("checked")) {
	        $('#online_profiles').fadeOut();
		} else {
	        $('#online_profiles').fadeIn();
		}	

    });

    // Parental Marital Status
    //if ($('#custody_details').val() == '') {
        var maritalStatus = $('input[@name="parental_marital_status"]').getValue();
        if (maritalStatus != 'Separated' && maritalStatus != 'Single' && maritalStatus != 'Other') {
            $('.custody_details').hide();
        };
    //};
    $('#parental_marital_status_married').click(function() {
        $('.custody_details').fadeOut();
    });
    $('#parental_marital_status_separated').click(function() {
        $('.custody_details').fadeIn();
    });
    $('#parental_marital_status_single').click(function() {
        $('.custody_details').fadeIn();
    });
    $('#parental_marital_status_other').click(function() {
        $('.custody_details').fadeIn();
    });

    // ----------------------------
    // Child Profile > Medical page
    // ----------------------------
    // Medical & Psychological Conditions
    if ($('#medical_condition_other').val() == '') {
        $('#medical_condition_other').hide();
    } else {
        $('a.medical_condition_other').hide();
        $('a.medical_condition_other').parent('div').html('<strong>Details</strong>');
    };
    function addDetails()
	{
		$('a.medical_condition_other').click(function() {
        	$(this).hide();
        	$(this).parent('div').html('<strong>Details</strong>');
        	$('#medical_condition_other').fadeIn();
        	return false;
    	});
	}
	addDetails();
	$('input[name="medical_condition[]"]').click(function(){
		var checked = $('input[name="medical_condition[]"]:checked');
		if(checked.length!=0)
		{
			$('a.medical_condition_other').hide();
			$('div.other_link').html('<strong>Details</strong>');
       		$('#medical_condition_other').fadeIn();
		} else {
			$('#medical_condition_other').fadeOut();
			$('div.other_link').html('<a class="medical_condition_other" href="#">Add details</a>');
       		$('a.medical_condition_other').show();
			addDetails();
		}
	});

	// Disabilities
	if ($('input[@name="disabilities"]').getValue() != '2') {
        if ($('#disabilities_description').val() == '') {
            $('.disabilities_description').hide();
        };
    };
    $('#disabilities_yes').click(function() {
        $('.disabilities_description').fadeIn();
    });
    $('#disabilities_no').click(function() {
        $('#disabilities_description').val('');
        $('.disabilities_description').fadeOut();
    });

	// Special Needs
	if ($('input[@name="special_needs"]').getValue() != '2') {
        if ($('#special_needs_description').val() == '') {
            $('.special_needs_description').hide();
        };
    };
    $('#special_needs_yes').click(function() {
        $('.special_needs_description').fadeIn();
    });
    $('#special_needs_no').click(function() {
        $('#special_needs_description').val('');
        $('.special_needs_description').fadeOut();
    });

    // Fingerprint Kit?
    if ($('input[@name="fingerprint_kit"]').getValue() != '2') {
        if ($('#fingerprint_location').val() == '') {
            $('.fingerprint_location').hide();
        };
    };
    $('#fingerprint_yes').click(function() {
        $('.fingerprint_location').fadeIn();
    });
    $('#fingerprint_no').click(function() {
        $('#fingerprint_location').val('');
        $('.fingerprint_location').fadeOut();
    });
    
    // DNA Profile Indicator
    if ($('input[@name="dna_profile"]').getValue() != '2') {
        if ($('#dna_location').val() == '') {
            $('.dna_location').hide();
        };
    };
    $('#dna_yes').click(function() {
        $('.dna_location').fadeIn();
    });
    $('#dna_no').click(function() {
        $('#dna_location').val('');
        $('.dna_location').fadeOut();
    });

    $('#child_select').change(function() {
        $('#child_form').submit();
    });

    // --------------------------------------------
    // Child Profile > Relationships > Relationship
    // --------------------------------------------
    // Relationship to Child
    if ($('#relationship_other').val() == '') {
        $('#relationship_other_div').hide();
    } else {
        $('#relationship_list_div').hide();
    };
    $('#relationship_list_div a').click(function() {
        $('#relationship_list_div').hide();
        $('#relationship_other_div').show();
        $('#relationship_other_div #relationship_other').show();
        return false;
    });
    $('#relationship_other_div a').click(function() {
        $('#relationship_list_div').show();
        $('#relationship_other_div').hide();
        return false;
    });
	$('#relationship_other_div a').click(function() {
		$('#relationship_other').val('');
	});
	
	// Relationship to Child on Add Children
	$('.relationship_other').each(function(){
		if($(this).val() == '') {
    		$(this).parent().hide();
    	} else {
    		$(this).parent().prev('.relationship_list_div').hide();
    	}
	});
	$('.relationship_list_div a').click(function() {
		$(this).parent().next('.relationship_other_div').show();
		$(this).parent().hide();
		return false;
	});
	$('.relationship_other_div a').click(function() {
		$(this).parent().prev('.relationship_list_div').show();
		$(this).parent().hide();
		return false;
	});
	$('.relationship_other_div a').click(function() {
		$(this).prev('.relationship_other').val('');
	});
		

    // Relationship modification
    $('.modify input.delete').hide();
    $('.modify input.delete').after(' <a class="deletebutton" href="#">Delete</a>');
    $('.modify a.deletebutton').click(function() {
        if (confirm('Are you sure you want to delete this item?')) {
            $(this).parent('form').submit();
        };
		return false;
    });
	//Relationship adding
    $('.modify input.add').hide();
    $('.modify input.add').after(' <a class="addbutton" href="#">Add</a>');
    $('.modify a.addbutton').click(function() {
    	$(this).parent('form').submit();
		return false;
    });
	
	//Relationship copying
    $('.modify input.copy').hide();
    $('.modify input.copy').after(' <a class="addbutton" href="#">Copy</a>');
    $('.modify a.addbutton').click(function() {
    	$(this).parent('form').submit();
		return false;
    });
    // ---------------
    // Profile Preview
    // ---------------
    $('h3.collapse').click(function() {
        $(this).next('table').slideToggle();
    });
    $('h3.collapse').addClass('js')

    // -----------
    // Date Picker
    // -----------
    Date.format = 'mm/dd/yyyy';
    $('.date-pick').datePicker({clickInput:true, startDate:'01/01/1990'});

    $('#num_children').change(function() {
		var cost = $('#num_children').getValue() * 29.95;
		$('#totalcost').html(" = <strong>$" + cost.toFixed(2) + "</strong>");
	});
	
	
	// On the fly error checking
	$('.js_validate').each(function () {
		num = get_digits($(this));
		if (num > 0) {
			if (!valid_digits($(this).val(), num, true)) {
				$(this).parent().addClass('jserror');
				$(this).next().children('.status').html('<strong>Invalid</strong>');
			}
		}
	});
	
	$('.js_validate').blur(function() {
		num = get_digits($(this));
		if (num > 0) {
			if (valid_digits($(this).val(), num, true))	{
				$(this).parent().removeClass('jserror');
				if ($(this).val().length > 0)
					$(this).next().children('.status').html('');
				else
					$(this).next().children('.status').html('');
			}
			else {
				$(this).parent().addClass('jserror');
				$(this).next().children('.status').html('<strong>Invalid</strong>');
			}
		}
	});

	// I will make you cooperate, IE 6.
	$('input[@type="radio"], input[@type="checkbox"]').css("border", "none");
	
	
    // ---------------
    // Add secret field to Profile Identity form to allow page jump
    // ---------------
	$('input[name="btnSubmitIdentityContinue"], input[name="btnSubmitMedicalContinue"]').click(function(){
		var section = $(this).attr('alt');
		$(this).parent().append('<input type="hidden" name="scroll_to" value="'+section+'" class="hidden" />');
		return true;
	});
	
	
	// ---------------
    // Add secret field to contact form to stop spam
    // ---------------
	$('#contact').append('<input type="hidden" name="super_secret" value="ihatebots" class="hidden" />');
	
	
	// ---------------
    // Hide tips and save to db
    // ---------------
	$('#hide_tips').click(function(){
		$('#tips').slideUp();
		$.get('/ajax.php', { save_hide_tips: "true" } );
	});
	
	
	// ---------------
    // Signup Page
    // ---------------
	if($('#heard_about').val()=='my-gym')
		$('#my_gym').show();
		
	$('#heard_about').change(function(){
		val = $(this).val();
		if(val=='my-gym')
			$('#my_gym').slideDown();
		else
			$('#my_gym').slideUp();
	});
	
	
	// ---------------
    // Give-A-Gift
    // ---------------
	$('a.edit_details').click(function(){
		$('#gag-details').slideToggle();
		return false;
	});
	
	
	// --------------------------
	// Newsletter Form Validation
	// --------------------------
	$('#newsletter').validate();
	
	
	// ------------------------------
	// Test n Target - make divs hide
	// ------------------------------
	/*$('form div:visible').filter(function(index){
		var opt = $('input[type="hidden"]',this).size();
		return opt;
	}).hide();
	*/
});

function get_digits(el) {
	if (el.hasClass('ssn'))
		return 9;
	else if (el.hasClass('phone'))
		return 10;
	else
		return 0;
}
function valid_digits(string, num, empty) {
	if (empty && string == '')
		return true;
	digits = string.replace(/[^0-9]/g,'');
	if (digits.length == num)
		return true;
	else
		return false;
}

