/**
 * 
 * COPYRIGHT MORTEN BARKLUND & KONSTELLATION APS 2009
 * 
 * ALL RIGHTS RESERVED
 * 
 * FACEBOOK AND ALL RELATED TRADEMARKS ARE PROPERTY OF FACEBOOK
 */

if (!window.FBC) {
	window.FBC = {
		
		
		// CONNECTION
		is_connected: function() {
			FB.Connect.ifUserConnected(FBC._connected, FBC._not_connected);
		},
		connect: function() {
			FB.Connect.requireSession(FBC._connected);
		},
		_connected: function() {
			FB.Facebook.apiClient.users_getInfo([FB.Connect.get_loggedInUser()], FBC._attributes, FBC._on_load_connect_info);
		},
		_on_load_connect_info: function(data) {
			FBC._call("connect", [true, data[0]]);
			
		},
		_not_connected: function() {
			FBC._call("connect", [false]);
		},
		
		
		//streamPublish(String  user_message,  Object  attachment,  Object  action_links,  String  target_id,  String  user_message_prompt,  Function  callback,  Boolean  auto_publish,  String  actor_id)
		// POST 
		post: function(data) {
			var message = 'I wonder who that could be :)';
			var attachment = {
				'name': 'The new face of the Middle East is already popular among the girls',
				'href': data.url,
				'description': 'A reportage from the Man of the Year event where the announcement of who is going to be part of the Holsten world wide campaign was released.',
				'media': [
					{
						'type': 'image',
						'src': data.img,
						'href': data.url
					}
				]
			};
			var action_links = [
				{
					'text':'Become a fan',
					'href':'http://www.facebook.com/imholsten'
				},
				{
					'text':'Try it yourself',
					'href':'http://www.imholsten.com/'
				}
			];
			FB.Connect.streamPublish(message,attachment,action_links,null, 'Write a cool message to your friends', function(){});

		},
		_posted: function(result) {
			// result comes back null when dialog is closed or "null" when dialog is skipped
			FBC._call("posted", [result != null && result != "null"]);
		},
		
		// POSTIMAGE 
		postImage: function(data) {
			
			var message = data.message;
			var attachment = {
				'name': data.title,
				'href': 'http://www.imholsten.com/', 
				'media': [
					{
						'type': 'image',
						'src': data.img,
						'href': data.url
					}
				]
			};
			var action_links = [
				{
					'text':'Become a fan',
					'href':'http://www.facebook.com/imholsten'
				},
				{
					'text':'Try it yourself',
					'href':'http://www.imholsten.com/'
				}
			];
			FB.Connect.streamPublish(message,attachment,action_links,null, 'Write a cool message to your friends', function(){});

		},
		_image_posted: function(result) {
			FBC._call("posted", [result != null && result != "null"]);
		},
		
		life: function(data) {
			var message = data.name + " is now hanging out with all the beautiful people";
			var attachment = {
				'name': 'Super model Melissa Campell',
				'href': data.url,
				'caption': 'Super model Melissa Campell loves Man of the year!',
				'description': "Super model Melissa has completely fallen for Man of the year who has swept her feet away...",
				'media': [
					{
						'type': 'image',
						'src': data.img,
						'href': data.url
					}
				]
			};
			var action_links = [
				{
					'text':'Become a fan',
					'href':'http://www.facebook.com/imholsten'
				},
				{
					'text':'Try it yourself',
					'href':'http://www.imholsten.com/'
				}
			];
			FB.Connect.streamPublish(message,attachment,action_links,null, 'Write a cool message to your friends',  function(){});

		},
		_life_posted: function(result) {
			// result comes back null when dialog is closed or "null" when dialog is skipped
			FBC._call("posted", [result != null && result != "null"]);
		},
		
		
		
		// ALBUMS photos_getAlbums(String  uid,  Array  aids,  Object  onRequestCompleted)
		load_albums: function() {
			FB.Facebook.apiClient.photos_getAlbums(FB.Connect.get_loggedInUser(),null, FBC._load_albums_ids);
		},
		_load_albums_ids: function(ids) {			
			// make array of their cover ids
			var i;
			var albums_cids = [];
			FBC._album_info = {};
			for (i = 0; i < ids.length; i++) {
				FBC._album_info[ids[i].aid] = ids[i];
				albums_cids[i] = ids[i].cover_pid;
			}
			// send array to photos.get
			FB.Facebook.apiClient.photos_get(null,null, albums_cids, FBC._on_load_album_photos)
		},	
		_on_load_album_photos: function(albums_imgs){
			// make array of albums
			var i;
			for (i = 0; i < albums_imgs.length; i++) {
				var album = FBC._album_info[albums_imgs[i].aid];
				albums_imgs[i].album_name = album.name;
			}
			FBC._call("albums", [albums_imgs]);
		},
		
		
		// IMAGES IN ALBUM  photos_get(Object  subj_id,  Object  aid,  Array  pids,  Object  onRequestCompleted)
		load_images: function(aid){
			FB.Facebook.apiClient.photos_get(null,aid, null, FBC._on_load_images)
		},
		
		_on_load_images: function(imgs){
			FBC._call("images", [imgs]);
		},
			
		
		// FRIENDS
		load_friends: function() {
			FB.Facebook.apiClient.friends_get('', FBC._load_friend_info)
		},
		_load_friend_info: function(ids) {
			FB.Facebook.apiClient.users_getInfo(ids, FBC._attributes, FBC._on_load_friend_info);
		},
		_on_load_friend_info: function(data) {
			FBC._loaded_friend_data = data;			
			FBC._call("friends", [data]);			
			FBC._loaded_friend_data = null;
		},
		
		
		// NOTIFY
		notify: function(users, message) {
			FB.Facebook.apiClient.notifications_send(users, message, FBC._notified);
		},
		_notified: function() {
			FBC._call("notify");
		},
		
		// FAN
		fan: function() {
			var win = window.open("http://www.facebook.com/pages/Holsten-Official-Fan-Page/117455624944147", "_blank");
			if (!win) {
				if (!window.flashUrl) {
					var flashUrl = '';
				}
				var fbfan = $('<div id="fbfan" style="position: absolute; left: 0; top: 0;  width: 100%; height: 100%;">'+
					'<a id="fbfanbtn" target="_blank" href="http://www.facebook.com/pages/Holsten-Official-Fan-Page/117455624944147" style="display: block; margin-left: -240px; width: 480px; height: 288px; margin-top: -144px; position: absolute; left: 50%; top: 50%; z-index: 1; border: 5px solid white">' +
						'<img src="'+flashUrl+'fb_fan.png" border="0">' +
					'</a>' +
				'</div>');
				$("body").append(fbfan);
				$("#fbfanbtn").click(function() {
					fbfan.remove();
				});
			}
		},
		
		
		// GENERAL STUFF
		_attributes: ['uid','name','first_name', 'last_name','pic_square', 'current_location', 'email', 'birthday_date'],
		init: function(api_key, channel_path) {
			FBC._api_key = api_key;
			FBC._channel_path = channel_path;
			FBC._prefix = "_FBC_"+(new Date().getTime())+"_";
		},
		_call: function(funcname, args) {
			var f;
			if (navigator.appName.indexOf("Microsoft") != -1) {
				f = window[FBC._flashID];
			} else {
				f = document[FBC._flashID];
			}
			f[FBC._prefix+funcname].apply(f, args);
		},
		getFlashVars: function() {
			return {
				'fbc_flash_id': FBC._flash_id,
				'fbc_prefix': FBC._prefix
			};
		},
		setFlashID: function(id) {
			FBC._flashID = id;
		},
		write: function(locale) {
			if (!locale) locale = "en_US";
			document.write('<scr'+'ipt type="text/javascript" src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/'+locale+'"></scr'+'ipt>'); 
			function start() {
				FB_RequireFeatures(["Api", "Connect"], function(){
					FB.Facebook.init(FBC._api_key, FBC._channel_path);
					FBC._ready();
				});
			}
			if (jQuery) {
				jQuery(start);
			} else if (typeof window.onload == "function") {
				var f = window.onload;
				window.onload = function() {
					f();
					start();
				}
			} else {
				window.onload = start;
			}
		},
		ready: function(callback) {
			FBC._ready_callback = callback;
		},
		_ready: function() {
			if (typeof FBC._ready_callback == 'function') {
				FBC._ready_callback();
			}
		}
	}
}

