$(document).ready(function () {
	setStructDiv();
	InitializeTable();
});

function setStructDiv(){
	var div = $('#structdiv');
	var konto = $('#account_code').text() + '';
	var lista = $.parseJSON($("input[name='structureanalyticalpart_list']").val());
	var html = '';
	html = html + '<table style="border-collapse: collapse ; border: 0px solid black;width:10px"><tr>';
	html = html + '<td style="border-collapse: collapse ; border: 1px solid black;width="10">&nbsp;&nbsp;'+ konto.substring(1,2) +'&nbsp;&nbsp</td>';
	html = html + '<td style="border-collapse: collapse ; border: 1px solid black;width="10">&nbsp;&nbsp;'+ konto.substring(2,3) +'&nbsp;&nbsp</td>';
	html = html + '<td style="border-collapse: collapse ; border: 1px solid black;width="10">&nbsp;&nbsp;'+ konto.substring(3,4) +'&nbsp;&nbsp</td>';
	html = html +'<td style="border-collapse: collapse ; border: 0px solid black;width="10">&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;</td>';
	if (lista != '[]')
		$.each(lista, function (idx, obj){
			for(var i=0; i < obj.value ; i++)
			html = html +'<td style="border-collapse: collapse ; border: 1px solid black;width="10">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp</td>';
			if(idx!=lista.length-1)
				html = html +'<td style="border-collapse: collapse ; border: 0px solid black;width="10">&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;</td>';
		});
	
	html = html + '</tr></table>';
	div.html(html);
}

function InitializeTable() {
	$('#tableAnalyticalAccounts').appendGrid({
		hideRowNumColumn: true,
		initRows: 0,
		columns: [
			{
				name: 'code',
				display: 'Konto',
				type: 'text',
				ctrlClass: 'inputs',
				ctrlAttr : {readonly : 'readonly'},
				displayCss: {'width': '30%', 'vertical-align': 'middle'}
			}, {
				name: 'name',
				display: 'Nazwa',
				type: 'text',
				ctrlClass: 'inputs',
				ctrlAttr : {readonly : 'readonly'},
				displayCss: {'width': '65%', 'vertical-align': 'middle'}
			}, ],
		i18n: {
			rowEmpty: 'Brak rekordów'
		},
		hideButtons: {
			removeLast: true,
			 append: true,
			insert: true,
			 remove : true,
			moveUp: true,
			moveDown: true
		}
	});
	LoadDataFromAnalyticalAccountsList();
	$('thead').removeClass('ui-widget-header');
	$('td').removeClass('ui-widget-header');
	$('tbody').removeClass('ui-widget-content');
	$('td').removeClass('ui-widget-content');
	$('tfoot').removeClass('ui-widget-header');
}

function LoadDataFromAnalyticalAccountsList() {
	var action_list = $.parseJSON($("input[name='analyticalaccounts_list']").val());
	if (action_list.length > 0) {
		$.each(action_list, function(idx2, obj2) {
				$('#tableAnalyticalAccounts').appendGrid('insertRow', [ {
					code : obj2.code,
					name : obj2.name,	
				} ], 0);
			console.log(obj2.code);
		});
	}
}