$(document).ready(function() {
	setTime();
	var tmp = getProductList($('[name="record"]').val());
	InitializeEcmProducts(tmp);
});

function setTime() {
	var time = $("#mojtime").html();
	var hours = 0;
	var minutes = 0;
	var seconds = 0;
	time = parseInt(time);
	minutes = Math.floor(time / 60);
	hours = Math.floor(minutes / 60);
	seconds = time - (minutes * 60);
	minutes = minutes - (hours * 60);
	if(hours<10){
		hours = "0"+hours;
	}
	if(minutes<10){
		minutes = "0"+minutes;
	}
	if(seconds<10){
		seconds = "0"+seconds;
	}
	$("#mojtime").html("" + hours +"."+ minutes +":"+ seconds);
}

function getProductList(request) {
	var tmp = null;
	$.ajax({
		type: "POST",
		url: "index.php?module=EcmActions&action=dbpost&to_pdf=1",
		dataType: "json",
		async: false,
		data: {
			job: "getProductList",
			id: request,
		},
		success: function (data) {
			tmp = data;
		}
	});
	return tmp;
}

function InitializeEcmProducts(tmp) {
	$('#tableEcmProducts').appendGrid({
		hideRowNumColumn: false,
		initData: tmp,
		initRows: 0,
		columns: [
			{ 
				name: 'code',
				display: 'Indeks',
				type: 'custom',
				ctrlClass: 'inputs',
				ctrlAttr : {readonly : 'readonly'},
				displayCss: {
					'width': '20%',
					'vertical-align': 'middle',
					'text-align': 'center',
					'background-color': 'rgb(224,240,255)',
					'border': '1px solid rgb(48,192,255)',
					'height': '0px',
					'font-family': 'Arial',
					'font-size': '12px',
					'color': 'black',
				},
				value: ['', '', ''],
                customBuilder: function (parent, idPrefix, name, uniqueIndex) {
                    // Prepare the control ID/name by using idPrefix, column name and uniqueIndex
                    var ctrlId = idPrefix + '_' + name + '_span_' + uniqueIndex;
                    var ctrl = document.createElement('span');
                    $(ctrl).attr({ id: ctrlId, name: ctrlId }).appendTo(parent);
                    return ctrl;
                },
                customGetter: function (idPrefix, name, uniqueIndex) {
                    return '0';
                },
                customSetter: function (idPrefix, name, uniqueIndex, value) {
                    var spanId = idPrefix + '_' + name + '_span_' + uniqueIndex;
					var spanUrl = document.createElement('a');
					spanUrl.href = 'index.php?module=EcmProducts&return_module=EcmProducts&action=DetailView&record=' + value['id'];
					spanUrl.onclick = function (){
						window.open('index.php?module=EcmProducts&return_module=EcmProducts&action=DetailView&record=' + value['id'],'1426150651552','width=700,height=500,toolbar=0,menubar=0,location=0,status=0,scrollbars=1,resizable=1,left=0,top=0');
						return false;
					};
					spanUrl.title = value['code'];
					spanUrl.textContent = value['code'];
					$('#' + spanId).html(spanUrl);
                }
            },{ 
				name: 'name',
				display: 'Nazwa',
				type: 'custom',
				ctrlClass: 'inputs',
				ctrlAttr : {readonly : 'readonly'},
				displayCss: {
				//	'width': '30%',
					'vertical-align': 'middle',
					'text-align': 'center',
					'background-color': 'rgb(224,240,255)',
					'border': '1px solid rgb(48,192,255)',
					'height': '0px',
					'font-family': 'Arial',
					'font-size': '12px',
					'color': 'black',
				},
				value: ['', '', ''],
                customBuilder: function (parent, idPrefix, name, uniqueIndex) {
                    // Prepare the control ID/name by using idPrefix, column name and uniqueIndex
                    var ctrlId = idPrefix + '_' + name + '_span_' + uniqueIndex;
                    var ctrl = document.createElement('span');
                    $(ctrl).attr({ id: ctrlId, name: ctrlId }).appendTo(parent);
                    return ctrl;
                },
                customGetter: function (idPrefix, name, uniqueIndex) {
                    return '0';
                },
                customSetter: function (idPrefix, name, uniqueIndex, value) {
                    var spanId = idPrefix + '_' + name + '_span_' + uniqueIndex;
					var spanUrl = document.createElement('a');
					spanUrl.href = 'index.php?module=EcmProducts&return_module=EcmProducts&action=DetailView&record=' + value['id'];
					spanUrl.onclick = function (){
						window.open('index.php?module=EcmProducts&return_module=EcmProducts&action=DetailView&record=' + value['id'],'1426150651552','width=700,height=500,toolbar=0,menubar=0,location=0,status=0,scrollbars=1,resizable=1,left=0,top=0');
						return false;
					};
					spanUrl.display = true;
					if (typeof value['name'] === "undefined") {
						spanUrl.textContent = '';
					}else{
						spanUrl.textContent = value['name'].replace('&quot;','"');
					}
					spanUrl.title = value['name'];
					$('#' + spanId).html(spanUrl);
                }
            },
		],
		i18n: {
			rowEmpty: 'Brak rekordów'
		},
		hideButtons: {
			removeLast: true,
			append: true,
			insert: true,
			remove : true,
			moveUp: true,
			moveDown: true
		}
	});
	$('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');
}