function Sestava(id, name) {
	this.id = id;
	this.name = name;
	this.components = new Array();
	this.boxes = new Array();
	this.groups = new Array();
	this.actual = new Array();
	this.variations = new Array();
	this.disabledComponents = new Array();
	this.actualVariation = '';	// aktualni varianta
	this.defaultVar = '';				// defaultni varianta
	this.actualPrice = 0;				// aktualni cena bez DPH
	this.actualPriceTax = 0;		// aktualni cena s DPH
	this.preview = 1;						// flag urcujici zda je zobrazen nahled
	this.actualId = -1;					// id nastavovane komponenty
	this.previousId = -1;				// id predchozi nastavovane komponenty
	this.actualBoxId = -1;			// id aktualne nastavovaneho boxu

}

function component_add(comp) {
	this.components[comp.id] = comp;
}

Sestava.prototype.addComponent = component_add;

function box_add(boxId, boxName, boxMulti, boxItems) {
	this.boxes[boxId] = new Array();
	this.boxes[boxId]["name"] = boxName;
	this.boxes[boxId]["id"] = boxId;
	this.boxes[boxId]["multi"] = boxMulti;
	this.boxes[boxId]["items"] = new Array();
	if (boxItems != '')	{
		this.boxes[boxId]["items"] = boxItems.split(";");
	}
}

Sestava.prototype.addBox = box_add;

function group_add(grpId, grpName, grpItems) {
	this.groups[grpId] = new Array();
	this.groups[grpId]["name"] = grpName;
	this.groups[grpId]["id"] = grpId;
	this.groups[grpId]["items"] = new Array();
	if (grpItems != '')	{
		this.groups[grpId]["items"] = grpItems.split(";");
	}
}

Sestava.prototype.addGroup = group_add;

function var_add(variation) {
	this.variations[variation.id] = variation;
}

Sestava.prototype.addVariation = var_add;

function var_addnew(varId, varName, def) {
	newVar = new Variation(varId, varName, def);
}

Sestava.prototype.addNewVariation = var_addnew;

function box_recalc(box, id) {
	for (boxItem in this.boxes[box]["items"])
	{
		var compId = this.boxes[box]["items"][boxItem];
		var eCompId = compId;
		if (eCompId < 0) { eCompId = 'integr'}
		var basePrice = this.components[id].price;
		var elem = document.getElementById(box+eCompId+'_price');
		if (compId != id)	{
			var newPrice = (this.components[compId].price - basePrice).toFixed(2);
			if (newPrice > 0)	{ newPrice = '+'+newPrice; }
			elem.innerHTML =  number_format(newPrice, 0, "", " ") + ' Kč';
		} else {
			elem.innerHTML = '';
		}
	}
}

Sestava.prototype.recalcBoxPrices = box_recalc;

function variation_set(varId) {
	window.status = 'Nastavuji sestavu ... ';

	this.actualVariation = varId;
	this.actual = new Array();

	this.unsetMulti();
	
	var noItems = new Array();			// polozky, ktere jiz nejsou v sestave dostupne (box => id)

	for (compItem in this.variations[varId].items) {
		for (compIt in this.variations[varId].items[compItem]["checked"]) {
			var compId = this.variations[varId].items[compItem]["checked"][compIt];
			if (this.components[compId] == null) {
				noItems[compItem] = compId;
			} else {
				selComp(compItem, compId, 1);
			}
		}
		for (compIt in this.variations[varId].items[compItem]["disabled"]) {
			var compId = this.variations[varId].items[compItem]["disabled"][compIt];
			this.disabledComponents.push(compId);
			this.components[compId].disabled = true;
		}
	}
	
	// oznaci polozky u zatim neoznacenych
	this.setUnmarked();

	// nastavi ceny u boxu ktere jsou multi
	this.setMultiPrices();

	// prepocita celkovou cenu
	this.recalcPrices();
	
	// opravi nahled
	//if (this.preview == 1) { this.displayDetails(); }
	window.status = 'Nastavuji sestavu ... OK';
	window.setTimeout('window.status = "";' , 450);
    
	return false;
}

Sestava.prototype.setVariation = variation_set;

function variation_setdefault() {
	if (this.actualVariation != '') {
		this.setVariation(this.actualVariation);
		return;
	}
	for (varId in this.variations) {
		if (this.variations[varId].def == 1)	{
			this.setVariation(varId);
			return;
		}
	}
}

Sestava.prototype.setDefaultVariation = variation_setdefault;

