// Begin jQuery

$(document).ready(function(){
// Set variables
	$.metadata.setType('attr','data-json');
	
// 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 ){
		// 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;
		}
		
	});

	// Set registration event
	$('.submit.register').click(function( e ){
		$.voAjax({
			dataContainer:'#register',
			actionController: '/action/public/vo/register',
			success:[showResponse]
		});
	});
	
	function showResponse( data, status)
	{
		var message = data.message;
		var context = $('.voForm:visible');
		
		$('.message',context).fadeOut('slow',function(){
			if(message == 'Success'){
				window.location.href=data.redirect_url;
			}else{
				$(this).empty().append(message).fadeIn('slow');
			}
		});	
	};
	
	$('#dialog-forgot-password').dialog({
		'autoOpen':false,
		'bgiframe':true,
		'height':300,
		'width':400,
		'modal':true,
		'title':'Forgot My Password',
		'buttons':{
			'Ok':function(){
				$.voAjax({
					'dataContainer':this,
					'actionController':'/action/public/vo/forgot-password',
					'success':[function( data ){ 
						$('#dialog-forgot-password .message').text(data.message).fadeIn('slow');
						setTimeout("$('#dialog-forgot-password').dialog('close')",4000);
						setTimeout("$('#dialog-forgot-password .message').fadeOut('slow')",3000);
					}]
				});
			},
			'Cancel':function(){ $('#dialog-forgot-password').dialog('close'); } 
		}
	});
	$('#forgot-my-password').click(function(){
		$('#dialog-forgot-password').dialog('open');
	});
	
});
