// 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();
		},
		success: function(data){		
	
			var makehtml = '';
			$('select[name="make"]').show();
			$('select[name="model"]').show();
			$('select[name="loadingmake"]').remove();
			$('select[name="loadingmodel"]').remove();
			
			var make4quicksearch="";
			make4quicksearch+="<ul>";
			$.each(data, function(make){
				makehtml += '<option value="'+ make +'">'+ make +'</option>'
				if (document.URL.indexOf("quicksearch.asp")>-1){
					make4quicksearch+="<li><a rel='nofollow' href='#' class='quick' name='" + make + "' >" + make + "</a></li>"; //di na mureload quicksearch
				}else{
					make4quicksearch+="<li><a href='quicksearch.asp?item=" + make + "'>" + make + "</a></li>"; //para sa lain page na quicksearch
				}
			});
			make4quicksearch+="</ul>";
		
			$("#xmlgetmake").html(make4quicksearch);
			
				
			$('.quick').click(function(){
				$('select[name="make"]').val($(this).attr("name"));
				$('select[name="make"]').change();
				return false;
			});
			
			var domCount = 1;
			
			$('select[name="make"]').append(makehtml).change(function(){
				var modelhtml = '';
				var makeTemp = $(this).val();
				
				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);
			});

			 var stringURL=document.location.href;
			 if (stringURL.search(/item/i) >0){
				var splitHref=stringURL.split("=")
				//alert(unescape(splitHref[1]));
				document.getElementById("makefield").value=unescape(splitHref[1]);
				$('select[name="make"]').change();
			 }
	 
	 
			$('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>';
					*/

					tablehtml += '<tr id="indexid' + vehicleindex + '">';
					tablehtml += '<td>' + vehicleinfo['Year'] + '</td>';
					tablehtml += '<td>' + Math.ceil(parseInt(vehicleinfo['KMS'])) + '</td>';
					tablehtml += '<td>' + vehicleinfo['Color'] + '</td>';
					tablehtml += '<td>' + vehicleinfo['Trans'] + '</td>';
					tablehtml += '<td>' + vehicleinfo['Chassis'] + '</td>';
					tablehtml += '</tr>';
					
					$("#QtxtChassis").val(vehicleinfo['Chassis']);
					var currentId=$("#txtVehicleID").val();
					$("#Qidvehicle").val(currentId)

					
					//START UPDATE NEW QUICKSEARCH RESULT
					document.getElementById("rightcontent").style.display = "none";
					document.getElementById("maincontent").style.width = "701px";
					document.getElementById("vehiclequicksearchcontainer").style.width = "701px";
					if(document.all)
					{
						//for ie browser
						document.getElementById("vehiclequicksearchcontent").style.width = "701px";
						document.getElementById("vehiclequicksearchcontentLeft").style.width = "477px";
						document.getElementById("vehiclequicksearchcontentRight").style.width = "215px";						
					for (i=0; i<document.getElementsByTagName("div").length; i++)
					{
						if (document.getElementsByTagName("div")[i].className=="mmlabelh3")
						{
						document.getElementsByTagName("div")[i].style.width = "231px";
						}
					}
						document.getElementById("makefield").style.width = "227px";
						document.getElementById("modelfield").style.width = "227px";
						document.getElementById("vehiclequicksearchresultBody").style.width = "462px";			
					}
					else
					{
						//mozilla
						document.getElementById("vehiclequicksearchcontent").style.width = "699px";
						document.getElementById("vehiclequicksearchcontentLeft").style.width = "467px";
						document.getElementById("vehiclequicksearchcontentRight").style.width = "205px";	
						for (i=0; i<document.getElementsByTagName("div").length; i++)
						{
							if (document.getElementsByTagName("div")[i].className=="mmlabelh3")
						{
							document.getElementsByTagName("div")[i].style.width = "230px";
						}
					}			
						document.getElementById("makefield").style.width = "225px";
						document.getElementById("modelfield").style.width = "225px";			
						document.getElementById("vehiclequicksearchresultBody").style.width = "460px";			
					}
		
					var h2bg = document.getElementById("vehiclequicksearchcontainer");
					h2bg.getElementsByTagName("h2")[0].style.background="url(/images/autoterminalrevamp/headerRevampBG2.jpg)";
					document.getElementById("footerContainer").style.width = "701px";
					//right template visibility:none for the wide quicksearch.asp end by:jcheng

					var strMake, strModel
					strMake = document.getElementById('makefield').options[document.getElementById('makefield').selectedIndex].value; 
					strModel = document.getElementById('modelfield').options[document.getElementById('modelfield').selectedIndex].value; 

					if (strModel != 0){
						
						//
						document.getElementById('vehiclequicksearchcontent').style.backgroundImage="none";
						document.getElementById('vehiclequicksearchcontainer').style.background="#eff9f1";
						document.getElementById('footerContainer').style.backgroundImage="url(/images/autoterminalrevamp/contentRevampBG15.jpg)";
						//
					
						document.getElementById('vehiclequicksearchpageContent').innterHTML="Loading may take long for large number of vehicles. Please wait...";
						
							$("#quicksearchlist").css("display","block");
							$("#result_instruction").css("display","none");
							$("#vehiclecontrols").css("display","block");
							
							
							$("#quicksearch_footer_after").css("display","block");
							$("#quicksearch_footer_before").css("display","none");
							
					
					}else{
						alert('Please select a model!')
					}
							
					//END UPDATE NEW QUICKSEARCH RESULT
				});
				
				$('#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 + ' '  + '</h3>'
				carhtml += '<p style="float:left;"><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" /></p>';
				
				carhtml += '<ul style="float:left; padding-top:10px;">'
					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);
			
				$("#QtxtChassis").val(data[makeval][modelval][index]['Chassis']);
				var currentId=$("#txtVehicleID").val();
				$("#Qidvehicle").val(currentId);
			}
			
			$('.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');
}



