//Perso SSO
var persoSsoShowBottomLogin = true;
var persoSsoTimer;
var persoSsoTimerMilliseconds = 100;
var persoSsoLoginFromBottomNavi = null; // used in close_dialog.html
var persoSsoSalutationHTML = "Willkommen #1 #2 #3 #4!" // default, the value should be defined in /country/language/_configuration.html in tab perso_engin
var persoSsoLoginText = "Login" // default, the value should be defined in /country/language/_configuration.html in tab perso_engin
var persoSsoLogoutText = "Logout" // default, the value should be defined in /country/language/_configuration.html in tab perso_engin

function persoSsoLoadJs(){
  if(typeof confPersoEngineSSOEnabled == "boolean"){
    if(confPersoEngineSSOEnabled){
      var e = document.createElement("script");
      e.src = confPersoEngineProfileController + "?rtyp=js&pid=" + confPersoEnginePid;
      e.type="text/javascript";
      document.getElementsByTagName("head")[0].appendChild(e);
      e = document.createElement("script");
      e.src = getWcmsPrefix() + "/_common/silo/user_status.js.jsp?country="+confCountryId+"&language="+confLanguageId;
      e.type="text/javascript";
      document.getElementsByTagName("head")[0].appendChild(e);
      persoSsoTimer = setTimeout('persoSsoCheckJs()', persoSsoTimerMilliseconds);
    }
  }
}

