function setUserActions(userAct, user_message, title, description, actionLink, linkBack, media) {
  if (typeof user_message !== 'undefined' && user_message) {
    userAct.setUserMessage(user_message);
  }
  if (typeof title !== 'undefined' && title) {
    userAct.setTitle(title);
  }

  if (typeof description !== 'undefined' && description) {
    userAct.setDescription(description);
  }

  if (typeof actionLink !== 'undefined' && actionLink) {
    userAct.addActionLink(actionLink.text, actionLink.url);
  }

  if (typeof linkBack !== 'undefined' && linkBack) {
    userAct.setLinkBack(linkBack);
  }

  if (typeof media !== 'undefined' && media) {
    userAct.addMediaItem(media);
  }

}

function successCallback(event, message) {
    /*
    var dummy_div = document.getElementById('gigya_dummy_div');
    if (typeof message === 'undefined') {
      message = 'Done!';
    }
    dummy_div.className = "success";
    dummy_div.innerHTML = message; 
    dummy_div.style.display = "block";
    */
}

function errorCallback(event, message) {
    /*
    if (typeof message === 'undefined') {
      message = 'An error has occured' + ': ' + event.errorCode + '; ' + event.errorMessage;
    }
    var dummy_div = document.getElementById('gigya_dummy_div');
    dummy_div.className = "error";
    dummy_div.innerHTML = message;
    dummy_div.style.display = "block";
    */
}

function defaultCallback(response, message) {
  /*
  var dummy_div = document.getElementById('gigya_dummy_div');
  if (response.errorCode === 0) {
    if (typeof message === 'undefined') {
      message = 'Done!';
    }
    dummy_div.className = "success";
    dummy_div.innerHTML = message;
    dummy_div.style.display = "block";

  } else {
    var message = 'An error has occured';
    dummy_div.className = "error";
    dummy_div.innerHTML = message;
    dummy_div.style.display = "block";
  }
  */
}

function defaultCommentCallback(response, tmp) {
  if (typeof tmp.params['cid'] !== 'undefined') {
    var cid = tmp.params['cid'];
  } else {
    var cid='undefined';
  }
  //alert(JSON.stringify(response));
  resp_data = {category_id: response.sourceData.categoryID, stream_id: response.sourceData.streamID, text: response.commentText, post_ids : JSON.stringify(response.providerPostIDs), cid: cid, title: response.commentTitle};
  if (response.ratings) {
    resp_data.ratings = response.ratings._overall;
  }
  $.ajax({
    type: 'GET',
    url: '/save_comment',
    data: resp_data
  });
}

/*
  Wrapper for http://wiki.gigya.com/020_Client_API/020_Methods/socialize.showShareUI
 -------------------------------------------- start showShareUI -------------------------------------
*/
function gShare(user_message, title, description, actionLink, linkBack, media, optional_success_message) { 
  this.userAct = new gigya.services.socialize.UserAction();
  setUserActions(this.userAct, user_message, title, description, actionLink, linkBack, media);
  this.params = {};
  this.params['snapToElementID:'] = "btnShare";
  this.params['showMoreButton:'] = true;
  this.params['showEmailButton:'] = true;
  this.params['onSendDone'] = function(event) {successCallback(event, optional_success_message)};
  this.params['onError'] = function(event) {errorCallback(event)};
}

gShare.prototype.setUserActParams = function() {
  this.params['userAction'] = this.userAct;
}

gShare.prototype.addParams = function(param_name, param_value) {
  this.params[param_name] = param_value;
}

gShare.prototype.showShareUI = function() {
  this.setUserActParams();
  gigya.services.socialize.showShareUI(conf,this.params);
}
/*
  -------------------------------------------- end showShareUI -------------------------------------
*/

/*
  Wrapper for http://wiki.gigya.com/020_Client_API/020_Methods/socialize.showShareBarUI
  -------------------------------------------- start showShareBarUI -------------------------------------
*/

function gShareBar(container, user_message, title, description, actionLink, linkBack, media) {
  this.userAct = new gigya.services.socialize.UserAction();
  this.container = container;
  setUserActions(this.userAct, user_message, title, description, actionLink, linkBack, media);
  this.params = {};
  this.params["showSuccessMessage"] = true;
  this.params['shareButtons'] = 'share,facebook,twitter,email';
  this.params['layout'] = 'horizontal';
}  

