var $ = jQuery;

$(document).ready(function(){

              var country_code = '';
              var country_emails;
              //when a country flag is clicked we show the combo:

              $(".contacta_pais").click( function(){
                  $("#select_recipient h3").html('');
                  country_code = $(this).attr("id");
                  var country_name = $(this).html();
                  $("#select_recipient").prepend("<h3>Ha seleccionado "+country_name+" <img src=/image/?img_id='"+$(this).attr("rel")+"' /></h3>");
                  $("#select_recipient").fadeIn(1600);

                  var get_user_email_params = "country="+country_code;
                  $.ajax({
                        type: "POST",
                        url: "/ever-cumbres/html/portlet/contacta_cumbres/get_user_email.jsp",
                        data: get_user_email_params,
                        success: function(country_users_email_response) {
                            country_emails = eval('(' + country_users_email_response + ')');
                        }
                });
              });

              //when an option of the combo is changed we create the spans to choose council or court.
              //if "cumbre judicial" choosen no further option needed. so the form mail is showed.
              $("#select_recipient").bind(($.browser.msie ? "click" : "change"), function () {

              var option_selected = $('#select_recipient option:selected').attr("value");
              if(option_selected == 'cumbres'){
               $('.contacta_cumbres_wrapper').html('');
               $('#form_display').fadeIn(1600);
               $('#destinatario').val("mpena@ever.es");
              }else if (option_selected == "coo") {
                $('.contacta_cumbres_wrapper').html('');
                $('#form_display').hide();
                var html_coo = '<div class="contacta_cumbres_wrapper">';
                if (country_emails.email_coo_cs != '' && country_emails.email_coo_cs != null && typeof(country_emails.email_coo_cs) != undefined) {
                    html_coo = html_coo + '<span class="contacta_cumbres" value="'+option_selected+'" rel="cs">Corte Suprema</span>';
                }
                if (country_emails.email_coo_cj != '' && country_emails.email_coo_cj != null && typeof(country_emails.email_coo_cj) != undefined) {
                    html_coo = html_coo + '<span class="contacta_cumbres" value="'+option_selected+'" rel="cj">Consejo de la Judicatura</span></div>';
                }
                html_coo = html_coo + '</div>';
                $('#select_recipient').append(html_coo);
              }else{
                $('.contacta_cumbres_wrapper').html('');
                $('#form_display').hide();
                var html_coo = '';
                $('#select_recipient').append(html_coo);
              }
              });

              //we use intercept plugin to bind the containter and passively listen to other events

              $('#contacta_wrapper').intercept('click',
              {
                   '.contacta_cumbres':function(){
                   var coo_or_pre = $(this).attr("value");
                   var member_type= $(this).attr("rel");
                   var user_name = coo_or_pre+"-"+country_code+"-"+member_type;

                   $('.contacta_cumbres_wrapper').html('');
                   $('#form_display').fadeIn(1600);
                   $('#destinatario').val($.trim(country_emails['email_' + coo_or_pre + '_' + member_type]));
               }
              }
              );
});

