function init(){
    imgPopUp();
    cascade(1);

}

function lastPage (){
    pages = $('div[data-sg-id="item-page"]');
    // Iterate  and hide all pages
    pages.each(function(){
        $(this).css('display','none');
    })
    // Show last page
    $(pages[pages.length -1 ]).css('display','block');
    console.log('REMOVE IN PRODUCTION BEFORE PACK LO');
}

function pause(idVideo){ 
    $('#'+idVideo).trigger('pause'); 
}   


function cascade(cascadeGroup){
   var elements = $('[cascade='+cascadeGroup+']');
   var firstGroup = $('[cascade='+cascadeGroup+'][parent=0]');
   var restGroup = $('[cascade='+cascadeGroup+'][parent!=0]');
   var toggleElements = $('[cascade='+cascadeGroup+'][cascadeToggle]');

    restGroup.hide(); // Leave only initial show elements  (parent =0)

    toggleElements.each(function(){
        console.log(this);
        $(this).on('click', function(){
            var keyToggle = $(this).attr('cascadeToggle');
            var childs = $('[cascade='+cascadeGroup+'][parent='+keyToggle+']');
            childs.toggle("fold");
            $(this).unbind('click');

        })

    })
}

//  Requires :
//  PopUp and Button PopUp  with id = 100
//  big custom attr on img 
//  _big   suffix on file image
function imgPopUp(){
    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 MAX_HEIGHT_POPUP = 765;
    var contAspectRatio = MAX_WIDTH_IMG / MAX_HEIGHT_IMG;


     images.each(function(){


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

             imgContainer.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'});

             imgContainer.appendTo(content);  // ADDS the magic!! 
             btPopUp.trigger('click'); // Activate pop up 

             }); // End Load

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



