	/*
	* http://interaktiv.com.ua
	* categories.js
	* Author: Alex Baskov (http://www.devtrix.net)
	* (c) Devtrix, 2009
	*/

	var boolDoNotShowPopup = false;

	$(document).ready(function()
	{
		$(".tt").live("click", populateNodes);
		$("a.cart_link").live("click", addToCart);

		$("#do_not_show").live("click", function () {
			boolDoNotShowPopup = ($(this).is(":checked")) ? true : false;
		});

	});

	function populateNodes()
	{
		var cid = this.id.replace("nt_","");
		var cat = $("#nb_" + cid);
		var parentCat = $("#n_" + cid);
		var lvl = 0;

		if (parentCat.hasClass("tc") || parentCat.hasClass("tca"))
		{
			lvl = 1;
			parentCat.toggleClass("tca").toggleClass("tc");
		}
		if (parentCat.hasClass("ts") || parentCat.hasClass("tsa"))
		{
			lvl = 2;
			parentCat.toggleClass("tsa").toggleClass("ts");
		}
		if (parentCat.hasClass("ts2") || parentCat.hasClass("ts2a"))
		{
			lvl = 3;
			parentCat.toggleClass("ts2a").toggleClass("ts2");
		}

		if (!(cat.is(':visible')))
		{
			var nl = $("#nl_" + cid).get();
			if (nl != "")
			{
				$.ajax({
					url: "/includes/ajax_products.php",
					type: "GET",
					data: ({id : cid, level: lvl}),
					dataType: "html",
					success: function(htmlData) {
						//alert(htmlData);
						$(nl).remove();
						cat.html(htmlData);
						// be sure to uncomment the following line if you're not planning use LIVE onclick function... this is the way to 'update' onlicks for the new elements
						//$(".tt").click(populateNodes);
					}
				});
			}
		}

		cat.slideToggle("fast");

	} // /populateNodes()



	////
	// adds to cart
	//
	function addToCart()
	{
		var pid = $(this).attr("rel");

		if (pid > 0)
		{
			$.ajax({
				url: "/includes/ajax_cart.php",
				type: "GET",
				data: ({id : pid, action: "add_to_cart"}),
				dataType: "html",
				success: function(htmlData) {

					if (/^Error/.test(htmlData))
					{
						alert("Опля! Помилочка вийшла :(\nДзвоніть в ремонт!");
					}
					else
					{
						var tmp = htmlData.split("%%%");
						if (tmp[0] == "Success.")
						{
							var tmpArr = tmp[1].split("|||");
							var cartItemsCount = tmpArr[0] || "0";
							var cartItemsTotal = tmpArr[1] || "0.00";

							$("#topCartCount").html(""+cartItemsCount);
							$("#topCartTotal").html(""+cartItemsTotal);

							if (!boolDoNotShowPopup)
							{
								openPopup();
							}
							return true;
						}
					}

				}
			});
		}

	} // /addToCart()



	////
	// show popup
	//
	function openPopup()
	{
		var popupContent = $("#popupContentHolder").html();

		// open a welcome message as soon as the window loads

    Shadowbox.open({
        //content:    '<div id="welcome-msg">Welcome to my website!</div>',
        content:    popupContent,
        player:     "html",
        title:      "interaktiv.com.ua",
        height:     150,
        width:      350

    });

    return true;
	}