$j(document).ready(function() {
	$j("#formRoute").googleMapRedirect();
});

$j(window).load(function() {
	tabsGoogleMap = $j(".menuAppelMed .tabs").tabs({
		show: function() { handleTabsHoverState(this); },
		fx: { opacity: 'toggle' }
	});
});

var handleTabsHoverState = function(elt){
	if(typeof(tabsGoogleMap) != "undefined"){
		current = tabsGoogleMap.data('selected.tabs');
		
		if( $j("strong", elt).size() > 0 ){
			$j("strong", elt).each(function(){
				currentNode = $j("a:eq(0)", this)[0];
				src = $j("img:eq(0)", this).attr("src");
				
				if(src.indexOf("_on") > 0) src = src.replace("_on.","_off.");
				
				$j("img:eq(0)", this).attr("src", src);
				
				$j(this).parent().prepend(currentNode);
				$j(this).remove();
			});
		}
		
		$j("li:eq(" + current + ") a", elt).wrap("<strong></strong>");
		src = $j("li:eq(" + current + ") img", elt).attr("src");
		if(src.indexOf("_off") > 0)	src = src.replace("_off.","_on.");
		$j("li:eq(" + current + ") img", elt).attr("src", src);
	}
}

var handleRoute = function(type){
	if(type == "departure"){
		selectDeparture = $j("#departure");
		
		$j("option", selectDeparture).each(function(){
			if( $j(this)[0].selected ){
				value = $j(this).attr("value");
				
				if(value != "other"){
					$j("#otherDeparture:visible").animate({ opacity: "toggle" }, function(){
						$j(selectDeparture).next("span").fadeIn();
					});
					$j(selectDeparture).next("span").children("span").html(value);
				} else {
					$j("#departure").next("span").animate({ opacity: "toggle" }, function(){
						$j("#otherDeparture").fadeIn();
					});
				}
				
				return false;
			}
		});
	}
	
	if(type == "arrival"){
		selectArrival = $j("#arrival");
		
		$j("option", selectArrival).each(function(){
			if( $j(this)[0].selected ){
				value = $j(this).attr("value");
				
				if(value != "other"){
					$j("#otherArrival:visible").animate({ opacity: "toggle" }, function(){
						$j(selectArrival).next("span").fadeIn();
					});
					$j(selectArrival).next("span").children("span").html(value);
				} else {
					$j("#arrival").next("span").animate({ opacity: "toggle" }, function(){
						$j("#otherArrival").fadeIn();
					});
				}

				return false;
			}
		});
	}
}

$j.fn.googleMapRedirect = function(){
	if ( $j(this).size() < 1 ) return false;
	
	return this.each(function(){
		$j("input[type='image']", this).click(function(){
			departure = $j("#departure").val();
			
			if(departure == "other")
				departure = $j("#departure_other").val();
			
			arrival = $j("#arrival").val();
			
			if(arrival == "other")
				arrival = $j("#arrival_other").val();
			
			window.open("http://maps.google.com/maps?f=d&hl=fr&geocode=&saddr=" + escape(departure) + "&daddr=" + escape(arrival));
			
			return false;
		});
	});
}
