$.fn.validate = function(type) {
	var obj = $(this);
	obj.keypress(function(e) {
		if (e.which==0) return true;
		var isDigit = e.which>=48 && e.which<=57;
		var isPoint = e.which==46 || e.which==44;
		var isBack = e.which == 8;
		if (type=='int' && isPoint) return false;
		if (!isDigit && !isPoint && !isBack) return false;
		if (isPoint && obj.attr('value').indexOf('.')!=-1) return false;	
	}).keyup(function(e) {
		var v = obj.attr('value');
		v = v.replace(',','.');
		obj.attr('value', v);
		calculate();
	});
}

/**********************************************************************************************************/
function getArray(id, html) {
	var t = $('#' + id);
	var a = [];
	t.find('tr').not(':first').each(function() {
		var aa = [];
		$(this).find('td').each(function() {
			if (!html) aa.push($(this).text().replace('  ', ' '));
			else aa.push($(this).html())
		});
		a.push(aa);
	});
	return a;
}

function find(a, s) {
	for (var i = 0; i < a.length; i++) {
		if (a[i]==s) return true;
	}
	return false;
}

function noDbl(p,  n) {
	var a = [];
	for (var i = 0; i < p.length; i++) {
		if (!find(a, p[i][n])) {
			a.push(p[i][n]);
		}
	}
	return a;
}

function filter(a, n, v) {
	var aa = [];
	for (var i = 0; i < a.length; i++) {
		if (a[i][n] == v) {
			aa.push(a[i]);
		}
	}
	return aa;
}

function optionStr(a, n, m) {
	var s = '';
	for (var i = 0; i < a.length; i++) {
		s += '<option value="' + a[i][n] + '">' + a[i][m] + '</option>';
	}
	return s;
}


function getCol(a, n) {
	var aa = [];
	for (var i = 0; i < a.length; i++) {
		aa.push(a[i][n]);
	}
	return aa;
}

/***************************************************************************************************************************/

var brands = [];
var textures = [];
var others = [];

var brand = null;
var texture = null;

function calculate() {
	var sum = 0;
	var maxW = parseFloat($('#maxW').attr('value'));
	if (!maxW) maxW = 0;
	var maxL = parseFloat($('#maxL').attr('value'));
	if (!maxL) maxL = 0;
	var angles = parseInt($('#angles').attr('value'));
	var lamp = parseInt($('#lamp').attr('value'));
	var wiring = parseInt($('#wiring').attr('value'));
	var round = parseInt($('#round').attr('value'));
	var chandelier_hook = parseInt($('#chandelier_hook').attr('value'));
	var chandelier = parseInt($('#chandelier').attr('value'));
	
	var others_loc = filter(others, 0, brand)[0];
	var textures_loc = filter(textures, 0, texture)[0];
	var brands_loc = filter(brands, 0, brand)[0];
	if (angles > 4) sum += parseFloat(others_loc[1]);
	if (!lamp) lamp = 0;
	sum += parseFloat(others_loc[2]) * lamp;
	if (!wiring) wiring = 0;
	sum += wiring * others_loc[3];
	if (!round) round = 0;
	sum += round * others_loc[4];
	if (!chandelier_hook) chandelier_hook = 0;
	sum += chandelier_hook * others_loc[5];
	if (!chandelier) chandelier = 0;
	sum += chandelier * others_loc[6];
	var price = 0;
	var s = 0;
	if (maxL && maxL) {
		price += Math.ceil((maxW + maxL)*2/parseFloat(brands_loc[2].replace(/\,/g,'.')))*parseFloat(brands_loc[3]);
		s = maxW * maxL;
		maxW += 0.2;
		maxL += 0.2;
		price += maxL * maxW * textures_loc[4];
		
		if (brand==1) {
			
			if (s < 8) {
				
				price += price * 0.5;
			} else if (s >= 8 && s < 16) {
				price += price * 0.35;	
			} else {
				price += price * 0.25;		
			}
		} else {
			price += 8 * s;	
		}
	}
	sum += price;
	sum = sum.toFixed(2) + ' ' + brands_loc[4];
	$('#rezult').html(sum);
}

function init() {
	brands = getArray('brands');
	textures = getArray('textures');
	others = getArray('others');
	
	$('#brand').html(optionStr(brands, 0, 1)).change(function() {
		brand = $(this).attr('value');
		var data = filter(textures, 1, brand);
		$('#texture').html(optionStr(data, 0, 2)).change();
	}).change();
	
	$('#texture').change(function() {
		texture = $(this).attr('value');
		calculate();
	}).change();
	
	$('#maxW').validate('float');
	$('#maxL').validate('float');
	$('#angles').validate('int');
	$('#wiring').validate('int');
	$('#round').validate('int');
	$('#chandelier_hook').validate('int');
	$('#chandelier').validate('int');
	$('#lamp').validate('int');
}

$(document).ready(function() {
	init();
});