jQuery(document).ready(function($) {
  // Stuff to do as soon as the DOM is ready. Use $() w/o colliding with other libs;
  var current_textarea = {};
  var parent = null;
  var this_modal_window = null;
  var lang = <%= VALID_LOCALES.to_s %>;

$("textarea[func=input_unit]").each(function(){
    var id = $(this).attr("id");
    current_textarea[id] = $(this).val();    
})
console.log(current_textarea);

  // current_textarea['en'] = $("textarea[for=en]").val();
  // current_textarea['zh_tw'] = $("textarea[for=zh_tw]").val();

  

  $("div.controls").on("click","a.edit-btn",function(){
    parent = $(this).parent();
    this_modal_window = $(this).parent().parent().parent().parent().find(".modal");
   
    choosen_lang = $(this).parent().parent().find("ul li.active").attr("for");
    $(this).parents(".editMore").find(".modal_tab").removeClass("active in");
    $(this).parents(".editMore").find(".modal_tab[for="+choosen_lang+"]").addClass("active in");

    $(this).parents(".editMore").find(".modal-body li").removeClass("active");
    $(this).parents(".editMore").find(".modal-body li[for="+choosen_lang+"]").addClass("active");
    // switch($(this).parent().parent().find("ul li.active a").attr("href")){
    //   case ".tab1":
    
        // $("#tab3").addClass("active in");
        // $(".modal-body li").eq(0).addClass("active");
      //   break;
      // case ".tab2":
      //   $("#tab4").addClass("active in");
      //   $(".modal-body li").eq(1).addClass("active");
      //   break;
   
    
    $.each(lang,function(i,value){
      // console.log(this_modal_window);
        var sa = parent.find("input.street_address_"+lang[i]).val();
        var city = parent.find("input.city_"+lang[i]).val();
        var county = parent.find("input.county_"+lang[i]).val();
        var zip = parent.find("input.zip_"+lang[i]).val();
        var country = parent.find("input.country_"+lang[i]).val();
        this_modal_window.find('.tab-content div[for='+lang[i]+'] textarea[func=street_address]').val(sa);
        this_modal_window.find('.tab-content div[for='+lang[i]+'] input[func=city]').val(city);
        this_modal_window.find('.tab-content div[for='+lang[i]+'] input[func=county]').val(county);
        this_modal_window.find('.tab-content div[for='+lang[i]+'] input[func=zip]').val(zip);
        this_modal_window.find('.tab-content div[for='+lang[i]+'] input[func=country]').val(country);
        var verify = sa + city + country +county + zip;
       
        var combined = "";
        if(verify!="")
          combined = sa + "\n" + city + "\n" + county + "\n" + zip + "\n" + country+"\n";

        var Text0 = parent.find("div."+lang[i]+" textarea[func=input_unit]").val();
        Text1 = Text0.replace(/(\r\n|\n|\r)/gm,"");
        var Text2 = combined;
        Text2 = Text2.replace(/(\r\n|\n|\r)/gm,"");

        if(Text1 != Text2){
          this_modal_window.find('.tab-content div[for='+lang[i]+'] textarea[func=street_address]').val(Text0);
          this_modal_window.find("input").val("");
        }
      });
   });
  $("textarea[func=input_unit]").change(function(){
    var id = $(this).attr("id");
    var lang = $(this).attr("for");
    if(!parent)
      parent = $(this).parent().parent();
    console.log(current_textarea[id]);
    if(current_textarea[id] != $(this).val())
      parent.find("input.indicator_"+lang).val("true");
    else
      parent.find("input.indicator_"+lang).val("false");
  })
  
  $(".address-edit").on("hidden",function(){
    $("#tab3").removeClass("active in");
    $("#tab4").removeClass("active in");
    $(".modal-body li").removeClass("active");
    $(".modal-body input, .modal-body textarea").val("");

  })
  $(".bt-save").click(function(){
    for(i=0;i<2;i++){
      var sa = this_modal_window.find('.tab-content div[for='+lang[i]+'] textarea[func=street_address]').val();
      var city =  this_modal_window.find('.tab-content div[for='+lang[i]+'] input[func=city]').val();
      var county = this_modal_window.find('.tab-content div[for='+lang[i]+'] input[func=county]').val();
      var zip = this_modal_window.find('.tab-content div[for='+lang[i]+'] input[func=zip]').val();
      var country = this_modal_window.find('.tab-content div[for='+lang[i]+'] input[func=country]').val();
      parent.find("input.street_address_"+lang[i]).val(sa);
      parent.find("input.city_"+lang[i]).val(city);
      parent.find("input.county_"+lang[i]).val(county);
      parent.find("input.zip_"+lang[i]).val(zip);
      parent.find("input.country_"+lang[i]).val(country);
      var verify = sa + city + country +county + zip;
      var combined = "";
      if(verify!="")
        combined = sa + "\n" + city + "\n" + county + "\n" + zip + "\n" + country;
      parent.find("textarea[for="+lang[i]+"]").val(combined);
      parent.find("input.indicator_"+lang[i]).val("false");
      current_textarea[parent.find("textarea[for="+lang[i]+"]").attr("id")] = combined;
    }
    
  })

});