function set_unmarked() {
	for (boxId in this.boxes)	{
		if (this.boxes[boxId].multi == 1) {
			document.getElementById(boxId+'_info').innerHTML = this.boxes[boxId].name;
		}

		if (this.actual[boxId] == undefined && this.boxes[boxId].multi == 0)	{
			// neni zaskrtnuta zadna volba
			var elem = document.getElementById(boxId+'integr');
			if (elem == undefined || elem.disabled)	{
				// neexistuje integrovana varianta
				selComp(boxId, this.boxes[boxId]["items"][0], 1);
			} else {
				// existuje integrovana varianta
				selComp(boxId, this.boxes[boxId]["items"][this.boxes[boxId]["items"].length-1], 1);
			}
		} else {
			// kontrola jestli neni neco zaskrtleho disableovano
		}
	}
}

Sestava.prototype.setUnmarked = set_unmarked;

function set_multiprices() {
	for (boxId in this.boxes)	{
		if (this.boxes[boxId].multi == 1)	{
			for (itemId in this.boxes[boxId]["items"]) {
				var compId = this.boxes[boxId]["items"][itemId];
				var elem = document.getElementById(boxId+compId+'_price');
				elem.innerHTML = number_format(Number(this.components[compId].price).toFixed(2), 0, "", " ") + ' Kč';
			}
		}
	}
}

Sestava.prototype.setMultiPrices = set_multiprices;

function recalc_prices() {
	this.actualPrice = 0;
	this.actualPriceTax = 0;
	for (boxId in this.boxes) {
		if (this.boxes[boxId].multi == 0)	{
			var compId = this.actual[boxId];
			this.actualPrice += this.components[compId].price;
			this.actualPriceTax += (this.components[compId].price*((100+this.components[compId].tax)/100));
		} else {
			for (compItem in this.boxes[boxId].items) {
				var compId = this.boxes[boxId].items[compItem];
				if (document.getElementById(boxId+compId).checked) {
					this.actualPrice += this.components[compId].price;
					this.actualPriceTax += (this.components[compId].price*((100+this.components[compId].tax)/100));
				}
			}
		}
	}
	this.setOverallPrices();
}

Sestava.prototype.recalcPrices = recalc_prices;

function set_overallprices() {
	document.getElementById('price_small').innerHTML = number_format(Number(this.actualPrice).toFixed(2), 0, "", " ");
	document.getElementById('price_tax_small').innerHTML = number_format(Number(Math.round(this.actualPriceTax)).toFixed(2), 0, "", " ");
	
	document.getElementById('price_tax').innerHTML = number_format(Number(Math.round(this.actualPriceTax)).toFixed(2), 0, "", " ");
	document.getElementById('konf_price').innerHTML = number_format(Number(Math.round(this.actualPriceTax)).toFixed(2), 0, "", " ") + " Kč";
	//recalcPrice();
}

Sestava.prototype.setOverallPrices = set_overallprices;

function unset_multi() {
	for (boxId in this.boxes)	{
		if (this.boxes[boxId].multi == 1)	{
			for (compItem in this.boxes[boxId].items) {
				var compId = this.boxes[boxId].items[compItem];
                if (document.getElementById(boxId+compId)) {
  				  document.getElementById(boxId+compId).checked = false;
                }
			}
		}
	}
}

Sestava.prototype.unsetMulti = unset_multi;

/* --------------------------- Komponenty ------------------------------------- */

function Component(name, id, code, url, price, tax) {
	this.name = name;
	this.id = id;
	this.code = code;
	this.url = url;
	this.price = parseFloat(price);
	this.tax = parseFloat(tax);
	this.disabled = false;
}

/* --------------------------- Variace ------------------------------------- */

function Variation (name, id, def) {
	this.name = name;
	this.id = id;
	this.def = def;
	this.items = new Array();
}

function add_varItem(id, box, type) {
	if (this.items[box] == undefined) {
		this.items[box] = new Array();
	}
	if (this.items[box][type] == undefined) {
		this.items[box][type] = new Array();
	}
	this.items[box][type].push(id);
}

Variation.prototype.addItem = add_varItem;

/* --------------------------- Ostatni fce ------------------------------------- */

function collapseItem(code) {
  var elem = document.getElementById(code+'_box');
  if (elem.className == 'konf_box') {
    elem.className = 'konf_box box_hide';
  } else {
    elem.className = 'konf_box';
  }
}

