var amoeba_buy_stuff_onload_chain = window.onload;
var amoeba_buy_stuff_base_href = "/buy-stuff";

window.onload = amoeba_buy_stuff_onload;

function amoeba_buy_stuff_onload()
{
	var list = document.getElementById("buy-stuff-category-list");
	if (!list)
		return;
	var items = list.getElementsByTagName ? list.getElementsByTagName("div") : list.all.tags.div;

	for (var i = 0; i < items.length; i++)
	{
		if (items[i].className != "category")
			continue;

		items[i].onmouseover = function (event)
		{
			this.className += " hover";
		}
		items[i].onmouseout = function (event)
		{
			this.className = this.className.replace('hover', '');
		}
	}

	if (amoeba_buy_stuff_onload_chain)
		amoeba_buy_stuff_onload_chain();
}

var buy_stuff_top_start_t = 0;
var buy_stuff_top_duration = 1000;
var buy_stuff_top_delta = 0;

function buy_stuff_to_top()
{
	buy_stuff_top_start_t = new Date().getTime();

	var y;
	if (self.pageYOffset)
		y = self.pageYOffset;
	else if (document.documentElement.scrollTop)
		y = document.documentElement.scrollTop;
	else if (document.body)
		y = document.body.scrollTop;

	buy_stuff_top_duration = y / 3;

	buy_stuff_top_delta = y;
	buy_stuff_to_top_step();
}

function buy_stuff_to_top_step()
{
	var delta_t = new Date().getTime() - buy_stuff_top_start_t;
	var progress = 1 - (delta_t / buy_stuff_top_duration);

	var new_y = buy_stuff_top_delta * progress;

	if (new_y > 0)
	{
		window.scrollTo(0, new_y);
		window.setTimeout(buy_stuff_to_top_step, 0);
	}
	else
		window.location.hash = "#top";
}

function format_price(n)
{
	var dollars = parseInt(n);
	var cents = new String(parseInt((n - dollars) * 100));

	if (cents.length < 2)
		cents = "0" + cents;

	return dollars + "." + cents;
}

function buy_stuff_set_left_items(n)
{
	document.getElementById("buy-stuff-cart-box").className = n == 0 ? "buy-stuff-cart-box-empty" : "buy-stuff-cart-box-full";
	document.getElementById("buy-stuff-cart-box-links").style.display = n == 0 ? "none" : "block";
	document.getElementById("buy-stuff-cart-items").innerHTML = n;

	document.getElementById("header-buy-stuff-cart-box").style.display = n == 0 ? "none" : "block";
	document.getElementById("header-buy-stuff-cart-items").innerHTML = n;
}

function buy_stuff_index_addcart(item_id, type)
{
	function async(response)
	{
		var xml = xml_first_child(response.responseXML, "data");

		document.getElementById("buy-stuff-cart-error_" + item_id).innerHTML = xml_text_content(xml_first_child(xml, "message"));

		if (xml.getAttribute("ok") == "1")
		{
			buy_stuff_set_left_items(parseInt(document.getElementById("buy-stuff-cart-items").innerHTML) + parseInt(xml_text_content(xml_first_child(xml, "items"))));
			document.getElementById("buy-stuff-cart-total").innerHTML = xml_text_content(xml_first_child(xml, "new-subtotal"));
		}
	}

	http_get(amoeba_buy_stuff_base_href + "/cart.html?xml=add&item=" + item_id + "&type=" + type, async);

	return false;
}

function buy_stuff_cart_removecart(item_id, type)
{
	function async(response)
	{
		var xml = xml_first_child(response.responseXML, "data");

		if (xml.getAttribute("ok") == "1")
		{
			var items = parseInt(xml_text_content(xml_first_child(xml, "items")));

			buy_stuff_set_left_items(parseInt(document.getElementById("buy-stuff-cart-items").innerHTML) - items);
			document.getElementById("buy-stuff-cart-total").innerHTML = xml_text_content(xml_first_child(xml, "new-subtotal"));

			var category_items = document.getElementById("category-" + type + "-items");
			category_items.removeChild(document.getElementById("cart-item-" + type + "-" + item_id));

			if (category_items.getElementsByTagName("div").length == 0)
				document.getElementById("category-" + type).style.display = "none";

			document.getElementById("buy-stuff-cart-sub-total").innerHTML = document.getElementById("buy-stuff-cart-total").innerHTML;
		}
	}

	http_get(amoeba_buy_stuff_base_href + "/cart.html?xml=remove&item=" + item_id + "&type=" + type, async);

	return false;
}

