function setupAutocomplete() {
  $('#autocomplete-search').autocomplete('/search', {
    dataType: "json",
    parse: function(data) {
			var rows = new Array();
			for (var i=0; i < data.length; i++) {
			 rows[i] = { data:data[i], value:{ title: data[i].title, image: data[i].image, id: data[i].id }, result: data[i].title }
			}
			return rows;
		},  
		formatItem: function(data, i, max, value, term) {
		  if (value.image == '') {
		    var html = value.title
      } else {
        var html = '<a href="/vehicles/' + value.id + '"><img src="' + value.image + '" style="vertical-align: middle;" /></a>';
        html += '<div class="ac_title"><a href="/vehicles/' + value.id + '">' + value.title + '</a></div>';
        html += '<div class="spacer"></div>';
        html += '<a href="/vehicles/' + value.id + '" class="big"></a>';
      }
		  return html;
    }
    
  });
}  

function selectTrimMenu() {
  var slug = window.location.pathname;
  $("#trims li a").each( function (){
    var current = $(this).attr("href");
	  if (current == slug) {
	    $(this).parent().attr("class", "selected");
	    $(this).parent().children(".right").attr("src", "/images/listing_model_dkbutton_R.jpg");
      $(this).parent().children(".left").attr("src", "/images/listing_model_dkbutton_L.jpg");
	  }
  });
};


$(window).load( function() {
  $("#trims .selected").children(".right").attr("src", "/images/listing_model_dkbutton_R.jpg");
  $("#trims .selected").children(".left").attr("src", "/images/listing_model_dkbutton_L.jpg");
  
  $('#trims li').mouseover(function(){
    $(this).attr("class", "selected");
    $(this).children(".right").attr("src", "/images/listing_model_dkbutton_R.jpg");
    $(this).children(".left").attr("src", "/images/listing_model_dkbutton_L.jpg");
    selectTrimMenu();
  });

  $('#trims li').mouseout(function(){
    $(this).attr("class", "");
    $(this).children(".right").attr("src", "/images/listing_model_ltbutton_R.jpg");
    $(this).children(".left").attr("src", "/images/listing_model_ltbutton_L.jpg");
    selectTrimMenu();
  });
});

jQuery(function($) {
  Cufon.replace('h1');
  Cufon.replace('h2');
  Cufon.replace('#listing .model');
  //Cufon.replace('#top-nav a', { fontFamily: 'Warnock Pro', fontWeight:"bold" });

  $("div.overout").mouseover(function(){
        $("p:first",this).text("mouse over");
        $("p:last",this).text(++i);
      }).mouseout(function(){
        $("p:first",this).text("mouse out");
      });
  

  $('#slides') 
  .cycle({ 
      fx:     'fade', 
      speed:  'slow', 
      timeout: 0, 
      pager:  '.items',
      prev: '#gallery .navigation .prev',
      next: '#gallery .navigation .next' 
  });
  setupAutocomplete();
});
