jQuery( document ).ready( function( $ ){
	$( document ).on( 'click', '.copy-theme-settings .js-dismiss-notice', function(e) {
		e.preventDefault();
		var boxWrap = $(this).closest('.copy-theme-settings');
		boxWrap.slideUp('medium').remove();
		var baseUrl = $(this).attr('data-base_url');
		if ( 'undefined' !== baseUrl && '' !== baseUrl ) {
			history.pushState({}, '', baseUrl);
		}
	});
    // Metabox tabs
    $( document ).on( 'click', '.starterblog-mt-tabs-list a', function(e){
        e.preventDefault();
        var wrapper = $( this ).closest( '.starterblog-mt-tabs' );
        var layout = $( this ).attr( 'data-tab-id' ) || false;
        if ( layout ) {
            $( '.starterblog-mt-tab-cont', wrapper ).removeClass('active');
            $( '.starterblog-mt-tab-cont[data-tab-id="'+layout+'"]', wrapper ).addClass('active');

            $( '.starterblog-mt-tabs-list li', wrapper ).removeClass( 'active' );
            $( this ).closest('li').addClass( 'active' );
        }
    } );

	// Change style.
	if ( $('.starterblog-mt-tabs li.starterblog-mt-tab-cont').length > 2 ) {
		$('.starterblog-mt-tabs li.starterblog-mt-tab-cont a').css({'font-size': '0'});
	}

    if ( wp  && wp.media ) {


        var StarterBlogMedia = {
            setAttachment: function (attachment) {
                this.attachment = attachment;
            },
            addParamsURL: function (url, data) {
                if (!$.isEmptyObject(data)) {
                    url += (url.indexOf('?') >= 0 ? '&' : '?') + $.param(data);
                }
                return url;
            },
            getThumb: function (attachment) {
                var control = this;
                if (typeof attachment !== "undefined") {
                    this.attachment = attachment;
                }
                var t = new Date().getTime();
                if (typeof this.attachment.sizes !== "undefined") {
                    if (typeof this.attachment.sizes.medium !== "undefined") {
                        return control.addParamsURL(this.attachment.sizes.medium.url, {t: t});
                    }
                }
                return control.addParamsURL(this.attachment.url, {t: t});
            },
            getURL: function (attachment) {
                if (typeof attachment !== "undefined") {
                    this.attachment = attachment;
                }
                var t = new Date().getTime();
                return this.addParamsURL(this.attachment.url, {t: t});
            },
            getID: function (attachment) {
                if (typeof attachment !== "undefined") {
                    this.attachment = attachment;
                }
                return this.attachment.id;
            },
            getInputID: function (attachment) {
                $('.attachment-id', this.preview).val();
            },
            setPreview: function ($el) {
                this.preview = $el;
            },
            insertImage: function (attachment) {
                if (typeof attachment !== "undefined") {
                    this.attachment = attachment;
                }

                var url = this.getURL();
                var id = this.getID();
                var mime = this.attachment.mime;
                $('.starterblog-image-preview', this.preview).addClass('starterblog--has-file').html('<img src="' + url + '" alt="">');
                $('.attachment-url', this.preview).val(this.toRelativeUrl(url));
                $('.attachment-mime', this.preview).val(mime);
                $('.attachment-id', this.preview).val(id).trigger('change');
                this.preview.addClass('attachment-added');
                this.showChangeBtn();

            },
            toRelativeUrl: function (url) {
                return url;
            },
            showChangeBtn: function () {
                $('.starterblog--add', this.preview).addClass('starterblog--hide');
                $('.starterblog--change', this.preview).removeClass('starterblog--hide');
                $('.starterblog--remove', this.preview).removeClass('starterblog--hide');
            },
            insertVideo: function (attachment) {
                if (typeof attachment !== "undefined") {
                    this.attachment = attachment;
                }

                var url = this.getURL();
                var id = this.getID();
                var mime = this.attachment.mime;
                var html = '<video width="100%" height="" controls><source src="' + url + '" type="' + mime + '">Your browser does not support the video tag.</video>';
                $('.starterblog-image-preview', this.preview).addClass('starterblog--has-file').html(html);
                $('.attachment-url', this.preview).val(this.toRelativeUrl(url));
                $('.attachment-mime', this.preview).val(mime);
                $('.attachment-id', this.preview).val(id).trigger('change');
                this.preview.addClass('attachment-added');
                this.showChangeBtn();
            },
            insertFile: function (attachment) {
                if (typeof attachment !== "undefined") {
                    this.attachment = attachment;
                }
                var url = attachment.url;
                var mime = this.attachment.mime;
                var basename = url.replace(/^.*[\\\/]/, '');

                $('.starterblog-image-preview', this.preview).addClass('starterblog--has-file').html('<a href="' + url + '" class="attachment-file" target="_blank">' + basename + '</a>');
                $('.attachment-url', this.preview).val(this.toRelativeUrl(url));
                $('.attachment-mime', this.preview).val(mime);
                $('.attachment-id', this.preview).val(this.getID()).trigger('change');
                this.preview.addClass('attachment-added');
                this.showChangeBtn();
            },
            remove: function ($el) {
                if (typeof $el !== "undefined") {
                    this.preview = $el;
                }
                $('.starterblog-image-preview', this.preview).removeAttr('style').html('').removeClass('starterblog--has-file');
                $('.attachment-url', this.preview).val('');
                $('.attachment-mime', this.preview).val('');
                $('.attachment-id', this.preview).val('').trigger('change');
                this.preview.removeClass('attachment-added');

                $('.starterblog--add', this.preview).removeClass('starterblog--hide');
                $('.starterblog--change', this.preview).addClass('starterblog--hide');
                $('.starterblog--remove', this.preview).addClass('starterblog--hide');
            }

        };

        StarterBlogMedia.controlMediaImage = wp.media({
            title: wp.media.view.l10n.addMedia,
            multiple: false,
            library: {type: 'image'}
        });

        StarterBlogMedia.controlMediaImage.on('select', function () {
            var attachment = StarterBlogMedia.controlMediaImage.state().get('selection').first().toJSON();
            StarterBlogMedia.insertImage(attachment);
        });

        StarterBlogMedia.controlMediaVideo = wp.media({
            title: wp.media.view.l10n.addMedia,
            multiple: false,
            library: {type: 'video'}
        });

        StarterBlogMedia.controlMediaVideo.on('select', function () {
            var attachment = StarterBlogMedia.controlMediaVideo.state().get('selection').first().toJSON();
            StarterBlogMedia.insertVideo(attachment);
        });

        StarterBlogMedia.controlMediaFile = wp.media({
            title: wp.media.view.l10n.addMedia,
            multiple: false
        });

        StarterBlogMedia.controlMediaFile.on('select', function () {
            var attachment = StarterBlogMedia.controlMediaFile.state().get('selection').first().toJSON();
            StarterBlogMedia.insertFile(attachment);
        });


        $('.starterblog-mt-media').on('click', '.starterblog--add', function (e) {
            e.preventDefault();
            var p = $(this).closest('.starterblog-mt-media');
            console.log(p);
            StarterBlogMedia.setPreview(p);
            StarterBlogMedia.controlMediaImage.open();
        });

        $('.starterblog-mt-media').on('click', '.starterblog--remove', function (e) {
            e.preventDefault();
            var p = $(this).closest('.starterblog-mt-media');
            StarterBlogMedia.remove(p);
        });

    }


} );