// InfoBox Bike Rollover Selector Widget

var bikeSelectTimer; //Used on the inline onmouseover/onmouseout events that call these functions.

function selectBike(bikeId) {
	hideSelectableBikes();
	setCurrentSelectedButton(bikeId);
	var bike = document.getElementById("select"+bikeId);
	bike.style.display = 'block';
	var infoBox = document.getElementById('infoBox');
	var shadow = document.getElementById('infoBoxShadow');
	shadow.style.height = infoBox.offsetHeight + "px";
}

function getSelectableBikes() {
	var selectDiv = document.getElementById('selectDiv');
	var divs = document.getElementsByTagName('div');
	var bikes = new Array();
	for (var i=0;i<divs.length;i++) {
		if (divs[i].className == 'bikeInfo') {
			bikes.push(divs[i]);
		}
	}
	return bikes;
}

function hideSelectableBikes() {
	var bikes = getSelectableBikes();
	for (var i=0;i<bikes.length;i++) {
		bikes[i].style.display = 'none';
	}
}

function setCurrentSelectedButton(buttonId) {
	uncurrentifyBikeSelectButtons();
	jQuery( '#selectBtn' + buttonId ).addClass('current');
}

function uncurrentifyBikeSelectButtons() {
	jQuery( '.selectBtn' ).removeClass('current');
}

function activateChildSelectBtn( element ) {
	//jQuery( element ).find( 'a' ).mouseover();
	jQuery(element).addClass('current');
}

function deactivateChildSelectBtn( element ) {
	//jQuery( element ).find( 'a' ).mouseout();
	jQuery(element).removeClass('current');
}

function followChildSelectBtnLink( element ) {
	location = jQuery( element ).find( 'a' ).attr( 'href' );
	//jQuery( element ).find( '.selectBtn a' ).click();
}

function setCurrentBikeFamily(infoURL, e, country_id, language_id, model_year) {
	clearCurrentBikeFamily();
	e.parentNode.className = 'current';
	
	document.getElementById('infoBoxHeadline').innerHTML = e.innerHTML;
	document.getElementById('infoBoxContent').innerHTML = "<p style='text-align: center; margin: 0; padding: 0 50px 0 0;'><br /><br />Loading...</p>";
							
	showInfoBox('select', 550, 'relative', e);
		
	document.getElementById('infoBoxOverlay').onclick = function() {
		closeInfoBox();
		clearCurrentBikeFamily();
	}
	
	$('#infoBoxContent').load('/'+country_id+'/'+language_id+'/bikes/get_family_info/'+infoURL,null, function() {
		document.getElementById('infoBoxContent').style.height = "auto";
		document.getElementById('infoBoxContent').style.background = '#fff'
		setCurrentPage('select');
		selectBike('BikeFamilyMain');
	});
	var infoURLString = infoURL.toString();
	location.hash = '#'+infoURLString.substring(0, infoURLString.indexOf('/'));
	
	return false;
}

function clearCurrentBikeFamily() {
	var bikeFamilies = document.getElementById('bikeFamilies').getElementsByTagName('li');
	for (var i=0;i<bikeFamilies.length;i++) {
		bikeFamilies[i].className = '';
	}
}

function setCurrentBikeFamilyViaHash() {
	var url = location.toString();
	if ( typeof window.prevBikeURL == 'undefined' || window.prevBikeURL != url )
	{
		if ( url.lastIndexOf('#') != -1 && location.hash.length > 1 )
		{
			var family = url.substr( url.lastIndexOf('#') + 1 );
			var btn = jQuery('#bikeFamilies li a.'+family);
			jQuery(btn).click();
		} else {
			jQuery('#infoBoxOverlay').click();
		}
		window.prevBikeURL = url;
	}
}

function setDefaultLocation() {
	location.hash = '#';
}

// When document is ready, check the URL for current family. If found, activate that button.
jQuery(
	function() {
		setCurrentBikeFamilyViaHash();
		setInterval( setCurrentBikeFamilyViaHash, 50 )
	}
);