jQuery(document).ready(function(){    

    /* ADD MENU ARROWS */
    jQuery('nav.base-nav ul.mythemes-menu li.menu-item-has-children').prepend('<span class="menu-plus"></span>');

    jQuery( 'nav.base-nav ul li span.menu-plus' ).on( "click" , function(){
        if( jQuery( this ).hasClass( 'collapsed' ) ){
            jQuery( this ).parent().children('ul').hide( "slow" , function(){
                jQuery( this ).removeAttr( 'style' );
            });
            jQuery( this ).removeClass( 'collapsed' );
        }
        else{
            jQuery( this ).addClass( 'collapsed' );
            jQuery( this ).parent().children('ul').show( "slow" );
        }
    });

    /* ADD PLUS AND MINUS FOR MENU ITEMS WITH SUB MENU */
    jQuery( '.btn-collapse' ).click(function(){
        
        if( jQuery( this ).hasClass( 'collapsed' ) ){
            jQuery( this ).removeClass( 'collapsed' );
            jQuery( '.nav-collapse.in' ).each(function(){
                jQuery( this ).hide( 'slow' , function(){
                    jQuery( this ).removeClass( 'in' );
                    jQuery( this ).removeAttr( 'style' );
                });
            });
        }
        else{
            jQuery( '.btn-collapse' ).removeClass( 'collapsed' );
            jQuery( this ).addClass( 'collapsed' );

            var nav = jQuery( this ).attr( 'data-toggle' );

            jQuery( '.nav-collapse.in' ).each(function(){
                jQuery( this ).hide( 'slow' , function(){
                    jQuery( this ).removeClass( 'in' );
                    jQuery( this ).removeAttr( 'style' );
                });
            });

            jQuery( nav ).show( 'slow' , function(){
                jQuery( this ).addClass( 'in' );
                jQuery( this ).removeAttr( 'style' );
            });
        }
    });

    function header_image(){
        jQuery( '.mythemes-header-image img' ).removeAttr( 'style' );

        var div     = jQuery( '.mythemes-header-image' ).width();
        var img     = jQuery( '.mythemes-header-image img' ).width();
        var left    = 0;

        jQuery( '.mythemes-header-image img' ).load(function(){
            var img = jQuery( this ).width();

            if( img == 0 ){
                return;
            }
            
            if( img > div ){
                left = parseInt( ( div - img ) / 2 );
                jQuery( '.mythemes-header-image img' ).css( { 'margin-left' : left + 'px' } );
            }
            else{
                jQuery( '.mythemes-header-image img' ).css( { 'width' : '100%' , 'height' : 'auto' } );
            }
        });

        if( img == 0 ){
            return;
        }

        if( img > div ){
            left = parseInt( ( div - img ) / 2 );
            jQuery( '.mythemes-header-image img' ).css( { 'margin-left' : left + 'px' } );
        }
        else{
            jQuery( '.mythemes-header-image img' ).css( { 'width' : '100%' , 'height' : 'auto' } );
        }
    }

    header_image();

    function mythemes_load_images( count, self ){
        count--;
        if( count == 0 ){
            self.masonry();
        }

        return count;   
    }
    function mythemes_gallery(){
        jQuery( 'div.mythemes-gallery' ).each(function(){
            var self    = jQuery( this );
            var count   = jQuery( this ).find( 'figure.mythemes-item' ).length;

            jQuery( this ).find( 'figure.mythemes-item' ).each(function(){
                jQuery( this ).find( 'img' ).load(function(){
                    if( this.complete ){
                        count = mythemes_load_images( count, self );
                    }else{
                        jQuery( this ).one( 'load', function(){
                            count = mythemes_load_images( count, self );
                        });
                    }
                });
            });
        });
    }

    mythemes_gallery();

    /* CHANGE BORDER BOTTOM ON WINDOW RESIZE */
    jQuery( window ).resize(function() {

        jQuery( 'nav.base-nav ul span.menu-plus' ).removeClass( 'collapsed' );
        jQuery( 'nav.base-nav ul li ul' ).removeAttr( 'style' );
        header_image();

        if( jQuery( '.mythemes-gallery' ).length ){
            jQuery( '.mythemes-gallery' ).masonry();    
        }
        
    });

    /* TAGS WITH COUNTER */
    jQuery( 'div.widget_tag_cloud div.tagcloud' ).append( '<div class="clear clearfix"></div>' );
    
    jQuery( 'div.widget_tag_cloud div.tagcloud a, div.widget_post_tags div.tagcloud a' ).each(function(){

        jQuery( this ).removeAttr( 'style' );
        jQuery( this ).removeAttr( 'class' );

        var text = jQuery( this ).text();
        var nr   = jQuery( this ).attr( 'title' ).split( " " )[0];


        jQuery( this ).html( '<span>' +
            '<span class="icon"><i class="icon-tag"></i></span>' +
            '<span class="tag-name">' + text + '</span>' +
            '<span class="counter">' + nr + '</span>' +
            '</span>'
        );

        var icon            = jQuery( this ).find( 'span.icon' );
        var name            = jQuery( this ).find( 'span.tag-name' );
        var counter         = jQuery( this ).find( 'span.counter' );

        var icon_width      = icon.outerWidth();
        var counter_width   = counter.outerWidth();

        var diff            = counter_width - icon_width;
        var name_width      = name.outerWidth();
        var width           = 0;

        if( diff < 0 ){
            diff            = 0;
            width           = name_width + icon_width;
            counter.css({ 'width' : icon_width + 'px' });    
        }else{
            width           = name_width + counter_width;
        }

        counter.css({ 'margin-left' : diff + 'px' });
        jQuery( this ).css({ 'width' : width + 'px' });
    });
});