$(function(){
	initViewSurgeon();
	initSubmitLinks();
	initFAQ();
	initTellaFriend();
	AjaxInitDoctorLocationFinder();
	AjaxRemoveDoctorPhoto();
	initSearchClicked();
	initFooterFix();
	initRequestConsultation();
	
});



function initRequestConsultation(){
	$("#consultation_request a.btn").live('click',function(e){
		e.preventDefault();
		$(this).closest('form').submit();
	});
}

function initFooterFix(){
	if($("#sub_content").length > 0){
		$("#footer_wrapper").css("margin-top","0");
	}
}

function initSearchClicked(){
	$("div.search form input[type=text]").live('focus',function(e){
		$(this).val('');
	});
}

function initViewSurgeon(){
	$("#surgeon_results tr.view_surgeon").live('click',function(e){
		e.preventDefault();
		var id = $(this).attr('id');
		window.location = '/view/surgeons/'+id;
	});
}

function AjaxRemoveDoctorPhoto(){
	$("form#doctors_edit a.remove_photo").live('click',function(e){
		e.preventDefault();
		var id = $(this).attr('id');
		$.post('/doctors/AjaxRemoveDoctorPhoto/'+id,
		{},
		function(data){
			window.location.reload();
		});
	});
}

function AjaxInitDoctorLocationFinder(){
	$("form#add_new_locations input.location_key").live('keyup',function(e){
		var key = $(this).val();
		var block = $(this).closest('div.location_block');
		$.post(
			"/doctors/AjaxFindDoctorsLocations",
			{
				key:key
			},
			function(data){
				data = $.trim(data);
				if(data){
					block.find('p.location').html(data);
				}
			}
		);
	});
	
	$("form#add_new_locations a.add_another_location").live('click',function(e){
		e.preventDefault();
		var block = $("#add_new_location_block").clone();
		block.show();
		$("form#add_new_locations div.location_wrapper").append(block.html());
	});
}

function initSubmitLinks(){
	$("a.btn.submit").live('click',function(e){
		e.preventDefault();
		$(this).closest("form").submit();
	});
}

function initFAQ(){
	function hideAll(){
		$("ul.FAQ li").each(function(e){
			$(this).removeClass('selected').find('.answer').hide('fast');
		});
	}
	
	hideAll();
	
	$('ul.FAQ li .question a').live('click',function(e){
		e.preventDefault();		
		if(!$(this).closest('li').hasClass('selected')){
			hideAll();
			$(this).closest('li').addClass('selected').find(".answer").slideDown('fast');
		}else{
			$(this).closest('li').removeClass('selected').find(".answer").slideUp('fast');
		}
	});
}

function initTellaFriend(){
	$("form#tell_a_friend table tr td a.btn").click(function(e){
		e.preventDefault();
		$(this).closest("form").submit();
	});
}

function initGoogleMap(stores) {
    /*var player_div = $("#player")
    var preloader = $("<div class='google_map_preloader'></div>");
    preloader.appendTo(player_div);*/

    var myOptions = {
        zoom: 15,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("google_map"), myOptions);

    var geocoder = geocoder = new google.maps.Geocoder();
    var infowindow = new google.maps.InfoWindow();

    /*var marker_icon = '/design/lilly/img/logo_map_lilly.png';
    var marker_shape = {
        coord: [1, 1, 1, 20, 18, 20, 18 , 1],
        type: 'poly'
    };*/

    geocoder.geocode( {'address': stores[0].addr}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {

            map.setCenter(results[0].geometry.location);
            for(i=0; i<stores.length; i++){
                var latlng = new google.maps.LatLng(results[0].geometry.location.lat(),results[0].geometry.location.lng());
                var marker = new google.maps.Marker({
                   position: latlng,
                   map: map
                   //icon: marker_icon,
                   //shape: marker_shape,
                   //title: getStoreInfoMessage(stores[i])
                });
                handleMarkerSingleClick(marker);
                handelMarkerDoubleClick(marker);
            }
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });


    function getStoreInfoMessage(store){
        var html = "<div class='infowin'><span class='header_athletic'>" + store["firm"] +"</span><br/>" + "<br/>"+store["addr1"] + " <br />" +store["addr2"]+ " <br />" +  store["city"] + " " +  store["province"] + " " +  store["postal_code"] + "<br/>Phone: "+store["phone"]+ "</div>";
        return html;
    }

    function handleMarkerSingleClick(marker){
		return false;
        google.maps.event.addListener(marker, "click", function(event){

            var sHTML = this.title;
				
            if(!infowindow){
                infowindow = new google.maps.InfoWindow();
            }
            infowindow.setContent(sHTML);
            infowindow.open(map, marker);

            map.setCenter(marker.getPosition())
        });
    }

    function handelMarkerDoubleClick(marker){
		return false;
        google.maps.event.addListener(marker, "dblclick", function(event){
            map.setZoom(14);
            map.setCenter(marker.getPosition());
        });
    }

}
