
$(document).ready(
	function() {
		//on load activate the scrollpane stuff
		$("div.vehicle-list-panel div.scroll-pane").jScrollPane(
		{
			scrollbarWidth : 16,
			height : 362,
			width : 686
		});
	}
);

$('div.vehicle-list-panel').ready(
	function() {
		if ($('div.message-panel').length > 0) {
			$(document).find('div.wholesale-panel').css("display", "none");
		}
});

//configure the stocklist scripts
$('div.stocklist-view-mode').ready(
	function() {
		var container = $(this);

		if (container.find('div.jScrollPaneContainer div.scroll-pane').children().length > 0) {
			var firstVehicle = container.find('div.jScrollPaneContainer div.scroll-pane').children()[0];
			firstVehicle.className += " selected";
			var toggleAnchor = $(firstVehicle).find('a.toggle');
			toggleAnchor.addClass('selected');
		}

		if ($(this).find('div.vehicle-item.selected').find('div.vehicle-item-header').find('div.price').children().length > 0) {
			$(this).find('div.vehicle-item.selected').find('div.vehicle-item-header').find('div.price').addClass('selected');
			$(this).find('div.vehicle-item.selected').find('div.vehicle-item-header').find('div.newPrice').addClass('selected');
		}

		//handle the detail toggle
		$(this).find('a.icon.toggle').click(
			function(event) {
				event.preventDefault();

				//handle the details toggles
				handleStocklistViewDetailsToggle(container, $(this));
			}
		);

		//handle the hover of reduced price results (price disappears and discount appears when hovered over)
		$("div.price").hover(
		//Handle the roll-over

		    function() {
		    	var newPrice = $(this).find("div.newPrice");
		    	if (newPrice.length > 0) {
		    		newPrice.show();
		    	}
		    },
		    function() {
		    	var newPrice = $(this).find("div.newPrice");
		    	if (newPrice.length > 0) {
		    		newPrice.hide();
		    	}
		    }

		);


		//handle the camera hover
		$(this).find('a.icon.camera.has-image').hover(
			function() {
				cameraHoverOver($(this))
				$(this).parent().addClass('hover');
			},
			function() {
				cameraHoverOut($(this))
				$(this).parent().removeClass('hover');
			}
		);

		//handle the hover image clicks to send the user to the vehicle view page
		$(this).find('a.icon.camera.has-image div.thumbnails img').click(function(event) {
			event.stopPropagation();
			event.preventDefault();
			window.location.href = $(this).parents('a.icon.camera.has-image').attr('href');
		});

		//bind up the distance hover mejigger!
		$('div.vehicle-item-header').each(
			function() {
				var header = $(this);
				var distanceHoverOn = header.find('div.location-hover');
				var centreDiv = header.find('div.centre-distance');
				centreDiv.css({ 'display': 'none' });
				if ($('input.postcode').length > 0) {
					distanceHoverOn.hover(
						function() {
							centreDiv.css(
								{
									'display': 'block'
								});
						},
						function() {
							centreDiv.css({ 'display': 'none' });
						}
					);
				}
			}
		);
	}
);

//function used to handle the hover out for the camera popup
function cameraHoverOut(cameraContainer) {
	cameraContainer.removeClass('selected');
}

//function used to handle the hover over for the camera popup
function cameraHoverOver(cameraContainer) {
	cameraContainer.addClass('selected');
}

//function used to handle the toggling of which vehicle-items are visible
function handleStocklistViewDetailsToggle(container,item) {
	//if this is already selected then do nothing
	if(item.hasClass('selected')) {
		return;
	}	
	else {
		//deselect any headers and icons that are selected
		container.find('div.vehicle-item.selected, div.vehicle-item a.icon.toggle.selected, div.price.selected, div.newPrice.selected').each(
			function() {
				$(this).removeClass('selected');		
			}
			);
	
		if(item.parent('div').find('div.price').children().length > 0){		
			item.parent('div').find('div.price').addClass('selected');
			item.parent('div').find('div.newPrice').addClass('selected');
		}
		//now select the vehicle we want
		item.addClass('selected');
		item.parent('div').parent('div').addClass('selected');
		return;
	}	
}

