$j(function() {
	// javascript that runs for any options
	
	HNSITE.UI.Initializer();
	// Other tooltips
	$j("#vehicles").find('span.hnsite-ui-tooltip').live("mouseenter", HNSITE.UI.Tooltips.TipOver).live("mouseleave", HNSITE.UI.Tooltips.TipOut);
	
	// Quick links
	$j('#quick-links').bind('mouseover', function() {
		$j('#quick-links-box').slideDown("def").css('zIndex', 100);
		$j('#breadcrumbs').css('opacity', 0.4);
		$j('.vehicle').addClass('goBehind');
	});
	$j('#quick-links-box').bind('mouseleave',function() {
		$j('.vehicle').removeClass('goBehind');
	});
	$j('#sortbox > *:not(#quick-links,#quick-links-box),#hnbw-header,#vehicles,#footer').bind('mouseenter', function() {
		if ($j('#quick-links-box').css('display') != "none") { //IE opacity added height fix
			$j('#quick-links-box').slideUp("def").css('zIndex', 0);
			$j('#breadcrumbs').css('opacity', 1);
		}
	});
	
	// Keyword search shortcut
	$j('#stock').keydown(function(e) {
		if (e.keyCode == 13) {
			e.preventDefault();
			stocksearch($j(this).attr('rel'));
		}
	});
	
	if ($j('#breadcrumbs').length) {
		//set up breadcrumb scroller
		var firstDiv = $j('#breadcrumbs span.box:first');
		var bcWidth = $j('#breadcrumbs').outerWidth();
		var totalWidth = 0,excludeWidth = 0,numBoxen = 0,toExclude = 0;
		
		//get total width and number of elements in breadcrumbs
		$j.each($j('#breadcrumbs span.box'),function() {
			totalWidth += $j(this).outerWidth(true);
			numBoxen++;
		});
		
		if ( totalWidth > bcWidth ) {
			//find out how many breadcrumbs have to be scrolled so the last one would be showing
			while ( totalWidth > bcWidth ) {
				excludeWidth = firstDiv.outerWidth(true);
				toExclude++;
				totalWidth -= excludeWidth;
				firstDiv = firstDiv.next();
			}
			
			//exclude that many so you never are scrolling whitespace
			toExclude = numBoxen - (toExclude+1);
			
			$j('#breadcrumbNav').serialScroll({
				target:'#breadcrumbs',
				items:'span.box',
				prev:'#left',
				next:'#right',
				duration:200,
				exclude:toExclude
			});
		} else {
			//hide arrows
			$j('#breadcrumbNav #left,#breadcrumbNav #right').css('display','none');
			$j('#breadcrumbs').css('paddingLeft','0');	
		}
	}
	
	if ($j('#hnbw-header .customLinkbacks #inventoryHeader').length) {
		var headerWidth = $j('#hnbw-header .customLinkbacks #inventoryHeader').innerWidth();
		$j('#browseFullWidthContainer').wrap('<div></div>').parent().css({backgroundColor:'white',width:headerWidth,margin:'0 auto',padding:'20px 0'});
		$j('#hnbw-header').css('margin','0');
	}
});

function soryBy() {
	
	// purpose: go to loaded sortby url in option value
	var sortbybox = document.getElementById('sortby');
	var sortbyvalue = sortbybox.options[sortbybox.selectedIndex].value;
	redirect(sortbyvalue);
	
}

function compareVehicles(link){
	var element = 'input[name=compare]';
	var parent = "#vehicles";
	var compareUrl = $j('#bwUrl').html() + 'compare.asp?compare=';
	var results = HNSITE.UI.Checkboxes.Count(element, parent);
	if (results.checked > 1 && results.checked < 5) {
		location.href = compareUrl + HNSITE.UI.Checkboxes.Values(element, parent, function(cbThis) {
			return cbThis.checked;
		}).join('&compare=');
	} else {
		$j("#"+link).attr('title','Error: Choose two to four vehicles to compare.');
		HNSITE.UI.Tooltips.TipOver($j("#"+link).get(0));
		setTimeout(function() {
			HNSITE.UI.Tooltips.TipOut($j("#"+link).get(0));
		}, 2000);
	}
}

