/**
 * This javascript adjusts some html of the old structure
 * to get new appearance / functionality
 *
 * search old main.js for "@todo"
 * to see changes made there
 */
(function( $ ) {

    "use strict";

    /**
     * Hamburger Menu
     *
     * @todo needs data-navbar-toggle="closed" to body or .navbar-default
     */
    function adjustNavbarToggle() {

        $('button.navbar-toggle').click(function() {

            if($('*[data-navbar-toggle]').attr('data-navbar-toggle') === 'closed') {
                $('*[data-navbar-toggle]').attr('data-navbar-toggle', 'open');
            } else {
                $('*[data-navbar-toggle]').attr('data-navbar-toggle', 'closed');
            }
        });
    }

    /**
     * add neccessary elements to old header-carousel
     * div.item.text-on-top:
     * div.slider-text-container
     *      div.slider-text-h1
     *          span.slider-text-h1-deco
     *          span.slider-text-h1-text
     *
     * @type {never|$}
     */
    function adjustCarouselHeader() {
        
        var carouselHeader =  $('#carousel-header');
        //var itemDefault =  $('#carousel-header .item:not(.text-on-top)');
        var itemTextOnTop =  $('#carousel-header .item.text-on-top');
        
        // add div.row if not set
        // $(itemDefault).each(function(i, slide) {
        //     if( $(slide).find('.row').length == 0 ) {
        //         $(slide).find('.container').wrapInner( '<div class="row"></div>');
        //     }
        // });
        
        $(itemTextOnTop).each(function(i, slide) {
            if( $(slide).find('.slider-text-h1').length == 0 ) {
                var sliderText = $(slide).find('.slider-text');
                var textElem = $(sliderText).children();

                textElem.wrapAll('<div class="slider-text-container" />');
                
                var h1 = $(slide).find('h1, .h1');
                $(h1).html('<span class="slider-text-h1-text">' + $(h1).html() + '</span>');
                //$(h1).prepend('<span class="slider-text-h1-deco"></span>');
                $(h1).wrap('<div class="slider-text-h1" />');

                var event = jQuery.Event( 'adjust.carouselheader.textontop' );
                event.slide = slide;
                $( 'body' ).trigger( event );
            }
        });
    }

    /**
     * only for exisitng old forms
     * show label | remove placeholder | switch order of label and input
     */
    function adjustForm() {
        $('form .form-group').each(function(i, formGroup) {

            var lable = $( formGroup ).find('label');
            var formControl = $( formGroup ).find('.form-control');

            $( lable ).removeClass('sr-only');
            $( formControl ).removeClass('input-lg').attr('placeholder', '');

            // if label before formfield => switch it
            if( $(formControl).prev('label').length > 0 ) {
                $( lable ).before($(formControl));
            }
        });
    }


    $(document).ready(function() {
        adjustCarouselHeader();
        adjustForm();
        adjustNavbarToggle();
    });

}( jQuery ));