/**
 * Theme4Press Framework
 *
 * WARNING: This file is part of the Theme4Press Core Framework.
 * Do not edit the core files.
 * Add any modifications necessary under a child theme.
 *
 * @version: 1.0.0
 * @package  Theme4Press/Admin Interface
 * @author   Theme4Press
 * @link     http://theme4press.com
 */

( function( $ ) {

	"use strict";

	$( document ).ready( function() {

		// show or hide megamenu fields on parent and child list items
		t4p_megamenu.menu_item_mouseup();
		t4p_megamenu.megamenu_status_update();
		//$( '.edit-menu-item-megamenu-status' ).status_update();
		t4p_megamenu.update_megamenu_fields();

		// setup automatic thumbnail handling
		$( '.remove-t4p-megamenu-thumbnail' ).manage_thumbnail_display();
		$( '.t4p-megamenu-thumbnail-image' ).css( 'display', 'block' );
		$( ".t4p-megamenu-thumbnail-image[src='']" ).css( 'display', 'none' );

		// setup new media uploader frame
		t4p_media_frame_setup();

	});

	// "extending" wpNavMenu
	var t4p_megamenu = {

		menu_item_mouseup: function() {
			$( document ).on( 'mouseup', '.menu-item-bar', function( event, ui ) {
				if( ! $( event.target ).is( 'a' )) {
					setTimeout( t4p_megamenu.update_megamenu_fields, 300 );
				}
			});
		},

		megamenu_status_update: function() {

			$( document ).on( 'click', '.edit-menu-item-megamenu-status', function() {
				var parent_li_item = $( this ).parents( '.menu-item:eq( 0 )' );

				if( $( this ).is( ':checked' ) ) {
					parent_li_item.addClass( 't4p-megamenu' );
				} else 	{
					parent_li_item.removeClass( 't4p-megamenu' );
				}

				t4p_megamenu.update_megamenu_fields();
			});
		},

		update_megamenu_fields: function() {
			var menu_li_items = $( '.menu-item');

			menu_li_items.each( function( i ) 	{

				var megamenu_status = $( '.edit-menu-item-megamenu-status', this );

				if( ! $( this ).is( '.menu-item-depth-0' ) ) {
					var check_against = menu_li_items.filter( ':eq(' + (i-1) + ')' );


					if( check_against.is( '.t4p-megamenu' ) ) {

						megamenu_status.attr( 'checked', 'checked' );
						$( this ).addClass( 't4p-megamenu' );
					} else {
						megamenu_status.attr( 'checked', '' );
						$( this ).removeClass( 't4p-megamenu' );
					}
				} else {
					if( megamenu_status.attr( 'checked' ) ) {
						$( this ).addClass( 't4p-megamenu' );
					}
				}
			});
		}

	};

	$.fn.manage_thumbnail_display = function( variables ) {
		var button_id;

		return this.click( function( e ){
			e.preventDefault();

			button_id = this.id.replace( 't4p-media-remove-', '' );
			$( '#edit-menu-item-megamenu-thumbnail-'+button_id ).val( '' );
			$( '#t4p-media-img-'+button_id ).attr( 'src', '' ).css( 'display', 'none' );
		});
	}

	function t4p_media_frame_setup() {
		var t4p_media_frame;
		var item_id;

		$( document.body ).on( 'click.t4pOpenMediaManager', '.t4p-open-media', function(e){

			e.preventDefault();

			item_id = this.id.replace('t4p-media-upload-', '');

			if ( t4p_media_frame ) {
				t4p_media_frame.open();
				return;
			}

			t4p_media_frame = wp.media.frames.t4p_media_frame = wp.media({

				className: 'media-frame t4p-media-frame',
				frame: 'select',
				multiple: false,
				library: {
					type: 'image'
				}
			});

			t4p_media_frame.on('select', function(){

				var media_attachment = t4p_media_frame.state().get('selection').first().toJSON();

				$( '#edit-menu-item-megamenu-thumbnail-'+item_id ).val( media_attachment.url );
				$( '#t4p-media-img-'+item_id ).attr( 'src', media_attachment.url ).css( 'display', 'block' );

			});

			t4p_media_frame.open();
		});

	}
})( jQuery );