
	var Des = Des || {}

	Des.donation_form = function() {

		if ($('.donation_form')[0]) {
			function deal_with_it(response) {
				if (response) {
					donation_form
						.find('fieldset').animate({ opacity : 0 }, 250, 'linear').end()
						.closest('.donation_form_wrapper').html('<p class="confirm"><span>Your donation has been added to your giving basket</span> <a href="/gift-basket/">View Gift Basket &raquo;</a></p>').end()
						.remove()

					//	Insert HTML where necessary
					if (!$('.top_login .big_link')[0]) $('.top_login p').append(' | Total pledged: <strong>&pound;0</strong> | <a class="big_link" href="/gift-basket/">View Gift Basket</a>')
					if (!$('.pledged_val', values)[0]) values.append(' | <strong class="pledged_val">&pound;0</strong> in your <a href="/gift-basket/">Gift Basket</a>')

					top_current = $('.top_login strong').text().substr(1)
					top_new     = parseFloat(top_current) + amount;
					top_new     = RoundFixed(top_new, 2)
					$('.top_login p strong').html('&pound;' + top_new).effect('highlight', { color : '#fff' })

					current_val = $('.current_val', values).text().substr(1)
					new_val     = isNaN(current_val) ? amount : parseFloat(current_val) + amount;
					new_val     = RoundFixed(new_val, 2)
					$('.current_val', values).html('&pound;' + new_val).effect('highlight')

					pledged_cur = $('.pledged_val', values).text().substr(1)
					pledged_new = isNaN(pledged_cur) ? amount : parseFloat(pledged_cur) + amount;
					pledged_new = RoundFixed(pledged_new, 2)
					$('.pledged_val', values).html('&pound;' + pledged_new).effect('highlight')
				} else {
					error = "There was an error! Please try again!";
				}
			}

			$('.donation_form')
				.find('fieldset')
					.append('<input type="hidden" name="ajax" value="true" />')
						.end()
				.submit(function() {
					donation_form = $(this)

					button = $('button', donation_form)
					button.attr('disabled', 'disabled');

					error       = false;
					amount      = donation_form.find('input[name=amount]').val()
					values      = donation_form.closest('div.request_info').siblings('p.needs')
					target_val  = parseFloat($('strong.target_val',  values).text().substring(1))
					current_val = parseFloat($('strong.current_val', values).text().substring(1))
					max_val     = isNaN(current_val) ? target_val : target_val - current_val;
					max_val     = Round(max_val, 2)

					if (amount) {
						if (isNaN(amount)) {
							error = 'Your donation must be a number';
						} else if (amount == 0) {
							error = 'Your donation must be more than &pound;0';
						} else if (amount > max_val) {
							error = "Your donation can't exceed &pound;" + max_val;
						}
					} else {
						error = 'Your donation amount was empty!';
					}

					amount = parseFloat(amount)

					if (!error) {
						$.post(donation_form.attr('action'), donation_form.serialize(), function(response) { deal_with_it(response) }, 'text')
					} else {
						donation_form.find('label').html(error).addClass('error').effect('pulsate', { times : 2 })
						button.removeAttr('disabled')
					}

					return false;
				})
		}
	}



	Des.map = function() {
		if ($('.filter_map')[0]) {
			function map_highlight(region) {
				switch (region) {
					case 'north-east'       : bg_pos = '-280px  center'; break;
					case 'north-west'       : bg_pos = '-560px  center'; break;
					case 'yorkshire-humber' : bg_pos = '-840px  center'; break;
					case 'west-midlands'    : bg_pos = '-1120px center'; break;
					case 'east-midlands'    : bg_pos = '-1400px center'; break;
					case 'south-west'       : bg_pos = '-1680px center'; break;
					case 'south-east'       : bg_pos = '-1960px center'; break;
					case 'london'           : bg_pos = '-2240px center'; break;
					case 'east'             : bg_pos = '-2520px center'; break;

					case 'all-regions'      : bg_pos = '-2800px center'; break;

					default : bg_pos = 'left center'; break;
				}

				$('.filter_map img').css({ backgroundPosition : bg_pos })

				$('.filter_locations ul li a').removeClass('hover')

				if (region != 'default') $('.filter_locations ul li.' + region + ' a').addClass('hover')
			}

			current_region = $('.filter_map').attr('id').split('_')[1];

			map_src = '/img/design/map/js/'+current_region+'.png';

			$('<img />')
				.attr('src', map_src)
				.load(function() {
					$('.filter_map img').attr('src', '/img/design/map/trans.png')
					$('.filter_map img').css({ 'background-image' : 'url('+map_src+')', backgroundPosition : 'left center', backgroundRepeat : 'no-repeat'})

					$('map#locations area').hover(
						function() { map_highlight($(this).attr('class')) },
						function() { map_highlight('default') }
					)

					$('.filter_locations ul li').hover(
						function() { map_highlight($(this).attr('class')) },
						function() { map_highlight('default') }
					)
				})
		}
	}



	Des.requests = function() {
		$('.learn_more a').click(function() {
			$(this)
				.closest('li')
					.addClass('active')
					.find('div.request_info')
						.slideDown()
					.end()
				.end()
				.parent()
					.fadeOut()

			$('<div class="close_this"><a href="#">Close</a></div>')
				.appendTo($(this).closest('li'))
				.find('a')
					.click(function() {
						$(this)
							.closest('li')
								.removeClass('active')
									.find('div.request_info')
										.slideUp(500, function() {
											$(this)
												.find('label').text('How much would you like to donate?').removeClass('error')
											.end()
												.find('input').val('')
											.end()
												.closest('li')
													.find('p.learn_more')
														.fadeIn()
										})
								.end()
						.end()
						.fadeOut()

						return false;
					})

			return false;
		})

	}


	function Round(Number, DecimalPlaces) {
		return Math.round(parseFloat(Number) * Math.pow(10, DecimalPlaces)) / Math.pow(10, DecimalPlaces);
	}

	function RoundFixed(Number, DecimalPlaces) {
		return Round(Number, DecimalPlaces).toFixed(DecimalPlaces);
	}


	jQuery.fn.log = function(msg) { console.log("%s: %o", msg, this); return this; };

	//	Make Rocket Go Now
	$(function() {

		$('body').addClass('js')

		$.each([$('button'), $('input'), $('select'), $('textarea')], function() {

			$(this)
				.hover(
					function() { $(this).addClass('hover') },
					function() { $(this).removeClass('hover') }
				)
				.focus(function() { $(this).addClass('focus') })
				.blur(function()  { $(this).removeClass('focus') })

		})

		Des.donation_form()
		Des.map()
		Des.requests()

	})
