var menuTimerHandle = 0;
var menuTimerObject = false;

function navigation()
{
	$('ul#nav-main > li').mouseover( function()
	{
		$('a', this).addClass('selected');
		$('ul', this).css('display', 'block');
	});
	$('ul#nav-main > li').mouseout( function()
	{
		$('a', this).removeClass('selected');
		$('ul', this).css('display', 'none');
	});
}

function ro()
{
	$('.ro').mouseover( function()
	{
		rep = ".";
		ext = $(this).attr('src').substr(-4, 4);
		src = $(this).attr('src').replace(rep, "-on.");
		$(this).attr('src', src);
		
	});
	$('.ro').mouseout( function()
	{
		rep = "-on.";
		ext = $(this).attr('src').substr(-4, 4);
		src = $(this).attr('src').replace(rep, ".");
		$(this).attr('src', src);
		
	});
}

function removeItem(){
	$('.removeItem').click(function(evt){
		url = "/action/purchase/cartUpdate.php?product-id=" + $(this).attr('href') + "&send=cart&qty=0";		
		location.href=url;
		evt.preventDefault();
	});
}

function updateItem(){
	$('.updateItem').click(function(evt){
		evt.preventDefault();
		var $item_id = parseInt($(this).attr("rel"));
		var $qty = parseInt($('input[rel=' + $item_id + ']', $('#shopping_cart')).val());
		url = "/action/purchase/cartUpdate.php?product-id=" + $item_id + "&send=cart&qty=" + $qty;		
		location.href=url;		
	});
}

function MenuOpenStart()
{
	menuTimerObject = this;
	menuTimerHandle = setTimeout(MenuOpen, 400);
}

function MenuOpenStop() {
	menuTimerObject = false;
	if (menuTimerHandle) {
		clearTimeout(menuTimerHandle);
	}
}

function MenuOpen()
{
	if (menuTimerObject) {
		$('> ul', menuTimerObject).show();
		$(menuTimerObject).children('a.menu_toggle').children('img.menu_toggle').attr('src','/images/btn/btn-expanded.png');
	}
}

$(function(){
	navigation();
	ro();
	removeItem();	
	updateItem();
	$('#filterby').change(function(){
		location.href = '/products/brand/' + $('option:selected', $(this)).val() + '/';							   
	});
	$('#updateCart').click(function(evt){
		evt.preventDefault();
		$('#shopping_cart').submit();
	});
	$('#public_registry').change(function(){
		if($('#public_registry:checked').length > 0){
			$value = 0;
		} else {
			$value = 1;	
		}
		$.post("/action/accounts/changeWishlist.php", {value: $value});
	});

	//tag menu items that have children with a thing that calls the open menu function 10 secs later on mouseover
	//teg menu items with reset on mouseout
	$('#nav-category li').mouseenter(MenuOpenStart);
	$('#nav-category li').mouseleave(MenuOpenStop);
});