function persoSsoCheckJs(){
  if(typeof perso_profile == "object"){
    if((typeof perso_profile.data_loaded == "boolean") && (typeof silo_user_status == "string")){
      if(persoSsoShowBottomLogin && document.getElementById("bottomLogin") == null){
        persoSsoBuildBottomLoginHtml();
      }
      clearTimeout(persoSsoTimer);
      if (silo_sync_perso_sso_done) { // status from site
        if(silo_user_status != "no_user"){
          setVisibility("persoSsoLogout", 1, 'block');
          setVisibility("persoSsoLogin", 0, 'none');
        } else {
          setVisibility("persoSsoLogin", 1, 'block');
          setVisibility("persoSsoLogout", 0, 'none');
        }
      } else { // status from perso
        if(perso_profile.login_status != "not_logged_in"){
          setVisibility("persoSsoLogout", 1, 'block');
          setVisibility("persoSsoLogin", 0, 'none');
        }else if(perso_profile.login_status != "logged_in"){
          setVisibility("persoSsoLogin", 1, 'block');
          setVisibility("persoSsoLogout", 0, 'none');
        }
      }
      if(document.getElementById("persoSsoSalutation") != null){
        if(typeof perso_profile.userdata == "object"){
          if(typeof confPersoSsoSalutation == "string"){
            persoSsoSalutationHTML = confPersoSsoSalutation;
          }
          if((typeof perso_profile.userdata.salutation == "string") && (perso_profile.userdata.salutation != "")){
            persoSsoSalutationHTML = persoSsoSalutationHTML.replace(/\#1/, perso_profile.userdata.salutation);
          }else{
            persoSsoSalutationHTML = persoSsoSalutationHTML.replace(/\#1/, "");
          }
          if((typeof perso_profile.userdata.title == "string") && (perso_profile.userdata.title != "")){
            persoSsoSalutationHTML = persoSsoSalutationHTML.replace(/\#2/, perso_profile.userdata.title);
          }else{
            persoSsoSalutationHTML = persoSsoSalutationHTML.replace(/\#2/, "");
          }
          if((typeof perso_profile.userdata.firstname == "string") && (perso_profile.userdata.firstname != "")){
            persoSsoSalutationHTML = persoSsoSalutationHTML.replace(/\#3/, perso_profile.userdata.firstname);
          }else{
            persoSsoSalutationHTML = persoSsoSalutationHTML.replace(/\#3/, "");
          }
          if((typeof perso_profile.userdata.lastname == "string") && (perso_profile.userdata.lastname != "")){
            persoSsoSalutationHTML = persoSsoSalutationHTML.replace(/\#4/, perso_profile.userdata.lastname);
          }else{
            persoSsoSalutationHTML = persoSsoSalutationHTML.replace(/\#4/, "");
          }
          writeIntoLayer('persoSsoSalutation', persoSsoSalutationHTML);
          setVisibility("persoSsoSalutation", 1, 'block');
        }
      }
    }else{
      persoSsoTimer = setTimeout('persoSsoCheckJs()', persoSsoTimerMilliseconds);
    }
  }else{
    persoSsoTimer = setTimeout('persoSsoCheckJs()', persoSsoTimerMilliseconds);
  }
}

function persoSsoSendEvent(eventId, sessionId, rememberMe){
  if(typeof confPersoEngineSSOEnabled == "boolean"){
    if(confPersoEngineSSOEnabled){
      rememberMe = (rememberMe != "")?"&rme="+rememberMe:"";
      img = new Image();
      img.src = confPersoEngineSSOController + '&eid=' + eventId + '&sid=' + sessionId + rememberMe;
      return img;
    }
  }
  return null;
}

function persoSsoBuildBottomLoginHtml(){
  var newDiv = document.createElement("div");
  newDiv.id = "bottomLogin";
  //newDiv.style.position = "absolute";
  //newDiv.style.top = "0px";
  //newDiv.style.right = "124px";
  document.getElementById("bottomNavigation").appendChild(newDiv);

  var newA1 = document.createElement("a");
  newA1.id = "persoSsoLogin";
  newA1.href = "javascript:persoSSOLogin();";
  document.getElementById("bottomLogin").appendChild(newA1);
  addClassName("persoSsoLogin", "hidden");

  /*var newImg1 = document.createElement("img");
  newImg1.src = getWcmsPrefix() + "/_common/narrowband/img/palette/1x1_trans.gif";
  document.getElementById("persoSsoLogin").appendChild(newImg1);
  addClassName(newImg1, "arrow");*/
	
  if(typeof confPersoSsoLogin == "string"){
    persoSsoLoginText = confPersoSsoLogin;
  }
  var aText1 = document.createTextNode(persoSsoLoginText);
  document.getElementById("persoSsoLogin").appendChild(aText1);

  var newA1 = document.createElement("a");
  newA1.id = "persoSsoLogout";
  newA1.href = "javascript:persoSSOLogout();";
  document.getElementById("bottomLogin").appendChild(newA1);
  addClassName("persoSsoLogout", "hidden");

  /*var newImg2 = document.createElement("img");
  newImg2.src = getWcmsPrefix() + "/_common/narrowband/img/palette/1x1_trans.gif";
  document.getElementById("persoSsoLogout").appendChild(newImg2);
  addClassName(newImg2, "arrow");*/

  if(typeof confPersoSsoLogout == "string"){
    persoSsoLogoutText = confPersoSsoLogout;
  }
  var aText1 = document.createTextNode(persoSsoLogoutText);
  document.getElementById("persoSsoLogout").appendChild(aText1);
}

function persoSsoBuildLoginIframe(){
  if(document.getElementById("profileDialog") == null){
    var newDiv = document.createElement("div");
    newDiv.id = "profileDialog";
    newDiv.style.position = "absolute";
    newDiv.style.top = "101px";
    newDiv.style.left = "296px";
    newDiv.style.width = "700px";
    newDiv.style.height = "900px";
    newDiv.style.visibility = "hidden";
    newDiv.style.overflow = "hidden";
    newDiv.style.zIndex = "1000";
    document.getElementsByTagName("body")[0].appendChild(newDiv);

    var newIframe = document.createElement("iframe");
    newIframe.id = "profileDialogIFrame";
    newIframe.name = "profileDialogIFrame";
    newIframe.scrolling = "no";
    newIframe.src = "";
    newIframe.style.background = "transparent";
    newIframe.style.width = "700px";
    newIframe.style.height = "900px";
    newIframe.style.border = "none";

    var allowtrans = document.createAttribute("allowtransparency");
    allowtrans.nodeValue = "true";
    newIframe.setAttributeNode(allowtrans);

    var frameBd = document.createAttribute("frameborder");
    frameBd.nodeValue = "0";
    newIframe.setAttributeNode(frameBd);

    document.getElementById("profileDialog").appendChild(newIframe);
  }
}

function persoSSOLogin(){
  persoSsoLoginFromBottomNavi = true;
  persoSsoBuildLoginIframe();
  document.getElementById("profileDialogIFrame").src = getWcmsPrefix() + "/" + confCountryTopic + "/" + confLanguageTopic + "/_common/silo/login/login_flash.jsp?transfer=profile";
  setVisibility("profileDialog",1);
}

function persoSSOLogout(){
  persoSsoBuildLoginIframe();
  document.getElementById("profileDialogIFrame").src = getWcmsPrefix() + "/" + confCountryTopic + "/" + confLanguageTopic + "/_common/silo/login/logout.jsp?transfer=profile";
  setVisibility("profileDialog",1);
}

function persoSSOShowLogout(){
  setVisibility("persoSsoLogout", 1, 'block');
  setVisibility("persoSsoLogin", 0, 'none');
}

function persoSSOShowLogin(){
  setVisibility("persoSsoLogin", 1, 'block');
  setVisibility("persoSsoLogout", 0, 'none');
}
//Perso SSO
