define([
    'Global',
    'Clinic',
	'App'
],
    function(Global, Clinic, App) {
        var thisCollection,
            Clinics = Backbone.Collection.extend({
            model: Clinic,
            parse: function(response) {
                this.dictionary[response.clinicId] = response;
            },
            initialize: function() {
                this.dictionary = {};
                this.numberActive = 0;
	            this.invalidCallResponse = "";
                thisCollection = this;
            },
            getById: function(id) {
                return this.dictionary[id];
            },
            fetchClinics: function(options) {
                var ids = options.ids,
                    success = options.success,
	                error = options.error;

                this.urls = options.urls;

                this.deactivateClinics();

	            this.invalidClinics = [];
	            this.clinicsCounter = 0;

	            this.showLoadingSpinner();

                $.each(this.urls, function (key, value) {
                    thisCollection.fetchClinic({
                        url: value,
                        id: ids[key],
                        success: success,
	                    error: error,
                        length: thisCollection.urls.length
                    });
                });
            },
            fetchClinic: function(options) {
                var url = options.url,
                    id = options.id,
                    success = options.success,
	                error = options.error,
                    length = options.length,
	                that = this;

                new Clinic().fetch({
                    cache: false,
                    url: url,
                    success: function (response) {
                        response = response.toJSON();

                        thisCollection.dictionary[response.clinicId] = response;
                        thisCollection.numberActive++;

                        if (++thisCollection.clinicsCounter === length) {
	                        that.hideLoadingSpinner();

                            success();

	                        if (thisCollection.invalidClinics.length) {
		                        error({
			                        invalidClinics: thisCollection.invalidClinics,
			                        invalidCallResponse: thisCollection.invalidCallResponse
		                        });
	                        }
                        } else {
							that.showLoadingSpinner();
                        }
                    },
                    error: function (model, response) {
	                    thisCollection.invalidCallResponse += response.responseText + "\n\n";
						thisCollection.invalidClinics.push(id);

                        if (++thisCollection.clinicsCounter === length) {
	                        that.hideLoadingSpinner();

	                        error({
		                        invalidClinics: thisCollection.invalidClinics,
	                            invalidCallResponse: thisCollection.invalidCallResponse
	                        });

	                        if (thisCollection.numberActive) {
		                        success();
	                        }
                        } else {
							that.showLoadingSpinner();
                        }
                    }
                });
                // }
            },
            showLoadingSpinner: function() {
	            $.mobile.loading('show', App.getSpinnerOptions());
	            $('.ui-loader').css('display', 'block');
            },

            hideLoadingSpinner: function() {
	            $.mobile.loading('hide');
	            $('.ui-loader').removeAttr('style');
            },
            activeClinics: function () {
                var activeClinics = {},
                    keys = Object.keys(this.dictionary),
					clinicNum = 0;

                $.each(keys, function (key, value) {
                    var currentClinic = thisCollection.dictionary[value];

                    if (currentClinic.active) {
                        currentClinic.className = "clinic" + clinicNum++;

                        activeClinics[currentClinic.clinicId] = currentClinic;
                    }
                });

                return activeClinics;
            },
            deactivateClinics: function () {
                var keys = Object.keys(this.dictionary);

                this.numberActive = 0;

                $.each(keys, function (key, value) {
					var currentClinic = thisCollection.dictionary[value];

                    currentClinic.active = false;
					currentClinic.className = "";
                });
            },
	        getNumClinics: function () {
		        return this.numberActive;
	        }
        });

        Clinics.singleInstance = null;

        Clinics.instance = function () {
            Clinics.singleInstance = Clinics.singleInstance || new Clinics();
            return Clinics.singleInstance;
        };

        Clinics.clearInstance = function () {
            Clinics.singleInstance = null;
        };

        return Clinics;
    }
);
