var access_token = "";
var ownUserinfos = {};
var onFB = 0;
var view2 = (onFB == 1 ? document : parent.document);

FB.init({
	appId: application_id,
	status: true,
	cookie: true,
	xfbml: true
});

if(onFB == 1) {
	FB.Canvas.setAutoResize(91);
	FB.getLoginStatus(function(response) {
		//console.log(response);
		//if(response.session) {
			if(response.status != "not_authorized") {
				access_token = response.authResponse.accessToken;
				checkUserInfos();
								setTimeout(function() {
					regularLogin();
				},2000);
							} else {
				//console.log("jau");
				top.location.href = dialog_oauth;
			}
		/*} else {
						top.location.href = dialog_oauth;
					}*/
	});
}

function fb_login() {
	FB.getLoginStatus(function(response) {
		//if(response.session) {
			checkUserInfos();
			setTimeout(function() {
				regularLogin("http://www.goaligan.com/");
			},2000);
		//}
	});
}
	
function regularLogin(from) {
	if(typeof from == "undefined")
		from = "http://www.goaligan.com/facebook/";
	$.post(HOST+"ajax/fb_login.php",{"fb_uid":ownUserinfos.id, "fb_firstname":ownUserinfos.first_name, "fb_email":ownUserinfos.email, "fb_locale":ownUserinfos.locale}, function() {
		document.location.href = from;
	});
}

function checkUserInfos() {
	FB.api('/me', function(userinfos) {
		ownUserinfos = userinfos;
	});
}

function getFBPicture(uid,typ) {
	if(typeof typ == "undefined") typ = "square";
	return "http://graph.facebook.com/"+uid+"/picture?type="+typ;
}

function loadFBPics(scope) {
	if(typeof scope == "undefined") scope = document;
	$("img.fb_userpic",scope).each(function() {
		if($(this).attr("alt").length != 0 && $(this).attr("src").indexOf("ajax_loader") != -1) {
			$(this).attr("src",getFBPicture($(this).attr("alt")));
		}
	});
}

function loadFriendList() {
	$("#inviteFriendsBox").load(HOST+"ajax/fb_getFriendslist.php",function() {
		loadFBPics();
	});
}

function makeWallpost(infos) {
	if(typeof infos.me == "undefined") infos.me = "me";
	FB.api('/'+infos.me+'/feed','post',{description: infos.description, name: infos.name, link: infos.link, picture: infos.picture});
}

function getAllFBFriends() {
	
}

function getAllFriends() {
	return getAny("getAllFriends","POST","","json");
}

function makeUserIDs() {
	
}

function fb_sort(a,b) {
	var x = a.name.toLowerCase();
    var y = b.name.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function makeMoneySack(infos) {
	var tmp = getAny("get_moneySackInfos",infos,"POST","json");
	var skel = '<div class="inFriendsNavi_bg collectBox '+infos.build+'" rel="'+tmp.clubid+'" id="'+infos.build+'" style="top:'+infos.top+'px;left:'+infos.left+'px;cursor:pointer">';
	skel += '<div class="TopLeft"></div>';
	skel += '<div class="TopMiddle"></div>';
	skel += '<div class="TopRight"></div>';
	skel += '<div style="clear:both"></div>';
	skel += '<div class="MiddleLeft"></div>';
	skel += '<div class="MiddleMiddle" style="height:62px">';
	skel += '<div>Sammeln</div>';
	skel += '<div class="greenbold">';
	skel += '<img src="images/img-icon_muentze.png" alt="" /> +<span class="money_'+infos.build+'">'+tmp.coins+'</span>';
	skel += '</div>';
	skel += '<div class="greenbold">';
	skel += '<img src="images/img-icon_xp_small.png" alt="" /> +<span class="xps_'+infos.build+'">'+tmp.xp+'</span>';
	skel += '</div>';
	skel += '</div>';
	skel += '<div class="MiddleRight"></div>';
	skel += '<div style="clear:both"></div>';
	skel += '<div class="BottomLeft"></div>';
	skel += '<div class="BottomMiddle"><img src="images/inFriendsNavi_bgBottomMiddlet_arrow.png" alt="" /></div>';
	skel += '<div class="BottomRight"></div>';
	skel += '<div style="clear:both"></div>';
	skel += '</div>';
	
	return skel;
}

$(document).ready(function() {
	loadFriendList();

	$(".collectBox").live("click",function() {
		var ident = $(this).attr("id");
		//var money = $("#"+ident+" .money").text();
		var money = $(".money_"+ident).text();
		money=parseInt(money.replace(/\./,''));
		//var xps = $("#"+ident+" .xps").text();
		var xps = $(".xps_"+ident).text();
		xps=parseInt(xps.replace(/\./,''));
		var clubid = $(this).attr("rel");
		
		//updateAny("money",money);
		//updateAny("xps",xps);
		
		$.post(HOST+"ajax/makeGathering.php",{m_value: money, x_value: xps, bui: ident, g_clubid: clubid},function(feedback){ 
			increase_money(money);
			increase_xp(xps);
		});

		
		$(this).fadeOut('fast',function() {
			$(this).remove();
		});
	});
	
	$(".inviteNewFbFriends").live("click",function() {
		FB.api('/me/friends',function(response) {
			var endFriends = new Array();
			var friends = getAllFriends();
			var tmp = response.data.sort(fb_sort);
			$(tmp).each(function(i, v) {
				if($.inArray(v.id,friends) == -1)
					endFriends.push(v.id);
			});
			
			FB.ui({
				method: 'apprequests',
				title: 'Lade deine Freunde zu Goaligan ein!',
				message: 'Du wurdest eingeladen, um Goaligan beizutreten',
				filters: [{ name:'Alle Freunde',user_ids: endFriends }]
			},function(response) {
				if(response && response.request_ids) {
					$.post(HOST+"ajax/fb_makeFriends.php",{req_ids: response.request_ids});
				}
			});
		});
		return false;
	});
	
	$("#backtome").live("click",function() {
		$("#fb_friends_info").empty();
		$(".friendList",view2).removeClass("fbActive");
		$(".collectBox").remove();
		$("#missions").fadeIn();
		
		var oldLink = $("#game_map").attr("src");
		var nLink = oldLink.replace(/\d+/,clubid);
		$("#game_map").attr("src",nLink);
		var coordin = getAny("getBuildingsCoordinates","clubid="+clubid);
		$("#map").html(coordin);
		$('.map_data').maphilight({});
		return false;
	});
	
	$("#fancybox-wrap a.fb_wallpost").live("click",function() {
		var which = $(this).attr("id");
		var ueb = { which: which };
		if(which == "leaguegame") {
			ueb.gclub_id = $("#leaguegame_gclubid").text();
			ueb.sclub_id = $("#leaguegame_sclubid").text();
			ueb.resg = $("#leaguegame_gres").text();
			ueb.ress = $("#leaguegame_sres").text();
		} else if(which == "award") {
			ueb.id = $("#award_id").text();
			ueb.name = $("#award_name").text();
		} else if(which == "building") {
			ueb.bt_id = $("#building_btid").text();
		}
		
		$.post(HOST+"ajax/get_fbWallpost_text.php",ueb,function(resp) {
			makeWallpost(resp);
		},"json");
		
		$.fancybox.close();
		return false;
	});
});