gShareBar.prototype.setUserActParams = function() {
  this.params['userAction'] = this.userAct;
  this.params['containerID'] = this.container;
}

gShareBar.prototype.addParams = function(param_name, param_value) {
  this.params[param_name] = param_value;
}

gShareBar.prototype.showShareUI = function() {
  this.setUserActParams();
  gigya.services.socialize.showShareBarUI(conf,this.params);
}

/*
  -------------------------------------------- end showShareBarUI -------------------------------------
*/

/*
  Wrapper for http://wiki.gigya.com/020_Client_API/020_Methods/socialize.showReactionsBarUI
  -------------------------------------------- start showReactionsBarUI -------------------------------------
*/
function gReactBar(bar_id, container, body_text, reactions, user_message, title, description, actionLink, linkBack, media, optional_success_message) {
  this.body_text = body_text;
  this.reactions = reactions;
  this.container = container;
  this.bar_id = bar_id;
  this.userAct = new gigya.services.socialize.UserAction();
  setUserActions(this.userAct, user_message, title, description, actionLink, linkBack, media);
  this.params = {};
  this.params['bodyText'] = this.body_text;
  this.params['reactions'] = this.reactions;
  this.params['containerID'] = this.container;
  this.params['barID'] = this.bar_id;

  this.params['onSendDone'] = function(event) {successCallback(event, optional_success_message)};
  this.params['onError'] = function(event) {errorCallback(event)};
}

gReactBar.prototype.setUserActParams = function() {
  this.params['userAction'] = this.userAct;
}

gReactBar.prototype.addParams = function(param_name, param_value) {
  this.params[param_name] = param_value;
}

gReactBar.prototype.showShareUI = function() {
  this.setUserActParams();
  gigya.services.socialize.showReactionsBarUI(conf,this.params);
}

/*
  Wrapper for http://wiki.gigya.com/020_Client_API/020_Methods/socialize.showFeedUI
  -------------------------------------------- start showActivityFeed ------------------------------------
*/

function gActivityFeed(container, site_name) {
  this.container = container;
  this.site_name = site_name;
  this.params = {};
  this.params['width'] = 300;
  this.params['height'] = 400;
  this.params['containerID'] = this.container;
  this.params['updateFreq'] = 10; 
}

gActivityFeed.prototype.addParams = function(param_name, param_value) {
  this.params[param_name] = param_value;
}

gActivityFeed.prototype.showFeedUI = function () {
  gigya.services.socialize.showFeedUI(conf, this.params); 
}
/*
  -------------------------------------------- end showActivityFeed ------------------------------------
*/

/*
  Wrapper for http://wiki.gigya.com/020_Developer_Guide/65_The_Newsfeed_Widget
  -------------------------------------------- start publishFeed ------------------------------------
*/

function gFeedPublish(user_message, title, description, actionLink, linkBack, media, optional_success_message) {
  this.userAct = new gigya.services.socialize.UserAction();
  setUserActions(this.userAct, user_message, title, description, actionLink, linkBack, media);
  this.params = {};
  this.params['callback'] = function(event) {defaultCallback(event, optional_success_message)};
  this.params['scope'] = 'both';
  this.params['privacy'] = 'public';
}

gFeedPublish.prototype.setUserActMessageFromInput = function(input_id) {
  var user_message = document.getElementById(input_id).value;
  this.userAct.setUserMessage(user_message);
}

gFeedPublish.prototype.publishFeed = function() {
  this.setUserActParams();
  gigya.services.socialize.publishUserAction(conf, this.params);  
}

gFeedPublish.prototype.setUserActParams = function() {
  this.params['userAction'] = this.userAct;
}

gFeedPublish.prototype.addParams = function(param_name, param_value) {
  this.params[param_name] = param_value;
}
/*
  -------------------------------------------- end publishFeed ------------------------------------
*/

/*
  FOR ADVANCE SHARE ONLY, REMEMBER TO ADD ACTION ID
  -------------------------------------------- start settingsMethods -------------------------------
*/

function gModifyUserSettings(id, action, action_value, provider, group) {
 
  if (typeof group === 'undefined') {
    group = 'gigyaShareSettings';
  }
 
  if (action !== 'remove') {
    var settings = {};
    act_value = false;
    if (action_value) act_value = true;
    var setting = {'actionID': id};
    setting[action] =  act_value;
    if (typeof provider !== 'undefined') {
      setting['providers'] = provider;
    }
    settings[id] = setting;
    var params = {
      'settings' : settings,
      'group' : group
    }

    gigya.services.socialize.setUserSettings(conf,params);
  } else {
    var params = {
      'settings': id,
      'group':  group
      //'callback' : function(response) {alert(id);}
    };
    gigya.services.socialize.delUserSettings(conf,params); 
  }
}

