var userID;
var userInfo;
var friendInfo;

//STAGING OR PROD FB
if (document.location.href.indexOf("dev.") == -1){
	var api_key = "386a67614e196dcbe7f0fcc56a6392ad";//LIVE
}else{
	var api_key = "386a67614e196dcbe7f0fcc56a6392ad"; //DEV DEMO
}

function loginConnect(){
	statusMsg("Login FB Connect...");
	FB.Connect.requireSession();
	checkConnect();
}
function logoutConnect(){
	FB.Connect.logout(sendLogoutStatus);
}

function checkConnect(){
	//FACEBOOK WILL WAIT UNTIL THERE IS A CONNECTION BEFORE RUNNING FUNCTION
	statusMsg("Check FB Connect...");
	
	FB_RequireFeatures(["XFBML"], function()
	{
	  FB.Facebook.init(api_key, "xd_receiver.htm");	  
	  FB.Facebook.get_sessionWaitable().waitUntilReady(function()	  {
		userID = FB.Facebook.apiClient.get_session().uid;		
		if (!userID) {
			statusMsg("USER NOT VALID!");
		}else{
			getProfile();
		}
	  });
	});
}
function getProfile(){
	//Get the user's info	
	statusMsg("Loading profile..." + userID);
		
	FB.Facebook.apiClient.users_getInfo(userID,'first_name,last_name,name,pic_square,is_app_user',function(result, ex) {
		userInfo = result.slice();
		//statusMsg(userInfo[0].name);
		sendProfile();
		getFriends();
	});     	
}
function getFriends(){
	statusMsg("Loading friends...");
	
	//Get the friend's and their info        
	FB.Facebook.apiClient.friends_get(null, function(result, ex) {
		
		var ids = result.slice();
		
		FB.Facebook.apiClient.users_getInfo(ids,'name,pic_square,is_app_user',function(result, ex) {
			friendInfo = result.slice();
			sendFriends();
		});
	});    	
}
//SEND BACK TO FLASH
function sendProfile(){   
	//writeProfile();
	var flash = (navigator.appName.indexOf ("Microsoft") !=-1)?window["garagebandplayoff"]:document["garagebandplayoff"];
	statusMsg(flash);
	flash.returnProfile(userInfo);		
	statusMsg("Finished loading profile!");
}
function sendFriends(){
	//writeFriends();
	var flash = (navigator.appName.indexOf ("Microsoft") !=-1)?window["garagebandplayoff"]:document["garagebandplayoff"];
	flash.returnFriends(friendInfo,friendInfo.length); 	
	statusMsg("Finished loading friends!");
}
function sendLogoutStatus(){
	var flash = (navigator.appName.indexOf ("Microsoft") !=-1)?window["garagebandplayoff"]:document["garagebandplayoff"];
	flash.returnLogoutStatus(true); 	
	statusMsg("Finished logout");	
}


function statusMsg(msgIn){
	/*var fb_status = document.getElementById('fb_status');  	
	if (fb_status){
		fb_status.innerHTML += "<br />" + msgIn;
	}*/
}
function cbFeedPostFinished(){
	statusMsg("Post closed.");
}
function feedPostOpenInvite(band_image,song_name,band_name,user_name){
	statusMsg("Prompt feed post...");
	statusMsg(band_image);
	statusMsg(band_name);
	statusMsg(song_name);
	statusMsg(user_name);
	var feedPostID = 303830745421;
	var template_data = {
						"images":[
								{"src":"http://www.garagebandplayoff.com/johnflores/icon/FacebookGBPIcon.png", "href":"http://www.garagebandplayoff.com"},{"src":band_image, "href":"http://www.garagebandplayoff.com"}
						],
						"message":"this is the message Text"
	};
	var friends_ar = null;
	var body_text = user_name + " just voted for " +song_name+" by "+ band_name + " in U-TURN's Garage Band Playoff '09, sponsored by USAA. 24 songs battle it out to be named The Next Great American Road Song. Listen and vote for your favorites!";
	var user_message_prompt = "Invite your Facebook friends to vote for your favorite band."; 
	var user_message = {value: "Vote for your favorite band!"}; 
	//Must have setTimeout for IE Browser bug
	setTimeout(function(){
		FB.Connect.showFeedDialog(
			feedPostID, 
			template_data, 
			friends_ar, 
			body_text, 
			null, 
			FB.RequireConnect.promptConnect, 
			cbFeedPostFinished, 
			user_message_prompt, 
			user_message
		);
	}, 1000);
}


function sendNotification(uids,msg){
	//If uids is blank it will send to current user
	
	//alert(uids);
	
	//FB.Facebook.apiClient.notifications_send(uids,msg,'user_to_user');
}


/*

{
"video":{ "video_src":"http://www.youtube.com/v/NP3lltOrGM0", "preview_img":"http://s3.ytimg.com/vi/NP3lltOrGM0/default.jpg" }
}
*/