$(function(){
	
	$('#bb-popup').dialog({
		modal: true,
		autoOpen: false,
		buttons: {
			'OK': function(){$(this).dialog('close');}
		}
	});
	
	
	// --------------------------------------------------------------------------------- LOGIN / LOGOUT
	
	$('input[name=password]').focus(function(){
		var value = $(this).val();
		if(value == 'Password'){
			$(this).val('');
		}
	}).blur(function(){
		var value = $(this).val();
		if(value == ''){
			$(this).val('Password');
		}
	});
	$('input[name=username]').focus(function(){
		var value = $(this).val();
		if(value == 'E-Mail'){
			$(this).val('');
		}
	}).blur(function(){
		var value = $(this).val();
		if(value == ''){
			$(this).val('E-Mail');
		}
	});
	
	$('#bb-login').click(function(){
		var username = $('#username').val();
		var password = $('#password').val();
		$.ajax({
			url: 'processors/login.php',
			type: 'POST',
			data: {username:username,password:password},
			success: function(msg){
				$('#bb-popup').html('').dialog('option','title','Login Failed');
				switch(msg){
					case 'nm':
						$('#bb-popup').html('Your username and password do not match.').dialog('open');
					break;
					case 'np':
						$('#bb-popup').html('Your username and password do not match.').dialog('option', 'buttons', {
							"Ok": function() {
								$(this).dialog("close");
							},
							"Forgot Password": function(){
								var email = $('#username').val();
								getPass(email);
							}
						});
						$('#bb-popup').html('Your username and password do not match.').dialog('open');
					break;
					case 'va':
						$('#bb-popup').html('You must verify your account before you can login.').dialog('open');
					break;
					case '0':
						location.reload();
					break;
				}
			}
		});
		return false;
	});
	$('#bb-logout').click(function(){
		$.ajax({
			url: 'processors/logout.php',
			success: function(){
				window.location='index.php';
			}
		});
		return false;
	});
	
	var lha = $('#login-box').height();
	$('#login-box').css('top',-(lha) + 'px')
	
	$('#login-toggle').click(function(){
		var lta = $('#login-box').css('top');
		//alert('lta:' + lta + '; lha:' + lha);
		if(lta == -(lha) + 'px'){
			$('#login-box').animate({top:(-2)},300);
		}else{
			$('#login-box').animate({top:-(lha)},300);
		}
	});
	
	
	// --------------------------------------------------------------------------------- BUTTONS
	/*$('.bb-button, .button').live('mouseover',function(){
		$(this).addClass('ui-state-hover');
	});
	$('.bb-button, .button').live('mouseout',function(){
		$(this).removeClass('ui-state-hover');
	});*/
	$('.bb-load').live('click',function(){
		$('#bb-main-preloader-bg').css({opacity:.6}).show();
		$('#bb-main-preloader-fg').show();
	});
	
	$('button').button();
	$('input:submit').button();
	$('input:button').button();
	$('input:reset').button();
	$(':radio').buttonset();
	$('.box').button();
	$('.boxes').buttonset();
	
	
	// --------------------------------------------------------------------------------- REGISTER
	$('#bb-register-submit').click(function(){
		var nameFirst = $('#nameFirst').val();
		var nameLast = $('#nameLast').val();
		var email = $('#email').val();
		var vemail = $('#vemail').val();
		var passw = $('#passw').val();
		var vpass = $('#vpass').val();
		$.ajax({
			type: 'POST',
			url: 'processors/register.php',
			data: {nameFirst:nameFirst,nameLast:nameLast,email:email,vemail:vemail,pass:passw,vpass:vpass},
			success: function(msg){
				$('#registerSuccess, #registerFail').slideUp();
				$('#success-msg, #fail-msg').html('');
				$('input').removeClass('ui-state-error');
				if(msg != '0' || msg != 0){
					if(msg.match(/de/)){
						$('#fail-msg').html('There is an account already registered with that e-mail address. Please use another e-mail or request your <button type="button" id="passwordRequest" class="ui-state-default ui-corner-all bb-button">password</button>. ');
					}
					if(msg.match(/nc/)){
						$('#fail-msg').append('We could not connect to the database. Please try again later. ');
					}
					if(msg.match(/nf/)){
						$('#nameFirst').addClass('ui-state-error');
					}
					if(msg.match(/nl/)){
						$('#nameLast').addClass('ui-state-error');
					}
					if(msg.match(/em/)){
						$('#email, #vemail').val('').addClass('ui-state-error');
					}
					if(msg.match(/ve/)){
						$('#email, #vemail').val('').addClass('ui-state-error');
					}
					if(msg.match(/pa/)){
						$('#passw, #vpass').val('').addClass('ui-state-error');
					}
					if(msg.match(/vp/)){
						$('#passw, #vpass').val('').addClass('ui-state-error');
					}
					if(msg.match(/(nf|nl|em|ve|pa|vp)/)){
						$('#fail-msg').append('Please fix the following fields:');
					}
					$('#registerFail').slideDown();
				}else{
					$('#success-msg').text('Thank you for registering. Please Check your e-mail for further instructions.');
					$('.input-register').attr('disabled','disabled').addClass('ui-state-disabled');
					$('#registerSuccess').slideDown();
				}
			}
		});
	});
	
	
	// --------------------------------------------------------------------------------- PASSWORD REQUEST
	$('#passwordRequest').live('click',function(){
		var email = $('#email').val();
		getPass(email);
	});
	
	
	// --------------------------------------------------------------------------------- ACOUNT
	$('#account_tabs').tabs();
	
	$('#settings_pi').click(function(){
		var nameFirst = $('#nameFirst').val();
		var nameLast = $('#nameLast').val();
		var email = $('#email').val();
		var pass = $('#pass').val();
		var vpass = $('#vpass').val();
		var zone = $('#zone').val();
		$.ajax({
			url: 'processors/updateSettings.php?group=pi',
			type: 'post',
			data: {nameFirst:nameFirst,nameLast:nameLast,email:email,pass:pass,vpass:vpass,zone:zone},
			success: function(msg){
				updateSettings(msg,'pi');
			}
		});
	});
	$('#settings_ge').click(function(){
		var subscribe_news = 0;
		if($('#subscribe_news').attr('checked')){
			subscribe_news = 1;
		}
		$.ajax({
			url: 'processors/updateSettings.php?group=ge',
			type: 'post',
			data: {subscribe_news:subscribe_news},
			success: function(msg){
				updateSettings(msg,'ge');
			}
		});
	});
	
	
	// --------------------------------------------------------------------------------- ARTICLES
	$('.view_article').click(function(){
		var id = $(this).attr('id');
		var vis = $('#article_' + id).css('display');
		if(vis == 'none'){
			$('#article_' + id).slideDown(1000);
		}else{
			$('#article_' + id).slideUp(1000);
		}
	});
	
	
	
	// --------------------------------------------------------------------------------- PLACES
	$('table#places').dataTable({
		"bJQueryUI": true,
		"sPaginationType": "full_numbers"
	});
	
	
	// --------------------------------------------------------------------------------- EVENTS
	$('table#events').dataTable({
		"bJQueryUI": true,
		"sPaginationType": "full_numbers"
	});
	
	
	// --------------------------------------------------------------------------------- ORGANIZATIONS
	$('table#organizations').dataTable({
		"bJQueryUI": true,
		"sPaginationType": "full_numbers"
	});
	
	
	// --------------------------------------------------------------------------------- GROUPS
	$('table#groups').dataTable({
		"bJQueryUI": true,
		"sPaginationType": "full_numbers"
	});
	
	
	// --------------------------------------------------------------------------------- SERMONS
	$('#AudioSermons').dataTable({
		"bJQueryUI": true,
		"sPaginationType": "full_numbers",
		"aoColumns": [
			null,
			null,
			null,
			null,
			{
				"bSortable":false,
				"sWidth": "60px"
			}
		]
	});
});
	
	
	// --------------------------------------------------------------------------------- FUNCTIONS

