Spain legalizes same-sex marriage