You'll need to use an internal DNS server on your network to host a DNS zone for mydomain.com. Then when you're connected to your internal network mydomain.com will be resolved by your internal DNS server (assuming that you're using a DHCP server internally and that you're computer is configured to use DHCP for it's ip address configuration and that the DHCP server is configured to provide your internal DNS server to you via DHCP). When you're not connected to your internal network mydomain.com will be resolved by whatever DNS servers you're using on those other networks.
The problem with using the Hosts file is that it takes precedence over the DNS servers configured in your client TCP/IP configuration, so mydomain.com will always resolve to what is in the Hosts file, regardless of what network you're connected to.
You could certainly do something weird with scripting but I wouldn't recommend it. An approach as proposed by joeqwerty makes sense. Instead of going for a fully blown DNS server you might just maintain such an entry in the router that usually provides the DNS for small home office setups if your device supports that. – binaryanomaly – 2014-04-26T15:48:10.353