	FB.init({
		appId	 : '137390422943941',
		status : true, // check login status
		cookie : true, // enable cookies to allow the server to access the session
		xfbml	 : true	 // parse XFBML
	});
	
	var userID = '';
	
	function getUserLoginStatus()
	{
		//alert('Flash call for FB_User info');
		FB.getLoginStatus(function(response) {
			if (response.session) {
				//trace(response);
				userID = response.session.uid;
				// logged in and connected user, someone you know
				getUserInfo();
				//callUserAppPermissions(userID);
			} else {
				callUserLogin();
			}
		});
	}
	
	function getUserLoginStatusAlert(alertTxt)
	{
		alert(alertTxt);
		getUserLoginStatus();
	}
	
	function fixHeight()
	{
		FB.Canvas.setSize({ height: 800 });
	}
	
	function getUserInfo()
	{				
		//alert('API CALL User info: uid - ' +userID);
		FB.api('/me', function(response) {					
			userID = response.id;		
			var output = '{';

			output += '"data":"userInfo",';

			output += '"id":'+response.id+',';

			if (response.first_name != '')
				output += '"name":"'+response.first_name+'",';
			else
				output += '"name":"'+response.name+'",';
				
			output += '"gender":"'+response.gender+'",';
			
			if (response.location)
			{
				output += '"location_id":"'+response.location.id+'",';
				output += '"location_name":"'+response.location.name+'",';
				getCountryInfo(output);
			}
			else if (response.hometown)
			{
				output += '"location_name":"'+response.hometown.name+'","country":"null"}';		
				flashCallInterface(output);
			}
			else
			{
				output += '"location_name":"","country":""}';		
				flashCallInterface(output);
			}			
		});
	}
	
	function getCountryInfo(obj)
	{
		try
		{
			//Run some code here
			FB.api(
			{
				method: 'fql.query',
				query: 'SELECT current_location FROM user WHERE uid='+userID
			},
			function(locationData) {
				obj += '"country":"'+locationData[0].current_location.country+'"}';
				flashCallInterface(obj);
			});
		}
		catch(err)
		{		
			//Handle errors here
			trace(err);
			obj += '"country":""}';
			flashCallInterface(obj);
		}
	}
	
	function callUserAppPermissions(userID)
	{
		FB.api(
			{
			  method: 'users.hasAppPermission',
			  ext_perm: 'publish_stream',
			  uid:userID
			},function(response) {		  	
				checkPermissions(response);
			}
		);
	}
	
	function checkPermissions(response)
	{
		//alert('Permission callback 2:');
		//trace(response);
	}

	function getFriendsList()
	{
		FB.api('/me/friends', function(response) {					
			if (response.data)
			{
				var friendsArray = '';				
				for (var i=0;i<response.data.length;i++)
				{
					if (friendsArray != '') friendsArray += ',';
					friendsArray += response.data[i].id;
				}
				var output = '{"data":"friendsList","list":"'+friendsArray+'"}';
				flashCallPatternInterface(output);
			}
		});
	}


	function callUserLogin()
	{		
		//alert('required login');
		FB.login(function(response) {			
			if (response.session) {
				if (response.perms) {
				  //alert('full access');
				  getUserInfo();
				} else {
				  alert('Please grant all asked permissions.');
				  callUserLogin();
				}
			} else {
				alert('Please login, instead you cant use this application.');
				callUserLogin();
			}
		}, {perms:'read_stream,publish_stream,photo_upload'});
	}
	
	function facebookLogin()
	{
		FB.login(function(response) {
			if (response.session) {
				if (response.perms) {
					//trace(response);
				  	var obj = '{"data":"userLogged","uid":"'+response.session.uid+'"}';
					flashCallPatternInterface(obj);
				} else {
					var obj = '{"data":"loginFailed"}';
					flashCallPatternInterface(obj);
				}
			} else {
				var obj = '{"data":"loginFailed"}';
				flashCallPatternInterface(obj);
			}
		}, {perms:'read_stream,publish_stream,photo_upload'});
	}
	
	
  function trace(obj)
  {
	if (typeof obj == 'object')
	{
	  	for (var name in obj) {
			alert(name + ": " + obj[name]);
			if (typeof obj[name] == 'object')
			{
				trace(obj[name]);
			}
		}
	} else {
		alert(obj);
	}
  }
  
  
	function thisMovie(movieName) {
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName];
		} else {
			return document[movieName];
		}
	}

	function flashCallInterface(value) {
		//alert(value);
		thisMovie("EvianFBEditor").sendToActionScript(value);
	}
	
	function flashCallPatternInterface(value) {
		thisMovie("EvianFBEditor").sendToPatternActionScript(value);
	}
	
	
	
	function openInvite()
	{
		document.getElementById('invite_friend').className = 'visible';	
	}
	
	
	function old_publishStream(id,img_link,view_url,title,txt)
	{
		//alert("x");
		//postPhotoToAlbum();
		var facebookID = id;
		/*
		var postData = 
		{
			message: 'I just created my Live Young photo!',		
			picture: 'http://liveyoungcommunity.evian.com/img/library/fb-creations/523881450_387e06220a1356a69ab86ae4b71c9620_photo.jpg',
			link: 'http://liveyoungcommunity.evian.com/',
			name: 'Evian - Live young community',
			description: 'Discover the Liveyoung community now. Create your own photo and share it with your friends.'
		}
		*/
		var postData = 
		{
			name: 'Evian - Live young community',			
			picture: img_link,
			message: title,		
			description: txt,
			link: view_url					
		}
		FB.api('/'+facebookID+'/feed', 'post', postData, function(response) {
			if (!response || response.error) {
				alert('Post feed - Error occured');				
			} else {
				//alert('Post ID: ' + );
				//trace(response);
				//postPhotoToAlbum();
			}
		});		
	}
	
	function publishStream(id,img_link,view_url,title,txt)
	{
		FB.ui(
		   {
		     method: 'stream.publish',
		     message: title,
		     attachment: {
		       name: 'Evian - Live young community',
		       //caption: 'The Facebook Connect JavaScript SDK',
		       description: txt,
		       href: view_url,
		       media: [{ 
		        'type': 'image', 
		        'src': img_link, 
		        'href': view_url}],
		     target_id:id
		     },
		     user_message_prompt: title
		   },
		   function(response) {
		     if (response && response.post_id) {
		       //alert('Post was published.');
		     } else {
		       //alert('Post was not published.');
		     }
		   }
		 );
	}
	
	function postPhotoToAlbum()
	{
		alert("y");
		var photoData = 
		{
			source: 'http://liveyoungcommunity.evian.com/img/library/fb-creations/523881450_387e06220a1356a69ab86ae4b71c9620_photo.jpg',
			link: 'http://liveyoungcommunity.evian.com/',
			name: 'I just created my Live Young photo!',
			message: 'Discover the Liveyoung community now. Create your own photo and share it with your friends.'
		}
		FB.api('/me/photos', 'post', photoData, function(response) {
			if (!response || response.error) {
				alert('Insert Photo to FB Album - Error occured');
				trace(response);
			} else {
				//alert('Post ID: ' + );
				trace(response);
			}
		});
	}