function getPass(email){
		$.ajax({
			url: 'processors/requestPass.php',
			type: 'POST',
			data: {email:email},
			success: function(){
				window.location='index.php?page=passRequested';
			}
		});
	}// Request Email
function updateSettings(msg,group){
	$('#settingsSuccess, #settingsFail').slideUp();
	$('#success-msg, #fail-msg').html('');
	$('input').removeClass('ui-state-error');
	switch(group){
		case 'pi':
			if(msg != '0' || msg != 0){
				if(msg.match(/de/)){
					$('#fail-msg').html('There is an account already registered with that e-mail address. Please use another e-mail or request your <button type="button" id="passwordRequest" class="ui-state-default ui-corner-all bb-button">password</button>.');
				}
				if(msg.match(/nc/)){
					$('#fail-msg').append('We could not connect to the database. Please try again later. ');
				}
				if(msg.match(/nf/)){
					$('#nameFirst').addClass('ui-state-error');
				}
				if(msg.match(/nl/)){
					$('#nameLast').addClass('ui-state-error');
				}
				if(msg.match(/em/)){
					$('#email').addClass('ui-state-error');
				}
				if(msg.match(/pa/)){
					$('#pass, #vpass').val('').addClass('ui-state-error');
				}
				if(msg.match(/vp/)){
					$('#pass, #vpass').val('').addClass('ui-state-error');
				}
				if(msg.match(/(nf|nl|em|ve|pa|vp)/)){
					$('#fail-msg').append('Please fix the following fields:');
				}
				$('#settingsFail').slideDown();
			}else{
				$('#success-msg').text('Your settings have been updated.');
				$('#settingsSuccess').slideDown();
			}
		break;
		case 'ge':
			if(msg != '0' || msg != 0){
				if(msg.match(/nc/)){
					$('#fail-msg').append('We could not connect to the database. Please try again later. ');
				}
				$('#settingsFail').slideDown();
			}else{
				$('#success-msg').text('Your settings have been updated.');
				$('#settingsSuccess').slideDown();
			}
		break;
	}
	}
function viewArticle(id,count){
	var vis = $('#article_' + id).css('display');
	if(vis == 'none'){
		$.ajax({
			type: 'post',
			url: 'processors/article_view.php',
			data: {id:id,count:count}/*,
			success: function(msg){
				alert(msg);
			}*/
		});
	}
	}