// JavaScript Document
function toggleProductMenu(init)
{
	speed = (init) ? 0 : 500;
	//update closed position
	window.closedPos = 120 - $(".productBarWrap").height() + 37;
	if ($(".contentWrap").css("margin-top") == window.closedPos + "px")
	{
		$(".contentWrap").animate({marginTop: 120}, speed );
		$(".productBarContent").fadeIn(speed);
		//return false;
	}
	else
	{
		$(".contentWrap").animate({marginTop: window.closedPos}, speed );
		$(".productBarContent").fadeOut(speed);
		
	}
	return false;
}
function getParent(itemId)
{
	if ($("#item" + $("#item" + itemId).parent().attr("id").substr(3)).length)
	{
		return $("#item" + itemId).parent().attr("id").substr(3);
	}
	else
	{
		return false;
	}
}
function selectParent(itemId, init)
{
	speed = (init) ? 0 : 500;
	parentArray = new Array();
	//include first item
	parentArray.push(itemId);
	nextId = itemId;
	while (p = getParent(nextId))
	{
		parentArray.unshift(p);
		nextId = p;
	}
	//animate out current
	$(".opened").not($("#item" + itemId).parent()).fadeOut(speed);
	$(".opened").not($("#item" + itemId).parent()).removeClass("opened");
	//open all categories
	for (id in parentArray)
	{
		//unselect current
		$("#item" + parentArray[id]).parent().children().removeClass("selected");
		
		//select new
		$("#item" + parentArray[id]).addClass("selected");
		$("#item" + parentArray[id]).parent().show();
		//TODO: look at better way of excluding first category
		$("#item" + parentArray[id]).parent().not(".first").addClass("opened");
		//adjust height of selected link to cover category
		$("#item" + parentArray[id]).parent().height(Math.max($("#item" + parentArray[id]).parent().height(), $("#cat" + parentArray[id]).height()));
		//$("#cat" + parentArray[id]).animate({opacity: 1}, 1000);
		$("#cat" + parentArray[id]).css({marginLeft: $("#item" + parentArray[id]).parent().outerWidth(true), opacity: 1});
		//$("#cat" + parentArray[id]).css({marginLeft: $("#item" + parentArray[id]).parent().outerWidth(true)});
	}
	$(".productBar").animate({height: $(".productBarContent").height()}, speed);
}

$(document).ready(function()
{
	//hide sub categories
	var totalCategories = 26;
	for (var i = 1; i <= totalCategories; i++)
	{
		if (i != 1)
		{
			$(".cat" + i).css({opacity: 0, position: "absolute", top: 0});
			$(".cat" + i).hide();
		}
		else
		{
			$(".cat" + i).addClass("first");
		}
		$(".cat" + i).css({zIndex: totalCategories - i});
	}
	
	window.closedPos = 120 - $(".productBarWrap").height() + 37;
	$(".contentWrap").css("margin-top", window.closedPos);
	$(".productBarWrap").show();
	//$(".productBar").css({height: $(".productBarContent").height()});
	$(".productBar").css({height: $(".productBarContent").height()});
	$(".productBarContent").hide();
	$(".productsLink").click(function()
	{
		toggleProductMenu();
		return false;
    });
	$(".menuCategory a").click(function()
	{
		//if has sub category
		//item clicked
		itemId = $(this).attr("id").substr(4);
		//sub category of item clicked
		subCategory = $("#cat" + itemId);
		if (subCategory.length)
		{
			//animate out open sub categories
			//TODO: may not work beyond 3 levels
			$(".opened").not($(this).parent()).not(subCategory).fadeOut();
			$(".opened").not($(this).parent()).not(subCategory).removeClass("opened");
			//unselect previous
			$(this).parent().children().removeClass("selected");
			subCategory.children().removeClass("selected");
			//set selected
			$(this).addClass("selected");
			subCategory.addClass("opened");
			//adjust height of selected link to cover category
			$(this).parent().height(Math.max(subCategory.height(), $(this).parent().height()));
			//animate in sub category
			subCategory.css({opacity: 0, marginLeft: $(this).parent().outerWidth(true) - $(this).parent().width()});
			subCategory.show();
			//$("." + $(this).attr("id")).animate({marginLeft: 0, opacity: 1}, 500);
			subCategory.animate({marginLeft: $(this).parent().outerWidth(true), opacity: 1}, 500);
			//expand menu area
			$(".productBar").animate({height: $(".productBarContent").height()}, 500);
			return false;
		}
	});
	$(".footerCategories a").click(function()
	{
		//selectParent($(this).attr("id").substr(9));
		//return false;
	});
});
