(function () {
	'use strict';

	var FIREBASE_URL = 'https://angular-node-base.firebaseio.com';

	angular.module('theApp.service.firebase', ['firebase'])
	.factory('FirebaseAuthRef', [ '$firebaseAuth', function ($firebaseAuth) {
		/**
		 * Devuelve la instancia de FirebaseAuth para el manejo de usuarios.
		 * @param {[type]} $firebaseAuth [description]
		**/
		var ref = new Firebase(FIREBASE_URL);

	    return $firebaseAuth(ref);
	}])
	.factory('GetFirebaseRef', ['Firebase', function (Firebase) {
		var DBWEB_URL = FIREBASE_URL + '/web';

		return function(path) {
        	return new Firebase(pathRef([DBWEB_URL].concat(Array.prototype.slice.call(arguments))));
      	}

      	//return new Firebase(FIREBASE_URL + '/web');
	}])
   .service('SyncFireDataService', ['$firebaseObject', 'GetFirebaseRef', SyncFireDataService]);

   /**
   * A simple utility to create $firebase objects from angularFire
   * @name SyncDataService
   * @param {String|Array...} path
   * @param {int} [limit]
   * @return a Firebase instance
   **/
   function SyncFireDataService ($firebaseObject, GetFirebaseRef) {

		return function(path, limit) {
    		var ref = GetFirebaseRef(path);
	    	limit && (ref = ref.limit(limit));

	    	return $firebaseObject(ref);
      	}
   };

   function pathRef (args) {

	   for(var i=0; i < args.length; i++) {
	      if( typeof(args[i]) === 'object' ) {
	         args[i] = pathRef(args[i]);
	      }
	   }

   		return args.join('/');
	};

})();
