(function(){"use strict";var a=Backbone.Collection.extend({fetch:function(a){return a||(a={}),a.dataType="xml",Backbone.Collection.prototype.fetch.call(this,a)},parse:function(a){return WCS.Core.Parse.parse(a).coverageDescriptions}}),b=this;b.define(["backbone","communicator","globals","models/DownloadModel","hbs!tmpl/Download","hbs!tmpl/SelectCoverageListItem","hbs!tmpl/CoverageInfo","hbs!tmpl/CoverageDownloadPost","underscore"],function(b,c,d,e,f,g,h,i){var j=b.Marionette.ItemView.extend({tagName:"div",id:"modal-start-download",className:"panel panel-default download",template:{type:"handlebars",template:f},modelEvents:{reset:"onCoveragesReset"},events:{"click #btn-select-all-coverages":"onSelectAllCoveragesClicked","click #btn-invert-coverage-selection":"onInvertCoverageSelectionClicked",'change input[type="checkbox"]':"onCoverageSelected","click #btn-start-download":"onStartDownloadClicked"},initialize:function(a){this.coverages=new b.Collection([])},onShow:function(b){this.listenTo(this.coverages,"reset",this.onCoveragesReset),this.$(".close").on("click",_.bind(this.onClose,this)),this.$el.draggable({containment:"#content",scroll:!1,handle:".panel-heading"});var c=this.$("#download-list");c.children().remove();var d=_.map(this.model.get("products"),function(b,c){var d=new a([]),e={};b.get("timeSlider")&&(e={subsetTime:[getISODateTimeString(this.model.get("ToI").start),getISODateTimeString(this.model.get("ToI").end)]}),e.subsetCRS="http://www.opengis.net/def/crs/EPSG/0/4326";var f=this.model.get("AoI").getExtent();return e.subsetX=[f[0],f[2]],e.subsetY=[f[1],f[3]],d.url=WCS.EO.KVP.describeEOCoverageSetURL(b.get("download").url,c,e),d},this),e=_.invoke(d,"fetch");$.when.apply($,e).done(_.bind(function(){_.each(d,function(a){a.each(function(b){b.set("url",a.url)})});var a=_.flatten(_.pluck(d,"models"));this.coverages.reset(a)},this))},onSelectAllCoveragesClicked:function(){this.$('input[type="checkbox"]').prop("checked",!0).trigger("change")},onInvertCoverageSelectionClicked:function(){this.$('input[type="checkbox"]').each(function(){var a=$(this);a.prop("checked",!a.is(":checked")).trigger("change")})},onCoveragesReset:function(){var a=this.$("#download-list");this.coverages.each(function(b){var c=b.toJSON(),d=$(g(c));a.append(d),d.find("i").popover({trigger:"hover",html:!0,content:h(c),title:"Coverage Description",placement:"bottom"})},this)},onCoverageSelected:function(){this.$("input:checked").length?this.$("#btn-start-download").removeAttr("disabled"):this.$("#btn-start-download").attr("disabled","disabled")},onStartDownloadClicked:function(){var a=$("#div-downloads"),b={},c=this.model.get("AoI").getExtent();b.subsetX=[c[0],c[2]],b.subsetY=[c[1],c[3]],b.format=this.$("#select-output-format").val(),b.outputCRS=this.$("#select-output-crs").val();var d=this.model.get("AoI").getCoordinates()[0];if(d.length>5){var e=[];_.each(d,function(a){e.push(a[0]),e.push(a[1])}),b.mask=e.join(" ")}this.$('input[type="checkbox"]').each(_.bind(function(c){if($('input[type="checkbox"]')[c].checked){var d=this.coverages.models[c],e=getCoverageXML(d.get("coverageId"),b),f=d.get("url").split("?")[0]+"?",g=$(i({url:f,xml:e}));a.append(g),_.delay(function(){g.submit()},1e3*c)}},this))},onClose:function(){c.mediator.trigger("ui:close","download"),this.close()}});return{DownloadView:j}})}).call(this);