/* == Share Options Hover Javascript == */
/**
 * A Share Options object
 *
 * @param string shareFriendsCallbackFunction	a callback function (or function name) to utilise when clicking the 'Share Encounters' button
 * @param string createEncounterCallbackFunction	a callback function (or function name) to utilise when clicking the 'Create new Encounter' button
 * @param string exploreLatestCallBackFunction	a callback function (or function name) to utilise when clicking the 'Explore latest encounters' button
 * @param string closeCallbackFunction	a callback function (or function name) to utilise when closing the share options hover
 */
var ShareOptions = function( shareFriendsCallbackFunction, createEncounterCallbackFunction, exploreLatestCallBackFunction, closeCallbackFunction )
{
	var _shareFriendsCallbackFunction;
	var _createEncounterCallbackFunction;
	var _exploreLatestCallBackFunction;
	var _closeCallbackFunction;


	/**
	 * Initialisation
	 *
	 * @param string shareFriendsCallbackFunction	a callback function (or function name) to utilise when clicking the 'Share Encounters' button
	 * @param string createEncounterCallbackFunction	a callback function (or function name) to utilise when clicking the 'Create new Encounter' button
	 * @param string exploreLatestCallBackFunction	a callback function (or function name) to utilise when clicking the 'Explore latest encounters' button
	 * @param string closeCallbackFunction	a callback function (or function name) to utilise when closing the share options hover
	 */
	this.init = function( shareFriendsCallbackFunction, createEncounterCallbackFunction, exploreLatestCallBackFunction, closeCallbackFunction )
	{
		_shareFriendsCallbackFunction = shareFriendsCallbackFunction;
		_createEncounterCallbackFunction = createEncounterCallbackFunction;
		_exploreLatestCallBackFunction = exploreLatestCallBackFunction;
		_closeCallbackFunction = closeCallbackFunction;
	}

	/**
	 * Displays the hover for the share options
	 */
	this.show = function()
	{
		var self = this;

		$('#hoverContainerShare').load('/share/hover/', function()
		{
			// Center the container using the jquery.center plugin
			// http://andreaslagerkvist.com/jquery/center/
			//$("#hoverContainerShare").center();
			centerPanel($("#hoverContainerShare"));

			// Fade in the container for the form
			$('#hoverContainerShare').fadeIn(1200, function(){

				// Ensure the 'Share Encounters' button calls the 'Share with Friends' action
				$("#btnShareEncounters").click( function()
				{
					self.shareFriends();
				});

				// Ensure the 'Create a new Encounter' button calls the 'Create new encounter' action
				$("#btnCreateEncounter").click( function()
				{
					self.createEncounter();
				});

				// Ensure the 'Explore latest encounters' button closes the hover and then redirects
				$("#btnExploreLatestEncounters").click( function()
				{
					self.exploreLatestEncounters();
				});

				// Ensure the close button closes the hover
				$("#btnCloseShare").click( function()
				{
					self.close();
				});

				// @todo - Get a design
				$('.addthis_button').click( function()
				{
					var shareUrl = 'http://www.murdochencounters.com.au';
					var addThisUrlPattern = 'url=([^&]+)';
					var urlMatches = this.href.match(addThisUrlPattern);
					if( urlMatches )
					{
						shareUrl = urlMatches[1];
					}


					addthis_open(this, '', shareUrl, 'Murdoch Encounters');

					setTimeout(function(){
						$('div#at15s').css('left', $('div#at15s').offset().left - 20);
						$('div#at15s').css('top', $('div#at15s').offset().top - 200);
					}, 50);
					
					
					return false;
				});

				// Omniture tracking
				$.getJSON('/author/loggedInJson/', function(data)
				{
					if( 'No' == data.optin )
					{
						omnitureTracking.share(false);
					}
					else
					{
						omnitureTracking.share(true);
					}
				})
			});
		});
	}

	/**
	 * Initiaties the share with friends action
	 */
	this.shareFriends = function()
	{
		// Adjust the z-index of the share options hover if necessary so the share friends
		// hover can overlay it.
		if( $("#hoverContainerShare").css('z-index') == '99' )
		{
			$("#hoverContainerShare").css('z-index', '80');
		}

		// Issue the callback function if exists
		if( typeof _shareFriendsCallbackFunction == 'function' )
		{
			_shareFriendsCallbackFunction();
		}
	}

	/**
	 * Initiaties the create encounter action
	 */
	this.createEncounter = function()
	{
		// Issue the callback function if exists
		if( typeof _createEncounterCallbackFunction == 'function' )
		{
			this.close();

			_createEncounterCallbackFunction();
		}
	}

	/**
	 * Initiaties the explore latest encounters action
	 */
	this.exploreLatestEncounters = function()
	{
		// Issue the callback function if exists
		if( typeof _exploreLatestCallBackFunction == 'function' )
		{
			this.close();

			_exploreLatestCallBackFunction();
		}

		return false;
	}

	/**
	 * Closes the Share Options hover
	 */
	this.close = function()
	{
		$('#hoverContainerShare').fadeOut(800, function()
		{
			$('#hoverContainerShare').empty();

			// Issue the callback function if exists
			if( typeof _closeCallbackFunction == 'function' )
			{
				_closeCallbackFunction();
			}
		});
	}


	// Initialise
	this.init(shareFriendsCallbackFunction, createEncounterCallbackFunction, exploreLatestCallBackFunction, closeCallbackFunction);
}
