var eq2 = {
	'icons':{'items':[],'spells':[]}
	
};






/* ICOPICKER HANDLING & EVENTS */

//Manually change item icon
$(document).on('change', '.item-edit-input[data-field="icon"]', function() {
	var i = $(this).val(),
		ctn = $('.edit-item-ico-ctn'),
		ico = 'img/eq2/items/'+i+'.png';
	ctn.empty().append(''
	+'<img class="edit-item-ico vm animated tada" src="img/eq2/items/'+i+'.png">'
	+'<br>'
	+'<small><strong class="color-success-700">Icon ID</strong>: '+i+'</small>');
	
});

//Select icon.
$(document).on('click', '.ico-picker-i', function() {
	var view = $('.edit-item-ico-ctn'),
		e = $('.item-edit-input[data-field="icon"]'),
		i = $(this).data('icon-id');
	e.trigger('change');
	$('.icon-picker-ctn').slideUp('slow');
	view.empty().append(''
	+'<img class="edit-item-ico vm animated tada" src="img/eq2/items/'+i+'.png"/>'
	+'<br>'
	+'<small><strong class="color-success-700">Icon ID</strong>: '+i+'</small>');
	e.val(i);
});


//Toggle icon picker tools
$(document).on('click', '.ico-picker-btn, .cancel-icon-picker', function() {
	var c = $('.icon-picker-ctn'),
		visible = c.is(":visible");
	if(visible) {
		c.slideUp('slow');
	} else {
		c.slideDown('fast');
	}
});


var icoLoader = [];
$(document).on('click', '.icopicker-category, .ico-picker-page', function() {
	var c = $(this).data('category'),
		ctn = $('.ico-picker-list'),
		p = 1;
		
	if($(this).hasClass('ico-picker-page')) {
		p = $(this).data('page');
		$(this).empty().append('<i class="fad fa-spinner fa-spin fast-spin"></i>');
		$('.ico-picker-page.btn-success').switchClass('btn-success', 'btn-warning');
	} else {
		$(this).parent().find('.btn-success').switchClass('btn-success','btn-default');
		$(this).switchClass('btn-default', 'btn-success');
	}
	//ctn.empty().append('<h2 class="text-center">Fetching icons ...</h2>');
	if(icoLoader.length > 0) {
		$.each(icoLoader, function(k,v) {
			clearTimeout(icoLoader[k]);
			icoLoader.splice(k,1);
		});
	}
	$.post('autoload.php', {'get-item-icons': c, 'page':p}, function(data) {
		ctn.empty().append(data);
		var intv = 0;
		icoLoader = $.each($('.ico-unloaded'), function(k,v) {
			var iconid = $(this).data('icon-id');
			icoLoader[icoLoader.length+1] = setTimeout(function() {
				$('.ico-picker-i[data-icon-id="'+iconid+'"]').removeClass('ico-unloaded');
				$('.ico-picker-i[data-icon-id="'+iconid+'"]').empty().append('<img class="ico-picker-img animated zoomIn" src="img/eq2/items/'+iconid+'.png" />');
				
			},intv);
			intv = intv+5;
			
		});
		
	});
	
});


/* NAVIGATION HANDLING & EVENTS */
$(document).on('click', '.eq-nav-link', function() {
	var m = $(this).data('module'),
		e = $(this),
		t = $(this).find('.nav-link-text').text();
		console.log(t);
	$(this).find('a').empty().append('<span class="inline animated fadeInLeft"><i class="fad fa-spinner-third fa-spin fast-spin"></i></span>&nbsp;&nbsp;<span class="nav-link-text">'+t+'</span>');
	$.post('autoload.php', {'set-navigation':m}, function(data) {
		location.reload();	
	});
});


/* ITEM EDITOR FUNCTIONS & EVENTS */

//Save Changes to Item
var confrmChanges;
$(document).on('click', '.item-edit-save', function() {
	var f = $('.item-edit-changed'),
		b = $(this),
		confrm = b.hasClass('are-you-sure'),
		saving = b.hasClass('saving-item-changes');
	
	if(f.length > 0) {
		if(!confrm && !saving) {
			b.switchClass('btn-default', 'btn-warning');
			b.addClass('are-you-sure');
			b.empty().append('Are you sure?');
			confrmChanges = setTimeout(function() {
				b.switchClass('btn-warning','btn-default');
				b.removeClass('are-you-sure');
				b.empty().append('<i class="fas fa-save color-info-400"></i> Save');
			},3000);
		} else {
			clearTimeout(confrmChanges);
			b.switchClass('btn-warning', 'btn-info');
			b.removeClass('are-you-sure');
			b.addClass('saving-item-changes');
			b.empty().append('<i class="fas fa-draw-circle fa-spin"></i> Saving ...');
			var itemData = {};
			
			$.each(f, function() {
				var e = $(this),
					id = e.data('id'),
					key = e.data('key'),
					t = e.data('table'),
					f = e.data('field'),
					v = e.val();
				if(e.hasClass('custom-control-input')) {
					v = (e.is(":checked") ? 1:0);
				}
				if(e.is("select")) {
					v = $(e, 'option:selected').val();
				}
				if(!itemData[t]) { itemData[t] = {'fields':{},'key':key,'id':id}; }
				itemData[t]['fields'][f] = v;
			});
			
			$.post('autoload.php', { 'edit-item':true, 'data':itemData }, function(data) {
				b.switchClass('btn-info', 'btn-default');
				b.removeClass('saving-item-changes');
				setTimeout(function() {
					b.empty().append('<i class="fas fa-save color-info-400"></i> Save');
				},1000);
				$('.item-edit-changed').removeClass('item-edit-changed');
				$('.item-edit-changed-label').removeClass('item-edit-changed-label');
				$('.item-edit-changed-label-switch').removeClass('item-edit-changed-label-switch');
				console.log(data);
			}, 'json');
		}
	
	} else {
		b.removeClass('animated shake').addClass('animated shake');
		
	}
});


