var shortcode_template;



var popup_template;















/**********************************



    WNagish Express Accordion



***********************************/







jQuery(document).ready(function(){







});



// Open popup window



jQuery(document).on('click', '#insert-wah-accodrion', function(){



    open_wah_popup_window( title = 'WNagish Express Accordion' );



});



// Close popup window



jQuery(document).on('click', '.close-wah-popup', function(){



    close_wah_popup_window();



});



// Delete popup item



jQuery(document).on('click', '.wah-accordion-item-actions button.delete-item', function(e){



    e.preventDefault();



    var index = jQuery(this).data('index');



    remove_wah_accordion_item( index );



    recalculate_wah_item_index();



});



// Add popup item



jQuery(document).on("click", ".wah-add-accodrion-item", function(){



    var counter = jQuery(".wah-accordion-item").length;



    add_wah_accordion_item( counter );



});



// Open popup settings



jQuery(document).on("click", ".wah-popup-settings", function(){



    jQuery('.wah-popup-settings-wrapper').toggleClass('active');



});



// Generate and print shortcode



jQuery(document).on("click", ".get-shortcode-code", function(){



    var accordion_form = jQuery('.wah-accordion-shortcode-generator').serialize();



    if( accordion_form ) {



        generate_accordion_shortcode(accordion_form);



    }



});







function generate_accordion_shortcode(accordion_form){



    jQuery.ajax({



       type       : "post",



       dataType   : "json",



       url        : ajaxurl,



       data       : {



           action : "generate_accordion_shortcode",



           form   : accordion_form



       },



       success: function(response) {



           if( typeof response.html != 'undefined' && response.html ) {







               if( jQuery('#shortcode-response-wrapper').length ) {



                   jQuery("#shortcode-response-wrapper").remove();



               }



               jQuery('.wah-popup-footer')



                .append('<div class="shortcode-response" id="shortcode-response-wrapper"><textarea>'+response.html+'</textarea></div>');



                jQuery('.wah-popup-inner').animate({



                    scrollTop: jQuery( "#shortcode-response-wrapper" ).offset().top},



                    'slow');



           }



       }



    });



}







function open_wah_popup_window( title ) {







    var wah_popup_template = '<div class="wah-popup-wrapper">'+



        '<div class="wah-popup-inner">'+



            '<div class="wah-popup-header">'+title+'<button class="button wah-popup-settings">Settings</button></div>'+



            '<div class="wah-popup-body">'+



                '<form class="wah-accordion-shortcode-generator">'+



                    '<div class="wah-popup-settings-wrapper">'+



                        '<div class="wah-settings-item">'+



                            '<span>Active background color:</span> <input name="wah-acc-bg-active" type="color" value="#2e829c" />'+



                        '</div>'+



                        '<div class="wah-settings-item">'+



                            '<span>Default background color:</span> <input name="wah-acc-bg-default" type="color" value="#236478" />'+



                        '</div>'+



                        '<div class="wah-settings-item">'+



                            '<span>Active text color:</span> <input name="wah-acc-text-active" type="color" value="#FFFFFF" />'+



                        '</div>'+



                        '<div class="wah-settings-item">'+



                            '<span>Default text color:</span> <input name="wah-acc-text-default" type="color" value="#FFFFFF" />'+



                        '</div>'+



                        '<div class="wah-settings-item is-wah-checkbox">'+



                            '<label><input name="wah-acc-animations" type="checkbox" /> <span>Enable animations:</span></label>'+



                        '</div>'+



                        '<div class="wah-settings-item is-wah-checkbox">'+



                            '<label><input name="wah-acc-first-is-active" type="checkbox" /><span>First acordion item active?</span></label>'+



                        '</div>'+



                    '</div>'+



                    '<div class="wah-accordion-fields" id="wah-sortable-widgets"></div>'+



                '</form>'+



                '<button type="button" class="button wah-add-accodrion-item">Add item</button>'+



            '</div>'+



            '<div class="wah-popup-footer">'+



                '<div class="column column-left">Naghish Accessibility</div>'+



                '<div class="column column-right">'+



                '<button type="button" class="button get-shortcode-code">Get shortcode</button>'+



                '<button type="button" class="button button-primary close-wah-popup">Close</button>'+



                '</div>'+



            '</div>'+



        '</div>'+



    '</div>';







    jQuery('body').append( wah_popup_template );







}







function update_sortable_list(){



    jQuery('.wah-accordion-fields').sortable({



        placeholder: "ui-state-highlight",



        update: function( event, ui ) {



            recalculate_wah_item_index();



        }



    });



    jQuery('.wah-accordion-fields').disableSelection();



}







function add_wah_accordion_item( counter ){







    if( typeof counter == 'undefined' ) {



        counter = 1;



    }







    counter = counter++;







    var wah_accordion_item = '<div class="wah-accordion-item" data-index="'+counter+'">'+



        '<div class="wah-order-counter"><span class="number">'+counter+'</span></div>'+



        '<div class="wah-item-container">'+



            '<div class="wah-accordion-item-title"><input type="text" placeholder="Accordion item title" name="item-'+counter+'[wah-acc-title]" /></div>'+



            '<div class="wah-accordion-item-content"><textarea id="item-textarea-'+counter+'" name="item-'+counter+'[wah-acc-content]" placeholder="Accordion item content" /></textarea></div>'+



            '<div class="wah-accordion-item-actions"><button class="button delete delete-item" data-index="'+counter+'">Delete</button></div>'+



        '</div>'+



    '</div>';







    jQuery(".wah-accordion-fields").append( wah_accordion_item );







    // enable tinyMCE



    // if ( typeof( tinyMCE ) == "object" && typeof( tinyMCE.execCommand ) == "function" ) {



    //     tinyMCE.execCommand("mceAddEditor", false, 'item-textarea-'+counter);



    // }







    update_sortable_list();







}







function remove_wah_accordion_item( index ) {



    jQuery('.wah-accordion-item[data-index="'+index+'"]').fadeOut().remove();



}







function close_wah_popup_window(){



    jQuery(".wah-popup-wrapper").fadeOut(250, function(){



        jQuery(this).remove();



    });



}







function recalculate_wah_item_index() {



    var index_counter = 0;



    jQuery('.wah-accordion-item').each( function(){



        jQuery(this).attr('data-index', index_counter);



        jQuery(this).find('.number').html( index_counter );



        jQuery(this).find('.wah-accordion-item-actions .delete-item').attr('data-index', index_counter);



        index_counter++;



    });



}



