$(document).ready(function() { 
	$('#browse > li > a').click(function() {
		if (!$(this).hasClass('on')) {
            hideBrowseMenus();
            $(this).siblings('ul').show();
            $(this).addClass('on');
		} else {
		  hideBrowseMenus();
		}
	});
	
	$('#meeting-description a').tooltip({ 
		track: true, 
		delay: 0, 
		showURL: false, 
		showBody: " - ", 
		fade: 250 
	});
	
	$("#select_fellowship ul li a").bind('click', function(e) {
        var fellowship = $(this).attr('name');
        var text = $(this).html();
        $("#fellowship").val(fellowship);
        
        $(this).parent().parent().parent().children('a').html(text + '<span></span>').click();
	   
	});

	$("#select_day ul li a").bind('click', function(e) {
        var day = $(this).attr('name');
        var text = $(this).html();
        $("#day").val(day);
        
        $(this).parent().parent().parent().children('a').html(text + '<span></span>').click();
	   
	});
	
	$("#select_start_time select").bind('change', function(e) {
        var startTime = $("#start_hour").val() + ':' + $("#start_min").val() + ' ' + $("#start_am_pm").val();
        $("#start_time").val(startTime);
        $("#select_start_time a").html(startTime + '<span></span>');
	});

	$("#select_end_time select").bind('change', function(e) {
        var endTime = $("#end_hour").val() + ':' + $("#end_min").val() + ' ' + $("#end_am_pm").val();
        $("#end_time").val(endTime);
        $("#select_end_time a").html(endTime + '<span></span>');
	});

    $("#zip").bind('change', function(e) {
        var zip = $(this).val();
        var element = $(this);
        
        var geocoder = new google.maps.Geocoder();
    
        geocoder.geocode({
            'address': zip + ", USA"
        }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                clearMarkers();

                map.setCenter(results[0].geometry.location);
                map.setZoom(10);
                var cityState = results[0].address_components[2].short_name + ', ' + results[0].address_components[3].short_name;
                element.parent().parent().parent().children('a').html(cityState + '<span></span>').click();
            }
        });
    
    });
    
    $("#filter-results").submit(function(e) {
        var address = $('#search_zip').val();
        var lat = $('#latitude').val();
        var long = $('#longitude').val();
        
        if (lat != 'false' && long != 'false') {
            return true;
        }
        
        if (address != '') {
            var geocoder = new google.maps.Geocoder();
        
            geocoder.geocode({
                'address': address
            }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    $("#latitude").val(results[0].geometry.location.lat());
                    $("#longitude").val(results[0].geometry.location.lng());
                    $("#filter-results").submit();
                }
                
            });
        } else {
            return true;
        }
        
        return false;

    });
    

    $("#search_form").submit(function(e) { return false; });
    
    $('#add-meeting').bind('click', function(e) {
        $.ajax({
            type: "POST",
            url: "/meetings/ajax/add_meeting",
            data: 'meeting_time_id=' + $(this).attr('name'),
            dataType: "json",
            success: function(json, text) {
                alert(json.message);
            }
        });

    
    });
    
    $("#lead-form").validate({
        submitHandler: function(form) {
        
            $.ajax({
                type: "POST",
                async: false,
                url: "/meetings/ajax/captcha",
                data: 'cccid=' + $('#cccid').val() + '&word=' + form.word.value,
                dataType: "json",
                success: function(json, text) {
                    if (json.success) {
                        form.submit();
                    } else {
                        alert('Please re-enter phrase.');
                        $('#word').css('border', '1px solid red').focus();
                    }
                }
            });
        }
    });


}); // document.ready


function hideBrowseMenus() {
    $('#browse li a').removeClass('on');
    $('#browse li ul').hide();

}

