var fields = null;
var  myObject;

/*
http://wiki.developers.facebook.com/index.php/Attachment_(Streams)
var attachment = {'media': [{'type':'image','src':'http://bit.ly/AJTnf','href':'http://bit.ly/hifZk'}]};

user_message	String	
The main user-entered message for the post. This field should typically be blank- only fill this field with content the user has actually entered themselves.
attachment	Object	
A dictionary object containing the text of the post, relevant links, and optionally a media type. You can also include other key/value pairs which will be stored as metadata. See Stream Attachments for details.
action_links	Object	
A dictionary of Action links objects, containing the link text and a hyperlink.
target_id	String	
The ID of the user or the Page where you are publishing the content. If you specify a target_id, the post appears on the wall of the target user, not the user that published the post. This mimics the action of posting on a friend's Wall.
user_message_prompt	String	
The sentence that appears before the user message field (e.g. "What's on your mind?")
callback	Function	
A function that takes two parameters: post_id and exception. post_id returns the id of the published post (which can be null if the user cancels). exception returns error description if an error occurred.
auto_publish	Boolean	
If the user has granted the publish_stream extended permission AND this parameter is true, then the post will be published without user approval. If the user has not set the extended permission, then the method will fail. (Default value is false)
actor_id	String	
Allows the logged in user to publish on a Facebook Page's behalf if the user is an admin of the Page. If specified, actor_id indicates the ID of the Page that will publish the post. The post will appear on the Page's Wall as if the Page had posted it. (Default value is null)


*/

function postToSilkor(q1,a1,q2,a2){
var attachment = {
	'name': 'Win Free Laser with Silkor Online Competition',
	'href': 'http://silkor.com/index.asp#competition',
	'caption': '{*actor*} Filled in the Silkor Competition with the following answers:',
	'properties': {
		'Q1' : q1,
		'A1': a1,
		'Q2' : q2,
		'A2': a2
			},
	'media': [{ 'type': 'image', 'src': 'http://silkor.com/silkorCompetition.jpg', 'href': 'http://www.silkor.com/index.asp#competition'}]
			}; 
var action_links = [{'text':'Recaption this', 'href':'http://bit.ly/19DTbF'}];  
				
				postOnWall('',attachment,action_links,null,'What do u think...',false,null);
				
}


//called upon wall publish
function posted(post_id,exception){
	 if(post_id != "null" || post_id.length <=0) {
		 window.document["sample"].SetVariable("FB_posted","true" );
		//	alert('posted on ur wall....[' + post_id + ']');
	 }
	 else {
		 window.document["sample"].SetVariable("FB_posted","false" );
			//alert('NOT posted');
		}
}

//check the postonwall call example in the next function
function postOnWall(user_message,attachment,action_links,target_id,user_message_prompt,auto_publish,actor_id){
	if(target_id!=null)//this caswe is for you to send the user id that you want in the postOnWall call 
		FB.Connect.streamPublish(null,attachment,action_links,target_id,user_message_prompt,posted,auto_publish,actor_id);
	else{//this case is the default behavior if no target was specified the default would be the logein user
		FB.Connect.streamPublish(null,attachment,action_links,FB.Connect.get_loggedInUser(),user_message_prompt,posted,auto_publish,actor_id);
	}
}


function renderXfbml(){
	
	//this function is called once the user is connected or once the button is pressed and the user was already connected
	 FB.Facebook.apiClient.users_getInfo(FB.Connect.get_loggedInUser(),fields, function(newresult){
																						
				//this object contains the fields you requested via login function, check the retrieve example below
				myObject = eval(newresult);
				
		//	alert(myObject[0]['first_name']);
			window.document["sample"].SetVariable("FB_first_name",myObject[0]['first_name'] );
			window.document["sample"].SetVariable("FB_last_name",myObject[0]['last_name'] );
			window.document["sample"].SetVariable("FB_pic_square",myObject[0]['pic_square'] );
			window.document["sample"].SetVariable("FB_logged_in", "true" );
				//myObject[0]['first_name']);
				//myObject[0]['last_name']);
				//myObject[0]['pic_square']);
				
				//postToSilkor('ballouta','ballouta','ballouta','ballouta');
	});
}

function renderXfbml2(){
			window.document["sample"].SetVariable("FB_logged_in", "false" );
}

function login(arrfields, api_key, xd_receiverURL){
	fields = arrfields;
	//alert("conesting...")
	FB_RequireFeatures(["XFBML"], function()
	{
	  FB.Facebook.init(api_key,xd_receiverURL, {"ifUserConnected" : renderXfbml});
	  FB.Connect.requireSession(null,renderXfbml2);
	});
}