var g_loaded = false;

function removeFromCart(form, product_id){
	var element = form + "[quantity_" + product_id + "]";
	document.forms[form].elements[element].value = 0;
	
	document.forms[form].submit()
}

function formatPrice(number){
	var decimals = 2;
	if (isNaN(number)) return 0;
	if (number == '') return 0;
	
	var snum = new String(number);
	var whole = parseFloat(snum);
	var result = '';
	
	result = new String(Math.round(whole));
	withSpaces = new String();
	// put spaces inside
	for(x = 0; x + 3 < result.length; x += 3)
		withSpaces = "." + result.substring(result.length - x - 3, result.length - x) + withSpaces;
	withSpaces = result.substring(0, result.length - x) + withSpaces;
	
	result = withSpaces;
	
	if(Math.round(whole) != whole){
		decimal = Math.round(whole * Math.pow(10, decimals));
		decimal = decimal % 100;
		result += "," + (decimal < 10 ? "0" : "") + new String(decimal);
	}
	
	return result;
}

function recalculateCart(form, product_id){
	var element = form + "[quantity_" + product_id + "]";
	
	var quantity = parseInt(document.forms[form].elements[element].value);
	if(!quantity || quantity < 0) quantity = 0;
	
	var price = document.forms[form].elements["price_"+product_id].value;
	
	// older template
	if(document.forms[form].elements["add_vat"]){
	
		// boolean
		var add_vat = document.forms[form].elements["add_vat"].value;
		// VAT value, eg. 19
		var vat = parseFloat(document.forms[form].elements["vat"].value);
		
		var multiplier;
		if(add_vat) multiplier = (100 + vat)/100;
		else multiplier = 1;
		
		var total = quantity * price * multiplier;
		
		var previous = document.forms[form].elements["total_"+product_id].value;
		var difference = total - previous;
		
		if(add_vat) difference_secondary = difference / multiplier;
		else difference_secondary = difference * ((100.0 + vat)/100);
		
		// update product price
		document.forms[form].elements["total_"+product_id].value = total;
		document.getElementById("total_"+product_id+"_span").firstChild.nodeValue = formatPrice(total);
		
		// update total prices
		updateTotal(form, difference, "total_goods");
		updateTotal(form, difference_secondary, "total_goods_secondary");
	
	// new template
	} else {

		var total = quantity * price;
		var previous = document.forms[form].elements["total_"+product_id].value;
		var difference = total - previous;
		document.forms[form].elements["total_"+product_id].value = total;
		document.getElementById("total_"+product_id+"_span").firstChild.nodeValue = formatPrice(total);
		updateTotal(form, difference, "total_goods");
		
		// no-vat prices also
		if(document.forms[form].elements["price_no_vat_"+product_id]){
			price = document.forms[form].elements["price_no_vat_"+product_id].value;
			total = quantity * price;
			previous = document.forms[form].elements["total_no_vat_"+product_id].value;
			difference = total - previous;
			document.forms[form].elements["total_no_vat_"+product_id].value = total;
			document.getElementById("total_no_vat_"+product_id+"_span").firstChild.nodeValue = formatPrice(total);
			updateTotal(form, difference, "total_goods_secondary");
		}
	}
}

function getItem(list, index){
	if(list[index]) return list[index]; // because of konqueror
	else return list.item(index);
}

function changeFreight(form){
	var f = document.forms[form].elements[form + "[freight]"];
	var p = document.forms[form].elements[form + "[payment]"];
	// selected value, associated object
	var v, o;
	for(x = 0; x < f.length; x++) if(getItem(f, x).checked) v = getItem(f, x).value;
	for(x = 0; x < freight.length; x++) if(freight[x].id == v) o = freight[x];
	
	if(o){
		var firstAvail = -1;
		var selectedDisabled = false;
		
		for(x = 0; x < p.length; x++){
			
			var a = false;
			for(y = 0; y < o.available.length; y++){
				if(o.available[y] == getItem(p, x).value) a = true;
			}
			
			// is available
			if(a){
				if(firstAvail == -1) firstAvail = x;
				getItem(p, x).disabled = false;
				$(getItem(p, x)).siblings("label").removeClass("disabled");
				
			// else	
			} else {
				if(getItem(p, x).checked) selectedDisabled = true;
				getItem(p, x).disabled = true;
				$(getItem(p, x)).siblings("label").addClass("disabled");
			}
		}
		if(firstAvail != -1 && selectedDisabled){
			var select = $(getItem(p, firstAvail));
			select.attr('checked', true);
			select.change();
		}
	}
}

