var GDirs =
{
	dBox: null,
	originalText: null,
	addyBox: null,

	init: function()
	{
		$('#member-detail a.external').each(function() {
			$(this).click(function() {
				var $newWindow = window.open($(this).attr('href'));
				$newWindow.focus();
				return false;
			});
		});

		GDirs.addyBox = $('#toaddress');
		GDirs.originalText = GDirs.addyBox.val();
		GDirs.dBox = $('#google-div');
		GDirs.dBox.before('<a href="#" id="google-map-link" class="member-button" title="Get Driving Directions"> Directions </a>');
		GDirs.dBox.hide();
		$('#google-map-link').bind('click', GDirs.toggleBox);
		$('#GDirs').bind('submit', GDirs.submitForm);
		GDirs.addyBox.bind('focus', GDirs.clearForm);
		GDirs.addyBox.bind('blur', GDirs.fillForm);
        $('.list-add-link').click(function(){
            var member_id = $(this).attr('rel');
            $.ajax({
                type: "POST",
                url: glm_base_url + "trip-planner-list/" + member_id + "/" + glm_catid + "/",
                beforeSend: function() {$('#loading-planner').show()},
                complete: function() {$('#loading-planner').hide()},
                success: function(ret){
                    GDirs.toggleTravelLink(ret);
                }
            });
            return false;
        });
        
	},
    
    toggleTravelLink: function(event) {
        GDirs.updateList(event);
        var html = '<a href="' + glm_base_url + 'my-west-michigan-' + glm_trip_planner_id + '/" flexy:if="plink" class="list-view-link">';
        html += '<img alt="View Your Travel List" title="View Your Travel List" src="';
        html += glm_base_url + 'assets/viewTravelPlanner.png" style="float: right; margin: 10px;clear: right;">';
        html += '</a>';
        $('.list-add-link').replaceWith(html);
    },

	toggleBox: function(event)
	{
		event.preventDefault();
		GDirs.dBox.slideToggle('normal');
	},

	submitForm: function(event)
	{
		if (GDirs.addyBox.val() == GDirs.originalText || GDirs.addyBox.val() == '') {
			event.preventDefault();
			alert('Please fill in your Address');
		}
	},

	clearForm: function(event)
	{
		if (GDirs.addyBox.val() == GDirs.originalText) {
			GDirs.addyBox.val('');
		}
	},

	fillForm: function(event)
	{
		if (GDirs.addyBox.val() == '') {
			GDirs.addyBox.val(GDirs.originalText);
		}
	},
    updateList: function (listCount){
        $(".trip-list-count").html(listCount);
    }
};

$(document).ready(GDirs.init);

