var sharingCacheTimeout = 1000
var USER;
var onUserStateChange;
$(document).ready(function() {
	if(!gigya) {
	    if(console && console.error) {
	      console.error("Gigya socialize.js not on page -- do not load Gigya / Disqus integration!");
	    }
	    return;
	  }
  // Bind to login/logout links
  $(".gigya_login").on("click", function() {
	gigya.accounts.showScreenSet({screenSet:'Login-web', mobileScreenSet:'Mobile-login'})
    return false;
  });
  $(".gigya_logout").on("click", function() {
	  gigya.accounts.logout();
    return false;
  });
  $(".gigya_register").on("click", function() {
	  gigya.accounts.showScreenSet({screenSet:'Login-web', mobileScreenSet:'Mobile-login', startScreen:'gigya-register-screen'});
    return false;
  });
  $(".gigya_edit_profile").on("click", function() {
	  gigya.accounts.showScreenSet({screenSet:'Profile-web', mobileScreenSet:'Mobile-profile', onAfterSubmit:on_profile_change})
    return false;
  });
  
  // Render UI based on user state
  

  var onLogin = function(user) {
    USER = user;
    onUserStateChange();
  }

  var onLogout = function() {
    USER = undefined;
    onUserStateChange();
  }

  onUserStateChange = function() {
    if(USER) {
      $(".anon").hide();
      $(".auth").show();
      
      var msg = "Welcome " + USER.profile['firstName'] + " "+ USER.profile['lastName'];
      $('#id_welcome_msg').html(msg)
      
    } else {
      // Logged out
      $(".anon").show();
      $(".auth").hide();
    }
  }

  gigya.accounts.addEventHandlers({
    onLogin: onLogin,
    onLogout: onLogout
  });

  gigya.accounts.getAccountInfo({
    callback: function(response) {
      USER = response.errorCode === 0 ? response : undefined;
      $(document).ready(onUserStateChange);
    }
  });
  
  load_sharing_tallies();
});

function isLoggedIn()
{
	if(USER)
		return true
	else
		return false
} 


function load_sharing_tallies()
{
	$(".share-count").each(function(){
		url = $(this).attr("data-for_url")
		if(url==null)
		{
			// This is just saving an API call. Setting the reponse of as it would
			// come in if we called it
			response = {'errorCode':400002, 'context':$(this),
					     'errorMessage':'Missing URL'}
			display_sharing_tally(response)
			return;
		}
		gigya.socialize.getProviderShareCounts({URL:url, callback:display_sharing_tally, context:$(this), cacheTimeout:sharingCacheTimeout});
	})
}

function display_sharing_tally(response)
{	
	total = '?'
	if(response.errorCode==0)
	{
    	total = 0;
    	for(key in response.shareCounts)
			total+=response.shareCounts[key]
	}
	response.context.find(".qty").html(total)
}

function on_profile_change()
{
	$("body").trigger("GigyaProfileChanged");
	setTimeout(update_account_info, 2000) 		
}

function update_account_info()
{
	gigya.accounts.getAccountInfo({ callback: function(response) {
		USER = response
		onUserStateChange()
	}});
}
