//  Requires :
//  PopUp and Button PopUp  with id = 100
//  big custom attr on img 
//  _big   suffix on file image
function imgPopUp(){
    var images = $('img[big]');
    var btPopUp = $('[data-sg-id="btn-popup"][data-sg-key="100"]');
    var popUp = $('[data-sg-id="item-popup"][data-sg-key="100"]');
    var MAX_WIDTH_IMG = 900;
    var MAX_HEIGHT_IMG = 550;
    var contAspectRatio = MAX_WIDTH_IMG / MAX_HEIGHT_IMG;
    var divLupa = $('<div class="bgLupa"></div>');

     images.each(function(index){

         var idImg = $(this).prop('id');
         var apa = $(this).attr('apa');

         if(typeof idImg  === typeof undefined || idImg == false) {
             idImg='';
         }

         if(typeof apa  === typeof undefined || apa == false) {
             apa=null;
         }
         else{
             MAX_HEIGHT_IMG-= 70;  // reserve 70px for display Apa CC at end of popup
         }

         var width = this.width;
         var height = this.height;
         var imgContainer =  $('<div id="contImg'+idImg+'" class="decoImg2 sg-center" >');
         var nlupa = divLupa.clone(); // same div for all images 

         imgContainer.css({'height': height,  'width': width});
         imgContainer.after($('</div>'));

         $(this).before(imgContainer);
         $(this).appendTo(imgContainer);
         nlupa.appendTo(imgContainer);

         var imgOriginal = $(this);

         imgContainer.on('click', function(){
             var content=popUp.find('#imgContainer'); 
             content.html(''); //Clear any prev content
             var src = $(imgOriginal).attr('src');
             var baseName =  src.substr(0, src.lastIndexOf('.')) || input;
             var fileExt = src.split('.').pop();
             var big = baseName+"_big."+fileExt;
             var popUpImgContainer = $('<img id="imgContainer">'); 
             var heightApa = 0;

             popUpImgContainer.attr("src",big).load(function(){

                 var width = this.width; 
                 var height = this.height;

                 if(width > MAX_WIDTH_IMG){
                     ratio = MAX_WIDTH_IMG / width;        // get ratio for scaling image
                     $(this).css("width", MAX_WIDTH_IMG);  // Set new width
                     $(this).css("height", height * ratio);   // Scale height based on ratio
                     height = height * ratio;                 // Reset height to match scaled image
                     width = width * ratio;                   // Reset width to match scaled image
                 }

                 // Check if current height is larger than max
                 if(height > MAX_HEIGHT_IMG){
                     ratio = MAX_HEIGHT_IMG / height; // get ratio for scaling image
                     $(this).css("height", MAX_HEIGHT_IMG);   // Set new height
                     $(this).css("width", width * ratio);    // Scale width based on ratio
                     width = width * ratio;    // Reset width to match scaled image
                 }

                 $(this).css({'display': 'block' , 'margin': 'auto'});

                 popUpImgContainer.appendTo(content);  // ADDS the magic!! 
                 if ( apa != null ){
                     var  parrafoApa = $('<div class="ccImgPopUp">'+ apa +'</div> ');
                     parrafoApa.appendTo(content);
                 }

                 btPopUp.trigger('click'); // Activate pop up 

             }); // End Load

         });
     });
} // Fin Img Pop Up Carlos U


function init(){
    imgPopUp();
}