var gigya_current_user;
var gigya_login_conf;
var zdeAccountStatus = "";

/** Comments **/
function showGigyaComments(apikey,categoryid,streamid,containerid){

  var gigya_comments_conf = { APIKey: apikey }
  
  var params ={
                categoryID: categoryid ,
                streamID: streamid,
                containerID: containerid,
                onError: commentsError,
                onCommentSubmitted: commentSubmitted,
                useSiteLogin: false,
                onSiteLoginClicked: showGigyaLogin,
                connectWithoutLoginBehavior: 'alwaysLogin',
                enabledProviders: 'facebook,linkedin,twitter,google,yahoo'
               }

  // display the comments UI
  gigya.services.socialize.showCommentsUI(gigya_comments_conf,params)

   // get the comment count
  if ($("#comment_count").length){
     comment_count_params ={
                  categoryID: categoryid,
                  streamID: streamid,
                  callback: updateCommentCount
                 }

     gigya.services.comments.getStreamInfo(gigya_comments_conf,comment_count_params);  
   }

}

function showGigyaLogin(eventName,source,context){
  gigya.services.socialize.showLoginUI({},{});  
}


function initGigyaSession(apikey,accountComplete){

 gigya_login_conf = { APIKey: apikey }

  if(accountComplete){
    zdeAccountStatus = "complete";
  }else{
    zdeAccountStatus = "incomplete";
  }

 // see who is logged in
 //gigya.services.socialize.getUserInfo(gigya_login_conf,{callback:setCurrentUser});

 // add handlers
 gigya.services.socialize.addEventHandlers(gigya_login_conf,{onLogin:gigyaLogin,onLogout:logoutRedirect,onConnectionAdded:gigyaLogin,callback:generic});

}

function setCurrentUser(result){

  gigya_current_user = result.user;

  // set the appropriate buttons based on the current user status
  if (typeof gigya_current_user === 'undefined' || result.user.UID==""){
    $("#logout").css("visibility","hidden");
    $("#menu_bar").css("visibility","hidden");
  }else{
    // if the user does not have a valid email address, boot them
    $("#logout").css("visibility","visible");
    $("#menu_bar").css("visibility","visible");
  }

 }


function gigyaLogout(apikey){

  var logout = {callback:logoutRedirect,forceProvidersLogout:true}
  gigya.services.socialize.logout(gigya_login_conf,logout)
  gigya.services.socialize.removeConnection({},{}); 

}

function commentSubmitted(result){

  // record the comment
  if(result.source == "showCommentsUI"){
    result.user.action_type = "comment";
    result.user.action_url = document.location.href;
    result.user.action_message = result.commentText;
    result.user.action_providers = result.user.providers.join();
    result.user.action_catid = catId;
    result.user.action_itemid = itemId;
  }else{
    result.user = {};
    result.user.action_type = "share";
    result.user.action_url = document.location.href;
    result.user.action_message = result.userMessage;
    result.user.action_providers = result.providers;
    result.user.action_catid = catId;
    result.user.action_itemid = itemId;
  }

  mboxTrack('eweek_commentSubmitted');

  $.post('zdeprofile/controllers/router.php?option=action',result.user,dump);

}

function dump(data){
}

function logoutRedirect(result){

 $.get('zdeprofile/view/logout.php',"");
 $("#user_info_bar").css("visibility","hidden");
 $("#menu_bar").css("visibility","hidden");
 $("#logout").css("visibility","hidden");

}

function generic(result){
  //console.log(result);
}

function gigyaLogin(result){
  
  if (result.eventName == "login"){

    //save the user info
    gigya_current_user = result.user;

    // send the data off to the server
    result.user.UID = encodeURIComponent(result.user.UID);
    result.user.source = "eweek";
    $.post('zdeprofile/view/login.php',result.user, loginResult);

    // set the username 
    //$("#user_info_bar").html("Hello " + result.user.firstName);

    // allow a logout
    //$("#user_info_bar").css("visibility","visible");
    //$("#logout").css("visibility","visible");
    //$("#menu_bar").css("visibility","visible");

  }
}

function loginResult(data,status,x){

/*
  if (data == "complete"){
    zdeAccountStatus = "complete";
  }else if(data=="failed"){
    zdeAccountStatus = "failed";
    gigyaLogout();
  }else{
    zdeAccountStatus = "incomplete";
  }
*/
}

function gigyaMultiShare(apikey,title,url,introtext){

  // Constructing a UserAction Object
  var act = new gigya.services.socialize.UserAction();
  
  // Setting the Title
  act.setTitle(title);
  
  // Adding a Link Back
  act.setLinkBack(url);
  
  // Setting the Description
  act.setDescription(introtext);

  // Adding a Media (image)
  act.addMediaItem( { 
      type: 'image',      // Type of the media (image/flash/mp3)
      src: 'http://www.eweek.com/images/eweek-meta.jpg',   // URL to the image source
      href: url    // URL to redirect the user when he clicks the image
   });

  // Activate the Share Plugin
  var gigya_share_conf = { 
                             connectWithoutLoginBehavior: 'alwaysLogin',
                             APIKey: apikey,
                             onSendDone: commentSubmitted
                         }

 gigya.services.socialize.showShareUI(gigya_share_conf, {  userAction:act });
}


function commentsError(result){
  //console.log(result);
}

function updateCommentCount(response){ 

    if ( response.errorCode == 0 ) {               
       $("#comment_count").html(response.streamInfo.threadCount);
    }
}

function gigyaShareBar(apikey,url,title,container)
{

        var ua = new gigya.services.socialize.UserAction();
        ua.setLinkBack(""+url+"");
        ua.setTitle(""+title+"");

        var conf = {
           APIKey:apikey
        }

        d = new Date();

        var shareBarParams ={
                userAction:ua,
                shareButtons: "linkedin,twitter,facebook,google-plusone",
                containerID: container,
                sessionExpiration: 30
        }
        gigya.services.socialize.showShareBarUI(conf,shareBarParams);
}

