//Local Variable with Flash Content id. default = flashcontent
var as_swf_name = "flashContent";
var fb_Api_key = "a323e689e6decb8ebe596e9aa97598c7";

window.fbAsyncInit = function() {
	
	setTimeout(function(){
		//alert("F");
		FB.init({appId: fb_Api_key, status: true, cookie: true, xfbml: true});
	},
	2000);
};

//Initialize Facebook
function fbInit(pAsSwfName, pApi_key, pReceiver){
	as_swf_name = pAsSwfName;
	//FB.init(pApi_key, pReceiver);
	/*
	FB.init({
      appId  : pApi_key,
      status : true, // check login status
      cookie : true, // enable cookies to allow the server to access the session
      xfbml  : true  // parse XFBML
    });
 	*/
	//FB.XFBML.parse();
}

//JavaScript Connect methods
function login(){
	//FB.Connect.requireSession( onLoginHandler );
	//alert("login");
	
	FB.login(loginHandler, {perms:'read_stream, publish_stream, user_photos, friends_photos, user_photo_video_tags, friends_photo_video_tags, offline_access'});
}
//-------------------------------------------------------------------------------------------------------
// User objects
//-------------------------------------------------------------------------------------------------------
function getUserObject(){
	FB.api('/me', function(response) {
	 // alert(response.name);
		sendToActionScript([ "getUserObject", response])
	});
}

function getInfoByID(id){
	FB.api('/' + id, function(response) {
	 // alert(response.name);
		sendToActionScript([ "getInfoByID", response])
	});
}

function getGraphData(connectionsPath, callbackID){
	// sample: connectionsPath = 'me/likes';
	// callbackID is used by flash to dispatch proper event, string
	FB.api(connectionsPath, function(response) {
	  	//alert(callbackID + "  " + response.data);
		sendToActionScript([ callbackID, response])
	});
}



function getPhotos(){
	
	FB.api('/534205968/photos', function(response) {
		
		//alert("data: " + response.data);
		for (var key in response){
			//alert(key + " type:" + typeof key);
			//alert(response[key]);
			for (var i=0;i<key.length-1;i++){
				//alert(key[i]+'TEST1');
				
			}
		}
		sendToActionScript([ "getPhotos", response]);
	});
	
}


//-------------------------------------------------------------------------------------------------------
// Publishing
//-------------------------------------------------------------------------------------------------------

function commandStreamPublish(__message, __attachment, __links, __target_id) {
    // Do a direct wall post, with no extended permissions, and prompt the user for data approval
    // Fails when cross-domain cookies are disabled (FDT)
    //FB.Connect.streamPublish(__message, __attachment, __links, __target_id, null, onCommandStreamPublish);
	

	//var myPath = 'http://www.jamesdeagle.com/FacebookWidget/PosterWidget.swf?myVar=691816828';
	//var myPath = 'http://www.jamesdeagle.com/FacebookWidget/PosterWidget.swf?myVar=' +  __attachment.media[0].poster_id;
	var myPath = 'http://expendablesthemovie.com/areyouexpendable/PosterWidget.swf?myVar=' +  __attachment.media[0].poster_id;
	var imagePath = 'http://expendablesthemovie.com/areyouexpendable/output/' + __attachment.media[0].poster_id + 'Thumb.jpg'
	//'http://dev.expendablesthemovie.com/areyouexpendable/media/images/FB_ExpendablesIcon.jpg'
	//alert("path: " + myPath);
	
	var attachment = { 
		'name': __attachment.name, 
		'href': __attachment.href, 
		'caption': __attachment.caption, 
		'description': __attachment.description, 
		//'properties': { 'category': { 'text': 'humor', 'href': 'http://bit.ly/KYbaN'}, 'ratings': '5 stars' }, 
		'media': [{'type':'flash','swfsrc': myPath,'imgsrc':imagePath,'width':'90','height':'90','expanded_width':'460','expanded_height':'131'}] 
	}; 
	
	//alert("swf: " + __attachment.media[0].swfsrc);
	//var action_links = [{'text':'Recaption this', 'href':'http://bit.ly/19DTbF'}];
	
	
	var streamParams =  {
	     method: 'stream.publish',
	     message: __message,
	     attachment: attachment,
	     action_links:__links,
	     user_prompt_message: 'Share your thoughts about Connect'
	   };
	
	FB.ui( streamParams, onCommandStreamPublish);
	
}

function onCommandStreamPublish(response) {
	
	 if (response && response.post_id) {
       //alert('Post was published.');
     } else {
       //alert('Post was not published.');
     }

	sendToActionScript([ "onFacebookCommandStreamPublish", response])
}

function loginHandler(response) {
	 if (response.session) {
	    if (response.perms) {
	      // user is logged in and granted some permissions.
	      // perms is a comma separated list of granted permissions
		 // alert('user is logged in and granted some permissions');
			flashCallBack( "onLogIn" );
	    } else {
	      // user is logged in, but did not grant any permissions
	      alert('user is logged in, but did not grant any permissions');
		flashCallBack( "onLogIn" );
	    }
	 } else {
	    // user is not logged in
	 	alert('user is not logged in');
	 }

}

//Event Handlers
function onLoginHandler(){
	flashCallBack( "onLogIn" );
}

function onPublishHandler(){
	flashCallBack( "onPublish" );
}

function thisMovie(movieName) {
   	if (navigator.appName.indexOf("Microsoft") != -1) {
    	return window[movieName];
    } else {
      	return document[movieName];
    }
}

function sendToActionScript(value) {
   thisMovie(as_swf_name).sendToActionScript(value);
}


//Method to dispatch an Event to Flash
function flashCallBack ( func ) {
	if( arguments.length > 1 ){
		document[as_swf_name][func]( Array.prototype.slice.call(arguments).slice(1)[0]);
	}else{
		document[as_swf_name][func]();
	}
}






