/*
 * Copyright 5D Research Inc. 2009.
 */

/*
 * Convenience method for moving a div a particular distance in the
 * positive x direction.
 */
function moveToPrevious(divToMove, distance)
{
  new Effect.Move(divToMove, { x: distance, y: 0, transition: Effect.Transitions.sinoidal, queue: 'end' });
}

/*
 * Convenience method for moving a div a particular distance in the
 * negative x direction.
 */
function moveToNext(divToMove, distance)
{
  new Effect.Move(divToMove, { x: -distance, y: 0, transition: Effect.Transitions.sinoidal, queue: 'end' });
}

/*
 * local variable to keep track of the last movement time.  This lets
 * us prevent automatic scrolling after the user has clicked a button.
 */
var shar_lastMove = 0;

/*
 * Move the given divToMove relative to the parent boxDivID, the given 
 * distance multiplied by the index.
 */
function moveToPosition(boxDivID, divToMove, distance, index)
{
  shar_lastMove = new Date().getTime();
  
  var boxPosition = $(boxDivID).cumulativeOffset();
  var contentPosition = $(divToMove).cumulativeOffset();
  
  var newOffset = (index * distance);
  
  var newPosition = (boxPosition[0] - newOffset);
  var move = (newPosition - contentPosition[0]);
  
  new Effect.Move(divToMove, { x: move, y: 0, transition: Effect.Transitions.sinoidal, queue: 'end' });
}

/*
 * Return the last movement time.
 */
function getLastMovementTime() {
	return shar_lastMove;
}

/*
 * Reload the window.
 */
function reload() {
	window.location = window.location;
}

/*
 * If the user is connected to Facebook, this function is called to log
 * them out.
 */
function performLogout(uid) {
	FB.Connect.logoutAndRedirect("/logout/");
}

/*
 * Helper function for logging the user out of Facebook.
 */
function fb_logout() {
	FB.Connect.ifUserConnected(performLogout, "/logout/");
}

/*
 * Log the user in ane reload the page.
 */
function fb_login() {
    FB.Connect.requireSession(
        function() {
        	// reload the window location so that we can pull the session
        	// information out of the cookie.
        	reload();
		},		
		function() {
		    
	    }, true
    );
}
    
/*
 * Callback for granting a new high score for the user.
 */
function highScoreGranted(asset_name, asset_description, asset_id, width, height, thumbnail_width, thumbnail_height, thumbnail_url, score) {
    
    var app_width = 460;
    var app_height = 345;
    
	if (width < app_width) { 
		app_width = width; 
	}
	app_height = app_width * (height / width);
	if (app_height > 345) { 
		var ratio = 345 / app_height; 
		app_height = 345;  
		app_width = app_width * ratio; 
	}
	
	var app_thumbnail_width = 130;
	var app_thumbnail_height = 130 * (thumbnail_height / thumbnail_width);
    
	var action_links = new Array(1);
	action_links[0] = {'text': 'Beat their score!', 'href': 'http://sharendipity.com/assets/' + asset_id + '/' };

	var attachment = {
      'caption': '{*actor*} just beat their best score in ' + asset_name + ' with a score of ' + score,
      'description': asset_description,                  
      'media': [{'type': 'flash', 'swfsrc': 'http://static.sharendipity.com/player.swf?aid=' + asset_id, 'imgsrc': thumbnail_url, 'width': app_thumbnail_width, 'height': app_thumbnail_height, 'expanded_width': app_width, 'expanded_height': app_height }]
    };
      
    FB.Connect.streamPublish('', attachment, action_links, '', 'You beat your high score in ' + asset_name + '!'); 	
}

/**
 * Callback for granting an achievement to the user.
 */
function achievementGranted(asset_name, asset_description, asset_id, width, height, thumbnail_width, thumbnail_height, achievement_name, achievement_description, achievement_thumbnail) {
	
    var app_width = 460;
    var app_height = 345;

	if (width < app_width) { 
		app_width = width; 
	}
	app_height = app_width * (height / width);
	
	if (app_height > 345) { 
		var ratio = 345 / app_height; 
		app_height = 345;  
		app_width = app_width * ratio; 
	}

	var app_thumbnail_width = 130;
	var app_thumbnail_height = 130 * (thumbnail_height / thumbnail_width);
	
	var action_links = new Array(1);
	action_links[0] = {'text': 'Get it Yourself!', 'href': 'http://sharendipity.com/assets/' + asset_id + '/' };
	
	var attachment = {
      'caption': '{*actor*} was awarded the \"' + achievement_name + '\" achievement in ' + asset_name,
      'description': 'Achievement Desription: \"' + achievement_description + '\"',                  
      'media': [{'type': 'flash', 'swfsrc': 'http://static.sharendipity.com/player.swf?aid=' + asset_id, 'imgsrc': achievement_thumbnail, 'width': app_thumbnail_width, 'height': app_thumbnail_height, 'expanded_width': app_width, 'expanded_height': app_height }]
    };
    
	FB.Connect.streamPublish('', attachment, action_links, '', 'You were awarded the \"' + achievement_name + '\" achievement!');
}
