if(typeof logc !== 'function'){
	function logc(inp){
		if (window.console){
			console.log(inp);
		}
		return inp;
	}
}

(function(){

	var //toaster = $('#toaster').toaster().data('toaster'), // Create toaster instance store references
	
		channels = Chemistry.channels, // Create a shortcut to Chemistry.channels 	
		
		polls = {
	
			master: new Chemistry.Poll('master', { url:'/UINotification/UINotificationPoll' }), // Create an instance of the master poll.			
			invite: new Chemistry.Poll('invite', { url:'/HandShaking/Poll', frequency:5000, channel: channels.INVITATION }) // Create an instance of the invitation poll.
	
		};
	

	// Initialization for master polling.
	(function(){

		// Subscriptions consumed by the master poll.
		$.subscribe( channels.MASTER, _masterSubscription );			// Subscribe to the master thread and specific data threads.
		$.subscribe( channels.OPEN_INVITE, _openInviteSubscription );	// Informs of open invitations, used to toggle between polls.
		$.subscribe( channels.COUNTS, _countSubscription );			// Publishes counts.
		
		// Break apart the data received by the master poll and publish it to more specific channels.
		function _masterSubscription( response ) {
		
			if (response.NavigationTotals)
			{
				$.publish(channels.COUNTS, [ response.NavigationTotals ]);
			}

			if (response.PresenceDictionary)
			{
				$.publish(channels.PRESENCE, [ response.PresenceDictionary ]);
			}
			
			if (response.ToastList)
			{
				$.publish(channels.NOTIFICATIONS, [ response.ToastList ]);
			}
			
			$.publish(channels.OPEN_INVITE, [ response.OnlineInvitationExists ]); // Data is a boolean value, always publish it.
		
		}		
		
		function _openInviteSubscription( open ) {				
			
			// If there is an open invitation..
			if ( open )
			{
				// and the invitation poll isn't running...
				if (!polls.invite.active())
				{				
					// stop all running polls and start up the invitation poll.
					Chemistry.Poll.stopAll();
					
					polls.invite.start();
					
					polls.invite.fetch();
				}

			}
			else			
			{	
				// If there isn't an open invitation and the master poll isn't running.
				if(!polls.master.active())
				{
					// stop all polls that are running and start up the master poll.
					Chemistry.Poll.stopAll();
					
					polls.master.start();
				}			
			}			
		}

		function _countSubscription( data ) {
		
			if (!data){ return; }
		
			var counters = $('[data-count-key]');
			
			if (counters.length){
			  
			  counters.each(function(){
				
				var current = $(this), key = current.data('count-key');
				
				data[key] && current.text( data[key] );      
			  
			  });    
			
			}    
			
		}    
			
		// Subscriptions consumed by the invitation poll.
		$.subscribe( channels.INVITATION, _invitationSubscription );	// Publishes open invitation data.
								
		function _invitationSubscription( data ){
					
			var invitation = Chemistry.Invitation.current, element, 
			
				invite = { 
				
					model: data.HandShakeResponse,
					
					content: data.ViewMarkup 
				
				};

			// Update the existing instance if there is one.        
			if ( invitation ) {
			
				// Update the invitation model and content.
				invitation.update( invite );
						
			}
			else
			{				
				// There isn't a current invitation object, create a toast for it.
				element = toaster.toast({ sticky: true, cssClass:'toast-invite' }).element;
				
				// Create a new invitation and associate the toast with it.
				new Chemistry.Invitation( element, invite );      

			}

			if (data.HandShakeResponse.IsPollTerminatingResponse)
			{
				// Publish false to the OPEN_INVITE channel so that the invite poll gets turned off.
				$.publish( channels.OPEN_INVITE, [ false ] );

			}
			
			
		}
		
		// If there navigation on the page, we're not on a secure page, and there is no open Invitation, start the master poll.
//		if ($('#nav').length && window.location.protocol.indexOf('https') === -1 && !Chemistry.Invitation.current)
		if ($('#nav').length && window.location.protocol.indexOf('https') === -1)
		{
			polls.master.start();
		}
	  
	})();   
	
	// Initialization for toasting.
	(function(){/*
						
		// Check to see if there is a toast that was rendered on the server.
		var toasts = toaster.element.children();

		if (toasts.length){
				
			// For each toast rendered by the server, create a javascript toast instance and associate it with the toast element using inherited defaults and sensible overrides.
			toasts.each(function(){
					
				var toast = $(this), sticky = toast.data('sticky'), 
							
					configuration = {
						
						duration: toaster.configuration.duration,

						sticky: sticky,

						element: toast

					};
					
				toast.data('toast', new Chemistry.Toast( configuration ));
				
				// If sticky, it must be a game invitation; parse Invitation data from the data-invitation attribute and instatiate an Invitation. server and hand responsibility over to the invitation engine.
				   
				if ( sticky ){

					toast.addClass('toast-invite');
					
					new Chemistry.Invitation( toast,  {
					
						model: toast.data('model'), // JSON-serialized HandShakeRegistration object.
					
						content: toast.html()       // The current html that's been rendered.
									
					});
					
					$.publish( channels.OPEN_INVITE, [ true ] ); // Publish to the OPEN_INVITE channel and let it know that there is an open invitation.
					
				}
			   
			});
		
		}

	*/})();


	// TODO: The following code is for testing purposes only. The click event bound should be attached to links that start a game invitation.
	// The tag that the event is bound to should have two data attributes: model and action
	//
	//  @{ var hsinvitation = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(new { GuestUserId = Model.UserId, GameSessionId = Guid.NewGuid() }); }        
	//  <a class="initiate-invite" data-model="@hsinvitation" data-action="@Url.Action("Invite", "HandShaking")">Invite User</a>
	//

	(function(){/*

		$('body').delegate('.initiate-invite', 'click', function(){

			if ( !Chemistry.Invitation.current )
			{
				var anchor = $(this), data = anchor.data();

				if ( data && data.model ){

					var toast = toaster.toast({ sticky:true, cssClass:'toast-invite' }).element; //Create an empty toast object that the invitation can use.

					new Chemistry.Invitation( toast, data ).invite();

				}

			}

			return false;

		});

*/	})();


})();
