function fblogin()
{
	$('#fb_login_image').hide();
	$('#fb_loading_image').show();

	FB.login(function(response) {
	  if (response.session) {
		ajax_fblogin(true);
	  } else {
		 $('#fb_login_image').show();
		 $('#fb_loading_image').hide();
	  }

	}, {perms:'email,user_birthday'});

}

function ajax_fblogin(openIntegrationBox)
{
	$('#fb_login_image').hide();
	$('#fb_loading_image').show();

	jQuery.post('/users/ajax_fblogin', null, function(data){
		if (data.status == 'success') {
			window.location.reload();
		} else if(data.status == 'first') {
			if (openIntegrationBox)
				$.fn.colorbox({href:'/users/ajax_fbnew', open:true});
			else
			{
				$('#fb_login_image').show();
				$('#fb_loading_image').hide();
			}
		} else {
			$('#fb_login_image').show();
			$('#fb_loading_image').hide();
			alert(data.status+" "+data.message);
			//alert("Zalogowanie nieudane. Spróbuj ponownie.");
		}
	}, "json");
}

function fblogout(redir)
{
	FB.getLoginStatus(function(response) {
		if (response.session)
			FB.logout(function(response) {
				window.location = redir;
			});
		else
			window.location = redir;
	});
	
}

function createCookie(name,value,mins) {
	if (mins) {
		var date = new Date();
		date.setTime(date.getTime()+(mins*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else
		var expires = "";
	
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}