/////////////////////////////////////////////////////////////////////////////////////////

var queryeditor_selected_id = 0;

/////////////////////////////////////////////////////////////////////////////////////////

function queryeditor_before_popup1()
{
	var ht = $('.panel-card-body').css('max-height');
	ht = parseInt(ht.replace("px", "")) - 40 - 24 - 18;
	var clht = ht -  $('#qe-legend-querylist').height() - $('#qe-legend-iconlist').height();
	if(clht > 480)
		clht = 480;
	$('#querylist').css('height', clht*2/3);
	$('#qiconlist').css('height', clht*1/3);
	
	var htu = ht - $('#qe-legend-name').height() - $('#queryeditor-name').height() - $('#qe-legend-query').height() - $('#qe-legend-param').height();
	$('#qe-query').css('height', htu*2/3);
	$('#qe-param').css('height', htu*1/3);
}


/////////////////////////////////////////////////////////////////////////////////////////

function init_panel_queryeditor()
{
	$('#qe-query').val('');
	$('#qe-param').val('');
	queryeditor_selected_id = 0;
	var datastr = get_ajax_datastr("get", "query", "info");
	ajax_call(datastr, ajax_init_queryeditor, null);
}

/////////////////////////////////////////////////////////////////////////////////////////

function ajax_init_queryeditor(x, jqXHR, textStatus, params)
{
	if(x != null) {
		var cbdata = '';
		var list = '';
		var iconlist = '';
		if(x.status == 'ok') {
			var count = x.ids.length;
			for(var i = 0; i < count; i++) {
				var name = x.ids[i].name;
				var id = x.ids[i].id;
				var qicon = x.ids[i].qicon;
				var caticonidname = qicon.replace(".png", "-png");
				if(id > 1) {
					cbdata += "<li class=\"ui-selectee\" id=\"li-queryeditor-" + id  + "\">";
					cbdata += "<div class=\"handle\"><span id=\"qicon-" + id + "-" + caticonidname + "\" class=\"ui-icon grip-drag-action-button\" style=\"background-position: 0 0; background-image: url(lib/layouts/images/icons/categories/" + qicon + ")\"></span></div>";
					cbdata += name + "</li>\n";
				}
			}
			
			count = x.icons.length;
			for(var i = 0; i < count; i++) {
				var name = x.icons[i];
				var img = 'lib/layouts/images/icons/categories/' + name;
				var idname = name.replace(".png", "-png");
				iconlist += '<li id="li-qicon-' + idname + '" class="list-group-item ui-selectee" style=\"background-position: 0 0px; background-image: url(' + img + ')\">' + name + '</li>'; 
			}
		}
		$('#querylist').html(cbdata);
		$('#qiconlist').html(iconlist);
		
		var ht = 20*10;
		$('#querylist').css('max-height', ht);
		$('#qiconlist').css('max-height', ht*2/3-40);
	}
	
	if(queryeditor_selected_id != "") {
		$('#querylist' + ' li').each(function() {
			$(this).removeClass('ui-state-highlight');
			$(this).removeClass('ui-selected');
		});
		$('#li-queryeditor-' + queryeditor_selected_id).addClass('ui-state-highlight');
		$('#li-queryeditor-' + queryeditor_selected_id).addClass('ui-selected');
		setTimeout(function(){categoryEditorSelected('li-queryeditor-'+queryeditor_selected_id);}, 150); 
		showSelector('#querylist', '#li-queryeditor-' + queryeditor_selected_id);
	}
	else {
		$("#qiconlist").addClass('disabled',true);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////

function queryEditorSelected(liid)
{
	queryeditor_selected_id = liid.replace("li-queryeditor-", "");
	
	var qname = $('#'+liid).text();
	$('#queryeditor-name').val(qname);
	
	$('#qiconlist' + ' li').each(function() {
		$(this).removeClass('ui-state-highlight');
		$(this).removeClass('ui-selected');
	});
	
	var qicon = $("#"+liid).find(".grip-drag-action-button").attr("id");
	qicon = qicon.replace("qicon-"+queryeditor_selected_id+"-", "");
	
	$('#li-qicon-' + qicon).addClass('ui-state-highlight');
	$('#li-qicon-' + qicon).addClass('ui-selected');
	
	showSelector('#qiconlist', '#li-qicon-' + qicon);
	
	$("#qiconlist").removeClass('disabled',true);
	
	var datastr = get_ajax_datastr("get", "query", "info");
	datastr += "&id=" + queryeditor_selected_id;
	ajax_call(datastr, ajax_selected_queryeditor, null);
}

/////////////////////////////////////////////////////////////////////////////////////////

function ajax_selected_queryeditor(x, jqXHR, textStatus, params)
{
	if(x != null) {
		var cbdata = '';
		var list = '';
		var iconlist = '';
		if(x.status == 'ok') {
			var query = x.data['query'];
			var params = x.data['params'];
			$('#qe-query').val(query);
			$('#qe-param').val(params);
		}
	}
}

/////////////////////////////////////////////////////////////////////////////////////////

function queryeditor_submit()
{
	var iconid = "";
	$('#qiconlist' + ' li').each(function() {
		if($(this).hasClass('ui-selected')) {
			iconid = $(this).attr('id');
			iconid = iconid.replace("li-qicon-", "");
			iconid = iconid.replace("-png", ".png");
		}
	});
	
	var aqids = [];
	$('#querylist' + ' li').each(function() {
		var id = $(this).attr('id').replace("li-queryeditor-", "");
		aqids.push(id);
	});
		
	var datastr = get_ajax_datastr("set", "query", "info");
	datastr += "&id=" + queryeditor_selected_id;
	datastr += "&name=" + encodeURIComponent($('#queryeditor-name').val());
	datastr += "&icon=" + encodeURIComponent(iconid);
	datastr += "&query=" + encodeURIComponent($('#qe-query').val());
	datastr += "&params=" + encodeURIComponent($('#qe-param').val());
	datastr += "&ids=" + encodeURIComponent(aqids.join(","));
	ajax_call(datastr, ajax_submit_queryeditor, null);
}

/////////////////////////////////////////////////////////////////////////////////////////

function ajax_submit_queryeditor(x, jqXHR, textStatus, params)
{
	if(x != null) {
		var cbdata = '';
		var list = '';
		var iconlist = '';
		if(x.status == 'ok') {
			if(x.changed > 0) {
				$("#domainquerytable").trigger("reloadGrid"); 
				var icon = x.icon;
				var id = x.id;
				var liid = "li-queryeditor-" + id;
				var elem = $('#' + liid).find('span');
				var bgimage = 'url(lib/layouts/images/icons/categories/' + icon + ')';
				$(elem).css('background-image',bgimage);
			}
		}
	}
}

/////////////////////////////////////////////////////////////////////////////////////////

