 	var loggedIn = false;
    FB_RequireFeatures(["XFBML"], function()
    {
		
      FB.Facebook.init("485480be7e71b00940269090b68dfbae", "xd_receiver.htm");

      FB.Connect.get_status().waitUntilReady(function( status ) { 
          switch ( status ) { 
          case FB.ConnectState.connected:
            loggedIn = true; 
          	break; 
          case FB.ConnectState.appNotAuthorized: 
          case FB.ConnectState.userNotLoggedIn: 
          	loggedIn = false;
          }
      }); 
      FB.Facebook.get_sessionState().waitUntilReady(function()
      {

      });
    });

  	function fblogin() {
    	FB.Facebook.apiKey = "485480be7e71b00940269090b68dfbae";
  		FB.Connect.ifUserConnected(fbSessionReady, FB.Connect.requireSession)
	}
  	function fbSessionReady() {
  		console.log("Session is ready");
  		console.log("Your session : %o" , FB.Facebook.apiClient.get_session());
  		
  		if(thisMovie("flashContent") != undefined) {
			console.log("flashContent",FB.Facebook.apiClient.get_session());
  			thisMovie("flashContent").userLogedIn(FB.Facebook.apiClient.get_session());
  	  	}
	}
	function getFriends() {
		FB.Connect.ifUserConnected(function() {
			FB.Facebook.apiClient.friends_get(null, function(result, exception){
					
				console.log("friendsResult from non-batch execution: %o", result);
				console.log("friendsResult exception: %o",exception);
				
				FB.Facebook.apiClient.users_getInfo(result, 'name, pic_square', function(friendNames, exec) {
		        	console.log("first name in friends list: %o", friendNames);
		        	thisMovie("flashContent").friendsLoaded(friendNames);
		        });
	  	  	});
		}, FB.Connect.requireSession);
	}
	
	function getUserDetails(uid, data) {
		FB.Connect.ifUserConnected(function() {
			FB.Facebook.apiClient.users_getInfo(uid, data, function(userData, exec) {
	        	console.log("userdetails is: %o", userData);
	        	thisMovie("flashContent").userDetailsLoaded(userData);
	        });
		}, FB.Connect.requireSession);
	}

	
	function postBillCallback(post_id, exception, data) { 
		if(post_id) {
			thisMovie("flashContent").billPosted(true);
			console.log("räkningen skickad");
		}
		else {
			thisMovie("flashContent").billPosted(false);
			console.log("räkningen EJ skickad");
		}
	}

	function postBill(uid, mp3, title, artist, album, message, uri) {
		var _message = Url.decode(message);
		var _header = Url.decode('Meddelande:');
		var _actionLinkName = 'Läs mer';//Url.decode('Läs mer');
		console.log("postBill till: %o", uid);
		var _attachmentTitle = Url.decode(title);
		var _attachmentArtist = artist;//Url.decode(artist);
		var _attachmentAlbum = Url.decode(album);
		var _attachmentName = 'Få tillbaka dina småpengar med SEB Enkla återbäringen';//'Prova SEB Enkla återbäringen du också!';//Url.decode('Besök SEB Enkla Vardagen');
		var _attachmentCaption = Url.decode('du är skyldig {*actor*} pengar!');
		var _attachmentDescription = Url.decode('where is this description. 300 first chars is displayed, then you have to push read more');
		
		FB.Connect.ifUserConnected(function() {
			var attachment = {
					'media':[
					         {'type':'mp3','src':mp3,'title':_attachmentTitle,'artist':_attachmentArtist,'album':_attachmentAlbum}
					    ],
					'href':uri,
					'name':_attachmentName//,
					//'caption': _attachmentCaption,
					//'description': null;_attachmentDescription
				};
			var actionLinks = [{ "text": _actionLinkName, "href": uri}];
				console.log("skickar räkning till: %o", uid);
				FB.Connect.streamPublish(message, attachment, actionLinks, uid, _header, postBillCallback);
		}, FB.Connect.requireSession);
	}

	
	
	
	
	
	function shareTipsCallback(post_id, exception, data) { 
		if(post_id) {
			thisMovie("flashContent").tipsPosted(true);
			console.log("tips skickat");
		}
		else {
			thisMovie("flashContent").tipsPosted(false);
			console.log("tips EJ skickat: %o", exception);
		}
	}
	function shareTips(uid, subject, message, uri) {

		var _attachmentCaption = subject;//Url.decode(subject);
		var _attachmentDescription = message;//Url.decode(message);
		var _header = Url.decode('Meddelande:');
		var _actionLinkName = 'Läs mer';//Url.decode('Läs mer');
		
		var _attachmentName = 'Besök SEB Enkla vardagen';//Url.decode('Besök SEB Enkla Vardagen');
		//var _attachmentCaption = Url.decode('ett tips från {*actor*}!');
		//var _attachmentDescription = Url.decode('här är tipset.....');
		
		FB.Connect.ifUserConnected(function() {
			var _attachment = {
					'href':uri,
					'name': _attachmentName,
					'caption': _attachmentCaption,
					'description': _attachmentDescription
				};
			var _actionLinks = [{ "text": _actionLinkName, "href": uri}];
				console.log("skickar tips till: %o", uid);
				FB.Connect.streamPublish(null, _attachment, _actionLinks, uid, _header, shareTipsCallback);
		}, FB.Connect.requireSession);
	}
	
	
	
	function shareCallback(post_id, exception, data) { 
		if(post_id) {
			thisMovie("flashContent").sharePosted(true);
			console.log("har delat");
		}
		else {
			thisMovie("flashContent").sharePosted(false);
			console.log("EJ delat: %o", exception);
		}
	}
	function share(subject, message, uri) {
		
		var _subject = subject;//Url.decode(subject);
		var _message = message;//Url.decode(message);
		var _uri = uri;//Url.decode(uri);
		
		var _attachmentName = 'Besök SEB Enkla vardagen';//Url.decode('Besök SEB Enkla Vardagen');
		//var _attachmentCaption = Url.decode('ett tips från {*actor*}!');
		var _attachmentDescription = 'här är tipset.....';//Url.decode('här är tipset.....');
		
		FB.Connect.ifUserConnected(function() {
			
			var _attachment = {
					'href':_uri,
					'name': _subject,
					//'caption': _attachmentCaption,
					'description': _message
				};
			
			var _actionLinks = [{ "text": _subject, "href": _uri}];
			
			console.log("sharing...");
			FB.Connect.streamPublish(null, _attachment, _actionLinks, null, "Dela på facebook:", shareCallback, true);
		}, FB.Connect.requireSession);
	}

	function fixPermissionsDone() {
		thisMovie("flashContent").hasPermission(true);
	}
	function fixPermissions() {
		FB.Connect.showPermissionDialog('publish_stream', fixPermissionsDone, true);
	}

    function thisMovie(movieName) {
        if (navigator.appName.indexOf("Microsoft") != -1) {
            return window[movieName];
        } else {
            return document[movieName];
        }
    }
