//var referralPath = "http://www.shrek.com/";
//var imagePath = "http://www.shrek.com/media/ogresFB/";

var referralPath = "http://www.shrekinternational.com/intl/es/mainsite/";
var imagePath = "http://www.shrekinternational.com/intl/es/mainsite/media/ogresFB/";

//var referralPath = "http://www.d13network.com/shrek4/dev_international_ogreizer/";
//var imagePath = "http://www.d13network.com/shrek4/dev_international_ogreizer/media/ogresFB/";

FB.Bootstrap.requireFeatures(["Connect"], function()
	{
		//FB.Facebook.init("6bdfbbeb9fcbf3c4777189cf783e1337","/xd_receiver.htm");	//d13.com
		//FB.Facebook.init("e248f7cbb34c841962d1f2939192c64d","/xd_receiver.htm");	//shrek.com
		FB.Facebook.init("7041549640792c63be4b6b9852681c11","/xd_receiver.htm");	//shrekinternational.com
	});

function loginFBConnect()
{
	FB.ensureInit(function()
	{
		FB.Connect.requireSession(function () {getUserInfo();}, logoutFBConnect);	
	});
}

function finalizePublish(userId, userName, userTitle, bottomLinkTitleTxt, titleTxt, bodyTxt) 
{
	//FB.fbml.refreshImgSrc(imagePath+userId+'.png');
	
   var attachment = {
     'name':userName.toUpperCase()+' - '+userTitle.toUpperCase()+' | '+titleTxt,
     'href':referralPath+'?referralId='+userId,
     'caption':bodyTxt,
     'media':[{
       'type':'image',
       'src':imagePath+userId+'.png?fbrefresh='+new Date().valueOf(),
       'href':referralPath+'?referralId='+userId
     }]};
    
	var action_links = [{'text':bottomLinkTitleTxt,'href':referralPath+'?referralId='+userId}];
	FB.Connect.streamPublish('', attachment, action_links);
 }
 
function sharePublish(userId, userName, userTitle, bottomLinkTitleTxt, titleTxt, bodyTxt) 
{
   var attachment = {
     'name':userName.toUpperCase()+' - '+userTitle.toUpperCase()+' | '+titleTxt,
     'href':referralPath+'?referralId='+userId,
     'caption':bodyTxt,
     'media':[{
       'type':'image',
       'src':imagePath+userId+'.png?fbrefresh='+new Date().valueOf(),
       'href':referralPath+'?referralId='+userId
     }]};
    
	var action_links = [{'text':bottomLinkTitleTxt ,'href':referralPath+'?referralId='+userId}];
	FB.Connect.streamPublish('', attachment, action_links);
}

function getUserInfo()
{
	FB.Facebook.get_sessionState().waitUntilReady(function()
		{
			//alert("login return")
			var uid = FB.Facebook.apiClient.get_session().uid ;
			FB.Facebook.apiClient.fql_query("select publish_stream from permissions where uid="+uid, function(result, ex) 
				{
					if(result[0]['publish_stream']=="0")
					{
						FB.Connect.showPermissionDialog("publish_stream", function(perms) {});	
					}
				});
			//alert("permission worked");
			var sql = "SELECT first_name, current_location, hometown_location FROM user WHERE uid ="+uid;
			FB.Facebook.apiClient.fql_query(sql, function(result, ex) 
				{
					//alert("fql return ");
					//alert("|"+result+"|");
					userName= result[0]['first_name'];
					var zip = "";
					
					/*if((result[0]['current_location']!=null)&&(result[0]['current_location']['city']!=null))
					{
						zip =  result[0]['current_location']["city"]+","+result[0]['current_location']["state"];
					}
					else
					{
						if((result[0]['hometown_location']["city"]!=null)&&(result[0]['hometown_location']["state"]!=null))
						{
							zip =  result[0]['hometown_location']["city"]+","+result[0]['hometown_location']["state"];
						}
						else
						{
							zip = "";	
						}
					}*/
					
					//alert("before send to flash "+uid+" "+userName+" "+zip);
					thisMovie("flashObj").getFBInfoFromBrowser(uid, userName, zip);
					//alert("after send to flash");
				});
		});
	//alert("end of function");
}

function logoutFBConnect()
{
	FB.Connect.logout(function(){thisMovie("flashObj").confirmFBLoggedOut()});
}

// Browser check for External Int. Calls
function thisMovie(movieName) 
{
	if (navigator.appName.indexOf("Microsoft") != -1) 
	{
	 return window[movieName];
	} 
	else 
	{
	return document[movieName];
	}
}
