// 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.Open=false; this.WidthMax=false; this.HeightMax=false; this.isImg=false; }
myPop.prototype.init = function(url,width,height){

  this.Open=true;
  var urlMin = url.toLowerCase();
  if( urlMin.indexOf('.jpg')>0 || urlMin.indexOf('.jpeg')>0 || urlMin.indexOf('.gif')>0 || urlMin.indexOf('.png')>0 ){
    this.isImg=true;
    $("body").append('<div id="PopBack">&nbsp;</div><div id="PopContent"><div style="position:relative"><img src="" onload="RCBox.loadImg()" style="display:none" alt="" /><div id="PopClose" style="display:none">&nbsp;</div></div></div>');
  }else{
    $("body").append('<div id="PopBack">&nbsp;</div><div id="PopContent"><div style="position:relative"><iframe frameborder="0" border="0" allowTransparency="true"></iframe><div id="PopClose">&nbsp;</div></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); });

  $("#PopBack, #PopContent").show();
  this.setPosition();
  

}

myPop.prototype.loadImg = function(){
  $("#PopContent > div > img, #PopClose").show();
  this.setPosition();
};


myPop.prototype.setPosition = function(scroll){

  if(this.Open){

    if( this.isImg ){
      $("#PopContent > div > img").removeAttr('width').removeAttr('height');
      this.Width = $("#PopContent > div > img").width();
      this.Height = $("#PopContent > div > img").height();
    }

    if( this.Height<=100 || this.Width<=100 ){
      var t = this; setTimeout(function(){ t.setPosition(); },100);
      return;
    }
  
    if(!scroll){
      $("#PopBack").css({width:0,height:0});
      if( this.isImg ) $("#PopContent > div > img").removeAttr('width').removeAttr('height');
      else $("#PopContent > div > iframe").removeAttr('width').removeAttr('height');
    }

    if(this.Width=='max'){ this.Width = $(window).width()-40; this.WidthMax=true; } //taille Bloc bottom 40
    if(this.Height=='max'){ this.Height = this.getInnerHeight()-40; this.HeightMax=true; }//taille Bloc bottom 50
    

    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.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.6'});
    this.ContentCss = ({position:'absolute','z-index':'100000',top:this.FrameTop+'px',left:this.FrameLeft+'px', background:'#fff'});
    this.ImgCss = ({border:0,'z-index':'100000',margin:'0px'});
    this.IframeCss = ({border:0,'z-index':'100000',background:'transparent',margin:'0px'});
    this.CloseCss = ({'position':'absolute','top':0,'right':0,'text-align':'right','z-index':'100000','font-weight':'bold',padding:'0px','background':'#000'});
    this.CloseHTML = '<a href="javascript:;" id="PopCloseLink" title="Fermer la fenêtre" style="color:#fff;">Fermer la fenêtre</a>';

    var t = this;
    $("#PopBack").css(this.BackCss).click(function(){ t.Close(); });
    $("#PopContent").css(this.ContentCss);
    if( this.isImg ) $("#PopContent > div > img").css(this.ImgCss);
    else $("#PopContent > div > iframe").css(this.IframeCss);
    $("#PopClose").css(this.CloseCss).html(this.CloseHTML);
    $("#PopCloseLink").click(function(){ t.Close(); });
    if( this.isImg ) $("#PopContent > div > img").attr('width',this.Width).attr('height',this.Height);
    else $("#PopContent > div > 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").remove();//.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; 
  if(this.isImg) $("#PopContent > div > img").attr('src',url);
  else $("#PopContent > div > 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();
}

