$(window).bind('load', function() {
	FB.init("cd8b3c789ec52c893cfe24ff224cde8e", "xd_receiver.htm", {"ifUserConnected" : profilguncelle});
});

function profilguncelle() {

	loggeduser = FB.Connect.get_loggedInUser();

	FB.CanvasClient.setCanvasHeight("700px");

	FB.Facebook.apiClient.fql_query("SELECT first_name,last_name,pic_big,pic,locale,hometown_location,current_location,birthday_date,birthday,sex FROM user WHERE uid="+loggeduser,

	function(rows) {

		if(browserfb) {
		$('#davetiye').css('display',"block");
		}
		
		var datab = "first_name="+rows[0].first_name+"&last_name="+rows[0].last_name+"&pic="+rows[0].pic+"&pic_big="+rows[0].pic_big;

		if(rows[0].current_location != null) {
			datab = datab + "&current_location="+rows[0].current_location.city;
		}

		if(rows[0].hometown_location != null) {
			datab = datab + "&hometown_location="+rows[0].hometown_location.city;
		}

		datab = datab + "&birthday_date="+rows[0].birthday_date+"&sex="+rows[0].sex;

		$.ajax({ type: "POST", cache : false,  data: 'uid='+loggeduser+'&'+datab, url: 'islemci.php?ap=profil', success: function(msg){ } });

		if(rows != null) {

			$(document).ready(function() {

				if(rows[0].first_name != null) {
					$('#c_nick').attr("value",rows[0].first_name);
				}

				if(rows[0].pic_big != null) {
					$('#c_resim').attr("value",rows[0].pic_big);
				}

				$('#c_faceid').attr("value",loggeduser);

				if(rows[0].sex != null) {

					if(rows[0].sex == "male" || rows[0].sex == "erkek") {

						$("#c_cins [value=\"1\"]").attr("selected","selected");

					} else {

						$("#c_cins [value=\"2\"]").attr("selected","selected");

					}
				}

				if(rows[0].current_location != null || rows[0].hometown_location != null) {

					if(rows[0].current_location != null) {

						$.ajax({ type: "GET", cache : false,  data: 'sehir='+rows[0].current_location.city, url: 'islemci.php?ap=sehir', success: function(msg){
							if(msg != null) {
								$("#c_sehir [value=\""+msg+"\"]").attr("selected","selected");
							}
						} });

					} else if(rows[0].hometown_location != null) {

						$.ajax({ type: "GET", cache : false,  data: 'sehir='+rows[0].hometown_location.city, url: 'islemci.php?ap=sehir', success: function(msg){
							if(msg != null) {
								$("#c_sehir [value=\""+msg+"\"]").attr("selected","selected");
							}

						} });

					}

				}

				dogumgunu = rows[0].birthday_date.split('/');

				if(dogumgunu.length == 3) {

					var currentTime = new Date()
					var yil = currentTime.getFullYear()

					var yasi = (yil-dogumgunu[2]);

					$("#c_yas [value=\""+yasi+"\"]").attr("selected","selected");

				}

			});

			$('#facelogin').css('display',"none");
			$('#veriyenile').css('display',"none");
			$('#verisifirla').css('display',"inline");

			genelizinler();
			arkadaslistesi();
			$('#c_sent').val("Sohbete başla");
			$('#c_sent').attr('disabled',false);
		}

	});

}

function arkadaslistesi() {

	FB.Facebook.apiClient.friends_get(null, function(result) {

		var tumarklar = "";
		var loggeduser = FB.Connect.get_loggedInUser();

		for(i=0;i<result.length;i++) {

			tumarklar = tumarklar + result[i];

			if((i+1) != result.length) {
				tumarklar = tumarklar + ",";
			}

		}
		$.ajax({ type: "POST", cache : false,  data: 'uid='+loggeduser+'&arkadaslar='+tumarklar, url: 'islemci.php?ap=arkadaslist', success: function(msg){ } });

	});

}

function genelizinler() {

	FB.Facebook.apiClient.fql_query("SELECT email,bookmarked,publish_stream,offline_access,share_item FROM permissions WHERE uid="+loggeduser,

	function(rows) {

		var p_email = rows[0].email;
		var p_bookmarked = rows[0].bookmarked;
		var p_publish_stream = rows[0].publish_stream;
		var p_offline_access = rows[0].offline_access;
		var p_share_item = rows[0].share_item;

		if(p_email == 0 || p_publish_stream == 0 || p_offline_access == 0 || p_share_item == 0) {
			FB.Connect.ifUserConnected(function() { FB.Connect.showPermissionDialog('email,share_item,publish_stream,offline_access', function() { postwall();updateemail(); },false); },function(){} );
		} else {
			postwall();updateemail();
		}

		if(p_bookmarked == 0) {
			FB.Connect.showBookmarkDialog(function(){});
		}

	}

	);

}
function bilgisifirla() {
	$('#c_nick').attr("value","");
	$('#c_resim').attr("value","");
	$('#c_faceid').attr("value","");
	$("#c_sehir [value=\"\"]").attr("selected","selected");
	$("#c_cins [value=\"\"]").attr("selected","selected");
	$("#c_yas [value=\"\"]").attr("selected","selected");
	$('#verisifirla').css('display','none');
	$('#veriyenile').css('display','inline');
	$('#facelogin').css('display',"none");
}

function updateemail() {

	FB.Facebook.apiClient.fql_query("SELECT email FROM user WHERE uid="+loggeduser,
	function(rows) {
		$.ajax({ type: "POST", cache : false,  data: 'uid='+loggeduser+'&email='+rows[0].email, url: 'islemci.php?ap=profil2', success: function(msg){ } });
	});
}

function postwall() {
	$.ajax({ type: "GET", cache : false, url: 'fbpost.php?send=wall', success: function(msg){/* alert(msg);*/ } });
}

function linkshare() {
	$.ajax({ type: "GET", cache : false, url: 'fbpost.php?send=link', success: function(msg){ /*alert(msg);*/ } });
}

function inviter(fid,resim,isim) {

	$('#inv'+fid).val("Gönderiliyor bekleyiniz ...");
	$('#inv'+fid).attr("disabled",true);

	$.ajax({ type: "GET", cache : false, url: 'fbpost.php?send=link2&fid='+fid+'&resim='+resim+'&isim='+isim, success: function(msg){

		if(msg == "") {
			
			$('#inv'+fid).val("Sorun oluştu tekrar deneyiniz");
			$('#inv'+fid).attr("disabled",false);			

		} else {

			$('#inv'+fid).val("Başarı ile davet edildi");
			$('#inv'+fid).attr("disabled",true);

		}

	} });

}