var intervalVar;

var modelDrop = {
	'id' : 'bikeModelDrop',
	'dropLeft' : null,
	'hitAreaTopX' : null,
	'hitAreaTopY' : null,
	'hitAreaBottomX' : null,
	'hitAreaBottomY' : null
}

var familyDrop = {
	'id' : 'bikeFamilyDrop',
	'dropLeft' : null,
	'hitAreaTopX' : null,
	'hitAreaTopY' : null,
	'hitAreaBottomX' : null,
	'hitAreaBottomY' : null
}

var categoryDrop = {
	'id' : 'bikeCategoryDrop',
	'dropLeft' : null,
	'hitAreaTopX' : null,
	'hitAreaTopY' : null,
	'hitAreaBottomX' : null,
	'hitAreaBottomY' : null
}

var bikeDropDowns = [
     modelDrop,
     familyDrop,
     categoryDrop
];

$(document).ready(function() {
	// model drop down
	$(".bikeName").mouseover(function() { 
		modelDrop.dropLeft = getRealLeft(document.getElementById('bikeNameMarker'));
		showDropDown( modelDrop ); 
	}).mouseout(function() { 
		return; 
	});
	
	// category drop down
	$("p.categoryName").eq(0).mouseover(function() { 
		categoryDrop.dropLeft = getRealLeft(document.getElementById('categoryNameMarker'));
		showDropDown( categoryDrop ); 
	}).mouseout(function() { 
		return; 
	});
	
	// family drop down
	$("p.familyName").eq(0).mouseover(function() { 
		familyDrop.dropLeft = getRealLeft(document.getElementById('familyNameMarker'));
		showDropDown( familyDrop ); 
	}).mouseout(function() { 
		return; 
	});
	
	// enhance compare link - set new compare URL in subnav
	$("#subNavInner ul li.compare a").attr("href", $("p.compareBikes a").attr("href"));
	
	// update compare links if we came from the compare page
	if (document.referrer) {
		var checkMatch;
		checkMatch = document.referrer;
		if (checkMatch.indexOf('/bikes/compare/') > 0) {
			$("#subNavInner ul li.compare a").click(function() {
				history.go(-1);
				return false;
			});	
			$("#bikeHeader .right p.compareBikes a").click(function() {
				history.go(-1);
				return false;
			});
		}
	}
	
	/* Zoom Image */
	var settings = {
		bigImage_always_visible: false,
		drag_mode: false
	};
	
	if ( typeof luckyZoom == 'function') {
		zoom = new luckyZoom('sc2', 'bikeImage', 'bc2', 'bikeImageLarge', settings);
		zoom.initZoom();
	}
	
	// tabs
	$("div.hidden").hide();
	$("div.tabHeaders ul li a").click(function() { 
		$(this).parent().parent().children().removeClass('selected'); // remove currently selected
		$(this).parent().addClass('selected'); // make this selected
		var linkHash = this.hash; // find selected tab
		if (linkHash.length > 1) {
			linkHash = linkHash.substring(1,linkHash.length);
		}
		$("#"+linkHash).siblings().hide(); // hide siblings
		$("#"+linkHash).show(); // show selected tab
		$(this).blur();
		return false;
	});
	jQuery('a.reviewLink').click( function() {
		jQuery("div.tabHeaders ul li a.reviews").click();
		//return false;
	});
	
	// geometry
	if ($.cookie('MEASUREMENT_UNITS') != undefined) {
		document.getElementById('geometryTable').className = $.cookie('MEASUREMENT_UNITS');
		if ($.cookie('MEASUREMENT_UNITS') == 'imperial') {
			document.getElementById('measureImperial').checked = true;
		} else {
			document.getElementById('measureMetric').checked = true;
		}
	}
});

function hideDropDowns()
{
	for ( var i=0; i < bikeDropDowns.length; i++ )
	{
		jQuery('#'+bikeDropDowns[i].id).hide();
	}
}

function showDropDown( dropDown ) {
	hideDropDowns();
	var id = "#" + dropDown.id;
	$(id).show();
	getDropDownHitArea( dropDown ); 
	$(id).css("left", dropDown.dropLeft);
	$().mousemove(function(e){
		if (!stillShowDropDown(dropDown, e.pageX, e.pageY)) {
			$(id).hide();
		}
	});
}

function getDropDownHitArea( dropDown ) {
	if (document.getElementById(dropDown.id)) {
		// find position of hit area
		position = findPos(document.getElementById(dropDown.id));
		dropDown.hitAreaTopX = position[0];
		dropDown.hitAreaTopY = position[1] - 30;
		dropDown.hitAreaBottomX = dropDown.hitAreaTopX + $('#'+dropDown.id).width() + 50;
		dropDown.hitAreaBottomY = dropDown.hitAreaTopY + $('#'+dropDown.id).height() + 30;
	}
}

function stillShowDropDown( dropDown, mouseX, mouseY) {
	if ((mouseX < dropDown.hitAreaTopX) || (mouseY < dropDown.hitAreaTopY)) {
		return false;
	} else if ((mouseX > dropDown.hitAreaBottomX) || (mouseY > dropDown.hitAreaBottomY)) {
		return false;
	} else {
		return true;
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}


$(function() {
	var msrp = document.getElementById('msrp');
	if (msrp) {
		var disclaimerText = msrp.getAttribute('title');
		msrp.setAttribute('title', '');
		msrp.onmouseover = function(){
			var disclaimerP = document.createElement('p');
			disclaimerP.innerHTML = disclaimerText;
			disclaimerP.id = 'disclaimer';
			disclaimerP.onmouseout = function(){
				this.parentNode.removeChild(this);
			}
			document.getElementById('msrpContainer').appendChild(disclaimerP);
		};
		msrp.onmouseout = function(){
			var disclaimerP = document.getElementById('disclaimer');
			if (disclaimerP != undefined) {
				disclaimerP.parentNode.removeChild(disclaimerP);
			}
		};
	}
});

function setMeasurementSystem(systemString) {
	$.cookie('MEASUREMENT_UNITS', systemString, {expires: 365});
	document.getElementById('geometryTable').className = systemString;
}