function changePayment(form){
	var p = document.forms[form].elements[form + "[payment]"];
	// selected value, associated object
	var v, o;
	for(x = 0; x < p.length; x++) if(getItem(p, x).checked) v = getItem(p, x).value;
	for(x = 0; x < payment.length; x++) if(payment[x].id == v) o = payment[x];
}

function updateTotal(form, difference, element){
	if(document.forms[form].elements[element]){
		var price = parseFloat(document.forms[form].elements[element].value) + difference;
		document.forms[form].elements[element].value = price;
		document.getElementById(element+"_span").firstChild.nodeValue = formatPrice(price);
	}
}


function Variants(data, selected){
	
	this.data = data;
	this.selected = selected;
	
	this.show = function(name, parent){
		var element;
		if(parent == null) element = $("."+name);
		else element = parent.find("."+name);
		
		element.each(function(){
			if(this.firstChild.nodeType == 8){ // document.COMMENT_NODE or Node.COMMENT_NODE
				$(this).html(this.firstChild.nodeValue);
			}
		});
		
		element.removeClass("display_none");
	}
	
	this.hide = function(name, parent){
		var element;
		if(parent == null) element = $("."+name);
		else element = parent.find("."+name);
		
		element.each(function(){
			if(this.firstChild.nodeType != 8){ // document.COMMENT_NODE or Node.COMMENT_NODE
				$(this).wrapInner(document.createComment(""));
			}
		});
		
		element.addClass("display_none");
	}
	
	this.load = function(data){
		// not existent combination
		if(data == null){
			$("ul.product-price").css("visibility", "hidden");
			this.hide("sale");
			this.show("no-sale");
			
		// ok - show the data
		} else {
			var list = $("ul.product-price");
			
			list.css("visibility", "visible");
			
			if(data.no_sale){
				this.hide("sale");
				this.show("no-sale");
			} else {
				this.show("sale");
				if($("div.sale span.spinner").length == 0) $("div.sale [jg=spin]").spin();
				this.hide("no-sale");
			}
			
			if(data.price_before){
				this.hide("not-discounted", list);
				this.show("discounted", list);
			} else {
				this.show("not-discounted", list);
				this.hide("discounted", list);
			}
			
			list.find("span.product-price").html(data.price);
			list.find("span.product-price-before").html(data.price_before);
			list.find("span.product-price-secondary").html(data.price_secondary);
			
			if(data.discount_until){
				this.show("discount-until", list);
				list.find("span.product-discount-until").html(data.discount_until);
			} else this.hide("discount-until", list);
			
			if(data.price_original){
				this.show("price-original", list);
				list.find("span#product-price-original").html(data.price_original);
			} else this.hide("price-original", list);
			
			if(data.availability){
				this.show("availability", list);
				list.find("span#product-availability").html(data.availability);
			} else this.hide("availability", list);
			
			if(data.catalog_number){
				this.show("catalog-number", list);
				list.find("span#product-catalog-number").html(data.catalog_number);
			} else this.hide("catalog-number", list);
			
			list.find("span#product-attr1").html(data.attr1);
			list.find("span#product-attr2").html(data.attr2);
			
			$("span.product-variant").html(data.name);
			
			$("div.product-level-1 div.product-level-image").css("backgroundImage", data.param1_image ? "url('"+data.param1_image+"')" : "none");
			$("div.product-level-2 div.product-level-image").css("backgroundImage", data.param2_image ? "url('"+data.param2_image+"')" : "none");
			
			if(data.image){ 
				var image = $("div.product-image img");
				image.attr("src", data.image);
				image.attr("width", data.image_width);
				image.attr("height", data.image_height);
			}
		}
	}
	
	this.init = function(){
		with({variants : this}){
			$("ul.product-level li a")
			.add("div.product ul.level li a").hover(
				// in
				function(){
					// get id in form of "subproduct_x"
					var id = $(this).get(0).id;
					if(id) id = id.split("_");
					if(id[0] == "subproduct"){
						id = id[1];
						if(id == 0) variants.load(null);
						else variants.load(variants.data[id]);
					}
				},
				// out
				function(){
					variants.load(variants.data[variants.selected]);
				}
			);
		}
	}
	this.init();
}