$(document).ready(function() {
  //imageview gallery
	$(function() {
		$('.lightbox').lightbox({
					allSet: true,
					fileLoadingImage: 'http://www.randles.co.uk/src/loading.gif',
					fileBottomNavCloseImage: 'http://www.randles.co.uk/src/closelabel.gif',
			    fitToScreen: true,
			    scaleImages: true,
		    });
	});

	//keywords
	t='Keywords e.g. Blue';
  if ($('#keywords').val()=='') { $('#keywords').val(t) }
  $('#keywords').focus(function() {
    if (this.value==t) { this.value=''; }
  });
  $('#keywords').blur(function() {
    if (this.value=='') { this.value=t; }
  });
  $('.submitbutton').click(function() {
    if ($('#keywords').val()==t) { $('#keywords').val(''); }
  });
  //end keywords

	$('form#qSearch select#make').change(function() {
		getsearchdata($(this).attr('id'));
  });
	$('form#qSearch select#model').change(function() {
		getsearchdata($(this).attr('id'));
  });
  //getsearchdata(''); //moved this to PHP instead
});

function getsearchdata(id) {
	//load data from ajax
	var data = {make:$('#make').val(),
							min_pr:$('#min_pr').val(),
							max_mileage:$('#max_mileage').val(),
							//keywords:$('#keywords').val(),
							model:$('#model').val(),
							max_pr:$('#max_pr').val(),
							max_age:$('#max_age').val(),
							//sort:$('#sort').val(),
							searched_channel:$('#searched_channel').val()};
	$.getJSON('usedlookup.php',data,function(data) {
   		$.each(data, function(field, arrays){
				if (id != field) { //load if it's not model
				  var options = '';
				  var selected='';
				  //console.log(field);
				  for (i in arrays) {
						//console.log(arrays[i]);
						selected='';
						if (data.session[field] == arrays[i][0]) { selected=' selected=selected'; }
						options += '<option value="' + arrays[i][1] + '"'+selected+'>' + arrays[i][0] + '</option>';
			 		}
			 		$('#' + field).html(options);
		 		}//field
			});
	  });
}
