Optical galaxy cluster detection and weak gravitational lensing derived galaxy cluster mass functions : the development of a new algorithm and its application to the Millennium Simulation, CFHTLS-Deep, and CFHTLS-Wide surveys