$(function() {
	// Identify an iOS device
	var browsertype = null;
	if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
		$('body').attr('browsertype','ios');
		browsertype = 'ios';
	}
	
	if ($("span.detailsOverlay").length == 0) return false;
	
	// reset all the open popups on unload
	$(window).unload(runOverlaySetup);
	
	// Run the overlay setup
	runOverlaySetup();
	
	// Function: Position the Vehicle Detail Blocks
	function runOverlaySetup() {
		
		$("span.detailsOverlay").each(function() {
			detailsClose($(this), false);
			$(this).css("visibility","visible");
			var parentli = $(this).parents('li');
			parentli.add($('*', parentli)).css('cursor','pointer');
			parentli = null;
		});
	}
	
	// Function: Setup the close javascript
	function setupClose(etarget) {
	
		var tmpThis = etarget;
		clearInterval(etarget.timer);
		etarget.timer = setTimeout(function() {
			var clearQueue = true;
			var gotoEnd= false;
			$("span.detailsOverlay", $(tmpThis)).stop(clearQueue , gotoEnd);
			detailsClose($("span.detailsOverlay", $(tmpThis)), true);
		},200);
	}
	
	// Function: Close the Vehicle details
	function detailsClose(thisOverlay, animate) {
					
		var lipos = $(".detailsBlock", thisOverlay).position().top;
		// var liheight = $(".vehicleDetails li:eq(1)", thisOverlay).outerHeight() - 1;
		
		thisOverlay.parents('li.vehicle').removeClass('overlayopen');
		
		if (animate === false) {
			thisOverlay.css('bottom','-' + (thisOverlay.outerHeight() - lipos) + 'px');
		} else {
			thisOverlay.animate({'bottom':'-' + (thisOverlay.outerHeight() - lipos) + 'px'}, 100);
		}				
	}
	
	// Function: Setup the open javascript
	function setupOpen(etarget) {
		
		var tmpThis = etarget;
		clearInterval(etarget.timer);
		etarget.timer = setTimeout(function() {
			var clearQueue = true;
			var gotoEnd= false;
			$("span.detailsOverlay", $(tmpThis)).stop(clearQueue , gotoEnd);
			detailsOpen($("span.detailsOverlay", $(tmpThis)), true);
		},600);
	}
	
	// Function: Open the Vehicle Details
	function detailsOpen(thisOverlay, animate) {
		
		// Close all open overlays
		$("li.overlayopen").each(function() {
			
			detailsClose($('span.detailsOverlay', $(this)), true);
		});
		
		// Mark this overlay as open
		thisOverlay.parents('li.vehicle').addClass('overlayopen');
		
		// Open the overlay
		if (animate === false) {
			thisOverlay.css('bottom','0px');
		} else {
			thisOverlay.animate({'bottom':'0px'}, 125);
		}
	}
	
	// Event: Mark/unmark scroll event
	$(window).scroll(function() {
	
	});
	
	// Event: Open/close vehicle details on hover
	$(".vehicle").hover(function() {
			
			var browsertype = $('body').attr('browsertype');
			
			if (browsertype == 'ios') return false;
			
			setupOpen(this);
		},
		function() {
			
			var browsertype = $('body').attr('browsertype');
			
			if (browsertype == 'ios') return false;
		
			setupClose(this);
		}
	);
	
	// Event: Click event on vehicles
	$(".vehicle").click(function(e) {
	
		var thisli = $(this);
		
		var browsertype = $('body').attr('browsertype');
			
		if (browsertype == 'ios' && !thisli.hasClass('overlayopen')) {
			
			// Open the overlay (if iOS)
			setupOpen(this);
			
		} else if (browsertype == 'ios' && thisli.hasClass('overlayopen')) {
			
			// Close the overlay (if iOS)
			setupClose(this);
			
		} else if (browsertype != 'ios') {
			
			e.preventDefault();
			
			// Go the the first link in the element
			var fistAnchor = $("a:eq(0)", thisli);
			window.location = fistAnchor.attr('href');
		}
	});
});
