	/*
	* http://tiptop.com.ua
	* common.js
	* Author: Alex Baskov (http://www.devtrix.net)
	* (c) Devtrix, 2009
	*/

	var COOKIE_OPTIONS = { path: '/', expires: 7 };

	$(document).ready(function()
	{
		$(".cat_title").mouseover(function() { $(this).addClass("cat_title_over")}).mouseout(function () { $(this).removeClass("cat_title_over")});
		$(".cat_title").live("click", populateNodes);

		$(".cat_title").each(function() {
			var cid = $(this).attr("id").replace("cat_","") || 0;
			var cName = "catShown_" + cid;

			if ($.cookie(cName) == "y")
			{
				$("#cat_" + cid).addClass("cat_title_active");
				$("#cat_" + cid + "_c").show();
			}

		});

		$(".b").mouseover(function() { $(this).addClass("b_over"); }).mouseout(function() { $(this).removeClass("b_over"); });
		$(".b_row").mouseover(function() { $(this).addClass("b_row_over"); }).mouseout(function() { $(this).removeClass("b_row_over"); });
		$("#btnChangeView").click(changeView);
		$("#btnChangeViewSearch").click(changeViewSearch);

	});

	function populateNodes()
	{
		var cid = this.id.replace("cat_","");
		var cat = $("#cat_" + cid);
		var catContent = $("#cat_" + cid + "_c");

		// this *MUST* be before slideToggle or it will always return true
		var cookieName = "catShown_" + cid;
		var cookieVal = (catContent.is(":visible")) ? "n" : "y";
		$.cookie(cookieName, cookieVal, COOKIE_OPTIONS);

		$(this).toggleClass("cat_title_active");
		catContent.slideToggle("fast");

		return true;
	} // /populateNodes()


	////
	// sends request when price values are specified
	//
	function filterPrice(subCatID, brandID)
	{
		var priceFrom = $("#price_from").val();
		var priceTo = $("#price_to").val();

		if (priceFrom != "") priceFrom = (!isNaN(priceFrom)) ? parseFloat(priceFrom) : "";
		if (priceTo != "") priceTo = (!isNaN(priceTo)) ? parseFloat(priceTo) : "";

		if (subCatID > 0)
		{
			winLocation = '/category/' + subCatID + '/brand/' + brandID + '/price/' + priceFrom + '-' + priceTo + '/';
			window.location = winLocation;
		}

		return true;
	} // /filterPrice



	////
	// sends request for viewing order info
	//
	function viewOrder()
	{
		var codeVal = $("input#code").val();

		if (codeVal != "")
		{
			window.location = "/orders/" + URLEncode(URLEncode(codeVal));
		}

		return false;
	} // viewOrder()



	////
	// this function handles properly
	//
	function URLEncode(clearString)
	{
		var output = '';
		var x = 0;
		clearString = clearString.toString();
		var regex = /(^[a-zA-Z0-9_.]*)/;
		while (x < clearString.length)
		{
			var match = regex.exec(clearString.substr(x));
			if (match != null && match.length > 1 && match[1] != '')
			{
				output += match[1];
				x += match[1].length;
			}
			else
			{
				if (clearString[x] == ' ')
				{
					output += '+';
				}
				else
				{
					var charCode = clearString.charCodeAt(x);
					var hexVal = charCode.toString(16);
					output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
				}
				x++;
			}
		}
		return output;
	}



	////
	//
	//
	function changeView()
	{
		var cookieName = "products_display";
		var cookieVal = ($.cookie(cookieName) == "rows") ? "blocks" : "rows";
		$.cookie(cookieName, cookieVal, COOKIE_OPTIONS);

		window.location = window.location;
		return false;
	} // /changeView()



	////
	//
	//
	function changeViewSearch()
	{
		var cookieName = "products_display";
		var cookieVal = ($.cookie(cookieName) == "rows") ? "blocks" : "rows";
		$.cookie(cookieName, cookieVal, COOKIE_OPTIONS);

		$("#form_search").submit();
		return false;
	} // /changeViewSearch()