jQuery(function() {
// third
	var gallery = new indexGallery(fused,"preowned");
	jQuery(".home-boxes .preowned .nav-next").click(function() {
		gallery.next();
		return false;
	}).css("")
	jQuery(".home-boxes .preowned .nav-prev").click(function() {
		gallery.prev();
		return false;
	})	
	gallery.bind();

var gallery2 = new indexGallery(fnewford,"preowned2");

	jQuery(".home-boxes .preowned2 .nav-next").click(function() {

		gallery2.next();
		return false;
	}).css("")
	jQuery(".home-boxes .preowned2 .nav-prev").click(function() {
		gallery2.prev();
		return false;
	})	
	gallery2.bind();
	
var gallery3 = new indexGallery(fnewchevy,"preowned3");

	jQuery(".home-boxes .preowned3 .nav-next").click(function() {

		gallery3.next();
		return false;
	}).css("")
	jQuery(".home-boxes .preowned3 .nav-prev").click(function() {
		gallery3.prev();
		return false;
	})	
	gallery3.bind();
	
})		
/*
	// second
	var secondGallery = new indexGallery(fnewford,"ford");
	jQuery(".fnewford .nav-next").click(function() {
		secondGallery.next();
		return false;
	}).css("")
	jQuery(".fnewford .nav-prev").click(function() {
		secondGallery.prev();
		return false;
	})	
	secondGallery.bind();
*/

indexGallery = function(imagesArray,blockId)
{
	return {
		imagesArray: imagesArray,
		blockId: blockId,
		current: 0,
		count: 0,
		bind: function()
		{
			this.count = this.imagesArray.length;
			
			
		},
		show: function(ind)
		{
			this.current = ind;
			
			jQuery(".home-boxes ."+blockId+" .imageContainer .loader").show().css('z-index',3);
			imgEl = jQuery(".home-boxes ."+blockId+" .image");
			imgEl.fadeOut();
			img = new Image();
			img.onload = function() {
				imgEl.css("width",imgEl.width()).css("height",imgEl.height()).html( img ).fadeIn();
				jQuery(".home-boxes ."+blockId+" .name").html( imagesArray[ind]["name_link"]);
				jQuery(".home-boxes ."+blockId+" .price").html( imagesArray[ind]["price"]);
				jQuery(".home-boxes ."+blockId+" .imageContainer .loader").hide();
			}
			img.src = imagesArray[ind]["image"];
			
		},
		next: function()
		{
			if(this.current==this.count-1) this.current = 0;
			else this.current++;
			
			this.show(this.current);
		},
		prev: function()
		{
			if(this.current==0) this.current = this.count -1;
			else this.current--;
			
			this.show(this.current);
		}
	}
}