$(document).ready(function()
{
	$('a[@rel=external]').click(function()
	{
		$(this).attr('target', '_blank');
	});

	$("#scroller").simplyScroll({
		autoMode: 'loop'
	});

	$('a[@rel*=lightbox]').lightBox(
	{
		imageLoading: '/images/lightbox/lightbox-ico-loading.gif',
		imageBtnPrev: '/images/lightbox/lightbox-btn-prev.gif',
		imageBtnNext: '/images/lightbox/lightbox-btn-next.gif',
		imageBtnClose: '/images/lightbox/lightbox-btn-close.gif',
		imageBlank: '/images/lightbox/lightbox-blank.gif',
		txtImage: '',
		txtOf: ' / '
	});
	
	$('.order a, #order a').click(function()
	{
		var id = $(this).attr('id').replace('product', '');
		$.get('/pages/addtobasket/' + id + '/', function(data)
		{
			data = data.split('|');
			$('#basket_count').html(data[0]);
			$('#basket_total').html(data[1]);
			$('#basket').show();
		});
	});
	
	$('a#clear').click(function()
	{
		$.get('/pages/clearbasket/', function(data)
		{
			$('#basket').fadeOut('slow');
		});
	});

	$('.basketremove').click(function()
	{
		var id = $(this).attr('rel');
		$.get('/pages/basketremove/' + id + '/');
		$(this).parents('li:first').remove();

		if ($('.basketremove').length == 0)
		{
			$('#text').html('<strong>Jūsų krepšelis tuščias.</strong>');
		}
	});

	$('#pay input').click(function()
	{
		var type = $(this).attr('id');
		if (type == 'payment_cash')
		{
			$('#places_holder').show();
		}
		else
		{
			$('#places_holder').hide();
		}
	});
});

function validate(f)
{
	var failed = false;

	$('form#' + f.id + ' .required').each(function()
	{
		var id = $(this).attr('id');
		var label = $('label[@for=' + id + ']');

		label.removeClass('error');
		if ($(this).val() == '')
		{
			label.addClass('error');
			failed = true;
		}
	});

	if (failed)
	{
		$('#error').html('Užpildykite visus privalomus laukus.');
		$('#error').fadeIn(400);
	}

	return !failed;
}