d3.select("li#evento").style("background-color","#cedeef");
var li = d3.select("article.label","ul").selectAll("li");
var img_path = "/home/cal/img/";
var json_path = "/admin/json/";
/* menu hide and show edit window*/
li.on("click",function(){
  li.style("background-color","#e6eeff");
  d3.select(this).style("background-color","#cedeef");
  d3.selectAll("div.ehide").style("display","none");
  d3.select( "div#"+d3.select(this).attr("value") ).style("display","block");
});

/*preview event image*/
function updateImage(){
  var iname=d3.select("select#eimg").node().value;
  if(iname != 0){
    d3.select("div#esimg").style("background-image","url("+img_path+iname+")");}
  else{ d3.select("div#esimg").style("background-image","none"); }
}
d3.select("select#eimg").on("change",updateImage);

/*preload form contents for event*/
d3.select("select#eprev").on("change",function(){
  var eprev_id=d3.select("select#eprev").node().value;
  d3.json( json_path+"event/"+eprev_id ,function(d){ 
    if(d !=undefined) {if(d.cid != 0){
      dropdownUpdate("ecourse",d.cid);
      dropdownUpdate("eplace",d.pid);
      d3.select("input#dini").attr("value",d.dini);
      dropdownUpdate("mini",d.mini);
      dropdownUpdate("yini",d.yini);
      d3.select("input#dend").attr("value",d.dend);
      dropdownUpdate("mend",d.mend);
      dropdownUpdate("yend",d.yend);
      d3.select("input#cost").attr("value",d.cost);
      d3.select("input#promo").attr("value",d.promo);
      d3.select("input#pen").attr("checked",d.pen);
      d3.select("input#een").attr("checked",d.een);
      dropdownUpdate("eimg",d.eimg);
      updateImage();
}} }); });

/*preload form content for course*/
d3.select("select#cprev").on("change",function(){
  var cprev_id=d3.select("select#cprev").node().value;
  d3.json(json_path+"course/"+cprev_id ,function(d){ 
    if(d!=undefined){if(d.cid != 0){
      d3.select("input#cname").attr("value",d.cname);
      d3.select("textarea#csubjects").text(d.csubjects);
      d3.select("textarea#cservices").text(d.cservices);
}} }); });

/*preload form content for place*/
d3.select("select#pprev").on("change",function(){
  var pprev_id=d3.select("select#pprev").node().value;
  d3.json(json_path+"place/"+pprev_id ,function(d){ 
    if(d!=undefined){if(d.pid != 0){
      d3.select("input#pname").attr("value",d.pname);
      d3.select("input#paddr").attr("value",d.paddr);
      d3.select("input#pobs").attr("value",d.pobs);
      d3.select("input#pto").attr("value",d.pto);
      d3.select("input#pst").attr("value",d.pst);
      d3.select("input#pco").attr("value",d.pco);
      d3.select("input#plat").attr("value",d.plat);
      d3.select("input#plng").attr("value",d.plng);
      d3.select("input#pen").attr("checked",d.pen);
}} }); }); 

/*support "selected" option on other drop down*/
function dropdownUpdate(name,value){
  d3.select("select#"+name).selectAll("option").each(function(d,i) {
    d3.select(this).attr("selected",function(){
      return this.value == value? "selected":null; }); }); 
}
