var external_service_authorize_callback;

/**
 * Triggers a popup for authorizing to an external service
 *
 * @param string 	service 						Service label
 * @param string  callback						Javascript to execute after authorize
 */
function popup_external_service_authorize_display(service, callback)
{
	external_service_authorize_callback = callback;
	
	switch(service)
	{
		case 'facebook':
			var url 		= facebook_authorize_url;
			var width 	= 600;
			var height 	= 300;

			break;
		
		case 'twitter':
			var url 		= twitter_authorize_url;
			var width 	= 850;
			var height 	= 400;
			
			// Refresh the Twitter authorize URL in case user needs to generate prompt again
			fetch_twitter_authorize_url();
			
			break;
			
		case 'google':
			var url 		= google_authorize_url;
			var width 	= 850;
			var height 	= 500;
			
			// Refresh the Buzz authorize URL in case user needs to generate prompt again
			//fetch_google_authorize_url();
		
			break;
		case 'meetup':
			var url 		= meetup_authorize_url;
			var width 	= 600;
			var height 	= 800;
			
			//fetch_meetup_authorize_url();
			break;	
			
		case 'linkedin':
			var url 		= linkedin_authorize_url;
			var width 	= 600;
			var height 	= 800;
			
			//fetch_linkedin_authorize_url();
			break;	
	}
	
	
	// Generates popup
  var external_service_authorize_popup = window.open(url, 'external_service_authorize','menubar=no,width='+width+',height='+height+',toolbar=no,location=no,menubar=no'); 

  // Moves the popup to the center of the screen
  var x = Math.ceil((screen.width / 2) - (width / 2));
  var y = Math.ceil(200);
  external_service_authorize_popup.moveTo(x, y);
}

/**
 * Fetches an authorize URL for Twitter authentication
 *
 */
function fetch_twitter_authorize_url()
{
	var onSuccess = function(data) { twitter_authorize_url = data.authorize_url; };

	$.get(routes['twitter_authorize_url'], onSuccess);
}

/**
 * Fetches an authorize URL for LinkedIn authentication
 *
 */
function fetch_linkedin_authorize_url()
{
	var onSuccess = function(data) { linkedin_authorize_url = data.authorize_url; };

	$.get(routes['linkedin_authorize_url'], onSuccess);
}

/**
 * Fetches an authorize URL for Google authentication
 *
 */
function fetch_google_authorize_url()
{
	var onSuccess = function(data) { google_authorize_url = data.authorize_url; };

	$.get(routes['google_authorize_url'], onSuccess);
}

/**
 * Fetches an authorize URL for Meetup authentication
 *
 */
function fetch_meetup_authorize_url()
{
	var onSuccess = function(data) { meetup_authorize_url = data.authorize_url; };

	$.get(routes['meetup_authorize_url'], onSuccess);
}

