
	var delimiter = '|';
	var maxItems = 4;
	var compareVINs = Array();
	var wishlistVINs = Array();

	// Onload...
	$(document).ready( function(){

		initCompare();

		initWishlist();

		loadCarfax();

		$('img.listing-img').lazyload({
			effect: 'fadeIn'
		});

	});

//	######################################################################
//	# Replace broken images with "no image" placeholder
//	######################################################################
	function ImgError(source) {
		source.src = '/PTMSCMS/shell/css/images/no-image.png';
		source.onerror = '';
		return true;
	}

//	######################################################################
//	# Compare button clicked
//	######################################################################
	function compareButtonClicked() {

		if( compareVINs.length >= 2 ) {

			var VINs = compareVINs.join(',');

			$.cookie('compareVINs', null, {path:'/'});

			window.location.href = baseNVPath + '/vin/' + VINs;

		} else {

			alert("Please select two to four vehicles to compare.");

		}

	}

//	######################################################################
//	# Compare checkbox clicked
//	######################################################################
	function compareCheckboxClicked(chkElement) {

		parseCompareCookie();

		if( compareVINExists(chkElement.value) ) {

			removeCompareVIN(chkElement.value);

			chkElement.checked = false;

		} else {

			if( compareVINs.length < maxItems ) {

				addCompareVIN(chkElement.value);

				chkElement.checked = true;

			} else {

				chkElement.checked = false;

				alert("Please select no more than four items.");

			}

		}

		setCompareCookie();

	}

//	######################################################################
//	# Initialize compare
//	######################################################################
	function initCompare() {

		parseCompareCookie();

		syncCompareCheckboxes();

	}

//	######################################################################
//	# Sync page checkboxes to cookie
//	######################################################################
	function syncCompareCheckboxes() {

		var items = document.getElementsByTagName('input');

		for( x = 0; x < items.length; x++ ) {

			if( items[x].getAttribute('type') == 'checkbox' && items[x].getAttribute('name') == 'compare' ) {

				items[x].checked = compareVINExists(items[x].value) ? true : false;

			}

		}

	}

//	######################################################################
//	# Test to see if VIN exists in array
//	######################################################################
	function compareVINExists(value) {

		return $.inArray(value, compareVINs) == -1 ? false : true;

	}

//	######################################################################
//	# Add an item to the compare cookie
//	######################################################################
	function addCompareVIN(value) {

		if( ! compareVINExists(value) ) compareVINs.push(value);

	}

//	######################################################################
//	# Remove an item from the compare cookie
//	######################################################################
	function removeCompareVIN(value) {

		var result = Array();

		for( x = 0; x < compareVINs.length; x++ ) {

			vin = compareVINs[x];

			if( vin != value ) result.push(vin);

		}

		compareVINs = result;

	}

//	######################################################################
//	# Set cookie
//	######################################################################
	function setCompareCookie() {

		var list = compareVINs.join(delimiter);

		$.cookie('compareVINs', list, {path:'/'});

	}

//	######################################################################
//	# Load selected VINs into array
//	######################################################################
	function parseCompareCookie() {

		if( raw = $.cookie('compareVINs') ) compareVINs = raw.split(delimiter);

	}

//	######################################################################
//	# Initialize wishlist
//	######################################################################
	function initWishlist() {

		parseWishlistCookie();

	}

//	######################################################################
//	# Load wishlist VINs into array
//	######################################################################
	function parseWishlistCookie() {

		if( raw = $.cookie('wishlistVINs') ) wishlistVINs = raw.split(delimiter);

	}

//	######################################################################
//	# Test to see if VIN exists in wishlist array
//	######################################################################
	function wishlistVINExists(value) {

		return $.inArray(value, wishlistVINs) == -1 ? false : true;

	}

//	######################################################################
//	# Add to wish list
//	######################################################################
	function addToWishList(vin) {

		if( ! wishlistVINExists(vin) ) wishlistVINs.push(vin);

		setWishlistCookie();

		alert('The vehicle has been added to your wish list.');

	}

//	######################################################################
//	# Remove from wish list
//	######################################################################
	function removeFromWishList(value) {

		var result = Array();

		for( x = 0; x < wishlistVINs.length; x++ ) {

			vin = wishlistVINs[x];

			if( vin != value ) result.push(vin);

		}

		wishlistVINs = result;

		setWishlistCookie();

	}

//	######################################################################
//	# Set wishlist cookie
//	######################################################################
	function setWishlistCookie() {

		var list = wishlistVINs.join(delimiter);

		$.cookie('wishlistVINs', list, {path:'/', expires: 90});

	}

//	######################################################################
//	# Load Carfax info
//	######################################################################
	function loadCarfax() {

		$('.__carfax').each( function(index){

			var div = $(this);

			$.ajax({
				timeout: 20000,
				url: '/inventory/_carfax.php?vin=' + div.attr('id'),
				dataType: 'json',
				success: function(data){

					if( data.hasReport ) {

						if( data.oneOwner ) {

							div.attr('src', '/PTMSCMS/shell/css/images/badge_carfax-1-owner.gif');

						} else {

							div.attr('src', '/PTMSCMS/shell/css/images/badge_carfax.gif');

						}

					} else {

						div.attr('src', '/PTMSCMS/shell/css/images/1x1.gif');

					}

				},
				error: function(){

					div.attr('src', '/PTMSCMS/shell/css/images/1x1.gif');

				}
			});

		});

	}

