﻿var MaBulle;
var MonCoinTopLeft;
var MonCoinTopRight;
var MonCoinBotLeft;
var MonCoinBotRight;
var HiddenFieldCom;
var BulleTdContent;
var BulleLoader;
var BulleDivLoader;
var LblTitre;
var LblMessage;
var MonPath;
var MonBouton;
var TimerBulle;
var WaitImgTimer;
var ShowTimer;

function InitBulle()
{
  MaBulle = document.getElementById('Bulle_DivMessage');
  if (!MaBulle)
  {
    setTimeout("InitBulle()", 50);
  }
  else
  {
    MonCoinTopLeft = document.getElementById('Bulle_TopLeft');
    MonCoinTopRight = document.getElementById('Bulle_TopRight');
    MonCoinBotLeft = document.getElementById('Bulle_BotLeft');
    MonCoinBotRight = document.getElementById('Bulle_BotRight');

    LblTitre = document.getElementById('BulleMan_LblBulle_Titre');
    LblMessage = document.getElementById('BulleMan_LblBulle_Message');

    HiddenFieldCom = document.getElementById('BulleMan_HidComBulle');
    BulleTdContent = document.getElementById('BulleTdContent');
    BulleLoader = document.getElementById('BulleLoader');
    BulleDivLoader = document.getElementById('BulleDivLoader');

    var el = document.getElementsByTagName("html")[0];
    Add_Event(el, 'click', HideDivInfo);
  }
}
InitBulle();
function ShowDivInfoAjax(Bouton)
{
  MonBouton = Bouton;
  window.clearTimeout(ShowTimer);
  ShowTimer = setTimeout("ShowDivInfoAjaxReal()", MonBouton.attributes.ShowDelai.value);
}
function ShowDivInfoAjaxReal()
{
  if (MonBouton != null)
  {
    ShowDivInfo(MonBouton, true)

    window.clearTimeout(TimerBulle);
    TimerBulle = setTimeout("LanceRequest()", 500);
  }
}
function LanceRequest()
{
  HiddenFieldCom.value = "Titre:" + MonBouton.attributes.Titre.value + ";Message:" + MonBouton.attributes.Message.value + ";IsTrad:" + MonBouton.attributes.IsTrad.value + ";TypeTrad:" + MonBouton.attributes.TypeTrad.value + ";TradLangue:" + MonBouton.attributes.TradLangue.value + ";";
  __doPostBack('BulleMan_UpdPanBulle', 'UpdateBulle');

}
function ShowDivInfo(Bouton, IsAjax)
{
  MonBouton = Bouton;
  window.clearTimeout(ShowTimer);
  ShowTimer = setTimeout("ShowDivInfoReal(" + IsAjax + ")", MonBouton.attributes.ShowDelai.value);
}
function ShowDivInfoReal(IsAjax)
{
  LblTitre = document.getElementById('BulleMan_LblBulle_Titre');
  LblMessage = document.getElementById('BulleMan_LblBulle_Message');
  LblTitre.innerHTML = "";
  LblMessage.innerHTML = "";

  if (MonBouton.attributes.Type.value == 'Err')
  {
    LblTitre.className = 'win_bulle_erreur_titre';
    LblMessage.className = 'win_bulle_erreur';
  }
  else
  {
    LblTitre.className = 'win_bulle_aide_titre';
    LblMessage.className = 'win_bulle_aide';
  }

  //MaBulle.style.visibility = "hidden";
  MaBulle.style.display = "block";
  BulleTdContent.style.display = "block";

  // Non IE
  if (typeof (window.innerWidth) == 'number')
  {
    BulleTdContent.style.minWidth = MonBouton.attributes.BulleContentWidthPx.value;
    BulleTdContent.style.minHeight = MonBouton.attributes.BulleContentHeightPx.value;
  }
  else
  {
    BulleTdContent.style.width = MonBouton.attributes.BulleContentWidthPx.value;
    BulleTdContent.style.height = MonBouton.attributes.BulleContentHeightPx.value;
  }
  MaBulle.style.width = (parseInt(MonBouton.attributes.BulleContentWidthPx.value) + 70) + "px";
  MaBulle.style.height = (parseInt(MonBouton.attributes.BulleContentHeightPx.value) + 70) + "px";

  if (!IsAjax)
  {
    //Inscription des message et titre
    LblTitre.innerHTML = Url.decode(MonBouton.attributes.Titre.value);
    LblMessage.innerHTML = Url.decode(MonBouton.attributes.Message.value);
  }

  //MonPath = MonBouton.attributes.Path.value;

  SetPosition();

  if (!IsAjax)
  {
    BulleLoader.style.display = "none";
    BulleDivLoader.style.display = "none";
  }
}
function SetPosition()
{

  //Reinit
  MonCoinTopLeft.src = MonPath + 'TopLeft.png';
  MonCoinTopRight.src = MonPath + 'TopRight.png';
  MonCoinBotLeft.src = MonPath + 'BotLeft.png';
  MonCoinBotRight.src = MonPath + 'BotRight.png';

  var IFrame = document.getElementById(MonBouton.attributes.IdFrame.value);
  var DivOverFlow;
  if (IFrame)
  {
    DivOverFlow = IFrame.contentWindow.document.getElementById(MonBouton.attributes.IdDivOverflow.value);
  }
  else
  {
    DivOverFlow = document.getElementById(MonBouton.attributes.IdDivOverflow.value);
  }
  //Variables
  //Écran
  var LargeurEcran = GetClientWidth();
  var HauteurEcran = GetClientHeight();
  
  //Scroll
  var PosScrollTop = GetScrollTop();
  var PosScrollLeft = GetScrollLeft();
  
  //Bulle
  var LargeurBulle = BulleTdContent.offsetWidth + 70;
  var HauteurBulle = BulleTdContent.offsetHeight + 70;
  
  //enplacement de départ par rapport au Bouton
  var PosFrameLeft = (IFrame) ? getAbsoluteLeft(IFrame) - IFrame.contentWindow.GetScrollLeft() : 0;
  var PosDivOverFlowLeft = (DivOverFlow) ? DivOverFlow.scrollLeft : 0;
  var PosStartLeft = (getAbsoluteLeft(MonBouton) + Math.floor(MonBouton.offsetWidth / 2)) + PosFrameLeft - PosDivOverFlowLeft;
  var PosFrameTop = (IFrame) ? getAbsoluteTop(IFrame) - IFrame.contentWindow.GetScrollTop() : 0;
  var PosDivOverFlowTop = (DivOverFlow) ? DivOverFlow.scrollTop : 0;
  var PosStartTop = (getAbsoluteTop(MonBouton) + Math.floor(MonBouton.offsetHeight / 2)) + PosFrameTop - PosDivOverFlowTop;

  //Valeur de l'espace entre le point central du bouton et la fleche de la bulle
  var Space = Math.floor(MonBouton.offsetWidth / 2) + 1;

  //Placement initial en haut a droite
  if (HauteurBulle + Space <= PosStartTop - PosScrollTop && LargeurBulle + Space <= LargeurEcran - PosStartLeft + PosScrollLeft)
  {
    MonCoinBotLeft.src = MonPath + 'target_BotLeft.png';
    MaBulle.style.left = (PosStartLeft + Space) + 'px';
    MaBulle.style.top = (PosStartTop - HauteurBulle - Space) + 'px';
  }
  // en bas a droite
  else if (HauteurBulle + Space > PosStartTop - PosScrollTop && LargeurBulle + Space <= LargeurEcran - PosStartLeft + PosScrollLeft) //Si la bulle est coincer par le haut de l'écran, mais pas par la largeur
  {
    MonCoinTopLeft.src = MonPath + 'target_TopLeft.png';
    MaBulle.style.left = (PosStartLeft + Space) + 'px';
    MaBulle.style.top = (PosStartTop + Space) + 'px';
  }
  // en haut a gauche
  else if (LargeurBulle + Space > LargeurEcran - PosStartLeft + PosScrollLeft && HauteurBulle + Space <= PosStartTop - PosScrollTop)//Si la bulle est coincer par la largeur de l'écran, mais pas par le haut de l'écran
  {
    MonCoinBotRight.src = MonPath + 'target_BotRight.png';
    MaBulle.style.left = (PosStartLeft - LargeurBulle - Space) + 'px';
    MaBulle.style.top = (PosStartTop - HauteurBulle - Space) + 'px';
  }
  // en bas a gauche
  else if (HauteurBulle + Space > PosStartTop - PosScrollTop && LargeurBulle + Space > LargeurEcran - PosStartLeft + PosScrollLeft)//Si la bulle est coincer par la largeur de l'écran ET par le haut de l'écran
  {
    MonCoinTopRight.src = MonPath + 'target_TopRight.png';
    MaBulle.style.left = (PosStartLeft - LargeurBulle - Space) + 'px';
    MaBulle.style.top = (PosStartTop + Space) + 'px';
  }

  if (BulleTdContent.offsetWidth > 0)
  {
    BulleLoader.style.left = (Math.round(BulleTdContent.offsetWidth / 2) - 10) + "px";
    BulleLoader.style.top = (Math.round(BulleTdContent.offsetHeight / 2) - 10) + "px";
    BulleLoader.style.display = "block";

    BulleDivLoader.style.display = "block";
    BulleDivLoader.style.width = (BulleTdContent.offsetWidth - 6) + "px";
    BulleDivLoader.style.height = (BulleTdContent.offsetHeight - 6) + "px";
  }
}
function HideDivInfo()
{
  window.clearTimeout(WaitImgTimer);
  window.clearTimeout(ShowTimer);
  window.clearTimeout(TimerBulle);
  MonBouton = null;
  document.getElementById('Bulle_DivMessage').style.display = "none";
}
var timerbullescrollto;
var timerbullescrolltoframe;
function ShowError(p_Id, p_OptionalIdFrame)
{
  var IFrame = document.getElementById(p_OptionalIdFrame);
  var Bouton;
  if (IFrame)
  {
    Bouton = IFrame.contentWindow.document.getElementById(p_Id + '_ImgInfo');
  }
  else
  {
    Bouton = document.getElementById(p_Id + '_ImgInfo');
  }
  Bouton.style.display = 'block';
  Bouton.style.visibility = 'visible';
  clearTimeout(timerbullescrollto);
  clearTimeout(timerbullescrolltoframe);
  var scrolltop = getAbsoluteTop(Bouton) - 130;
  var scrollleft = getAbsoluteLeft(Bouton) - 20;
  if (IFrame)
  {
    var Iframescrolltop = getAbsoluteTop(IFrame) - 20;
    var Iframescrollleft = getAbsoluteLeft(IFrame) - 20;
    timerbullescrolltoframe = setTimeout('scrollTo(' + Iframescrollleft + ',' + Iframescrolltop + ')', 100);
    timerbullescrollto = setTimeout('document.getElementById(\'' + p_OptionalIdFrame + '\').contentWindow.scrollTo(' + scrollleft + ',' + scrolltop + ')', 200);
  }
  else
  {
    if (Bouton.attributes.IsAjax.value == 'True')
    {
      timerbullescrollto = setTimeout('scrollTo(' + scrollleft + ',' + scrolltop + ')', 200);
    }
    else
    {
      timerbullescrollto = setTimeout('scrollTo(' + scrollleft + ',' + scrolltop + ')', 0);
    }
  }
  if (Bouton.attributes.IsAjax.value == 'True')
  {
    ShowDivInfoAjax(Bouton);
  }
  else
  {
    ShowDivInfo(Bouton);
  }
}
function HideError(p_Id, p_OptionalIdFrame)
{
  var IFrame = document.getElementById(p_OptionalIdFrame);
  var Bouton;
  if (IFrame)
  {
    Bouton = IFrame.contentWindow.document.getElementById(p_Id + '_ImgInfo');
  }
  else
  {
    Bouton = document.getElementById(p_Id + '_ImgInfo');
  }
  Bouton.style.visibility = 'hidden';
  Bouton.style.display = 'none';
  HideDivInfo()
}
var BulleAllUrl = new Array();
var BulleAllImg = new Array();
function AjaxFinished()
{
  document.getElementById('__EVENTARGUMENT').value = "";
  if (MonBouton != null)
  {

    //getimage url dans progress.js
    LblTitre = document.getElementById('BulleMan_LblBulle_Titre');
    LblMessage = document.getElementById('BulleMan_LblBulle_Message');


    if (MonBouton.attributes.Type.value == 'Err')
    {
      LblTitre.className = 'win_bulle_erreur_titre';
      LblMessage.className = 'win_bulle_erreur';
    }
    else
    {
      LblTitre.className = 'win_bulle_aide_titre';
      LblMessage.className = 'win_bulle_aide';
    }

    BulleAllUrl.length = 0;
    BulleAllImg.length = 0;
    getimages(LblMessage, BulleAllUrl);
    //Creer les images
    for (var i = 0; i < BulleAllUrl.length; i++)
    {
      BulleAllImg[i + 1] = new Image();
      BulleAllImg[i + 1].src = BulleAllUrl[i];
    }
    //puis on attends les images
    window.clearTimeout(WaitImgTimer);
    WaitImgTimer = setTimeout("BulleWaitImage()", 50);
  }
}
function BulleWaitImage()
{
  window.clearTimeout(WaitImgTimer);
  window.clearTimeout(ShowTimer);
  window.clearTimeout(TimerBulle);

  //set les pos des loaders
  SetPosition();
  var AllLoaded = true;
  for (var i = 0; i < BulleAllImg.length - 1; i++)
  {
    try
    {
      if (!BulleAllImg[i + 1].complete)
      {
        AllLoaded = false;
      }
    } catch (ex)
           { }
  }
  if (AllLoaded)
  {
    BulleLoader.style.display = "none";
    BulleDivLoader.style.display = "none";
    MonBouton = null;
  }
  else
  {
    WaitImgTimer = setTimeout("BulleWaitImage()", 50);
  }
}
