$(document).ready(function() {
	$('.menu .element').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	
	$('#menu .menu .submenu .position:first-child, #main .news .news:first-child, #footer .menu .element:first-child').addClass('first-child');
	$('.submenu').each(function () { $(this).children('.position:odd').addClass('odd'); });
	
	// Avoid people clicking twice due to a slow response from OPST
	var preloaded_image = new Image();
	preloaded_image.src = '/images/blue_wait.gif';
	function remove_button_upon_submission(form, button) {
		$(form).submit(function () {
			$(button).html('Submitting details. <img src="/images/blue_wait.gif" alt="Please wait..." />');
		});
	}
	remove_button_upon_submission('form#new_user', 'form#new_user .submit');
	remove_button_upon_submission('form.forgot_password', 'form.forgot_password .submit');

	// Highlight question on FAQ
	var match = window.location.href.match(/#faq-.*$/)
	if (match) {
		var id = match[0];
		$(id).css({backgroundColor: '#FFF6BF', border: '2px solid #FFD324', padding: '1px 1em', marginBottom: '1em'});
	}

	// Conditional fields in signup form
	var $profession = $('#user_profession');
	if ($profession.size() > 0) {
		var $gmc_stuff = $('.gmc-number-stuff');
		var $profession_stuff = $('.profession-stuff');
		
		function toggle_gmc() {
			if ($profession.val() == 'gp' || $profession.val() == 'secondary') {
				$gmc_stuff.show('fast');
			}
			else {
				$gmc_stuff.hide('fast');
			}
		}

		function toggle_profession() {
			if ($('input[type=radio][id^=user_works_in_hc]:checked').val() == '1') {
				$profession_stuff.show('fast');
				toggle_gmc();
			}
			else {
				$profession_stuff.hide('fast');
				$gmc_stuff.hide();
			}
		}

		toggle_profession();
		$profession.click(toggle_gmc);
		$('input[type=radio][id^=user_works_in_hc]').click(toggle_profession);
	}
	
	// Toggle reward recipient label
	var recipient_wrap = $('#redeem #recipient-wrap');
	var recipient_label = $('#redeem .label');
	$('#redeem input[type=radio]').change(toggle_recipient_label);
	toggle_recipient_label();
	function toggle_recipient_label() {
		if ($('#redemption_redeem_as_cheque').attr('checked')) {
		    recipient_wrap.show('fast');
			recipient_label.html('Please specify the address where you would like to receive the cheque:');
		}
		else if ($('#redemption_redeem_as_donation').attr('checked')) {
		    recipient_wrap.show('fast');
			recipient_label.html('Please specify the name of the charity that should receive the donation:');
		}
		else {
	        recipient_wrap.hide();
		}
	}
	
	// "Tell a friend" textarea
	var tafarea = $('#friends textarea');
	if (tafarea.val() == '') {
		function clear_tafarea() {
			tafarea.val('');
			tafarea.attr('style', null);
			tafarea.unbind('click', clear_tafarea);
			tafarea.unbind('focus', clear_tafarea);
		}
		tafarea.val("friend.one@example.com\nfriend2@example.org\nfriend_three@example.net");
		tafarea.attr('style', 'color: #999;');
		tafarea.click(clear_tafarea);
		tafarea.focus(clear_tafarea);
	}
	
	// Google Map on "Contact us" page
	var contact = $('#contact-us');
	if (contact.size() > 0) {
		contact.append('<p id="location-map" style="height: 300px; width: 400px; float: right;"></p>');
		var GM = google.maps;
		var map = new GM.Map2($('#location-map').get(0));
		var center = new GM.LatLng(51.514992, -0.1373);
		var marker = new GM.Marker(center);
		
		map.setCenter(center, 15);
		map.addOverlay(marker);
		map.addControl(new GM.SmallMapControl());
	}
	
    // Redemption form
    $('#redemption-form').submit(function () {
        $('.submit').html('Processing. Please wait... <img src="/images/blue_wait.gif" />')
    });

});

// Inspired by http://jehiah.cz/archive/prototype-powered-popup-script
jQuery.fn.popup = function(options) {
	this.options = {
		url: this.get(0).href,
		width: 800,
		height: 600,
		name: "_blank",
		location: "no",
		menubar: "no",
		toolbar: "no",
		status: "yes",
		scrollbars: "yes",
		resizable: "yes",
		left: "",
		top: "",
		normal: false
	}
	this.options = jQuery.extend(this.options, options || {});
   
	if (this.options.normal){
		this.options.menubar = "yes";
		this.options.status = "yes";
		this.options.toolbar = "yes";
		this.options.location = "yes";
	}
   
	this.options.width  = this.options.width  < screen.availWidth  ? this.options.width  : screen.availWidth;
	this.options.height = this.options.height < screen.availHeight ? this.options.height : screen.availHeight;

	var openoptions = 'width=' + this.options.width + ',height=' + this.options.height + ',location=' + this.options.location + ',menubar=' + this.options.menubar + ',toolbar=' + this.options.toolbar + ',scrollbars=' + this.options.scrollbars + ',resizable=' + this.options.resizable + ',status=' + this.options.status;
	
	if (this.options.top != "") {
		openoptions += ",top=" + this.options.top;
	}
	if (this.options.left != "") {
		openoptions += ",left=" + this.options.left;
	}
	window.open(this.options.url, this.options.name,openoptions);
	
	return false;
}
