$(document).ready(function() {
	// latest offers form handling (in mini search row)
	$("#latestOffersForm").submit(function(){
		if($("#new_model", "#latestOffersForm").val() == ""){
			return false;
		}
		$("#latestOffersForm")[0].action = $("#new_model", "#latestOffersForm").val();
	});	
	
	// value my vehicle form handling 
	$("#value_vehicle").submit(function(){
		if($("#value_vehicle_reg").val() == ""){
			return false;
		}
	});		
	
	// all the call to action boxes.
	$('.call-to-action').each(function(intIndex){
		$(this).bind('click', function(){
			var loc = $(this).find('div.call-to-action-url').html();
			var is_new_window = $(this).find('div.new-browser-window');
			loc= loc.replace(/\&amp;/g,'&');
			if(is_new_window.length == "1"){
				window.open(loc); // new window
			} else {
				window.location.href = loc; // same window
			}
			return false;			
		})
	});		
	
	// new window
	$('a.new-window').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	
	
	

	// handle naviagation mouseover/mouseout
	$('ul.dd-nav > li')
		.bind('mouseover',function(){
			$('ul.dd-nav >li').removeClass('selected'); // remove 'selected' from all
			$(this).addClass('selected'); // add 'selected' to this one
		})
		.bind('mouseout',function(){
			$(this).removeClass('selected'); // remove 'selected' from this one
			$('ul.dd-nav').find('.current').addClass('selected');	// add back 'selected' to the originally selected li
		});

	// wire up all the buttons to the dialog boxes
	$('.open2bModal').click(function(){
		openModal($(this).metadata().open_id);
		Cufon.replace('.fnt-l', { fontFamily:'ford-light'});
		Cufon.replace('.fnt-b', { fontFamily:'ford-bold'});
		return false;
	});
	// and the close buttons
	$('.popover-close-btn').click(function(){
		$.modal.close();
		return false;
	});	
});

// modal windows
function openModal(thediv) {
	$("#"+thediv).modal({
		onOpen : function (dialog) {
			dialog.overlay.fadeIn('fast', function () {
				dialog.data.hide();
				dialog.container.fadeIn('fast');
				dialog.data.slideDown('fast');
			});
		}
		,
		onClose : function (dialog) {
			dialog.data.fadeOut('fast');
			dialog.container.slideUp('fast');
			dialog.overlay.fadeOut('fast', function () {
				$.modal.close(); // must call this!
			});
		}
		, overlayClose : true
	});
};
$('.navsubmenu').hide();
$(window).load(function(){
	if($('.call-us-menu').length){
		$('.call-us-menu').rb_menu({triggerEvent: 'mouseover', hideOnLoad: true, loadHideDelay: 0, autoHide: true});
	}
});

