var accessToken = "";

// FAN
function fan() {
	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();
		});
	}
}

// //----------- POSTING AND SHARING ------------/////
// for sharing purposes
function life(data) {
	FB
			.ui(
					{
						'method' : 'stream.publish',
						'message' : data.name
								+ " is now hanging out with all the beautiful people",
						'display' : 'popup',
						'attachment' : {
							'name' : 'Super model Melissa Campell',
							'description' : ("Super model Melissa has completely fallen for Man of the year who has swept her feet away..."),
							'caption' : 'Super model Melissa Campell loves Man of the year!',
							'href' : data.url,
							'media' : [ {
								'type' : 'image',
								'src' : data.img,
								'href' : data.url
							} ]
						},
						'action_links' : [ {
							text : 'Try it yourself',
							href : 'http://www.imholsten.com/'
						} ],

						'user_message_prompt' : 'Write a cool message to your friends'
					}, function(response) {
						if (response && response.post_id) {
							// console.log('Post published.');
				} else {
					// console.log('Post not published.');
				}
			});
}

function post(data) {
	FB.ui( {
		'method' : 'stream.publish',
		'message' : data.message,
		'display' : 'popup',
		'attachment' : {
			'name' : data.name,
			'description' : data.description.substring(0,
					data.description.length - 1),
			'href' : 'http://www.imholsten.com/',
			'media' : [ {
				'type' : 'image',
				'src' : data.img,
				'href' : data.url
			} ]
		},
		'action_links' : [ {
			text : 'Try it yourself',
			href : 'http://www.imholsten.com/'
		} ],

		'user_message_prompt' : 'Write a cool message to your friends'
	}, function(response) {
		if (response && response.post_id) {
			// console.log('Post published.');
		} else {
			// console.log('Post not published.');
		}
	});
}

// //----------- ALBUMS -----------/////
function getAlbums() {
	var query = FB.Data
			.query('SELECT aid, object_id, owner, cover_pid FROM album WHERE owner=me()');
	query.wait(function(result) {

		var new_results = [];
		for (i = 0; i < result.length; i++) {
			if (result[i].cover_pid == 0) {
				// no cover photo, probably no photos at all in album, skip
			continue;
		}
		var oid = result[i].object_id;
		var extra = "";
		if (oid == 0) {
			// no object id means we have the profile image album - use user's
			// own image as album cover
			oid = result[i].owner;
			extra = "type=large&";
		}
		var url = "https://graph.facebook.com/" + oid + "/picture?" + extra
				+ "access_token=" + accessToken;
		new_results.push( {
			url : url,
			aid : result[i].aid
		});
	}
	returnResult("albums", new_results);
})	;
}

function getAlbumImages(id) {
	var query = FB.Data
			.query('SELECT src_big, src, object_id FROM photo WHERE aid=' + id);
	query.wait(function(result) {
		for (i = 0; i < result.length; i++) {
			result[i]['url'] = "https://graph.facebook.com/"
					+ result[i]['object_id'] + "/picture?access_token="
					+ accessToken;
		}
		returnResult("images", result);
	});
}

function login() {
	if (!FB.getSession()) {
		var w = window.open("facebook", "__popup", "width=1000,height=550");
		w.focus();
	} else {
		loadUser();
	}
}

function setFBAccessToken(access_token) {
	if (typeof access_token == "string" && access_token.length > 10) {
		accessToken = access_token;
		FB.Auth.setSession( {
			'access_token' : access_token
		});
		loadUser();
	}
}

function loadUser() {
	FB.api('/me', function(response) {
		if (!response || response.error) {
			// console.log('Error occured');
		} else {
			returnResult("connect", response);
		}
	});
}

// returns results
function returnResult(func, result) {
	try {
		var f;
		if (navigator.appName.indexOf("Microsoft") != -1) {
			f = window[target];

		} else {
			f = document[target];
		}

		f[func](result);
	} catch (e) {
		// console.log(e);
	}
}

// sets flash target
var target = swfId;

window.fbAsyncInit = function() {

	// init
	FB.init( {
		apiKey : apiKey,
		cookie : false
	});

	FB.getLoginStatus(function(response) {
		if (response.session) {
			gotPerms(response);
		} else {
		}
	});
}

