$j(function(){
  $j("a#btnFormAddCV").click(function(){
    $j("#formAddCV").fadeIn();
    $j("a#btnFormAddCV").hide();
    if ( $j("div#selectCV").length > 0 ) {
      $j("input[@name='selectResume'][@type='radio']").each(function() {this.checked = false;});
      $j("div#selectCV").hide();
    }
    return false;
  })
  
  $j("#btnFormCancelCV").click(function(){
    $j("#formAddCV").hide();
    $j("a#btnFormAddCV").fadeIn();
    $j("input[@name='resumeName']").val("");
    $j("input[@type='file']").val("");
    if ( $j("div#selectCV").length > 0 ) {
      $j("div#selectCV").fadeIn();
    }
    return false;
  })
  
  $j("a.showPollResult").click(function(){
    $j("#poll-view-voting fieldset").hide();
    $j("#poll-view-result").fadeIn();
    return false;
  })

  $j("a.showPollVoting").click(function(){
    $j("#poll-view-result").hide();
	$j("#poll-view-voting fieldset").fadeIn();
    return false;
  })
  
});

function atome_gmapClick(map, lat, lon) {
  var m = Drupal.gmap.getMap(map);
  m.vars.latitude = lat;
  m.vars.longitude = lon;
  m.change('move',-1);
}

$j(document).ready(function() {
	$j(".swapValue").toggleVal("state-0");
  $j(".tooltip").tooltip({ 
    track: true,
    delay: 0, 
    top: -10, 
    showURL: false, 
    extraClass: "js-tooltip"
  });
  $j("#formRoute").googleMapRedirect();
	changeCommandePeriode();
});

$j(window).load(function() {
  showCommandePeriode();
});

$j.fn.toggleVal = function(focusClass) {
  return this.each(function() {
    $j(this).focus(function() {
			if($j(this).val() == this.defaultValue) { $j(this).val(""); $j(this).removeClass(focusClass); }
    }).blur(function() {
		  if($j(this).val() == "") { $j(this).val(this.defaultValue); $j(this).addClass(focusClass); }
    });
  });
}

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("small").fadeIn();
					});
					$j(selectDeparture).next("small").html(value);
				} else {
					$j("#departure").next("small").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("small").fadeIn();
					});
					$j(selectArrival).next("small").html(value);
				} else {
					$j("#arrival").next("small").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='submit']", 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;
		});
	});
}

var showCommandePeriode = function(){
	if ($j("#edit-contractType").children("[@selected]").text() == "CDI")
		$j("#commande_periode").css({ display: "none" });
	else
		$j("#commande_periode").css({ display: "block" });
}

var changeCommandePeriode = function(){
	$j("#edit-contractType").change(function(){
		if ($j(this).children("[@selected]").text() == "CDI")
			$j("#commande_periode").css({ display: "none" });
		else
			$j("#commande_periode").css({ display: "block" });
	});
}
