/*********/
/* popin */
/*********/
// Déclaration du plugin
(function ($)
{
  $.popin = function (message, title, options)
  {
    // On écrase les options par défaut si ils sont surchargées en paramètre
    var o = $.extend ({}, $.popin.defaults, options);
    
    // Cache ses éléments qui apparaitraient au dessus de la popin sous IE
    $ ('embed, object, select').css ( { 'visibility' : 'hidden' } );
    
    // Mise en place de la popin
    if (_exists () && _sameConfig ())
    {
      $ ('#taojquery-popin-contenu-title').html (title);
      $ ('#taojquery-popin-contenu-message').html (message);
      $ ('#taojquery-popin').fadeIn ();
      $ ('#taojquery-popin-overlay').fadeIn ();
      $.popin.repositionnement ();
    }
    else if (_exists ())
    {
      $ ('#taojquery-popin').remove ();
      $ ('#taojquery-popin-overlay').remove ();
      _init ();
    }
    else
    {
      _init ();
    }
    
    // Fonction privée pour afficher la popin
    function _init ()
    {
      if (o.withBtnClose)
    	if (title)
          $ ('body').append ('<div id="taojquery-popin-overlay"></div><div id="taojquery-popin" class="' + o.classe + '"><div id="taojquery-popin-container-box"><div id="taojquery-popin-container"><div id="taojquery-popin-contenu"><div id="taojquery-popin-contenu-title">' + title + '</div><div id="taojquery-popin-contenu-message">' + message + '</div></div><div><a id="taojquery-popin-btnClose"><img src="' + o.imageBtnClose + '"></a></div></div></div></div>');
    	else
    	  $ ('body').append ('<div id="taojquery-popin-overlay"></div><div id="taojquery-popin" class="' + o.classe + '"><div id="taojquery-popin-container-box"><div id="taojquery-popin-container"><div id="taojquery-popin-contenu"><div id="taojquery-popin-contenu-message">' + message + '</div></div><div><a id="taojquery-popin-btnClose"><img src="' + o.imageBtnClose + '"></a></div></div></div></div>');
      else
      	if (title)
          $ ('body').append ('<div id="taojquery-popin-overlay"></div><div id="taojquery-popin" class="' + o.classe + '"><div id="taojquery-popin-container-box"><div id="taojquery-popin-container"><div id="taojquery-popin-contenu"><div id="taojquery-popin-contenu-title">' + title + '</div><div id="taojquery-popin-contenu-message">' + message + '</div></div></div></div></div>');
      	else
          $ ('body').append ('<div id="taojquery-popin-overlay"></div><div id="taojquery-popin" class="' + o.classe + '"><div id="taojquery-popin-container-box"><div id="taojquery-popin-container"><div id="taojquery-popin-contenu"><div id="taojquery-popin-contenu-message">' + message + '</div></div></div></div></div>');
      
      // Mise en forme de la popin
      $ ('#taojquery-popin-overlay').css (
      {
        backgroundColor: o.overlayBgColor,
        opacity: o.overlayOpacity
      }).fadeIn ();

      // Positionnement de la popin
      $ ('#taojquery-popin').show ();
      
      $.popin.repositionnement ();
      
      // Définition des actions
      $ ('#taojquery-popin-overlay').click (function ()
      {
        _close ();
      });
      
      $ ('#taojquery-popin').click (function (event)
      {
    	if (event.target == event.currentTarget)
    	  _close ();
      });
      
      if (o.withBtnClose)
      {
        $ ('#taojquery-popin-btnClose').click (function ()
        {
          _close ();
          return false;
        });
      }
      
      $ (document).keydown (function (objEvent)
      {
        _keyboard_action (objEvent);
      });
      
      // Redimensionnement des éléments si la page est redimensionnée
      $ (window).resize (function ()
      {
    	$.popin.repositionnement ();
      });
    }
    
    // Fonction privée pour savoir si la popin existe déjà ou pas
    function _exists ()
    {
      return ($ ('#taojquery-popin').size () != 0);
    }
    
    // Fonction privée pour savoir si la popin demandé a la même config que la popin déjà existante
    function _sameConfig ()
    {
      if (!_exists ())
        return false;
      
      withBtnClose = ($ ('#taojquery-popin-btnClose').size () == 0) ? false : true;
      withTitle = ($ ('#taojquery-popin-contenu-title').size () == 0) ? false : true;
      classe = $ ('#taojquery-popin').attr ('class');
      
      
      return ((withBtnClose == o.withBtnClose)
             &&
             (withTitle == (title != ''))
             &&
             (classe == o.classe));
    }
    
    // Fonction privée pour afficher l'image
    function _close ()
    {
      $.popin.close ();
    }
    
    // Actions clavier
    function _keyboard_action (event)
    {
      keycode = event.keyCode;
      escapeKey = 27;
      
      // Touche pour fermer ?
      if (keycode == escapeKey)
        _close ();
    }
  };
  
  // Fonction publique pour le repositionnement de la popin
  $.popin.repositionnement = function ()
  {
    var arrPageSizes = ___getPageSize ();
    
    $ ('#taojquery-popin-overlay').css (
    {
      width: arrPageSizes[0],
      height: arrPageSizes[1]
    });
    
    var arrPageScroll = ___getPageScroll ();
    
    $ ('#taojquery-popin').css (
    {
      top: arrPageScroll[1] + (arrPageSizes[3] / 10),
      left: arrPageScroll[0]
    });
  };
  
  //Fonction publique pour fermer la popin
  $.popin.close = function (message, options)
  {
    $ ('#taojquery-popin').fadeOut ();
    $ ('#taojquery-popin-overlay').fadeOut ();
    $ ('embed, object, select').css ( { 'visibility' : 'visible' } );
  };
  
  // Fonction privée pour récupérer les dimensions de la page et de la fenêtre
  /**
  / THIRD FUNCTION
  * getPageSize() by quirksmode.com
  *
  * @return Array Return an array with page width, height and window width, height
  */
  function ___getPageSize ()
  {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY)
    {  
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
    {
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
    }
    else // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    {
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
    }
    
    var windowWidth, windowHeight;
    if (self.innerHeight) // all except Explorer
    {
      if (document.documentElement.clientWidth)
        windowWidth = document.documentElement.clientWidth; 
      else
        windowWidth = self.innerWidth;
      windowHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
    {
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
    }
    else if (document.body) // other Explorers
    {
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
    }   
    
    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight)
      pageHeight = windowHeight;
    else
      pageHeight = yScroll;
    
    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth)
      pageWidth = xScroll;        
    else
      pageWidth = windowWidth;
    
    arrayPageSize = new Array (pageWidth, pageHeight, windowWidth, windowHeight);
    
    return arrayPageSize;
  };

  // Fonction privée pour récupérer le scroll de la page
  /**
  / THIRD FUNCTION
  * getPageScroll() by quirksmode.com
  *
  * @return Array Return an array with x,y page scroll values.
  */
  function ___getPageScroll()
  {
    var xScroll, yScroll;
    if (self.pageYOffset)
    {
      yScroll = self.pageYOffset;
      xScroll = self.pageXOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
    {
      yScroll = document.documentElement.scrollTop;
      xScroll = document.documentElement.scrollLeft;
    }
    else if (document.body) // all other Explorers
    {
      yScroll = document.body.scrollTop;
      xScroll = document.body.scrollLeft; 
    }
    
    arrayPageScroll = new Array (xScroll, yScroll);
    
    return arrayPageScroll;
  };
  
  function debug ($message)
  {
    if (window.console && window.console.log)
      window.console.log ('taojquery - popin : ' + $message);
  };
  
  $.popin.defaults = {
    imageBtnClose: '/js/jquery/btn-close.gif',
    withBtnClose: true,
    overlayBgColor: "#000000",
    overlayOpacity: 0.8,
    classe: 'NOTICE'
  };
})(jQuery);