function buy_stuff_cart_quantity(item_id, type, quantity)
{
	function async(response)
	{
		var xml = xml_first_child(response.responseXML, "data");

		if (xml.getAttribute("ok") == "1")
		{
			var items = parseInt(xml_text_content(xml_first_child(xml, "items")));

			buy_stuff_set_left_items(parseInt(document.getElementById("buy-stuff-cart-items").innerHTML) + items);
			document.getElementById("buy-stuff-cart-total").innerHTML = xml_text_content(xml_first_child(xml, "new-subtotal"));

			document.getElementById("cart-item-" + type + "-" + item_id + "-price").innerHTML = xml_text_content(xml_first_child(xml, "item-subtotal"));

			document.getElementById("buy-stuff-cart-sub-total").innerHTML = document.getElementById("buy-stuff-cart-total").innerHTML;
		}
	}

	http_get(amoeba_buy_stuff_base_href + "/cart.html?xml=quantity&item=" + item_id + "&type=" + type + "&quantity=" + quantity, async);

	return false;
}

function buy_stuff_cart_promo(code)
{
	function async(response)
	{
		var xml = xml_first_child(response.responseXML, "data");

		document.getElementById("buy-stuff-cart-total").innerHTML = xml_text_content(xml_first_child(xml, "new-subtotal"));
		document.getElementById("buy-stuff-cart-sub-total").innerHTML = document.getElementById("buy-stuff-cart-total").innerHTML;

		if (xml.getAttribute("ok") == "1")
			document.getElementById("buy-stuff-cart-promo-error").innerHTML = "Your code has been applied.";
		else
			document.getElementById("buy-stuff-cart-promo-error").innerHTML = xml_text_content(xml_first_child(xml, "message"));
	}

	http_get(amoeba_buy_stuff_base_href + "/cart.html?xml=promo&code=" + code, async);

	return false;
}

function buy_stuff_cart_shipping_change(form)
{
	function async(response)
	{
		var xml = xml_first_child(response.responseXML, "data");

		document.getElementById("buy-stuff-cart-shipping").innerHTML = '$' + xml_text_content(xml_first_child(xml, "shipping-cost"));
	}

	if (form.country.value == "")
		document.getElementById("buy-stuff-cart-shipping").innerHTML = "&nbsp;";
	else
		http_get(amoeba_buy_stuff_base_href + "/cart.html?xml=shipping&country=" + form.country.value + "&state=", async);
}

function buy_stuff_checkout_same(form, toggle)
{
	if (!toggle)
		return;

	for (var i = 0; i < form.length; i++)
	{
		var el = form[i];

		if (typeof(el.name) != "undefined" && el.name.indexOf("shipping[") == 0)
			el.value = form["billing[" + el.name.substr(9)].value;
	}

	buy_stuff_checkout_shipping_change();
	document.getElementById("shipping_same").checked = toggle;
}

function buy_stuff_checkout_shipping_change()
{
	document.getElementById("shipping_same").checked = false;

	function async(response)
	{
		var xml = xml_first_child(response.responseXML, "data");

		document.getElementById("buy-stuff-checkout-total").innerHTML = xml_text_content(xml_first_child(xml, "total"));
		document.getElementById("buy-stuff-checkout-shipping").innerHTML = xml_text_content(xml_first_child(xml, "shipping-cost"));
		document.getElementById("buy-stuff-checkout-tax").innerHTML = xml_text_content(xml_first_child(xml, "tax"));
	}

	http_get(amoeba_buy_stuff_base_href + "/cart.html?xml=shipping&country=" + document.forms.checkout["shipping[country]"].value + "&state=" + document.forms.checkout["shipping[state]"].value, async);
}

