// Begin jQuery
var voUserLoginLoaded = 1;
$(document).ready(function(){

// Registration form
//	$('input[name=password2]').parent('li').hide();
//	$('div#login-now-register p:eq(1)').hide();

	// Disable login password if the new customer radio input is checked
	$('#login-now :radio[name=status]',this).change(function(e){
		if($(e.target).val() == 'new-customer'){
			$('#login-now :input.password').attr('disabled','disabled');
		}else{
			$('#login-now :input.password').removeAttr('disabled');
		}
	});

	$('.submit.login').click(function( e ){
		// Login
		$.voAjax({
			dataContainer:'#login-now',
			actionController: '/action/public/vo/login-limited',
			dataObject:{'vo-action':'login'},
			beforeSend:[function(){ $('#login-now .waiting-for-results').show(); }],
			success:[showResponse]
		});
		
		// If returning customer then submit to login
		// If not proceed to next form to register
		/*switch($('#login-now :radio[name=status]:checked').val()){
		case 'returning-customer':
			// Login
			$.voAjax({
				dataContainer:'#login-now',
				actionController: '/action/public/vo/login',
				success:[showResponse]
			});
			break;
			
		case 'new-customer':
			// Next step to register
			$('#register :input.email').val($('#login-now :input.email').val()).parent('li').removeClass('warning');
			$('#login-now').fadeOut('fast',function(){
				$('#register').fadeIn();
			});
			break;
		}*/
	});
	
	if($.jqPHP.getVar('jump_to_register')){
		$('#login-now').hide('fast',function(){
			$('#register').fadeIn('fast');
			$('#register').voCheckWarnings();
		});
	}else{
		//$('#login-now').fadeIn('fast');
	}

	// Set registration event
	$('.submit.register').click(function( e ){
		// Custom Herald Code: sets whether susbcriber gets the Herald delivered already or not.
		// Critical! Must be set before registration is set in order to route properly.
		$.voAjax({
			dataContainer:'#register',
			actionController:'/action/public/vo/herald-set-paper-delivery',
			success:[function(){
				$.voAjax({
					dataContainer:'#register',
					actionController: '/action/public/vo/register',
					success:[showResponse]
				});
			}]
		});
		
	});
	
	function showResponse( data, status)
	{
		$('#login-now .waiting-for-results').hide();
		var message = data.message;
		var context = $('.voForm:visible');
		
		$('.message',context).fadeOut('slow',function(){
			if(message == 'Success'){
				if( data.redirect_url ){
					window.location.href = data.redirect_url;
				}else{
					window.location.href = window.location.href;
				}
			}else{
				$(this).empty().append(message).fadeIn('slow');
			}
		});
		
		// Handle users that are limited to being logged in at one computer
		if( data.login_limit_message ){
			$loginLimitDialog = $('<div id="login-limit">'+ data.login_limit_message +'</div>').appendTo('body');
			$loginLimitDialog.dialog({
				title: 'Login Limit Alert',
				bgiframe: true,
				resizable: false,
				height:200,
				width:300,
				modal: true,
				overlay: {
					backgroundColor: '#000',
					opacity: 0.5
				},
				buttons: {
					'Cancel': function()
					{
						$.voAjax({
							actionController: '/action/public/vo/login-limited',
							dataObject:{'vo-action':'login_limit','override':0},
							success:[function(){
								$loginLimitDialog.dialog('close');
								$loginLimitDialog.dialog('destroy');
								$('#login-limit').remove();
							}]
						});
					},
					'Continue': function()
					{
						$.voAjax({
							actionController: '/action/public/vo/login-limited',
							dataObject:{'vo-action':'login_limit','override':1},
							success:[function(){
								$loginLimitDialog.dialog('close');
								$loginLimitDialog.dialog('destroy');
								$('#login-limit').remove();
								if( data.redirect_url ){
									window.location.href = data.redirect_url;
								}else{
									window.location.href = window.location.href;
								}
							}]
						});
						// signout other computers by destroying their sessions.
					}
				}
			});
		}
	};
	
	$('#dialog-forgot-password').dialog({
		'autoOpen':false,
		'bgiframe':true,
		'height':150,
		'width':350,
		'modal':true,
		'resizable':false,
		'title':'Forgot My Password',
		'buttons':{
			'Reset Password':function(){
				$.voAjax({
					'dataContainer':this,
					'actionController':'/action/public/vo/forgot-password',
					'success':[function( data ){ 
						if( data.success ){
							$('#dialog-forgot-password .message').empty().text(data.message).fadeIn();
							setTimeout("$('#dialog-forgot-password .message').fadeOut('slow')",6000);
						}else{
							$('#dialog-forgot-password .message').empty().text(data.message).fadeIn();
						}
					}]
				});
			},
			'Cancel':function(){ $('#dialog-forgot-password').dialog('close'); } 
		}
	});
	$('.forgot-my-password').click(function(){
		$('#dialog-forgot-password').dialog('open');
	});
	
});