function collapseAll(akce) {
	if (akce == 'hide' || akce == 'peri')	{
		var status = 'konf_box box_hide';
	} else {
		var status = 'konf_box';
	}

	var hide = true;

	for (boxId in myPC.boxes)	{
		//if (boxId == 'MOU' && akce == 'peri') {
		//	var hide = false;
		//}
		if (hide == true) {
          if (document.getElementById(boxId+'_box')) {
			document.getElementById(boxId+'_box').className = status;
          }
		}
	}
}


function selComp(box, id, def) {
	myPC.actualId = id;
	myPC.actualBoxId = box;
	var prevSelId = myPC.actual[box];
	if (prevSelId == undefined) { myPC.previousId = -1; } else { myPC.previousId = prevSelId; }
	if (myPC.boxes[box].ro == 1 && def != 1)	{
		if (prevSelId != undefined) {	selComp(box, prevSelId, 1);	}
		return false;
	}

	//if (myPC.components[id].disabled) { selComp(box, prevSelId, 1); return false; }

	if (myPC.actual[box] != undefined) {
		var prevComp = myPC.components[prevSelId];
		/*
		for (var i=0; i<prevComp.integrated.length; i++) {
			var boxId = prevComp.integrated[i];
			if(in_array(boxId, myPC.components[id].integrated) == false) { myPC.disableIntegr(boxId); }
		}
		*/
	}

	myPC.actual[box] = id;
	
	var inputId = id;
	if (id < 0)	{	inputId = 'integr';	}

	if (myPC.boxes[box].multi == 0 || def == 1) { document.getElementById(box+inputId).checked = true; }
	if (myPC.boxes[box].multi != 1)	{
		document.getElementById(box+'_info').innerHTML = myPC.components[id].name;
	} else {
		document.getElementById(box+'_info').innerHTML = myPC.boxes[box].name;
	}

	// nastavit neoznacene veci na integrovane pokud je to mozne, nebo na prvni polozku
	if (def != 1) { myPC.setUnmarked(); }

	// prepocitat ceny u boxu
	if (myPC.boxes[box].multi == 0)	{ myPC.recalcBoxPrices(box, id); }

	// spocitat novou cenu u sestavy
	if (def != 1)	{ 
	  myPC.recalcPrices();
	  //myPC.displayDetails();
	}

	myPC.actualId = -1;
	myPC.previousId = -1;
	myPC.actualBoxId = -1;
}


function recalcPrice() {
	/*
	var elem = document.getElementById('basketQty');
	if (elem.value.match(/^[0-9]+$/gim) == null) {
		elem.value = "";
		var prodCount = 0;
	} else {
		var prodCount = elem.value;
		if (prodCount > 999) { prodCount = 999; elem.value = '999'; }
	}
	*/
	
	/*
	var recFeeSpan = document.getElementById('recFee');
	var recFeeVatSpan = document.getElementById('recFeeVat');

	if (recFeeSpan != undefined && recFeeVatSpan != undefined) {
		var recFee = parseInt(recFeeSpan.innerHTML);
		var recFeeVat = parseInt(recFeeVatSpan.innerHTML);
	} else {
		var recFee = 0;
		var recFeeVat = 0;
	}
	*/

	//document.getElementById('prodCountTitle').innerHTML = prodCount;

	//var price = parseInt(document.getElementById('foxPriceTop').innerHTML.replace(/ /g, ""));
	//var priceVat = parseInt(document.getElementById('foxPriceTaxTop').innerHTML.replace(/ /g, ""));

	//document.getElementById('totalPrice').innerHTML = prodCount*(parseInt(price)+parseInt(recFee));
	//document.getElementById('totalPriceVat').innerHTML = prodCount*(priceVat+recFeeVat);
}

function toBasket() {
	var formular = document.getElementById('konf_form');
	formular.target = '_self';

	var hiddenBasket = document.getElementById('hiddenBasket');

	var kusu = document.getElementById('basket1').value*1;
	var price = document.getElementById('price_small').innerHTML*1;
    
    var newInput = document.createElement('input');
	newInput.type = 'hidden';
	newInput.name = 'konfigurace';
	newInput.value = 'ano';
	hiddenBasket.appendChild(newInput);
	
	var newInput = document.createElement('input');
	newInput.type = 'hidden';
	newInput.name = 'kusu';
	newInput.value = kusu;
	hiddenBasket.appendChild(newInput);
	
	var newInput = document.createElement('input');
	newInput.type = 'hidden';
	newInput.name = 'price';
	newInput.value = price;
	hiddenBasket.appendChild(newInput);

	formular.submit();
}