$(function() {
	
	$('.form a.fbutton').not('div.f-r-box a.fbutton').bind('click', function() {
		$(this).parents('form').first().submit();
		return false;
	});
	
	$('#find-link').fancybox({
		autoDimensions: false,
		scrolling: 'yes',
		width: 500,
		height: 500
	});
	
	$('#barcode-submit').bind('click',function() {
		var form = $(this).parent('form');
		form.submit();
		return false;
	});
	
	$('#keyword-submit').bind('click', function() {
		$('#keyword-results').fadeOut('fast', function() {
			$('#keyword-loading').fadeIn('fast', function() {
				$('#keyword-results').html('');
				$.ajax({
					url: $('#keyword-form').attr('action'),
					data: $('#keyword-form').serialize(),
					dataType: 'json',
					success: function(data) {
						if(data&&!data.Items.Request.Errors) {
							var items = data.Items.Item,
								table = $('#keyword-results').html($('<table>')).find('table').css({'list-style':'none', 'padding':'0px'});
								
							if (items.constructor.toString().indexOf("Array") == -1)
								items = [items];
							
							$.each(items, function(i,item) {
								var attributes = item.ItemAttributes,
									tr = $('<tr>');
								
								if(!item.SmallImage) item.SmallImage = {URL:'/bogo/assets/images/no_image.gif',width:75,height:75};
								
								tr.append('<td><img src="'+item.SmallImage.URL+'" /></td>');
								tr.append('<td valign="top">'+
											'<p>'+attributes.Title+'</p>'+
											'<p><strong>EAN:</strong> '+attributes.EAN+'</p>'+
										  '</td>');
								tr.append('<td><a class="insert-code" rel="'+attributes.EAN+'" href="">Insert Code</a></td>');
								
								table.append(tr);
							});
							
							$('#keyword-no-results').remove();
						}
						else {
							$('#keyword-no-results').remove();
							$('#keyword-results').prepend('<p id="keyword-no-results">Sorry this keyword could not be found.</p>');
						}
						$('#keyword-loading').fadeOut('fast', function() {
							$('#keyword-results').fadeIn('fast');
						});
					}
				});
			});
		});
		return false;
	});
	
	$('a.insert-code').live('click', function() {
		$.fancybox.close();
		$('#barcode').val($(this).attr('rel'));
		return false;
	});

	
	$('div.f-r-box a.fbutton').bind('click',function() {
		form = $(this).parents('form')[0];
		$('div.status').text('Sending...').fadeIn('fast',function() {
			$.ajax({
				url: $(form).attr('action'),
				async: true, 
				data: $(form).serialize(),
				dataType: 'json',
				success: function(response) {
					$('span.error').remove();
					if(response&&response.sent) {
						$('div.status').text('Message has been sent');
						setTimeout(function() {
							$(form).find('input,textarea').each(function() { $(this).val('') });
							$('div.status').fadeOut('fast');
						}, 2000);
					}
					else {
						if(response.errors) {
							$.each(response.errors,function(id,message) {
								$('li').has('#'+id).append('<span class="error">'+message+'</span>');
							});
							$('div.status').fadeOut('fast');
						}
						else {
							$('div.status').text('Some errors occured. Please try again later.');
							setTimeout("$('div.status').fadeOut('fast');", 2000);
						}
					}
				}
			});
		});
		return false;
	});
	
	$('#keyword-form input[type=text]').bind('keypress', function(e) {
		if(e.which == 13) {
			$('#keyword-submit').click();
			return false;
		}
	});

});
