/**
 * 
 * @application Beermat Challenge
 * @version 0.0.0.1
 * @author Chris Keegan
 * @copyright Evans & Finch, 2009
 * 
 */

function getSessionId()
{
	if ( typeof( window.m_tracker ) == "undefined" )
		return -1;
	
	return window.m_tracker.m_session_id;
}

function onSettingsLoaded( tracker )
{
	
}

function setFlashVersion( version )
{
	window.bmcPage.version = version;
}

function callPublishToFacebook( message, attachment, action_link, prompt_message )
{
  FB.ensureInit(function () 
  {
    FB.Connect.streamPublish(message, attachment, action_link, "", prompt_message, function( post_id, exception )
  	{
  		if ( post_id != null && post_id != "null" )
  		{
  			$( "#flash" ).get(0).gameEvent( { event: "share_success" } );
  		}
  		else
  		{
  			$( "#flash" ).get(0).gameEvent( { event: "share_fail" } );
  		}
  	});
  });
}

function publishToFacebook( idea_id, idea_text, my_idea )
{
	var post = 
	{
	 	message: my_idea ? "Everyone vote for my idea! Help me win prizes! Yay!" : "Vote for this great idea! Let's make it win!",
	  attachment: 
	  {
	    name: my_idea ? 'Vote for my Idea' : 'Vote for this Idea',
	    description: idea_text,
	    href: 'http://www.beermatchallenge.co.uk/idea/' + idea_id
	  },
	 	action_links: 
	 	[
      {
        text: 'Vote',
        href: 'http://www.beermatchallenge.co.uk/idea/' + idea_id
      },
    ],
	 	user_prompt_message: my_idea ? 'Win more votes by Badgering your Friends!' : 'Vote for this great Idea!'
  };
	 
	if ( typeof( window.m_tracker ) != "undefined" )
		window.m_tracker.trackConversion("share_on_facebook");
	
	//callPublishToFacebook( post.message, post.attachment, post.action_links[ 0 ] );
	//callPublishToFacebook( post.message, post.attachment, post.action_links, post.user_prompt_message );
	callPublishToFacebook( post.message, post.attachment, [], post.user_prompt_message );
	
	/*
	FB.publish( post, function( published_post )
	{
		if ( typeof( published_post ) != "undefined" )
		{
			$( "#flash" ).get(0).gameEvent( { event: "share_success" } );
		}
		else
		{
			$( "#flash" ).get(0).gameEvent( { event: "share_fail" } );
		}
		return true;
	});
	*/
	
	return this;
}; //publishtoFacebook


function shareIdea( idea )
{
	publishToFacebook( idea.idea_id, idea.idea_text, idea.my_idea );
}

( 
	function()
	{
		var bmcPage = window.bmcPage = function()
		{
			return bmcPage.fn.__init();
		};
	
		bmcPage.fn = (Object) (bmcPage.prototype =
		{
			version: "0.0.0",
			heat: 0,
				
			__init : function()
			{
				window.page = this;
				
				//FB.init( { apiKey: '72b70bc1014465aae4ecdc109da88bdc' } );
				FB.init( '72b70bc1014465aae4ecdc109da88bdc' );
			
				this.heat = $("meta[name=heat]").attr("content");
				
				if ( typeof( this.init ) != "undefined" )
					this.init();
				
				return this;
			} // init
			
		});

	}
)();

bmcPage.fn.__init.prototype = bmcPage.fn;

$().ready( function()
{
	var page = bmcPage();
});