//Tier Range Slider Value Changes
$(document).on('change', '.tier-range', function() {
	$('.tier-range-label').empty().append('Tier <strong class="text-success">'+$(this).val()+'</strong>');
});

//Item Editor Input Value Changes
$(document).on('change', '.item-edit-input', function() {
	if(!$(this).hasClass('item-edit-changed')) {
		$(this).addClass('item-edit-changed');
		if($(this).hasClass('custom-control-input')) {
			$(this).parent().find('.custom-control-label').addClass('item-edit-changed-label-switch');
		} else {
			$(this).parent().find('.input-group-text').addClass('item-edit-changed-label');
		}
	}
});


//Load Item Editor Pane
$(document).on('click', '.quick-item-search-row', function() {
	var id = $(this).data('id'),
		l = $('#item-editor-header-label'),
		c = $('#item-editor-content'),
		tb = $('.item-edit-toolbar');
	
	if(l.hasClass('hidden')) {
		l.removeClass('hidden').removeClass('animated').removeClass('fadeInRight');
		l.addClass('animated fadeInRight');
		l.empty().append('<i class="fad fa-caret-right"></i> Loading ...');
		tb.removeClass('hidden animated fadeInLeft').addClass('animated fadeInLeft');
	}
	
	c.empty().append('<p class="text-center"><div class="spinner-grow spinner-grow-lg text-primary" role="status"><span class="sr-only">Loading...</span></div></p>');
	$.post('autoload.php', {'get-edit-item':id}, function(data) {
		setTimeout(function() {
			
			c.empty().append(data);
		},700);
	});
});


//Quick Search Handler
var quicksearchtimer,
	quicksearchdata;
$(document).on('keyup', '#item-quick-search', function() {
	var s = $(this).val(),
		i = $('#quick-item-search-icon'),
		l = $('#quick-item-list');
	console.log('Keyup Detected');
	if(quicksearchtimer) { clearTimeout(quicksearchtimer) } //Clear timeout between keystrokes
	i.empty().append('<i class="fad fa-circle-notch text-success fa-spin fast-spin fs-xl"></i>');
	l.empty().append('<div class="panel-content text-center"><div class="spinner-grow spinner-grow-sm" role="status"><span class="sr-only"></span></div>&nbsp;&nbsp;&nbsp;<div class="spinner-grow spinner-grow-sm" role="status"><span class="sr-only"></span></div>&nbsp;&nbsp;&nbsp;<div class="spinner-grow spinner-grow-sm" role="status"><span class="sr-only"></span></div></div>');
	quicksearchtimer = setTimeout(function() {
		$.post('autoload.php', {'quick-item-search':s}, function(data) {
			quicksearchdata = data;
			build_quicksearch_list(data);	
		},'json');
	}, 800);
});

function build_quicksearch_list(d) {
	var i = $('#quick-item-search-icon'),
		l = $('#quick-item-list');
	i.empty().append('<span class="animated zoomIn inline"><i class="fad fa-search fa-info fs-xl"></i></span>')
	console.log(d);
	if(d.rows.length > 0) {
		l.empty().append('<p class="text-center"><small><span class="text-success">'+d.found+'</span> item(s) found - results limited to <span class="text-danger">'+d.count+'</span>.</small></p>');
		$.each(quicksearchdata.rows, function(k,v) {
			l.append(''
			+'<div class="row quick-item-search-row" data-id="'+v.id+'">'
			+'	<div class="hidden-xs-down col-sm-3 col-md-3 col-xl-2 text-center"><img class="quick-search-item-ico vm" src="img/eq2/items/'+v.icon+'.png"/></div>'
			+'	<div class="col-xs-12 col-sm-9 col-md-7 col-xl-8 text-left">'
			+'		<a href="#" class="text-primary">'+v.name+'</a> '
			+'		<small class="text-muted"><br><span class="badge badge-primary"><strong>ID</strong>: '+v.id+'</span> '+v.item_type+'</small>'
			+'	</div>'
			+'	<div class="hidden-sm-down col-md-1 col-xl-2 text-right text-primary" style="font-size:15pt;"><span class="vm"><i class="fad fa-angle-right vm"></i></span></div>'
			+'</div>');

		});
	} else {
		l.empty().append(''
		+'<div class="col-xs-12">'
		+'	<h4 style="padding-top:50px;padding-bottom:50px;" class="text-center">'
		+'		<span class="animated bounceInDown inline"><i class="fad fa-minus-hexagon text-danger"></i></span>'
		+'		No items were found matching your search.'
		+'	</h4>'
		+'</div>');
	}
}
