/*
	Author: Mathew Byrne <mathew@jbinteractive.com.au>
	Copyright (c) JB Interactive Pty. Ltd
	All Rights Reserved
*/

(function ($) {

$(document).ready(function ($) {
	
	$('label.radio')
		.click(function () {
			var id = $(this).attr('for');
			$('#' + id).attr('checked', true);
		});

	$("#purchase_order[value='']")
		.val('PO #')
		.addClass('faded')
		.one('focus', function () {
				$(this).val('')
			});

	
	// Add a # character to the deakin card text box.
	$("#deakin_card[value='']")
		.val('#')
		.addClass('faded')
		.one('focus', function () {
				$(this).val('')
			});
	
	// Gather vars for registration form.
	var $deakinCard = $('#deakin-card').remove();
	var $staffInfo = $('#info-1').remove();
	var $companyInfo = $('#info-2').remove();
	
	// For registration page. Ensures that fields are hidden depending on
	// what's selected when the page is ready.
	$('input[name=type]')
		.click(function () {

			var $info = $('#info');
			$info.children('div').remove();
			
			if ($(this).val() == '1') {
				$staffInfo.appendTo($info);
				$('#payment_method')
					.children().eq(0)
						.after($deakinCard);
			} else {
				$('#deakin-card').remove();
				$companyInfo.appendTo($info);
			}
		});
	
	// Setup state depending on what's set currently.
	var $checked = $('input[name=type]:checked');
	if ($checked.size() == 0) {
		$checked = $('input[name=type]').eq(0).attr('checked', 'checked');
	}
	$checked.trigger('click');
	
	// Animations when showing the forgotten login form.
	var animForm = function (left) {
		$('#carousel').animate ({'left': left}, 300, 'swing');
		return false;
	};
	$('#forgotten-show').click(function () { return animForm(0) });
	$('#login-show').click(function () { return animForm(-750) });
	
	// Preload mouseover image.
	var on  = '/images/layout/btn-login-on.gif';
	var off = '/images/layout/btn-login.gif';
	$('<img />').attr('src', on);
	$('#util img')
		.hover(
			function () { $(this).attr('src', on); },
			function () { $(this).attr('src', off); }
		);
	
	// Add the calendar control to the date field.
	var yesterday = new Date ();
	yesterday.setDate(yesterday.getDate() - 1);
	if ($('#date').size()) {
		$('#date')
			.calendar({
				closeAtTop: false,
				autoPopUp: 'both',
				buttonImageOnly: true,
				buttonImage: '/images/layout/ico-calendar.gif',
				currentText: '',
				minDate: yesterday
			});
	}
	
	// Hovers.
	$('button')
		.hover(
			function () { $(this).addClass('over') },
			function () { $(this).removeClass('over') }
		);
	
	// Email and Password text in login form.
	$('#login-form input')
		.filter('[name=username]')
			.focus(function() {
				if ($(this).val() == 'Email') {
					$(this)
						.val('')
						.removeClass('faded');
				}
			})
			.blur(function () {
				if (!$(this).val().length) {
					$(this)
						.val('Email')
						.addClass('faded');
				}
			})
			.trigger('blur')
		.end()
		.filter('[name=password]')
			.focus(function () {
				if ($(this).val() == 'Password') {
					$(this)
						.val('')
						.removeClass('faded')
				}
			})
			.blur(function () {
				if (!$(this).val().length) {
					$(this)
						.val('Password')
						.addClass('faded')
				}
			})
			.trigger('blur')
		.end();

});

})(jQuery);
