var flash_ID = "flashcontent";
var flash_Obj = null;
var bitlyParameters = {
	version : '2.0.1',
	login : 'svcesar',
	apiKey : 'R_07f08b51dd9f5e3126af74677eac1c21',
	history : '0'
};

function authorizeFB() {
	window.open('./authorizefb/index.php', 'windowpopup', 'width=800, height=600, toolbar=false');
}

function authorizeFBResult( userId ) {
	$('#flashcontent')[0].authorizeFBResult( userId );
}

function facebookShare( shareURLSuffix ) {
	var urlToShorten = "http://www.sv-cesar.nl/";
	if (shareURLSuffix != "" && shareURLSuffix != null) {
		urlToShorten += shareURLSuffix;
	}
	
	var prependURL 	= 'http://www.facebook.com/share.php?u=';
	var appendURL 	= '&t=SV%20Cesar';
	openShortURL(urlToShorten, prependURL, appendURL);
}

function twitterShare( shareURLSuffix,shareText ) {
	var urlToShorten = "http://www.sv-cesar.nl/";
	if (shareURLSuffix != "" && shareURLSuffix != null) {
		urlToShorten += shareURLSuffix;
	}
	shareText = escape(shareText);
	
	var prependURL = "http://twitter.com/home?status=" + shareText + " ";
	var appendURL = "";
	urlToShorten = escape(urlToShorten);
	openShortURL(urlToShorten, prependURL, appendURL);
}

function hyvesShare ( shareURLSuffix, shareTitle, shareText )
{
	var urlToShorten = "http://www.sv-cesar.nl/";
	if (shareURLSuffix != "" && shareURLSuffix != null) {
		urlToShorten += shareURLSuffix;
	}
	shareTitle = escape(shareTitle);
	shareText = escape(shareText);
	var prependURL = "http://www.hyves.nl/profielbeheer/toevoegen/tips/?name="+shareTitle+"&text="+shareText+" ";
	var appendURL = "&type=12&rating=5";
	openShortURL(urlToShorten, prependURL, appendURL);
}

function openShortURL(urlToShorten, prependURL, appendURL) {
	var bitlyUrl = "http://api.bit.ly/shorten?" + "version=" + bitlyParameters.version
				 + "&login=" + bitlyParameters.login
				 + "&apiKey=" + bitlyParameters.apiKey + "&history=" + bitlyParameters.history
				 + "&format=json&callback=?"
				 + "&longUrl=" + urlToShorten;

	jQuery.getJSON(bitlyUrl, function (data) {
		var s = '';
        var first_result;
		for(var r in data.results) {
				first_result = data.results[r]; break;
		}
		for (var key in first_result) {
			if (key == "shortUrl"){
				s = first_result[key].toString();
			}
		}
		var mainURL = s;
		window.open(prependURL + mainURL + appendURL, 'windowpopup', 'width=800, height=600');
	});
}
