$(document).ready(function() {
	var total = 0;

	$("#checkoutDialog").hide();
	$("#contactform").hide();
	$(".checkout").hide();

	$(".submit").fadeTo(1000, 0.35);
	
	$(".item:odd").css("background-color", "#F0F0F0");

	$(".slider").each(function() {
		var thisMax = parseInt($(this).text());
		$(this).empty();
		$(this).slider({
			min: 0,
			max: thisMax,
			range: "min",
			slide: function(event, ui) {
				var thisID = $(this).attr("id");
				$(this).next().next().next().text(ui.value);
				$(this).next().val(ui.value);

				var res = recalc_new(); 
				total = res["total_items"];
				
				$("h1 .total").html(res["total"]).formatCurrency({ "symbol" : "€" });
				$("h1 .discount").html(res["discount"] + "% Discount");
				$("h1 .rebate").html(res["rebate"]).formatCurrency({ "symbol" : "€" });
				
				if (total >= 10) { $(".not_enough_items").fadeOut(500, function() { $(".checkout").fadeIn(500); } ); }
				else { $(".checkout").fadeOut(500, function() { $(".not_enough_items").fadeIn(500); } ); }
				$(".items_total").html(total);
			},
			change: function(event, ui) {
				var thisID = $(this).attr("id");
				$(this).next().next().next().text(ui.value);
				$(this).next().val(ui.value);

				var res = recalc_new(); 
				total = res["total_items"];
				
				$("h1 .total").html(res["total"]).formatCurrency({ "symbol" : "€" });
				$("h1 .discount").html(res["discount"] + "% Discount");
				$("h1 .rebate").html(res["rebate"]).formatCurrency({ "symbol" : "€" });
				
				if (total >= 10) { $(".not_enough_items").fadeOut(500, function() { $(".checkout").fadeIn(500); } ); }
				else { $(".checkout").fadeOut(500, function() { $(".not_enough_items").fadeIn(500); } ); }
				$(".items_total").html(total);
			}
		});
	});

	$(".price").each(function() { $(this).formatCurrency({ "symbol" : "€" }); });
	
	$("h1 .total").html(total).formatCurrency({ "symbol" : "€" });
	$("h1 .rebate").formatCurrency({ "symbol" : "€" });
	
	$(".plus").live("click", function() { $("#" + $(this).attr("rel")).slider("value", $("#" + $(this).attr("rel")).slider("value") + 1); });
	$(".minus").live("click", function() { $("#" + $(this).attr("rel")).slider("value", $("#" + $(this).attr("rel")).slider("value") - 1); });
	
	$(".markall").live("click", function() { $(".slider").each(function() { $(this).slider("value", $(this).slider("option", "max")); }); });
	
	$(".clearall").live("click", function() {
		$("#globalwrapper").fadeTo(500, 0.5, function () {
			$(".slider").each(function() { $(this).slider("value", 0); });
		});
		$.ajax({
			url: "unset.php",
			type: "GET",
			success: function() { $("#globalwrapper").fadeTo(1000, 1); }
		});		
	});
	
	$(".checkout").live("click", function() {
		
		if ($(this).attr("rel") == "checkout") {
			$(this).attr("rel", "close");
			$("#wrapper").toggle("slide", { direction: "up" }, 1500, function() { $("#contactform").toggle("slide", {direction: "up" }, 1000); });
		}
		else if ($(this).attr("rel") == "close") {
			$(this).attr("rel", "checkout");
			$("#contactform").toggle("slide", { direction: "up" }, 1000, function() { $("#wrapper").toggle("slide", {direction: "up" }, 1500); });
		}

	});
	
	$(".required").live("change", function() {
		var check = checkContact();
		if (check) { $(".submit").fadeTo(500, 1).attr("disabled", false); }
		else { $(".submit").fadeTo(500, 0.3).attr("disabled", true); }
	});
	
	$(".submit").live("click", function() {
		$("#checkoutDialog").dialog({
			bgiframe: true,
			modal: true,
			resizable: false,
			buttons: { 'OK': function() {
					$(".itemCount").each(function() {
						if ($(this).val() == 0) {
							$(this).next().next().next().next().remove();
							$(this).remove();
						}
					});
					$("#cart").submit();
				}
			}
		});
	});
	
	$(".showavailable").live("click", function() { $(".soldout").parent().toggle("slow"); });
});

function checkContact() {
	var pass = true;
	$(".required").each(function() { if ($(this).val() == "") { pass = false; } });
	return pass;
}

function updateCalc(artid, val, price, uival) {
	var price_new = parseInt(price.substr(1) * 100 * uival);
	if ((val >= 5) && (val < 15)) { price_new = price_new * 0.9; }
	if ((val >= 15) && (val < 25)) { price_new = price_new * 0.8; }
	if (val >= 25) { price_new = price_new * 0.7; }

	var str = price_new.toString();
	var a = str.split(".");
	var output = a[0];	
	var teil1 = output.substr(0, output.length - 2);
	var teil2 = output.substr(output.length - 2);
	
	if (teil1 == "") { teil1 = "0"; }
	if (teil2 == "0") { teil2 = "00"; }
	
	return "€" + teil1 + "." + teil2;
}

function recalc_new() {
	var o = new Array();
	var t = 0;
	var titems = 0;
	var tmp_price;

	o["discount"] = 0;
	o["rebate"] = 0;
	
	$(".slider").each(function() {
		switch (parseInt($(this).next().next().next().next().next().attr("value"))) {
			case 1:
				tmp_price = 2;
				break;
			case 2:
				tmp_price = 2.9;
				break;
			case 3:
				tmp_price = 4.5;
				break;
			case 4:
				tmp_price = 2.5;
				break;
			case 5:
				tmp_price = 1;
				break;
		}
		t += tmp_price * parseInt($(this).slider("value"));
		titems += $(this).slider("value");
	});
	
	var discount = parseInt(titems / 10);
	switch (discount) {
		case 0:
			o["total"] = t;
			break;
		case 1:
			o["total"] = t;
			break;
		case 2:
			o["total"] = t * 0.95;
			break;
		case 3:
			o["total"] = t * 0.93;
			break;
		case 4:
			o["total"] = t * 0.9;
			break;
		case 5:
			o["total"] = t * 0.85;
			break;
		case 6:
			o["total"] = t * 0.80;
			break;
		case 7:
			o["total"] = t * 0.75;
			break;
		default:
			o["total"] = t * 0.70;
			break;
	}

	o["total_items"] = titems;
	
	o["total"] = Math.round(o["total"] * 100) / 100;
	if (o["total"] == 0) { o["discount"] = 0; }
	else { o["discount"] = Math.round(100 - (o["total"] / (t) * 100)); }
	o["rebate"] = Math.round((o["total"] - t) * 100) / 100;
	o["rebate"] = Math.round((t - o["total"]) * 100) / 100;
	
	return o;
}