(function(){var c=this,d=c.jQuery,b=(c.DDC=(c.DDC||{})),a=function(e){return function(){var f=Error.apply(this,arguments);f.name=e;return f}};b.GenericSmartbrowse=function(t){var n=this,u={},x=null,o="",w={},g=[],p=[],v=function(A){var B=A,y,z;for(z=0;z<p.length;z++){if(d.isFunction(p[z])){y=p[z].apply(this,[B]);if(y&&(typeof y==="object")&&!d.isArray(y)){B=y}else{throw new b.GenericSmartbrowse.errors.ObjectExpectedError("Request processor did not return an object.  "+p[z]+' returned:  "'+y+'"')}}}return B},j=function(z){var y=z,B,A;for(A=0;A<g.length;A++){if(d.isFunction(g[A])){B=g[A].apply(this,[y]);if(B&&(typeof B==="object")&&!d.isArray(B)){y=B}else{throw new b.GenericSmartbrowse.errors.ObjectExpectedError("Response processor did not return an object.  "+g[A]+' returned:  "'+B+'"')}}}return y},h=function(y){x=y},l=function(){return d.extend(true,{},x)},s=function(){return d.extend(true,{},u)},m=function(z,y){if((typeof z==="object")&&!y){u=d.extend(true,{},u,z)}else{if((typeof z==="object")&&y){u=d.extend(true,{},z)}else{throw new b.GenericSmartbrowse.errors.ObjectExpectedError("setParams() expected one argument, an object.")}}},e=function(){return d.extend(true,{},w[o])},i=function(y){if(w[y]){o=y}else{throw new b.GenericSmartbrowse.errors.InventoryTypeDoesNotExistError("Specified type: "+y+" does not exist.")}},q=function(y){if(d.isFunction(y)){p.push(y)}else{throw new b.GenericSmartbrowse.errors.FunctionExpectedError("addRequestProcessor() expected one argument, a function.")}},f=function(y){if(d.isFunction(y)){g.push(y)}else{throw new b.GenericSmartbrowse.errors.FunctionExpectedError("addResponseProcessor() expected one argument, a function.")}},k=function(y){if(y&&y.inventoryType){this.setInventoryType(y.inventoryType)}if(y&&y.params){this.setParams(y.params,y.clear)}this.makeRequest({params:v.apply(n,[s()]),inventoryType:e(),callback:function(z){if(z&&(typeof z==="object")&&!d.isArray(z)){h(j.apply(n,[z]));if(d.isFunction(n.view)){n.view.apply(n,[l()])}}else{throw new b.GenericSmartbrowse.errors.ObjectExpectedError('makeRequest() did not return an object.  Got "'+z+'" instead.')}}})},r=function(z){var y;this.getData=l;this.getParams=s;this.setParams=m;this.getInventoryType=e;this.setInventoryType=i;this.addResponseProcessor=f;this.addRequestProcessor=q;this.commit=k;w=d.extend(true,{},(z&&z.inventoryTypes));if(z&&z.requestProcessors){for(y=0;y<z.requestProcessors.length;y++){this.addRequestProcessor(z.requestProcessors[y])}}if(z&&z.responseProcessors){for(y=0;y<z.responseProcessors.length;y++){this.addResponseProcessor(z.responseProcessors[y])}}if(z&&z.view){this.view=z.view}m((z&&z.params)||{});return this};if(this!==b){return r.apply(this,arguments)}else{throw new b.GenericSmartbrowse.errors.InvalidInvocationError("DDC.GenericSmartbrowse is a constructor and may not be called directly.")}};b.GenericSmartbrowse.errors={InvalidInvocationError:a("InvalidInvocationError"),ObjectExpectedError:a("ObjectExpectedError"),ArrayExpectedError:a("ArrayExpectedError"),FunctionExpectedError:a("FunctionExpectedError"),SelectExpectedError:a("SelectExpectedError"),ArgumentsExpectedError:a("ArgumentsExpectedError"),URLExpectedError:a("URLExpectedError"),InventoryTypeDoesNotExistError:a("InventoryTypeDoesNotExistError"),PackageMissingError:a("PackageMissingError")};b.GenericSmartbrowse.errors.ObjectExpectedError.prototype=Error;b.GenericSmartbrowse.errors.ArrayExpectedError.prototype=Error;b.GenericSmartbrowse.errors.FunctionExpectedError.prototype=Error;b.GenericSmartbrowse.errors.SelectExpectedError.prototype=Error;b.GenericSmartbrowse.errors.ArgumentsExpectedError.prototype=Error;b.GenericSmartbrowse.errors.URLExpectedError.prototype=Error;b.GenericSmartbrowse.errors.InventoryTypeDoesNotExistError.prototype=Error;b.GenericSmartbrowse.errors.PackageMissingError.prototype=Error;b.GenericSmartbrowse.requests={ajax:function(e){e=d.extend(true,{params:{},inventoryType:{},callback:null},e);d.ajax({dataType:"json",success:e.callback,url:e.inventoryType.ajax+b.GenericSmartbrowse.url.urlize(e.params)})},api:function(e){e=d.extend(true,{params:{},service:null,callback:null,url:"http://apis.dealer.com/services/inventory/v1/smartbrowse"},e);if(b.Api&&b.Api.DataAccessManager){b.Api.DataAccessManager.get(e)}else{throw new b.GenericSmartbrowse.errors.PackageMissingError("DDC.Api.DataAccessManager is required to use the api request type.")}}};b.GenericSmartbrowse.requestProcessors={ajax:{compliance:function(e){if(!d.isArray(e)){throw new b.GenericSmartbrowse.errors.ArrayExpectedError("compliance() expected one argument, an array of franchises.")}return function(f){if(e.length===1){if((d.inArray("bmw",e)!==-1)&&(this.getInventoryType().name==="new")){this.setParams({SBmake:"BMW"});f.SBmake="BMW"}if(d.inArray("acura",e)!==-1){this.setParams({SBmake:"Acura"});f.SBmake="Acura"}if(d.inArray("honda",e)!==-1){this.setParams({SBmake:"Honda"});f.SBmake="Honda"}}return f}}}};b.GenericSmartbrowse.responseProcessors={ajax:{compliance:function(e){if(!d.isArray(e)){throw new b.GenericSmartbrowse.errors.ArrayExpectedError("compliance() expected one argument, an array of franchises.")}return function(g){var h,f;if(e.length===1){if((d.inArray("bmw",e)!==-1)&&(this.getInventoryType().name==="new")&&g.SBmake&&g.SBmake.BMW){g.SBmake={BMW:g.SBmake.BMW}}if((d.inArray("bmw",e)!==-1)&&(this.getInventoryType().name==="used")&&g.SBmake&&g.SBmake.BMW){h={BMW:g.SBmake.BMW};for(f in g.SBmake){if(f!=="BMW"){h[f]=g.SBmake[f]}}g.SBmake=h}if((d.inArray("acura",e)!==-1)&&g.SBmake&&g.SBmake.Acura){g.SBmake={Acura:g.SBmake.Acura}}if((d.inArray("honda",e)!==-1)&&g.SBmake&&g.SBmake.Honda){g.SBmake={Honda:g.SBmake.Honda}}}return g}},nameValueReplacement:function(f,e){if(!(f&&(e||(e==="")))){throw new b.GenericSmartbrowse.errors.ArgumentsExpectedError("nameValueReplacement() expected two arguments, a regular expression or string and a string.")}return function(h){var g,i;for(g in h){if((g!=="SBdefaults")&&(typeof h[g]==="object")&&!(h[g] instanceof Array)){for(i in h[g]){if(h[g][i]){h[g][i]=h[g][i].replace(f,e)}}}}return h}},removeNullKeyEntries:function(f){var e,g;for(e in f){if((e!=="SBdefaults")&&(typeof f[e]==="object")&&!d.isArray(f[e])){for(g in f[e]){if(!g){delete f[e][g]}}}}return f}}};b.GenericSmartbrowse.prototype={makeRequest:b.GenericSmartbrowse.requests.ajax};d.extend(b.GenericSmartbrowse,{url:{redirect:function(e){if(e){c.location=e}else{throw new b.GenericSmartbrowse.errors.URLExpectedError("redirect() expected one argument: a url.")}},urlize:function(m,j,h,g){j=j||"?";h=h||"&";g=g||"=";var f,n="",l,k=b.GenericSmartbrowse.url.urlize,e=function(i){return encodeURIComponent(i).replace(/\+/g,"%2B").replace(/\"/g,"%22").replace(/\'/g,"%27")};switch(typeof m){case"object":if(m){for(f in m){if(typeof(l=k(m[f]))!=="function"){if(n){n+=h}n+=e(k(f))+g+e(l)}}return(n.length>=1?j:"")+n}else{return"null"}break;case"string":return m;case"number":return String(m);case"boolean":return m.toString();default:return"null"}},heliosUrlize:function(k,f){var g,j={SByear:"year",SBmake:"make",SBmodel:"model",SBbodystyle:"bodyStyle",SBprice:"internetPrice",SBhighwayMPG:"highwayMpg"},e=["SBprice","SBhighwayMPG"],h={},i=f.toString();if(k&&(typeof k==="object")&&!d.isArray(k)){for(g in k){if(d.inArray(g,e)!==-1){if(j[g]){h[j[g]]=k[g].toString();h[j[g]]="["+h[j[g]].replace(/\s?\-\s?/g," TO ")+"]"}}else{if(j[g]){h[j[g]]=k[g].toString()}}}}else{throw new b.GenericSmartbrowse.errors.ObjectExpectedError("heliosUrlize() expected an object but got: "+k)}if(i){i.replace(/[&?]$/,"");h=b.GenericSmartbrowse.url.urlize(h);if(i.indexOf("?")!==-1){h=h.replace("?","&")}i+=h}else{throw new b.GenericSmartbrowse.errors.URLExpectedError("heliosUrlize() expects a URL as its second argument.")}return i}},ui:{clearOptions:function(f){if(f&&(f.tagName.toLowerCase()==="select")){for(var e=(f.options.length-1);e>=1;e--){f.options[e]=null}}else{throw new b.GenericSmartbrowse.errors.SelectExpectedError("clearOptions() expects one argument: a `select` element.")}return f}},util:{capitalize:function(e){return e.replace(/\w+/g,function(f){return f.charAt(0).toUpperCase()+f.substr(1).toLowerCase()})}}})}());