var doingThemeChange = 0;

$(document).ready(function() {
	$('#password').keypress(function(e) {
	    if(e.keyCode == 13) {
	    	doLogin();
	    }
	});
});

function doLogin()
{
	if($('#remember').is(':checked'))
	{
		var rememberData = '1';
	}
	else
	{
		var rememberData = '0';
	}
	$.ajax({
		type:	'POST',
		url:	'http://plustg.com/v2/_inc/_php/_ajax/doLogin.php',
		data:	{'email':$('#E-Mail').val(),'password': $('#password').val(),'remember': rememberData},
		success: function(xml){
			if($(xml).find('success').text() == 0)
			{
				showNotification('Error',$(xml).find('message').text());
			}
			else
			{
				location.reload();
			}
		}
	});
}

function doTheme(theme)
{
	if(doingThemeChange == 0)
	{
		doingThemeChange = 1;
		$.ajax({
			type:	'POST',
			url:	'http://plustg.com/v2/_inc/_php/_ajax/doTheme.php',
			data:	'theme='+theme,
			success: function(xml){
				if($(xml).find('success').text() == 0)
				{
					showNotification('Error',$(xml).find('message').text());
				}
				else
				{
					location.reload();
				}
				doingThemeChange = 0;
			}
		});
	}
	else
	{
		showNotification('Success','We\'re handling your theme change, please hang on tight!');
	}
}

function showNotification(type,message)
{
	var $alertdiv = $('<div class="notification'+type+'"/>');
	$alertdiv.text(message);
	$alertdiv.bind('click', function() {
	    $(this).slideUp(200, function(){
	    	$(this).remove();
	    });
	});
	$(document.body).append($alertdiv);
	$(".notification"+type).slideDown("slow"); 
	if(type != 'Error')
	{
		setTimeout(function(){ 
			$alertdiv.slideUp(200, function(){
		    	$(this).remove();
		    });
		}, 3000);
	}
}