function buy_stuff_play_mp3(mp3, link)
{
	hidden_play_mp3(mp3, link);
}

function buy_stuff_detail_addcart(item_id, type)
{
	function async(response)
	{
		var xml = xml_first_child(response.responseXML, "data");

		if (type != "bsh_track")
		{
			document.getElementById("buy-stuff-cart-error_" + item_id).innerHTML = xml_text_content(xml_first_child(xml, "message"));
			document.getElementById("buy-stuff-cart-error_" + item_id).style.display = "block";
		}
		else
			document.getElementById("buy-stuff-cart-track_" + item_id).innerHTML = '<div class="track-list-buy">Item Added</div>';

		if (xml.getAttribute("ok") == "1")
		{
			// Customize the message for this page.
			if (type != "bsh_track")
				document.getElementById("buy-stuff-cart-error_" + item_id).innerHTML = 'This item has been added to your <a href="' + amoeba_buy_stuff_base_href + '/cart.html">shopping cart</a>.';

			buy_stuff_set_left_items(parseInt(document.getElementById("buy-stuff-cart-items").innerHTML) + parseInt(xml_text_content(xml_first_child(xml, "items"))));
			document.getElementById("buy-stuff-cart-total").innerHTML = xml_text_content(xml_first_child(xml, "new-subtotal"));
		}
		else if (type == "bsh_track")
			alert(xml_text_content(xml_first_child(xml, "message")));
	}

	http_get(amoeba_buy_stuff_base_href + "/cart.html?xml=add&item=" + item_id + "&type=" + type, async);

	return false;
}

function buy_stuff_download_show_overlay(id)
{
	var overlay = document.getElementById("buy-stuff-overlay");
	if (!overlay)
	{
		overlay = document.createElement("div");
		overlay.id = "buy-stuff-overlay";
		document.body.appendChild(overlay);

		var overlay_holder = document.createElement("div");
		overlay_holder.id = "buy-stuff-overlay-holder";
		document.body.appendChild(overlay_holder);
	}

	overlay.style.display = "block";

	document.getElementById("buy-stuff-overlay-holder").style.left = document.getElementById("container").offsetLeft + "px";

	function overlay_resize()
	{
		if (document.getElementById("buy-stuff-overlay").clientHeight < document.getElementById("container").clientHeight)
			document.getElementById("buy-stuff-overlay").style.height = document.getElementById("container").clientHeight + "px";
		if (document.getElementById("buy-stuff-overlay").clientWidth < document.getElementById("container").clientWidth)
			document.getElementById("buy-stuff-overlay").style.width = document.getElementById("container").clientWidth + "px";
	}

	window.setTimeout(overlay_resize, 0);

	overlay.onclick = function ()
	{
		buy_stuff_download_hide_overlay();
	}

	var x, y;

	if (self.innerHeight)
		y = self.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		y = document.documentElement.clientHeight;
	else if (document.body)
		y = document.body.clientHeight;

	function later()
	{
		function reposition()
		{
			var top = window.pageYOffset ? window.pageYOffset : document.body.scrollTop;
			if (!top)
				top = document.documentElement.scrollTop;

			document.getElementById(id).style.top = ((y - document.getElementById(id).clientHeight) / 2 + top) + "px";
		}

		reposition();
		document.getElementById(id).style.visibility = "visible";

		var temp_onscroll = window.onscroll;
		window.onscroll = function ()
		{
			reposition();
			if (temp_onscroll)
				return temp_onscroll();
		}
	}

	try
	{
		var overlay_page = document.getElementById(id);
		document.getElementById("content").removeChild(overlay_page);
		document.getElementById("buy-stuff-overlay-holder").appendChild(overlay_page);
	}
	catch (e)
	{
	}

	document.getElementById(id).style.display = "block";
	document.getElementById(id).style.visibility = "hidden";
	window.setTimeout(later, 0);
}

function buy_stuff_download_hide_overlay()
{
	document.getElementById("buy-stuff-popup-download-help").style.display = "none";

	document.getElementById("buy-stuff-overlay").style.display = "none";
}