
$(document).ready(function(){
	
	var createRound = false;
	
	// Bugfix IE6 + Flash Proxy blank Page
	FB.Flash.hasMinVersion=function(){return false;};
	FB.init({appId: FACEBOOK_APP_ID, cookie: true, xfbml: true});
	

	$("a.login").hide();
	FB.getLoginStatus(function(response) {
		if (!response.session && window.location.href.indexOf("Logout")==-1 && window.location.href.indexOf("Price")==-1 && window.location.href.indexOf("Login")==-1) {
			// no user session available, someone you dont know
			//	FB.logout();
			try{
				$.ajax({
					url:self+"&fuseaction=App.Logout",
					dataType:"html",
					type:"GET",
					success:function(){}
				});
				FB.logout(function(response) { document.location.href=self+"&fuseaction=App.Logout"; });
			}catch(e){
				document.location.href=self+"&fuseaction=App.Logout";
			}
		}
		if(!response.session){
			$("a.login").show();
			$(".menue_2,.menue_3").attr("onclick","").unbind("click").click(function(event){
				createRound=true;
				//$('#loginbutton a').click();
				FB.login(function(response) {
					if(window.location.href.indexOf("EntryPage")==-1){
						window.location.href=self+"&fuseaction=App.EntryPage";
					} else {
						window.location.reload();
					}
				},{perms:"email,user_birthday,read_friendlists,user_likes,publish_stream"});
				event.stopPropagation();
				event.preventDefault();
			});
			$(".connect,a.login").attr("onclick","").unbind("click").click(function(event){
				//$('#loginbutton a').click();
				FB.login(function(response) {
					if(window.location.href.indexOf("EntryPage")==-1){
						window.location.href=self+"&fuseaction=App.EntryPage";
					} else {
						window.location.reload();
					}
				},{perms:"email,user_birthday,read_friendlists,user_likes,publish_stream"});
				event.stopPropagation();
				event.preventDefault();
			});
		} else {
			$(".connect,a.login").attr("onclick","").unbind("click").click(function(event){
				event.stopPropagation();
				event.preventDefault();
			});
		}
	});
	
	$(".connect,a.login").attr("onclick","").unbind("click").click(function(event){
		$('#loginbutton a').click();
		event.stopPropagation();
		event.preventDefault();
	});	
	
	
	$(".logout").attr("onclick","").unbind("click").click(function(event){
		try{
			$.ajax({
				url:self+"&fuseaction=App.Logout",
				dataType:"html",
				type:"GET",
				success:function(){}
			});
			FB.logout(function(response) { document.location.href=self+"&fuseaction=App.Logout"; });
		}catch(e){
			document.location.href=self+"&fuseaction=App.Logout";
		}
		event.stopPropagation();
		event.preventDefault();
	});	
	
	FB.Event.subscribe('auth.sessionChange', function(response) {
		if (response.session) {
			if(window.location.href.indexOf("App.")!=-1) {
		    	// A user has logged in, and a new cookie has been saved
		    	if(createRound){
		    		window.location.href=self+"&fuseaction=App.CreateRound";
		    	} else {
		    		window.location.href=self+"&fuseaction=App.EntryPage";
		    	}
			}
	    }
	});	
	
	
});
