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;
}

function buy_stuff_set_top_items(n)
{
	if(document.getElementById("header-buy-stuff-cart-box"))
	{
		document.getElementById("header-buy-stuff-cart-box").style.display = n == 0 ? "none" : "block";
	}
	if(document.getElementById("header-buy-stuff-cart-items"))
	{
		document.getElementById("header-buy-stuff-cart-items").innerHTML = n;
	}
}

function buy_stuff_listing_addcart(item_id, type)
{
	function async(response)
	{
		var xml = xml_first_child(response.responseXML, "data");

		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"))));
			buy_stuff_set_top_items(parseInt(document.getElementById("header-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"));
			document.getElementById("btn_" + item_id).className = 'addcart-button_listing_added';
		}
		else
		{
			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 + "&detail=false", async);

	return false;
}

function buy_stuff_index_addcart(item_id, type, detail)
{
	function async(response)
	{
		var xml = xml_first_child(response.responseXML, "data");

		if(xml_first_child(xml, "detail"))
		{
			if(xml_text_content(xml_first_child(xml, "detail")) == "1")
			{
				var totaltemp = xml_text_content(xml_first_child(xml, "message"));
				
				if(totaltemp.indexOf('your shopping cart.', 0) != -1)
				{
					totaltemp = totaltemp.replace('your shopping cart.', 'your <a href="/buy-stuff/cart.html" style="color:#006A9E; text-decoration:underline;">shopping cart.</a>');
				}
				document.getElementById("buy-stuff-cart-error_" + item_id).innerHTML = totaltemp;
			}
			else
			{
				document.getElementById("buy-stuff-cart-error_" + item_id).innerHTML = xml_text_content(xml_first_child(xml, "message"));
			}
		}
		else
		{
			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"))));
			buy_stuff_set_top_items(parseInt(document.getElementById("header-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 + "&detail=" + detail, 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")));

			if(document.getElementById("buy-stuff-cart-items"))
			{
				buy_stuff_set_left_items(parseInt(document.getElementById("buy-stuff-cart-items").innerHTML) - items);
			}
			
			if(document.getElementById("header-buy-stuff-cart-items"))
			{
				buy_stuff_set_top_items(parseInt(document.getElementById("header-buy-stuff-cart-items").innerHTML) - items);
			}
			
			if(document.getElementById("buy-stuff-cart-total"))
			{
				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";

			if(document.getElementById("buy-stuff-cart-sub-total"))
			{
				document.getElementById("buy-stuff-cart-sub-total").innerHTML = xml_text_content(xml_first_child(xml, "new-subtotal"));
			}
			
			/*
			if(document.getElementById("buy-stuff-cart-sub-total") && document.getElementById("buy-stuff-cart-total"))
			{
				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")));

			if(document.getElementById("buy-stuff-cart-items"))
			{
				buy_stuff_set_left_items(parseInt(document.getElementById("buy-stuff-cart-items").innerHTML) + items);
			}
			
			if(document.getElementById("header-buy-stuff-cart-items"))
			{
				buy_stuff_set_top_items(parseInt(document.getElementById("header-buy-stuff-cart-items").innerHTML) + items);
			}
			
			if(document.getElementById("buy-stuff-cart-total"))
			{
				document.getElementById("buy-stuff-cart-total").innerHTML = xml_text_content(xml_first_child(xml, "new-subtotal"));
			}
			
			if(document.getElementById("cart-item-" + type + "-" + item_id + "-price"))
			{
				document.getElementById("cart-item-" + type + "-" + item_id + "-price").innerHTML = xml_text_content(xml_first_child(xml, "item-subtotal"));
			}
			if(document.getElementById("buy-stuff-cart-sub-total"))
			{
				if(document.getElementById("buy-stuff-cart-total"))
				{
					document.getElementById("buy-stuff-cart-sub-total").innerHTML = document.getElementById("buy-stuff-cart-total").innerHTML;
				}
				else
				{
					document.getElementById("buy-stuff-cart-sub-total").innerHTML = xml_text_content(xml_first_child(xml, "new-subtotal"));
				}
			}
		}
	}

	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_async(response)
{
	var xml = xml_first_child(response.responseXML, "data");
	var map = {
		"buy-stuff-checkout-total": "total",
		"buy-stuff-checkout-shipping": "shipping-cost",
		"buy-stuff-checkout-tax": "tax",
		"buy-stuff-checkout-sub-total": "new-subtotal",
		"bs_promo_value": "bspromo",
		"custom_message": "custom_msg"
	};

	if (xml.getAttribute("ok") != "1")
		alert(xml_text_content(xml_first_child(xml, "message")));

	var autoParseValue = true;
	if(document.getElementById('bs_promo_div') && document.getElementById('bs_promo_value'))
	{
		if(xml_first_child(xml, map["bs_promo_value"]) && xml_text_content(xml_first_child(xml, map["bs_promo_value"])) != '')
		{
			if(xml_text_content(xml_first_child(xml, map["custom_message"])) != '')
			{
				document.getElementById('bs_promo_value').innerHTML = '';
				
				document.getElementById('bs_promo_value').style.color = '#000000';
				document.getElementById('bs_promo_value').style.backgroundColor = '#ffff00';
				
				document.getElementById('bs_promo_value').innerHTML = xml_text_content(xml_first_child(xml, map["custom_message"]));
				document.getElementById('bs_promo_code_applied').style.display = 'none';
				
				autoParseValue = false;
			}
			else
			{
				autoParseValue = true;
				document.getElementById('bs_promo_value').style.color = '#ffffff';
				document.getElementById('bs_promo_value').style.backgroundColor = '#000000';
				
				document.getElementById('bs_promo_code_applied').style.display = 'block';
			}
			document.getElementById('bs_promo_div').style.display = 'block';
		}
		else
		{
			document.getElementById('bs_promo_div').style.display = 'none';
		}
	}
		
	for (var id in map)
	{
		if (document.getElementById(id) && xml_first_child(xml, map[id]))
		{
			if(id == 'bs_promo_value')
			{
				if(autoParseValue == true)
			document.getElementById(id).innerHTML = xml_text_content(xml_first_child(xml, map[id]));
		}
			else
				document.getElementById(id).innerHTML = xml_text_content(xml_first_child(xml, map[id]));
		}
	}
}

function buy_stuff_checkout_shipping_change()
{
	document.getElementById("shipping_same").checked = false;

	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, buy_stuff_checkout_async);
}

function buy_stuff_checkout_promo(code)
{
	if(document.forms.checkout["shipping[country]"] && document.forms.checkout["shipping[state]"])
	{
		http_get(amoeba_buy_stuff_base_href + "/cart.html?xml=promo&country=" + document.forms.checkout["shipping[country]"].value + "&state=" + document.forms.checkout["shipping[state]"].value + "&code=" + code, buy_stuff_checkout_async);
	}
	else
	{
		http_get(amoeba_buy_stuff_base_href + "/cart.html?xml=promo&code=" + code, buy_stuff_checkout_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"))));
			buy_stuff_set_top_items(parseInt(document.getElementById("header-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";
}

function bsDrawSubs(element)
{
	var sign = document.getElementById('subcatsign_' + element);
	var subs = document.getElementById(element + '_subs');
	
	if(sign && subs)
	{
		if(subs.style.display == 'block')
		{
			subs.style.display = 'none';
			sign.className = 'subs_sign_plus';
		}
		else
		{
			subs.style.display = 'block';
			sign.className = 'subs_sign_minus';
		}		
	}
}

function bsSetSorting(value)
{
	var winhref = window.location.href;
	var fragments =  winhref.split('.html');
	var temphref = fragments[0] + '.html';
	if(fragments.length > 1)
	{
		for(var xxl=1; xxl<fragments.length; xxl++)
		{
			temphref = temphref + fragments[xxl];
		}
	}
	
	if(temphref.indexOf('?') != -1)
	{
		window.location.href = temphref + '&sort=' + value;
	}
	else
	{
		window.location.href = temphref + '?sort=' + value;
	}
	 
}
