(function($)
{
	//var changePaymentMethod = 
	
	$(function()
	{
		// Homepage
		var offers = $('.offer');
		if (offers.length)
		{
			var offer = 0
			var totalOffers = offers.length;
			
			setInterval(function()
			{
				offers.eq(offer).fadeOut(800);
				offer = (offer + 1) % totalOffers
				offers.eq(offer).fadeIn(800);
			}, 2000);
		}
		
		// Product search
		$('#brands').change(function()
		{
			$('.brand').hide();
			$('#brand-' + $(this).val()).show();
		}).change();
		
		$('.brand').change(function()
		{
			location.href = $(this).val();
			$('.searchinput').html('<div class="search-loading">Loading...</div>');
		});
		
		// Free search
		$('.searchinput').submit(function()
		{
			var search = $('.searchinput input[name=q]');
			
			if (search.val() == '')
			{
				var selectedBrand = $('#brands option:selected');
				if (selectedBrand.val() != '0')
					search.val(selectedBrand.text());
			}
			
			if (!search.val())
			{
				alert('Please enter something to search for.');
				return false;
			}
		});		
		
		// Product / basket page
		$('.condition').css(
		{
			position: 'absolute',
			marginLeft: 80,
			marginTop: -100,
			zIndex:10000,
			cursor: 'pointer'
		}).click(function()
		{
			$('.condition').fadeOut(300);
		});
		
		$('#condition').change(function()
		{
			$('.condition').fadeOut(300);
			$('#condition-' + $(this).val()).fadeIn(300);
			
			var price = parseFloat(prices[$(this).val()]);
			
			$('.price').text('£' + price.toFixed(2));
			
			if (price)
			{
				$('.price-tag').show();
				$('#zero-value').hide();
				$('#select-condition').hide();
				$('#choose-imei').show();
			}
			else if ($(this).val())
			{
				$('.price-tag').hide();
				$('#zero-value').show();
				$('#select-condition').hide();
				$('#choose-imei').show();
			}
			else
			{
				$('.price-tag').hide();
				$('#zero-value').hide();
				$('#select-condition').show();
				$('#choose-imei').hide();
			}
		}).change();
		
		$('#choose-imei').hide();
		
		$('input.imei').focus(function()
		{
			$(this).addClass('nonDefaultValue');
			if ((this.value == this.defaultValue) && !/^[\d ]*$/.test(this.defaultValue))
				this.value = '';
		}).blur(function()
		{
			if (!this.value.length || ((this.value == this.defaultValue) && !/^[\d ]*$/.test(this.defaultValue)))
			{
				this.value = this.defaultValue;
				$(this).removeClass('nonDefaultValue');
			}
			else
			{
				this.value = this.value.replace(/[^\d ]/g, '');
			}
		}).keypress(function(e)
		{
			var key = String.fromCharCode(e.which || e.keyCode);
			return /[\d ]/.test(key) || (key.charCodeAt(0) == 13);
		});
		
		$('#product-form').submit(function()
		{
			if (!$(this).find('select').val())
			{
				alert('Please select the condition of your product');
				return false;
			}
			
			if (!/^\d{15,16}$/g.test($(this).find('input.imei').val()))
			{
				alert('Please enter your 15 or 16 digit IMEI number');
				return false;
			}
			
			return true;
		});
		
		/*$('#checkout_0_form').submit(function()
		{
			if (!$(this).find('input').val())
			{
				alert('Please fill in your address');
				return false;
			}
			return true;
		});*/
		
		$('a.update').click(function()
		{
			$(this).closest('form').submit();
			return false;
		});
		
		// Checkout stuff
		if ($('#checkout_0').length)
		{
			$('.payment-method-detail').hide();
			
			$('input[name=checkout_0_payment_method]').change(function()
			{
				$('input[name=checkout_0_payment_method]').each(function()
				{
					var $this = $(this);
					$('#payment-' + $this.val()).css('display', $this.is(':checked') ? 'block' : 'none');
				});
			}).eq(0).change();
			
			$('#tandc-container').hide();
			$('#tandc-link').click(function()
			{
				$('#tandc-container').modal();
				return false;
			});
		}
		
		// FAQ page
		if ($('#faq').length)
		{
			$('h3').each(function()
			{
				var answer = [];
				var e = this.nextSibling;
				
				while (e && !$(e).is('h2') && !$(e).is('h3'))
				{
					if (e.nodeType == 1)
						answer.push(e);
					
					e = e.nextSibling;
				}
				
				answer = $(answer);
				
				answer.hide();
				var hidden = true;
				
				$(this).click(function()
				{
					answer.stop();
					
					if (hidden)
						answer.fadeIn();
					else
						answer.fadeOut();
					
					hidden = !hidden;
				}).css('cursor', 'pointer');
			});
		}
	});
})(jQuery);

