// PopBox - Popup interne
// Developpement Paul Fauchille
// Square Partners - Roussillon Connexion
// 2008-10-08 - 15:06
// Compatible Firefox 2/3, IE 6/7, Safari 3, Opera, Chrome

function myPop(){ this.Scroll=new Array(); this.isInit=false; this.Open=false; this.WidthMax=false; this.HeightMax=false; }
myPop.prototype.init = function(url,width,height){

  this.Open=true;
  if(!this.isInit){
    $("body").append('<div id="PopBack">&nbsp;</div><div id="PopContent"><iframe frameborder="0" border="0" allowTransparency="true"></iframe><div id="PopClose">&nbsp;</div></div>');

    if( jQuery.browser.msie ){
      this.Scroll[0]=$("html").css('overflow'); 
	  this.Scroll[1]=$("html").css('overflow-x'); 
	  this.Scroll[2]=$("html").css('overflow-y');
      this.Scroll[3]=$("body").css('overflow'); 
	  this.Scroll[4]=$("body").css('overflow-x'); 
	  this.Scroll[5]=$("body").css('overflow-y');
    }
	
	
    this.setURL(url);
    this.setTaille(width,height);

    var t = this;
    $(window).resize(function(){ t.setPosition(); });
    $(window).scroll(function(){ t.setPosition(true); });
    this.isInit=true;
  }
  $("#PopBack, #PopContent").show();
  this.setPosition();
  

}

myPop.prototype.setPosition = function(scroll){

  if(this.Open){

    if(!scroll){
      $("#PopBack").css({width:0,height:0});
      $("#PopContent > iframe").attr('width','0').attr('height','0');
    }

    var CloseHeight = $("#PopClose").height();
    
    if(this.Width=='max'){ this.Width = $(window).width()-40; this.WidthMax=true; }
    if(this.Height=='max'){ this.Height = this.getInnerHeight()-50-CloseHeight; this.HeightMax=true; }

    if( jQuery.browser.msie ) $("html,body").css({'overflow':'hidden','overflow-x':'hidden','overflow-y':'hidden'});

    this.DocWidth = $(document).width(); this.DocHeight = this.getInnerHeight();
    this.BackHeight = $(document).height();
    this.FrameLeft = (this.DocWidth-this.Width)/2; this.FrameTop = (this.DocHeight-this.Height)/2;

    this.FrameLeft -=10;
    this.FrameTop -= CloseHeight;

    this.ScrollTop = this.getScrollHeight(); this.FrameTop += this.ScrollTop;


    // CONFIG CSS + CLOSE
    this.BackCss = ({position:'absolute','z-index':'100000',top:0,left:'0',width:this.DocWidth,height:this.BackHeight,background:'#000',opacity:'0.5'});
    this.ContentCss = ({position:'absolute','z-index':'100000',top:this.FrameTop+'px',left:this.FrameLeft+'px',background:'#fff'});
    this.IframeCss = ({border:0,'z-index':'100000',background:'transparent',margin:'10px'});
    this.CloseCss = ({'text-align':'right','z-index':'100000','font-weight':'bold',padding:'5px'});
    this.CloseHTML = '<a href="javascript:;" id="PopCloseLink" title="Fermer" style="color:#000;">Fermer</a>';

    var t = this;
    $("#PopBack").css(this.BackCss).click(function(){ t.Close(); });
    $("#PopContent").css(this.ContentCss);
    $("#PopContent > iframe").css(this.IframeCss);
    $("#PopClose").css(this.CloseCss).html(this.CloseHTML);
    $("#PopCloseLink").click(function(){ t.Close(); });
    $("#PopContent > iframe").attr('width',this.Width).attr('height',this.Height);
    
    if(this.WidthMax) this.Width='max';
    if(this.HeightMax) this.Height='max';

  }

}

myPop.prototype.Close = function(){

  this.Open=false;
  $("#PopBack, #PopContent").hide();
  if( jQuery.browser.msie ){
    $("html").css('overflow',this.Scroll[0]);
    $("html").css('overflow-x',this.Scroll[1]);
    $("html").css('overflow-y',this.Scroll[2]);
    $("body").css('overflow',this.Scroll[3]);
    $("body").css('overflow-x',this.Scroll[4]);
    $("body").css('overflow-y',this.Scroll[5]);
	$("#PopContent > iframe").attr('src','about:blank');
  }

}

myPop.prototype.getScrollHeight = function(){
   var h = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
   return h ? h : 0;
}

myPop.prototype.getInnerHeight = function(){
  var h = window.innerHeight || document.body.clientHeight;
  return h ? h : 0;
}

myPop.prototype.setURL = function(url){
  this.URL = url;
  $("#PopContent > iframe").attr('src',url);
};

myPop.prototype.setTaille = function(width,height){
  // Si width == max ou height == max, 95% de la largeur et hauteur
  if(width!=''&&width!=null) this.Width = width;
  if(height!=''&&height!=null) this.Height = height;
  this.setPosition();
}
