// Navigation and shopping cart scripts
// Requires jquery, listen and color.

///// Fade functions ////////////////////////////////////////////

function startFade() {
	stopfade = false;
	faded = false;
	$('#shoppingcart').data('cartChanged', true);
	fadeUpdate();
}

function stopFade() {
	stopfade = true;
	faded = false;
	$('#shoppingcart').data('cartChanged', false);
	$('.cartb2').css('background-color', '#FFF');
}

function fadeUpdate() {
	if(stopfade) return;

	var col = faded ? '#FFF' : '#CDEB8B';
	faded = !faded;
	$('.cartb2').animate({backgroundColor:col}, 800, null, arguments.callee);
}

/////////////////////////////////////////////////////////////////

// This method is also defined in checkout/index.tpl
function amounts() {
	return $('#shoppingcart_full input').map(function() {
		return $(this).attr('value');
	}).get().join(',');
}

///// Add to cart function //////////////////////////////////////

function addToCart(id, amount, selector, layer, callback)
{
	if(selector == undefined) selector = "#shoppingcart";	

	$(selector).load("/shop/ajaxadd/" + id + "/" + amount, null, 
		function() { 
			$('.ct1').css('font-weight', 'bold').find('span').animate({fontSize: '145%'}, 800).animate({fontSize: '100%'}, 1000);
			$('#shoppingcart_full').show();
			
			if(callback)
				callback();
			
			if(pageTracker)
				pageTracker._trackPageview('/shop/cartadd/' + id);
		}
	);	
}


$(function() {

	///// Shopping cart /////////////////////////////////////////////
	
	// This part is dynamically loaded by ajax so the events needs to be
	// bound by listen.
	
	$('#shoppingcart').data('cartChanged', false);
		
	var faded = false;
	var stopfade = false;

	// Remove product item buttons
	$.listen('click', '.shoppingcart_removeitem', function() {
		var item = $('.shoppingcart_removeitem').index(this);
		$('#shoppingcart').load("/shop/ajaxremove/" + item);
		return false;
	});
	
	// Toggle cart display
	$.listen('click', '#shoppingcart_change,#shoppingcart_hide', function() {
		if(!$.browser.msie)	$('#shoppingcart_full').slideToggle('medium');
		else $('#shoppingcart_full').toggle();
		return false;
	});
	
	// Update button
	$.listen('click', '#shoppingcart_update', function() {
		stopFade();
		
		$('#shoppingcart_update_image').attr('src', '/images/ajaxloader_small.gif');
		$.post('/shop/ajaxmodifycart', { 'amounts': amounts() },
			function(data) {
				$('#shoppingcart').empty().prepend(data);
			}
		);
		return false;
	});

	// Checkout button, test for cart update before going to checkout.
	$.listen('click', '#shoppingcart_checkout', function() {
		if($('#shoppingcart').data('cartChanged')) {
			stopFade();
			$.ajaxSetup({async: false});
			$.post('/shop/ajaxmodifycart', { 'amounts': amounts() });
		}
		return true;
	});
	
	// Make the input colorfade the update button when changed
	$('#shoppingcart').listen('keypress', 'input', function(event) {
		if($(this).data('cartChanged')) return;
		startFade();
	});

	///// Navigation tree ///////////////////////////////////////////

	// Hide all nodes except top ones
	$('#nav3 > ul ul')
		.hide();
		
	// Make the category nodes toggle
	$('#nav3 li span')
		.css('cursor', 'pointer')
		.hover(
			function() { if(!$.browser.msie) $(this).addClass('nav-hover'); },
			function() { if(!$.browser.msie) $(this).removeClass('nav-hover'); }
		)
		.click(function() {
			if (!$.browser.msie)
				$(this).next('ul').slideToggle(300);
			else
				$(this).next('ul').toggle();

			$(this).parent('li').removeClass('nav-hover').toggleClass('nav-closed').toggleClass('nav-open');
		});

	// Open the selected node and set the parent category to open.
	$('#category_selected').parents('ul')
		.show()
		.parent('li').removeClass('nav-closed').addClass('nav-open');

	// Close the nav-closed nodes explicitly
	$('.nav-closed').find('ul:first').hide();

	if(autoSelected == false)
		$('#category_selected').addClass('nav-active');
	
	///// Label filter dropmenu /////////////////////////////////////
	
	$('#filterlabels').change(function () {
		var label = $(this).val();
		if(label > 0)
			window.location = '/search/products/' + escape(label);
	});
	
	///// Search functions //////////////////////////////////////////
	
	$('#search_btn').css('cursor', 'pointer').click(searchProducts);
	
	$('#search_text').keyup(function(event) {
		if(event.keyCode == 13) searchProducts();
	});
	
	function searchProducts() {
		var query = $('#search_text').val().replace('*', '');
		window.location = '/search/products/' + escape(query);
	}	
});

