/**
 * used vehicles js rotating script
 * @author	
 * @copyright 	Copyright (c) 2009+ Pictographics Ltd
 * @since		Feb 19, 2009
 */

var cars = new Array() ;
var this_car = 0 ;
var timeout ;

function fetchNewVehicles() {
  // low level ajax request to get car_data
  $.ajax( {
    type:       "GET",              /* get request */
    url:        "/inc/vehicles_info.php",      /* url to go to */
    dataType:   "json",             /* expecting json back */
    cache:      false,              /* don't cache results */
    timeout:    2000,               /* two second timeout */
    success:                        /* success function */
      function(car_data) {
        for(var x=0; x < car_data.length; x++) {
          // initialize array element to hold an array
          cars[x] = new Array() ;
          // load json object props into array object          
          cars[x]['name'] = car_data[x]['name'] ;
          cars[x]['href'] = car_data[x]['href'] ;
          cars[x]['pt_1'] = car_data[x]['point1'] ;
          cars[x]['pt_2'] = car_data[x]['point2'] ;
          cars[x]['pt_3'] = car_data[x]['point3'] ;
          cars[x]['file'] = car_data[x]['filename'] ;
        }  
        displayVehicles() ;
      }, 
    error:                          /* error function */
      function(xhr, status_code, error) {
        alert("Unable to load cars => error:" + error + " ... status: " + status_code) ;
      }
  }); 
}

function displayVehicles() {
  clearTimeout(timeout) ;
    // make sure vehicle id within range
  if (this_car >= cars.length) {
    this_car = 0 ;
  }
  
  //console.log("Vehicle ID: " + this_car) ;

  // change image
  $("#ad-img").attr("src", "/uploads/images/new_car/thumbs/" + cars[this_car]['file']) ;
  //console.log("ad-img has an src value of: " + $("#ad-img").attr("src")) ;
  // change title
  $("#ad_title").html(cars[this_car]['name']) ;
  var points = "<li>" + cars[this_car]['pt_1'] + "</li>"
             + "<li>" + cars[this_car]['pt_2'] + "</li>"
             + "<li>" + cars[this_car]['pt_3'] + "</li>" ;
                     
  $("#ad_detail").html(points) ;
  $("#ad_link").attr("href", cars[this_car]['href']) ;
 
   // increment car
   this_car++ ;
  // setInterval to run this again
  timeout = setTimeout("displayVehicles()", 9000) ;
}