(function ($) {
	var QuickList = {

		buildUl: function(settings) {
			//console.log('buildUl '+settings.dataset+' settings')
			//console.log(settings)
			settings.invBaseUrl = settings.invpaths[settings.invtype];
			var dataset = settings.dataset;
			var makeParam = (!settings.makeParam) ? '' : settings.makeParam; // creaeing either tHe SBmake string or a blank space
			var myList = [];
			var sbdata = settings.sbdata;
			var pushLi = function(value, valueString) {
				myList.push('<li class="invQListItem">');
				myList.push('<a href="', settings.invBaseUrl, '?reset=InventoryListing&', dataset, '=', value, makeParam, '">', valueString, '</a>');
				myList.push('</li>');
			};
			for (var key in sbdata[dataset]) {
				var value = key.toString().replace(' ','%20').replace('&','%26');
				var valueString = sbdata[dataset][key].replace('(','<span>').replace(')',' '+settings.labels.inStock+'</span>');
				if (dataset == "SBmileage") {
					value = (key - 9999) + ' - ' + key;
					valueString = (key - 9999).toString().replace(/(\d+)(\d{3})/, '$1' + ',' + '$2') + ' - ' + key.toString().replace(/(\d+)(\d{3})/, '$1' + ',' + '$2');
				}
				if (key != '') {
					 pushLi(value,valueString)
				}
			}
			myList = myList.join('');
			return myList;
		},

		compliantMake: false,

		doLoad: function (settings) {
			var that = this;
			var id = '#'+this.settings.id;
			$.ajax({
				beforeSend:function(){
					$(".invQList ").parent().addClass('loading')
					$(".invQList ").hide()
				},
				url: settings.url,
				dataType: 'json',
			success: function (sbdata) {
				that.settings.sbdata = sbdata;
				that.sb_load(that.settings);
				$(".invQList ").parent().removeClass('loading')
				$(".invQList ").show()
				}
			});
		},

		get_invtype: function() {
			var id = this.settings.id
			var formRef = document.forms[id];
			var invtype;

			if (typeof(formRef.invtype.length) === 'undefined')
				invtype = formRef.invtype.value;
			else
				for (i = 0; i < formRef.invtype.length; i++) {
					if (formRef.invtype[i].checked) {
						invtype = formRef.invtype[i].value;
						break;
					}
				}
			return(invtype);
		},

		getCompliancySettings: function(settings) {
			if (settings.franchises.length == 1) {
				var franchises = settings.franchises;
				if (franchises[0] == 'Honda' || franchises[0] == 'Acura') {
					this.settings = $.extend({
						compliantMake: franchises[0],
						disableMakes: 'true',
						showOthersMakes: 'none'
					}, settings);
				} else if (franchises[0] == 'BMW') {
					this.settings = $.extend({
						compliantMake: franchises[0],
						disableMakes: 'true',
						showOthersMakes: 'used'
						/*
												showOthersMakes: 'none'
							  */
					}, settings);
				}
			}

		},

		isSetup: false,


		needsReload: false,

		sb_ajax: function(selRef) {
			var settings = this.settings;
			var formId = this.settings.id;
			var formRef = document.forms[formId];
			settings.invtype = this.get_invtype();
			var invtype = settings.invtype;
			formRef.action = settings.invpaths[invtype];
			settings.url = settings.ajaxpaths[invtype] + '?' + ((selRef) ? selRef.name + '=' + escape(selRef.value) : 'reset=InventoryListing');
			for (var criteria in settings.showsettings) {
				settings.url += '&' + criteria + '=' + settings.showsettings[criteria];
			}
			if (settings.compliantMake != undefined && (settings.showOthersMakes != invtype)) {
					var compliantMake = settings.compliantMake;
					settings.url += '&SBmake=' + compliantMake;
			}
			settings.url += '&detect=false';

			//alert(settings.url);
			this.settings = settings;
			this.doLoad(settings);

		},

		sb_load: function (settings) {
			settings.makeParam = undefined;
			var buildUl = this.buildUl;
			var sbdata = settings.sbdata;
			var className;
			for (var dataset in sbdata) {
				if (dataset !== 'SBdefaults') {
					settings.dataset = dataset;
					if( settings.compliantMake != undefined && (settings.showOthersMakes != settings.invtype)){
						settings.makeParam = '&SBmake='+settings.compliantMake;
					}
					var Ul = buildUl(settings);
					className = dataset.replace('SB','.');
					$('.invQList',className).html(Ul)
				}
			}
			if (!this.isSetup || this.settings.needsReload === 'true') {
				this.isSetup = true;
			}

		},

		settings:{},

		setupMenulisting: function () {

		},

		init: function(settings) {
			var that = this
			this.settings = $.extend({
				franchises: [], // will be used to ensure complancy
				style:'style1',
				id:'invQListForm'
			}, settings);
			var id = '#'+this.settings.id;
			this.getCompliancySettings(settings);
			this.sb_ajax();
			$('input',id).click(function(){
				that.sb_ajax();
			})

			
		}
	};
	window.DDC.QuickList = QuickList;
})
		(jQuery);