/*
Written by Kyle Somogyi
*/
var filterList={
	'car':['Sedan', 'Coupe', 'Hatchback', 'Convertible', 'Wagon'],
	'suv':['SUV', 'Van', 'Car Van'],
	'truck':['Truck'],
	'all':['Truck', 'Sedan', 'Coupe', 'Hatchback', 'Convertible', 'SUV', 'Van', 'Wagon', 'Car Van']
};

function DropShow(__initArray)
{
	var me = this;

	this.vehicleDataCache=[];
	this.currentFilters = {
		'Type': 'all',
		'Price':
		{
			'min': 0,
			'max': 0
		}
	};
	this.withoutMake = me.setValue(__initArray.withoutMake, false);
	this.dealerID = me.setValue(__initArray.dealerID);
	this.lang = me.setValue(__initArray.lang, 2);
	this.showroom = me.setValue(__initArray.showroom, 'new-vehicle-showroom/');
	this.sortBy = me.setValue(__initArray.sortBy, 'Default');
	this.vehicleColor = me.setValue(__initArray.vehicleColor, false);
	this.scrollAmount = me.setValue(__initArray.scrollAmount, 10);
	this.multiFilter = me.setValue(__initArray.multiFilter, true);
}
DropShow.prototype.Init = function()
{
	var me = this;
	$.ajax({
		type: 'get',
		url: 'dropshow/showroomDriver.php',
		data: {
			'dID': me.dealerID,
			'lang': me.lang
		},
		success: function(data)
		{
			$('#dropContainer').find('#model_column').css('background', 'none');

			me.vehicleDataCache = data;
			for(x in data)
			{
				if(me.withoutMake == true && data[x].make != undefined)
					me.vehicleDataCache[x].model = me.stripMakeName(data[x].make, data[x].model);
			}

			if(me.sortBy != 'Default')
			{
				if(me.sortBy == 'Price')
					me.vehicleDataCache = me.vehicleDataCache.sort(me.sortList);
			}
			
			for(x in data)
			{
				if(me.vehicleDataCache[x].model != undefined)
					me.insertVehicle(data[x], true);
			}

			me.getVehicleData($('#dropContainer').find('#model_column #overflow_container span:first').attr('vid'));

			$('#body_row span').click(function()
			{
				if($(this).attr('filter') == 'all')
				{
					$('#price_row span').each(function(i, elem)
					{
						$(elem).removeClass('selected');
					});
					me.currentFilters.Price.min = 0;
					me.currentFilters.Price.max = 0;
				}
				if(me.multiFilter == false)
				{
					$('#price_row span').each(function(i, elem)
					{
						$(elem).removeClass('selected');
					});
					me.currentFilters.Price.min = 0;
					me.currentFilters.Price.max = 0;
				}
				$('#body_row span').each(function(i, elem)
				{
					$(elem).removeClass('selected');
				});
				$(this).addClass('selected');

				me.currentFilters.Type = $(this).attr('filter');
				me.filterResults();
			});
			$('#price_row span').click(function()
			{
				if(me.multiFilter == false)
				{
					$('#body_row span').each(function(i, elem)
					{
						$(elem).removeClass('selected');
					});
					me.currentFilters.Type = 'all';
				}
				$('#price_row span').each(function(i, elem)
				{
					$(elem).removeClass('selected');
				});
				$(this).addClass('selected');

				me.currentFilters.Price.min = $(this).attr('minprice');
				me.currentFilters.Price.max = $(this).attr('maxprice');

				me.filterResults();
			});

			me.initUIEvents();
		},
		dataType: 'json'
	});
}

