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



/* ICOPICKER HANDLING & EVENTS */
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 */

//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');
	
	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 ...');
	}
	
	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="col-hidden-xs col-sm-3 col-md-3 col-lg-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-lg-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="col-hidden-xs col-hidden-sm col-md-1 col-lg-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>');
	}
}
