var DeveloperPlayground = DeveloperPlayground || {};

DeveloperPlayground.DownloadView = Backbone.View.extend({
	el: '#download-bar',
	
	template: _.template( $('#download-bar-template').html() ),

	events: {
        'click .btn' : 'submit',
    },

    //  if (feature.get('display') !== false) {


	render: function() {
		renderedHtml = this.template();
		this.$el.html(renderedHtml);
        $("#download-bar").hide();
		return this;
		},

	setCollection: function(options){
        this.collection = options.collection;
    },

	submit: function(event) {

		if (this.collection.url.split("/").pop() == "operators.json?per_page=5000" ) {
			url = this.collection.url + "?onestop_id=" + this.collection.findWhere({display: true}).get("onestop_id")+'&per_page=5000';
		} else {
			url = this.collection.url;
		}

		// if only one operator is visible, then set url parameter to include that operator identifier
        
		if (event.target.id == "csv") {
			url = url.replace(".json", ".csv");
            ga('send', 'event', 'button', 'click', 'csv');
		} else if (event.target.id == "json") {
			var blob = new Blob([JSON.stringify(this.collection.toJSON())], {type: "application/json;charset=utf-8"});
			saveAs(blob, "download.json");
            ga('send', 'event', 'button', 'click', 'json');
            ga('send', 'event', 'button', 'click', 'data download: '+url);

		}

		window.open(url, '_blank');

	}


 });

@@ -1041,17 +1041,32 @@
 k', 'csv
-'
+ download: '+url
 );%0A%09%09%7D e
@@ -1310,84 +1310,8 @@
 ');%0A
-            ga('send', 'event', 'button', 'click', 'data download: '+url);%0A%0A
 %09%09%7D%0A

add analytics tracking for url to data download click events