The Zurich Extragalactic Bayesian Redshift Analyzer and its first application: COSMOS