// JavaScript Document
$(document).ready(function() {
	 $('img.closevehicle').click(function(){
		closeBubble();
	 });
	 $.ajax({
   		url: 'xml/IBCQuickSearch.js',
		dataType: 'json',
		beforeSend: function(){
			var loadingmake = $('<select name="loadingmake"><option>Loading Make....</option></select>');
			var loadingmodel = $('<select name="loadingmodel"><option>Loading Model....</option></select>');
			$('select[name="make"]').hide();
			$('select[name="model"]').hide();
			$('.makeload').append(loadingmake);
			$('.modelload').append(loadingmodel);
		},
		success: function(data){		
			var makehtml = '';
			$('select[name="make"]').show();
			$('select[name="model"]').show();
			$('select[name="loadingmake"]').remove();
			$('select[name="loadingmodel"]').remove();
			$.each(data, function(make){
				makehtml += '<option value="'+ make +'">'+ make +'</option>'
			});
			
			var domCount = 1;
			
			$('select[name="make"]').append(makehtml).change(function(){
				var modelhtml = '';
				var makeTemp = $(this).val();
				/*
				if(document.all)
				{
					if(domCount == 1)
					{
						$('select[name="model"] option:second-child').siblings().remove();
						domCount++;
					}
					else
					{
						$('select[name="model"] option:first-child').siblings().remove();
					}

					$.each(data[makeTemp],function(model,modelcount){
						modelhtml += '<option value="'+ model + '">' + model + ' - ' + modelcount.length + '</option>';
					});
				}
				else
				{
					if(domCount == 1)
					{
						$('select[name="model"] option:second-child').siblings().remove();
						domCount++;
					}
					else
					{
						$('select[name="model"] option:first-child').siblings().remove();
					}

					$.each(data[makeTemp],function(model,modelcount){
						modelhtml += '<option value="'+ model + '">' + model + ' - ' + modelcount.length + '</option>';
					});
				}
				*/
				if(domCount == 1)
					{
						$('select[name="model"] option:second-child').siblings().remove();
						domCount++;
					}
					else
					{
						$('select[name="model"] option:first-child').siblings().remove();
					}

					$.each(data[makeTemp],function(model,modelcount){
						modelhtml += '<option value="'+ model + '">' + model + ' - ' + modelcount.length + '</option>';
				});
				
				
				$('select[name="model"]').append(modelhtml);
			});

			$('select[name="model"]').change(function(){
				var makeval = $('select[name="make"]').val();
				var modelval = $(this).val();
				$('#quicksearchBubbleContainer').fadeIn('fast');
				$('table#vehiclelist').empty();
				var tablehtml = '';
				$.each(data[makeval][modelval],function(vehicleindex, vehicleinfo ){
					tablehtml += '<tr id="indexid' + vehicleindex + '">';
					tablehtml += '<td>' + vehicleinfo['Year'] + '</td>';
					tablehtml += '<td>' + vehicleinfo['Chassis'] + '</td>';
					tablehtml += '<td>' + vehicleinfo['Trans'] + '</td>';
					tablehtml += '<td>' + Math.ceil(parseInt(vehicleinfo['KMS'])) + '</td>';
					tablehtml += '<td>' + vehicleinfo['Color'] + '</td>';
					tablehtml += '</tr>';
				});
				$('#vehiclelist').html(tablehtml);
				$('#vehiclelist tr:eq(0)').addClass('selectedid');
				var indexVehicle = $('#vehiclelist tr:eq(0)').attr('id').replace('indexid','');
				getVehicle(makeval,modelval,indexVehicle);
				var listLength = $('#vehiclelist tr').length - 1;
				if (listLength == 0){
					$('.vehiclecontrols').hide();
				} else {
					$('.vehiclecontrols').show();
				}
				$('#vehiclelist tr').click(function(){
					var vehicleIndex = $(this).attr('id').replace('indexid','')
					getVehicle(makeval,modelval,vehicleIndex);
					$('#vehiclelist tr').removeClass();
					$(this).addClass('selectedid')
				}).css('cursor','pointer');
			});
			
			function getVehicle(makeval,modelval,index){
				var carhtml = '';
				carhtml += '<h3>' + data[makeval][modelval][index]['Year'] + ' ' + makeval + ' ' + modelval + ' ' + data[makeval][modelval][index]['Color'] + '</h3>'
				carhtml += '<img src="http://images.ibcjapan.co.jp' + data[makeval][modelval][index]['Photo'] + '/' + data[makeval][modelval][index]['ID'] + '_F_Small.jpeg" alt="Car Photo" title="Car Photo" />'
				carhtml += '<ul>'
				carhtml += '<li>' + data[makeval][modelval][index]['Trans'] + ' ' + data[makeval][modelval][index]['Color'] + ' ' + data[makeval][modelval][index]['Fuel'] + '</li>'
				carhtml += '<li>KMS: ' + Math.ceil(parseInt(data[makeval][modelval][index]['KMS'])) + '</li>'
				carhtml += '<li>Condition Grade:' + data[makeval][modelval][index]['Grade'] + '</li>'
				carhtml += '<li>Chassis no.: <strong>' + data[makeval][modelval][index]['Chassis'] + '</strong></li>'
				carhtml += '</ul>'
				$('#quicksearchresult').html(carhtml);
			}
			
			$('.prevvehicle').click(function(){
				var prevVehicle = $('tr.selectedid').prev().attr('id');	
				var makeval = $('select[name="make"]').val();
				var modelval = $('select[name="model"]').val();
				if (prevVehicle) {
					var valueprev = prevVehicle.replace('indexid','')
					$('tr.selectedid').removeClass().prev().addClass('selectedid');
					getVehicle(makeval,modelval,valueprev);
				} else {
					var resetIDprev = $('#vehiclelist tr:last-child').attr('id');
					$('#vehiclelist tr').removeClass();
					if (resetIDprev){
						var prevVal = resetIDprev.replace('indexid','');
						$('#vehiclelist tr:last-child').addClass('selectedid');
						getVehicle(makeval, modelval, prevVal);
					}
				}
				return false;
			});
			$('.nextvehicle').click(function(){
				var nextVehicle = $('tr.selectedid').next().attr('id');
				var makeval = $('select[name="make"]').val();
				var modelval = $('select[name="model"]').val();
				if (nextVehicle) {
					var valuenext= nextVehicle.replace('indexid','');
					$('tr.selectedid').removeClass().next().addClass('selectedid');
					getVehicle(makeval, modelval, valuenext);
				} else {
					var resetIDnext = $('#vehiclelist tr:first-child').attr('id');
					$('#vehiclelist tr').removeClass();
					if (resetIDnext) {
						var resetval = resetIDnext.replace('indexid','');
						$('#vehiclelist tr:first-child').addClass('selectedid');
						getVehicle(makeval, modelval, resetval);
					}
				}
				return false;
			});
		}	
	 });
});

function closeBubble(){
	resetForm('quicksearchform');					
	$('select[name="model"] option:first-child').siblings().remove();
	$('#quicksearchBubbleContainer').fadeOut('slow');
}

