/*
 PROPRIETARY RIGHTS NOTICE:  All rights reserved. No part of the code
 (source,object,executable), screen-images, report-formats, database
 schemas, and documentation of this program may be reproduced or transmitted
 in any manner without the License or expressed written permission of
 TEDS, LLC  P.O. Box 700, Atkins, VA 24311.
 All parts of the code (source,object,executable), screen-images,
 database schemas, report-formats, and documentation of this program
 are considered proprietary information and should only be accessible
 by those identified in the License.
 */

function linkedInLinkLoginFromButton (root, ie9Widget, eventListenerFunction) {
	var docModeIE = document.documentMode;
    // The document.documentMode test gives the CSS support version for IE
    // Its undefined if not IE, and under 10 if IE is below v10.
    if (typeof docModeIE !== 'undefined' && docModeIE < 10) {
    	ie9Widget.show();
    } else {
    	eventListenerForMessages(eventListenerFunction);
    	window.open(root.concat('/socialMediaAuthorization?media=LINKEDIN'), 'LinkedIn', 'dependent=yes, menubar=no, toolbar=no');
	}
}

function eventListenerForMessages(functn) {
  var docModeIE = document.documentMode;
  // The document.documentMode test gives the CSS support version for IE
  // Its undefined if not IE, and under 10 if IE is below v10.
  if (typeof docModeIE === 'undefined' || docModeIE >= 10) {
    window.addEventListener('message', functn, false);
  }
}
  
function showPullingDialog(widget, ie9Widget){
    var docModeIE = document.documentMode;
    // The document.documentMode test gives the CSS support version for IE
    // Its undefined if not IE, and under 10 if IE is below v10.
    if (typeof docModeIE !== 'undefined' && docModeIE < 10) {
      ie9Widget.show();
    } else if (typeof widget !== 'undefined' && widget !== null){
	  widget.show();
	}
}

/*
 * The remote commands called here are stored in the
 * socialMediaDialog.xhtml and login.xhtml files.
 */
function accountWindowRefresh(event) {
  if (event.data == 'success') {
    accountWindowSuccessRefreshRemoteCommand(); 
    linkAndPullRemoteCommand();
  } else if (event.data == 'failure') {
    accountWindowUnsuccessRefreshRemoteCommand();
  }
}
  
function loginWindowRefresh(event) {
  if (event.data == 'success') {
    loginWindowSuccessRefreshRemoteCommand();
  } else if (event.data.substring(0,18) == 'successAskQuestion') {
    loginWindowAskQuestionRemoteCommand([{name:'memberId', value:event.data.substring(28,event.data.length)}]);
  } else if (event.data == 'failure') {
    loginWindowUnsuccessRefreshRemoteCommand();
  } 
}

/**
 * toggleSocialChangePassword
 * Toggles visibility between the linking and change password screens 
 */
function toggleSocialChangePassword() {
  var anims = 'webkitAnimationEnd oanimationend msAnimationEnd animationend';
  var container = $(".social-dialog-container");
  var link = $(".social-link-container");
  if (container.hasClass("register-visible")) {
    link.addClass("open");
    link.one(anims, function() {
      link.removeClass("open");
    });
  }

  $(".social-dialog-container").toggleClass("socialChangePassword-visible");
}

/**
 * handleChangePasswordComplete
 * Hides the change password dialog only if the process was successfully completed
 * @param xhr
 * @param status
 * @param args
 */
function handleChangePasswordComplete(xhr, status, args) {
  if (args && !args.validationFailed) {
    PF('socialMediaDlg').hide();
    unlinkAfterChangePasswordRemoteCommand();
  }
}
