/*
	Copyright (c) 2007-2008 JB Interactive Pty. Ltd.
	All Rights Reserved
	http://www.jbinteractive.com.au/
	
	A short note on the contents of this file. This had been written keeping in
	mind the need to update the code in the CMS. As such this does a lot more
	than it really needs to. Hopefully this will be back-ported with some
	general improvements to the CMS in the near future. 
*/

(function ($) {

var STAFF_ONLY = '/pages/staff-only';

$(document).ready(function () {
	
	// Login link for Membership portal.
	$('#login')
		.click(function (e) {
			e.preventDefault();
			window.open('https://membership.dusa.org.au/', 'portal',
				[
					'toolbars=0',
					'scrollbars=1',
					'location=0',
					'statusbars=0',
					'menubars=0',
					'resizable=0',
					'width=820',
					'height=560'
				].join(', '));
		});
	
	// FancyZoom setup
	zoomImagesURI = '/img/zoom/';
	setupZoom();
	
	// IE hover fix
	$('li').hover(
		function () { $(this).addClass ('hover'); },
		function () { $(this).removeClass ('hover'); }
	);
	
	if (window.STAFF_ONLY_AUTHENTICATED) {
		return;
	}
	
	// Set Dialog defaults.
	JbCms.Dialog.default_transition = 'fade';
	
	// Create the Dialogs.
	var dialogs = window.dialogs = {
		login: JbCms.Dialog('#login_form', [300, 244]),
		reset: JbCms.Dialog('#reset_form', [300, 200]),
		success: JbCms.Dialog('#success_dialog', [300, 210])
	};
	
	var bindings = {
		
		'#login_button': function (e) {
			// Bind and submit form - trying to enforce browser to save 
			// username and password, seems to only happen when a form submission
			// occurs with autocomplete="on" for form element
			var form   = $('#overlay form');
			
			form.unbind('submit').bind('submit', function (e) {
				e.preventDefault();
				
				var url    = form.attr('action'),
					vals   = form.checkEmpty(),
					dialog = dialogs.login;

				if (form.find('#keep_alive:checked').length) {
					vals.keep_alive = 1;
				}

				if (!vals) {
					return;
				}

				$.post(url, vals, function (json) {
					if (json.valid) {
						// Load the Staff Only landing page.
						var loc = window.location.toString();
						var url = loc.indexOf(STAFF_ONLY) > 0
							? loc : STAFF_ONLY;
						window.location.assign(url);
					} else {
						dialog.element().parent().shake();
						$('#password').val('').focus();
					}
				}, 'json');
			}).trigger('submit');
		},
		
		'#forgotten_link': function (e) {
			dialogs.reset.show();
			$('#forgotten_username').label_fade();
		},
		
		'#forgotten_button': function (e) {
			var form   = $('#overlay form'),
				url    = form.attr('action'),
				vals   = form.checkEmpty();
			
			if (!vals) {
				return;
			}
			
			$.post(url, vals);
			dialogs.success.show();
			$('#forgotten_username').val('');
		},
		
		'#forgotten_cancel_button': function (e) {
			dialogs.login.show();
		},
		
		'#forgotten_ok': function (e) {
			dialogs.login.show();
		}
		
	};
	
	var fields = $('#username,#password');
	var close;
	
	var auth =  function () {
		dialogs.login.show();
		
		fields.label_fade();
		
		if (!close) {
			close = $('<a href="#close" class="dialog_close">Close</a>');
			close.appendTo('#dialog_container');
			close.click(function (e) {
				e.preventDefault();
				JbCms.Dialog.active().hide();
			});
		}
		
		setTimeout(function () {
			$('#username').focus();
		}, 100);
	};
	
	// Actually show the login dialog.
	$('.login_link').click(function (e) {
		e.preventDefault();
		auth();
	});
	
	// Submit logins with enter key
	fields.keydown(function (e) {
		if (e.keyCode == 13) {
			e.preventDefault();
			bindings['#login_button']();
		}
	});
	
	// Add functionality to Overlay for clicks!
	$.each(bindings, function (key, handler) {
		$(key).click(function (e) {
			e.preventDefault();
			handler(e);
		});
	});

	// Display the Login Dialog automatically if the user is currently on 
	// an unauthorized page.
	if (window.UNAUTHORIZED) {
		auth();
	}
});

})(jQuery);


