/*! Opensearchlight - v0.4.0 - 2013-08-16
* https://github.com/nsidc/OpenSearchlight
* Copyright (c) 2013 Regents of the University of Colorado; Licensed MIT
*/
var OpenSearchlight=OpenSearchlight||{};!function(){function a(a){var b=[];return _.isArray(a)?a:(b.push(a),b)}OpenSearchlight.query=function(a){var b;OpenSearchlight.ensureParamsNotEmpty(a),OpenSearchlight.ensureParamsHasOsdd(a),OpenSearchlight.ensureParamsHasSuccessHandler(a),b=OpenSearchlight.generateOsddSuccessFn(a),OpenSearchlight.openSearchService.query(a.osdd,b,a.error)},OpenSearchlight.generateOsddSuccessFn=function(a){return function(b){var c;a instanceof Object&&(_.each(a.parameters,function(a,c){b.set(c,a)}),void 0!==a.contentType&&b.setContentType(a.contentType),void 0!==a.requestHeaders&&b.setRequestHeaders(a.requestHeaders)),c=OpenSearchlight.extendWith({},a,["success","error","queryXhr"]),b.execute(c)}},OpenSearchlight.extendWith=function(b,c,d){var e=_.extend(b),f=[];return"object"!=typeof c?b:(f=a(d),_.each(f,function(a){c.hasOwnProperty(a)&&(e[a]=c[a])}),e)},OpenSearchlight.ensureParamsNotEmpty=function(a){if(void 0===a)throw new Error("Must pass a params object")},OpenSearchlight.ensureParamsHasOsdd=function(a){if("string"!=typeof a.osdd)throw new Error("Must pass an osdd value in the params object")},OpenSearchlight.ensureParamsHasSuccessHandler=function(a){if("function"!=typeof a.success)throw new Error("Must pass a success handler in the params object")}}(),function(a,b){var c;c=OpenSearchlight.OpenSearchQuery=function(){this.initialize.apply(this,arguments)},b.extend(c.prototype,{initialize:function(a){if(0===arguments.length)throw new Error("Must pass OSDD's XML");if(a instanceof OpenSearchlight.OpenSearchDescriptionDocument)this.openSearchDescriptionDocument=a;else{if("string"!=typeof a)throw new Error("Invalid argument to OpenSearchQuery");this.openSearchDescriptionDocument=this.createOpenSearchDescriptionDocument(a)}this.searchParams={}},set:function(a,b){return this.searchParams[a]=b,this},setContentType:function(a){return this.contentType=a,this},setRequestHeaders:function(a){return this.requestHeaders=a,this},execute:function(c){var d=this.openSearchDescriptionDocument.getQueryUrl(this.getParams(),this.getContentType()),e=this.getRequestHeaders(),f=a.ajax({url:d,beforeSend:function(a){b.each(e,function(b){a.setRequestHeader(b.name,b.value)},this)},success:function(a,b,d){c.success(d)},error:c.error});void 0!==c.queryXhr&&c.queryXhr(f)},get:function(a){return this.searchParams[a]},getRequestHeaders:function(){return this.requestHeaders},getContentType:function(){return this.contentType},getParams:function(){return this.searchParams},createOpenSearchDescriptionDocument:function(a){return new OpenSearchlight.OpenSearchDescriptionDocument(a)}}),b.extend(c,{})}(jQuery,_),function(a,b){OpenSearchlight.openSearchService={query:function(c,d,e){a.ajax({url:c,success:b.bind(function(a,b,c){d.call(this,this.createQueryObject(c.responseText))},this),error:b.bind(function(a){e.call(this,a)},this)})},createQueryObject:function(a){return new OpenSearchlight.OpenSearchQuery(a)}}}(jQuery,_),function(a,b){var c;c=OpenSearchlight.OpenSearchDescriptionDocument=function(){this.initialize.apply(this,arguments)},b.extend(c.prototype,{initialize:function(a){if(!c.validate(a))throw new Error("Error parsing xml");this.osddXml=a},getQueryUrl:function(a,b){var d,e,f;return d=c.extractTemplateUrls(this.osddXml),e=c.getBestTemplate(d,b,a),f=c.substituteTemplateParameters(e,a)}}),b.extend(c,{validate:function(a){return a?a.match(/http:\/\/a9.com\/-\/spec\/opensearch\/1.1\//)?!0:!1:!1},extractTemplateUrls:function(b){var c=[],d=a(b).find("url[rel!=self]");return d.each(function(){c.push({type:this.getAttribute("type"),template:this.getAttribute("template")})}),0===c.length?void 0:c},getBestTemplate:function(a,b,d){var e,f,g;return e=c.filterUrlTemplatesOnMimeType(a,b),f=c.filterUrlTemplatesWithMissingRequiredParams(e,d),g=c.findTemplateWithMostParamMatches(f,d),g.template},filterUrlTemplatesOnMimeType:function(a,d){return b.filter(a,function(a){return c.doesContentTypeMatch(d,a.type)})},doesContentTypeMatch:function(a,b){var c,d;return"*/*"===a?!0:a===b?!0:(c=a.split("/"),d=b.split("/"),c[0]===d[0]&&"*"===c[1]?!0:"*"===c[0]&&c[1]===d[1]?!0:!1)},filterUrlTemplatesWithMissingRequiredParams:function(a,d){return b.filter(a,function(a){return c.areAllRequiredParamsPresent(a,d)})},areAllRequiredParamsPresent:function(a,c){var d,e,f;return d=b.map(a.template.match(/\{[^}]*\}/g),function(a){return a.replace(/[{}]/g,"")}),e=b.filter(d,function(a){return"?"!==a.substring(a.length-1)}),f=b.keys(c),0===b.difference(e,f).length},XareAllRequiredParamsPresent:function(a,c){var d,e;return d=b.map(a.template.match(/\{.*?[^?]\}/g),function(a){return a.replace(/[{}]/g,"")}),e=b.keys(c),0===b.difference(d,e).length},findTemplateWithMostParamMatches:function(a,d){var e=b.sortBy(a,function(a){return c.countMatchingParams(d,a.template)});return b.last(e)},countMatchingParams:function(a,c){var d;return d=b.map(c.match(/\{.*?\}/g),function(a){return a.replace(/[{}]/g,"")}),b.reduce(d,function(b,c){return void 0===a[c]?b:b+1},0)},substituteTemplateParameters:function(a,c){var d=a;return b.each(c,function(a,b){d=d.replace(new RegExp("{"+b+"\\??}"),a)}),d=d.replace(/\{.*?\?\}/g,"")}})}(jQuery,_);