DropShow.prototype.getVehicleData = function(__vID)
{
	var me = this;
	$('#dropContainer').find('#vehicle_photo').css('background', 'url(loading.gif) center center no-repeat');

	for(x in me.vehicleDataCache)
	{
		if(me.vehicleDataCache[x].NewId == __vID)
		{
			var title = me.vehicleDataCache[x].model;

			var imageObj = new Image();
			imageObj.src = 'images/image.php?file=showroom_thumbs/'+me.vehicleDataCache[x].image;
			$('#dropContainer').find('#vehicle_photo').html('<a href="'+me.showroom+'/'+me.vehicleDataCache[x].FakeUrlFile+'"><img src="'+imageObj.src+'" /></a>');
			$('#dropContainer').find('#vehicle_photo').css('background', 'none');
			$('#dropContainer').find('#vehicle_title').html(title);
			$('#dropContainer').find('#vehicle_msrp').html('<span>MSRP*</span> '+me.c(me.vehicleDataCache[x].MSRPMin));

			var leadButtons = [
				'<a href="bap.php?id='+me.dealerID+'&make='+me.vehicleDataCache[x].make+'&model='+(me.vehicleDataCache[x].model.replace(' ', '+'))+'&country=ssccdn">Build and Price</a>',
				'<a href="https://www.agedstock.com/creditapp.php?id='+me.dealerID+'" target="_blank">Get Pre-Approved</a>',
				'<a href="'+me.showroom+'/'+me.vehicleDataCache[x].FakeUrlFile+'?mod=leads&new_id='+me.vehicleDataCache[x].NewId+'&id='+me.dealerID+'&lead=tradein" rel="Shadowbox; width=600; height=550" class="shadowbox">Trade in Quote</a>',
				'<a href="'+me.showroom+'/'+me.vehicleDataCache[x].FakeUrlFile+'?mod=leads&new_id='+me.vehicleDataCache[x].NewId+'&id='+me.dealerID+'&lead=testdrive" rel="Shadowbox; width=600; height=550" class="shadowbox">Schedule a test drive</a>'
			].join('');
			$('#dropContainer').find('#lead_buttons').html(leadButtons);
			Shadowbox.setup("a.shadowbox");

			break;
		}
	}
}

DropShow.prototype.initUIEvents = function()
{
	var me = this;
	$('#model_column #overflow_container span').hover(function()
	{
		if($(this).hasClass('disabled') != true)
		{
			$('#model_column #overflow_container span').each(function(i, elem)
			{
				$(elem).removeClass('selected');
			});

			$(this).addClass('selected');
			me.getVehicleData($(this).attr('vid'));
		}
	});
}

DropShow.prototype.filterResults = function()
{
	var me = this;
	var filters = me.currentFilters.Type;
	
	$('#dropContainer').find('#model_column #overflow_container').html('');

	for(x in me.vehicleDataCache)
	{
		if(isNaN(x) == false)
		{
			var found=false;

			if(filters == 'hybrid')
			{
				var model = me.vehicleDataCache[x].model;
				if(model.search(/Hybrid/i) > 0)
				{
					found=true;
				}
			} else {
				for(t in filterList[filters])
				{
					if(me.vehicleDataCache[x].asl_btype == filterList[filters][t])
					{
						found=true;
						break;
					}
				}
			}
			
			if(me.currentFilters.Price.min != 0 && me.currentFilters.Price.max != 0)
			{
				var __pMin = parseInt(me.currentFilters.Price.min);
				var __pMax = parseInt(me.currentFilters.Price.max);
				if(found == true)
				{
					if(__pMax == 'inf')
					{
						if(me.vehicleDataCache[x].MSRPMin > __pMin)
							me.insertVehicle(me.vehicleDataCache[x], true);
						else
							me.insertVehicle(me.vehicleDataCache[x], false);
					} else {
						if(me.vehicleDataCache[x].MSRPMin > __pMin && me.vehicleDataCache[x].MSRPMin < __pMax)
							me.insertVehicle(me.vehicleDataCache[x], true);
						else
							me.insertVehicle(me.vehicleDataCache[x], false);
					}
				} else {
					me.insertVehicle(me.vehicleDataCache[x], false);
				}
			} else {
				if(found == true)
					me.insertVehicle(me.vehicleDataCache[x], true);
				else
					me.insertVehicle(me.vehicleDataCache[x], false);
			}
		}
	}

	me.initUIEvents();
	$('#dropContainer').find('#model_column #overflow_container span').each(function(i, elem)
	{
		if($(this).hasClass('disabled') != true)
		{
			$('#model_column #overflow_container span').each(function(i, elem)
			{
				$(elem).removeClass('selected');
			});
			
			$(this).addClass('selected');
			me.getVehicleData($(this).attr('vid'));
			return false;
		}
	});
}

DropShow.prototype.c = function(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents)
}

DropShow.prototype.stripMakeName = function(__make, __model)
{
	var model = __model.replace(__make+' ', '');

	return model;
}

DropShow.prototype.sortList = function(a, b)
{
	if(a.MSRPMin == b.MSRPMin)
	{
		return 0;
	}

	return (a.MSRPMin < b.MSRPMin) ? -1 : 1;
}

// Value, Default Value
DropShow.prototype.setValue = function(v, dv)
{
	if(v != undefined && v != null)
		return v;
	else
		return dv;
}

DropShow.prototype.insertVehicle = function(vehicle, state)
{
	if(vehicle.model != undefined)
		$('#dropContainer').find('#model_column #overflow_container').append('<a href="new-vehicle-showroom/'+vehicle.FakeUrlFile+'"><span vid="'+vehicle.NewId+'"'+((state == false) ? ' class="disabled"' : '')+'>'+vehicle.model+'</span></a>');
}
