$(document).ready(function() {
   removeOldSDestinations();
   
   $("#nextimage").click(function() {
     if($(".activedestination").next().length == 0) {
       $(".destination").hide();
       $(".activedestination").removeClass('activedestination');
       $(".destination").first().addClass('activedestination');
       replaceImagPath();
       $(".destination").first().show();
     } else {
       $(".destination").hide();
       $(".activedestination").next().addClass('activenextdestination');
       $(".activedestination").removeClass('activedestination');
       $(".activenextdestination").addClass('activedestination');
       $(".activedestination").removeClass('activenextdestination');
       replaceImagPath();
       $(".activedestination").show();
     }
   });

   $("#previmage").click(function() {
      if($(".activedestination").prev().length == 0) {
         $(".destination").hide();
         $(".activedestination").removeClass('activedestination');
         $(".destination").last().addClass('activedestination');
         replaceImagPath();
         $(".destination").last().show();
      } else {
        $(".destination").hide();
        $(".activedestination").prev().addClass('activenextdestination');
        $(".activedestination").removeClass('activedestination');
        $(".activenextdestination").addClass('activedestination');
        $(".activedestination").removeClass('activenextdestination');
        replaceImagPath();
        $(".activedestination").show();
      }
   });
   
});

function replaceImagPath() {
       var imagePath = $(".activedestination").find("img").attr("src");
       var newimagePath = imagePath.replace(/\/internet\/ec.europa.eu/, "");
       $(".activedestination").find("img").attr("src", newimagePath); 
}

function removeOldSDestinations() {
   $(".destination").hide();
   $(".destination").each(function(index){
       var imagePath = $(this).find("img").attr("src"); 
       if(imagePath == "") {
          $(this).remove();
       } 
   });
   $(".destination").first().addClass('activedestination');
   replaceImagPath();
   $(".destination").first().show();
   $("#destinationSlider").show();
}
