jQuery(document).ready(function($){
    //Scroll to section
    $('body').on('click', '#sub-accordion-panel-relax_spa_homepage_panel .control-subsection .accordion-section-title', function(event) {
        var section_id = $(this).parent('.control-subsection').attr('id');
        scrollToSection( section_id );
    });
    
    //preview url of homepages templates 
     wp.customize.panel( 'relax_spa_homepage_panel', function( section ){
        section.expanded.bind( function( isExpanded ) {
            if( isExpanded ){
                wp.customize.previewer.previewUrl.set( data.home );
            }
        });
    });
     
});

function scrollToSection( section_id ){
    var preview_section_id = "banner_section";

    var $contents = jQuery('#customize-preview iframe').contents();

    switch ( section_id ) {
        
        case 'accordion-section-header_image':
        preview_section_id = "relax_spa_banner_section";
        break;

        case 'accordion-section-relax_spa_service_section':
        preview_section_id = "relax_spa_service_section";
        break;

        case 'accordion-section-relax_spa_who_we_are_section':
        preview_section_id = "relax_spa_who_we_are_section";
        break;

        case 'accordion-section-relax_spa_counter_sections':
        preview_section_id = "relax_spa_counter_sections";
        break;

        case 'accordion-section-relax_spa_why_choose_us_section':
        preview_section_id = "relax_spa_why_choose_us_section";
        break;

        case 'accordion-section-relax_spa_offer_section':
        preview_section_id = "relax_spa_offer_section";
        break;

        case 'accordion-section-relax_spa_testimonial_sections':
        preview_section_id = "relax_spa_testimonial_sections";
        break;
        
        case 'accordion-section-relax_spa_teams_sections':
        preview_section_id = "relax_spa_teams_sections";
        break;

        case 'accordion-section-relax_spa_pricing_table':
        preview_section_id = "relax_spa_pricing_table";
        break;
        
    }

    if( $contents.find('#'+preview_section_id).length > 0 && $contents.find('.home').length > 0 ){
        $contents.find("html, body").animate({
        scrollTop: $contents.find( "#" + preview_section_id ).offset().top
        }, 1000);
    }
}