function vehicle_carousel_initCallback(carousel) {	

	$('#vehicle_list-next').click(function() {
        carousel.prev(); 
        return false;
    });
    
    $('#vehicle_list-prev').click(function() {
    	
        carousel.next();
        return false;
    });
    
}


$(function(){
	
	$('div.date_select_container select').each(function(){
		$(this).change(function(){
			var objCont = $(this).parent();
			
			var date = $('select.date_select_year',objCont).val()+'-'+$('select.date_select_month',objCont).val()+'-'+$('select.date_select_day',objCont).val();
			$('input.date_select_hidden',objCont).val(date);
		});
	});
	
	var c = -500;
	var w = $(window).width();
	var wSub = -1800;
	var newOffset = parseInt(wSub+w);
	var totalOffset = (parseInt(c + newOffset)/2);
	$('#vehicle_list_wrap').css('marginLeft', totalOffset+'px');
	//alert(w);
	if ( $('#vehicle_list').size() == 1 ) {
		$('#vehicle_list').jcarousel({
			wrap: 'circular',
			initCallback: vehicle_carousel_initCallback,
			// This tells jCarousel NOT to autobuild prev/next buttons
	        buttonNextHTML: null,
	        buttonPrevHTML: null,
	        scroll: 1,
	        offset: 1

		});
		
		
	
	}

	$('#dash_all_cars_button img').hover(
		function(){
			$(this).attr('src','/images/dash/all-cars-button-over.png');
		}, 
		function(){			
			$(this).attr('src','/images/dash/all-cars-button-off.png');
		}
	);
	
	

	
	
	PositionContent();
	$(window).resize(function() {
		PositionContent();
	});
		
	$('#nav_ourcars').mouseover(function(){
		
		$('#nav_car_stack').show();
		
	});
	
	$('#nav_car_stack').hover(
		function () {
			//$(this).append($("<span> ***</span>"));
		}, 
		function () {
			$('#nav_car_stack').hide();
     	}
    );
    
    $('#nav_car_stack li').each(function(){
    	
    	var car = $(this).attr('id').replace('nav_','');
    	var label = $('a',this).text();
    	
    	$('a',this).html('<img src="/images/car_stack/'+car+'.png" alt="'+label+'" />');
    	
    });
    
    $('#main_nav_left li, #main_nav_right li, #main_nav_center li, #com_nav li').each(function(){
    	
    	var navpage = $(this).attr('id').replace('nav_','');
    	var label = $('a',this).text();
    	
    	$('a',this).html('<img src="/images/nav/'+navpage+'.png" alt="'+label+'" />');
    	
    });
    
    $('#main_nav_left, #main_nav_right, #main_nav_center, #com_nav').show();
    
    if($('#gallery_container').length){
    	$('#gallery_container').cycle({ 
    		fx:     'fade', 
		    speed:  'fast', 
		    timeout: 0, 
		    next:   '#gallery_next', 
		    prev:   '#gallery_prev' 
		}).css('visibility','visible');
    }
    
    $('a.month_select_link').click(function(){
    	
    	var month_num = $(this).attr('id').replace('month_select_','');
    	
    	$('div.week_selectors').hide();
    	$('#weeks_'+month_num).show();
    	
    	
    	
    	return false;
    });
    
    $('#driver_check').click(function(){
		toggleDriverDetails();
	});

	var xpos;
	var ypos;
	
	$().mousemove(function(e){
     	xpos = e.pageX; 
     	ypos = e.pageY;
    }); 


	// added by stu. 2010-03-17. IE 7 fix, menu wasn't staying open when hovered.
	$('#nav_car_stack').css({'background':'url(/images/1sq.gif) repeat left top', 'width':'90px'});

 	setInterval(function(e){
		
   		var p = $('#nav_car_stack').offset();
   		var width = $('#nav_car_stack').width();
   		/*
   		if($.browser.mozilla){
			console.clear();
			console.log('L:'+p.left+' R:'+(p.left+width));
			console.log('X:'+xpos+' Y:'+ypos);
		}
   		*/
/*
   		if(xpos < p.left || xpos > (p.left+width) && $('#nav_car_stack').css('display')=='block'){
			$('#nav_car_stack').hide();
		}
*/
		
	},1000);

	
	$('td.route').hover(function(){
		
		$('img.map_route').hide();
		
		var id = $(this).attr('id').replace('routecell_','');
		$('#route_'+id).show();
		
	},function(){
		$('img.map_route').show();
	});
	
	$('#split_logo img').hover(
		function(){
			$(this).attr('src','/images/vanilla-splits-b.png');
		},
		function(){
			$(this).attr('src','/images/vanilla-splits-a.png');
		}
	);
	
});

function PositionContent(){
	
	var content_height = $('#content_wrap').height();
	var margin_top = (content_height / 2) * -1;
	var window_height = $(window).height();
	var available_height = window_height - 220;
	var p = $('#content_wrap').offset();
	
	/*
	if($.browser.mozilla){
		console.clear();
		console.log('H:'+content_height+' T:'+p.top+' L:'+p.left);
		console.log('WH:'+window_height+' AH:'+available_height);
	}
	*/
	
	if(content_height > available_height){
		$('#content_wrap').css('top','50px');
		$('#content_wrap').css('margin-top','0px');
		$('#content_wrap').css('padding-bottom','170px');
	}else{
		$('#content_wrap').css('top','40%');
		$('#content_wrap').css('margin-top',margin_top+'px');
		$('#content_wrap').css('padding-bottom','0px');
	}
}


function toggleDriverDetails(){
	$('#driver_details select, #driver_details input').each(function(){
		if($('#driver_check').attr('checked')!=true){
			$(this).attr('disabled','disabled');
		}else{
			$(this).attr('disabled','');
		}
	});
}