/*
  -------------------------------------------- end settingsMethods ---------------------------------
*/

/*
  Wrapper for http://wiki.gigya.com/020_Developer_Guide/66_The_Comments_Plugin
  http://wiki.gigya.com/020_Client_API/020_Methods/socialize.showCommentsUI
  -------------------------------------------- start comments plugin -------------------------------
*/

function gCommentsUI(container_id, category_id, user_message, title, description, actionLink, linkBack, media) {
  this.container_id = container_id;
  this.category_id = category_id;
  // content here overrides default text shown in the social network feed
  this.userAct = new gigya.services.socialize.UserAction();
  setUserActions(this.userAct, user_message, title, description, actionLink, linkBack, media);
  this.params = {};
  this.params['onError'] = function(event) {errorCallback(event)};
  this.params["containerID"] = this.container_id;
  this.params["categoryID"] = this.category_id;
  // will show up in user ActivityFeed
  this.params['scope'] = 'both';
  this.params["privacy"] = 'public';
}

gCommentsUI.prototype.setUserActParams = function() {
  this.params['userAction'] = this.userAct;
}

gCommentsUI.prototype.addParams = function(param_name, param_value) {
  this.params[param_name] = param_value;
}

gCommentsUI.prototype.showCommentsUI = function() {
  this.setUserActParams();
  var tmp = this;
  this.params["onCommentSubmitted"] = function(event) {defaultCommentCallback(event, tmp)};
  gigya.services.socialize.showCommentsUI(conf,this.params); 
}


/*
  -------------------------------------------- end comments plugin -------------------------------
*/


/*
  Wrapper for http://developers.gigya.com/010_Developer_Guide/18_Plugins/025_Rating_Reviews
*/

function gRatingUI(container_id, category_id, user_message, title, description, actionLink, linkBack, media) {
  this.container_id = container_id;
  this.category_id = category_id;
  // content here overrides default text shown in the social network feed
  this.userAct = new gigya.services.socialize.UserAction();
  setUserActions(this.userAct, user_message, title, description, actionLink, linkBack, media);
  this.params = {};
  this.params['onError'] = function(event) {errorCallback(event)};
  this.params["containerID"] = this.container_id;
  this.params["categoryID"] = this.category_id;
  // will show up in user ActivityFeed
  this.params['scope'] = 'both';
  this.params["privacy"] = 'public';
}

gRatingUI.prototype.setUserActParams = function() {
  this.params['userAction'] = this.userAct;
}

gRatingUI.prototype.addParams = function(param_name, param_value) {
  this.params[param_name] = param_value;
}

gRatingUI.prototype.showRatingUI = function() {
  this.setUserActParams();
  var tmp = this;
  gigya.services.socialize.showRatingUI(conf,this.params);
}


/*
 -------------------------------------------- end rating plugin -------------------------------
*/

/*
  wrapper for http://developers.gigya.com/030_API_reference/010_Client_API/020_Methods/socialize.showAddConnectionsUI
  -------------------------------------------- show addConnectionsUI plugin -------------------------------
*/

function gAddConnection() {
  this.params = {};
}

gAddConnection.prototype.addParams = function(param_name, param_value) {
  this.params[param_name] = param_value;
}

gAddConnection.prototype.showAddConnectionsUI = function() {
  gigya.services.socialize.showAddConnectionsUI(conf,this.params);
}
/*
  -------------------------------------------- end addConnectionsUI plugin -------------------------------
*/

/*
  wrapper for http://developers.gigya.com/040_Demos/039_Chat_Plugin
  -------------------------------------------- chat plugin -------------------------------
*/

function gChatUI(container_id, category_id) {
  this.params = {};
  this.params["containerID"] = container_id;
  this.params["categoryID"] = category_id;
  this.params["width"] = 300;
  this.params["height"] = 450;
}

gChatUI.prototype.addParams = function(param_name, param_value) {
  this.params[param_name] = param_value;
}

gChatUI.prototype.showChatUI = function() {
  gigya.services.socialize.showChatUI(conf,this.params);
}
/*
  -------------------------------------------- end chat plugin -------------------------------
*/

