var countInput

$(document).ready(
	function ()
	{
		var counts = $('#content ul.collection li p.count span.count-value')

		if ( counts.length > 0 )
		{
			countInput = $('<input type="text" style="display:none;position:relative;width:3em;margin-bottom:-2px"/>')
			countInput.blur(
				function ()
				{
					$(this).css('display', 'none')
				}
			)

			countInput.keyup(
				function ()
				{
					var result = $(this).val()

					return false
				}
			)

			countInput.keypress(
				function (arg)
				{
					switch (arg.keyCode)
					{
						case 13:
							var result = parseInt( $(this).val() )

							if ( !isNaN(result) )
							{
								$('span.count-value', $(this).parent()).text( result )

								var temp

								var itemId
								var sizeId

								temp = $(this).parents('li').eq(0).attr('class')
								temp = temp.split(' ')

								itemId = temp[0].substring(4)
								sizeId = temp[1].substring(4)

								var location
								if ( loggedIn )
								{
									location = '/basket/?action=changeCount&sizeId=' + sizeId + '&itemId=' + itemId + '&count=' + result
								}
								else
								{
									location = '/basket/'
									userBasket.changeCount(
										{
											itemId: itemId,
											sizeId: sizeId
										},
										result
									)
								}
								window.location = location
							}

							$(this).blur()

							return false
						break
						case 27:
							$(this).blur()
							return false
						break
					}
				}
			)

			counts.each(
				function ()
				{
					$(this).click(
						function ()
						{
							countInput.css('left', '-' + $(this).width() + 'px')
							countInput.val( $(this).text() )

							$(this).parent().append( countInput )

							countInput.css('display', 'inline')
							countInput.focus()

							return false
						}
					)
				}
			)
		}
	}
)