var larg;
var haut;
var pic;
var picwidth;
var picheight;
var w;
var h;
var toppage;
var comm=0;
var previd;
var nextid;
var pclosed=true;
var showslide=false;
var sltimer;
var sllink;
var slplay="<a href='javascript:slideshow()' class='cbnred10'>Play</a>";
var slstop="<a href='javascript:stopslide()' class='cbnred10'>Stop</a>"


function showpid(pid){

   aindex=findindex(pid);
   previd=false;
   nextid=false;
   showslide=false;
   if(sllink=="" || sllink==undefined) sllink=slplay;

   if(aindex>0) previd=picids[aindex-1];
   if(aindex<picids.length-1) nextid=picids[aindex+1];
   showpic("http://www.clubbingbynight.com/image.php?id="+pid+"&s="+picids2[aindex]+"&width="+picmaxwidth+"&prop=1");
   comm=pid
}

function showpic(picture){
    pic=picture;
    GetScrollPage();

    if(document.all){
       larg=screen.availWidth;
       haut=screen.availHeight;
       thaut=haut+document.documentElement.scrollHeight;
   }else{
       larg = screen.width;
       haut = screen.height;
       thaut=document.getElementsByTagName('html')[0].offsetHeight;
   }

    if(document.getElementById("backpic")==null){
        bpic=document.createElement('div');
        bpic.id="backpic";
        bpic.style.position="absolute";
        bpic.style.top="0px";
        bpic.style.left="0px";
        if(document.all){
            bpic.filter="alpha(opacity=0)";
            //bpic.style.opacity=0;
        }else
            bpic.style.setProperty("-moz-opacity", 0, "");
        document.body.appendChild(bpic);
    }else
        bpic=document.getElementById("backpic");

    bpic.style.zIndex="9";
    bpic.style.top=toppage+'px';
    bpic.style.visibility='visible';
    bpic.style.width = larg+"px";
    bpic.style.height = thaut+"px";
    bpic.style.backgroundColor = "black";

    incback=0;
    if(pclosed) fadeback("backpic");

    if(document.getElementById("dpic")==null){
        dpic=document.createElement('div');
        dpic.id="dpic";
        dpic.style.position="absolute";
        dpic.style.top="0px";
        dpic.style.left="0px";
        dpic.style.zIndex="10";
        document.body.appendChild(dpic);
    }

    document.getElementById("dpic").style.top=toppage+'px';
    document.getElementById("dpic").style.visibility='visible';
    document.getElementById("dpic").style.width = larg+"px";
    document.getElementById("dpic").style.height = haut+"px";


    htmltable="<div id='wait' align='center' style='position:absolute;z-index:10;'><img src='images/circle_animation.gif'></div>";
    htmltable+="<table width='100%' height='100%'>";
    htmltable+="<tr><td colspan='2' align='center' valign='middle' id='td'><div id='centerpic' align='center' style='padding-top:10px;z-index:11;'></div></td></tr>";
    htmltable+="</table>";
    if(pclosed) document.getElementById("dpic").innerHTML =htmltable;
    document.getElementById("centerpic").innerHTML="";
    document.getElementById('wait').style.left=parseInt(larg/2-45)+"px";
    document.getElementById('wait').style.top=parseInt(haut/2-45)+"px";
    document.getElementById('wait').style.visibility='visible';
    document.body.onkeydown=pkey;

    fsimgPreloader = new Image();
    fsimgPreloader.src=picture;
    fsimgPreloader.onload = function(){
      fsimgPreloader.onload = null;
      picwidth=fsimgPreloader.width;
      if(picwidth<760) picwidth=760;
      picheight=fsimgPreloader.height;
      picheight+=80;
      document.getElementById('centerpic').style.backgroundColor='white';
      document.getElementById('wait').style.visibility='hidden';
      if(pclosed){
          document.getElementById('centerpic').style.height='50px';
          w=0;
          h=50;

          resizecenterw();
      }else{
          //display();
          resizecenterw();
      }

      pclosed=false;
    }

}

function resizecenterw(){
    document.getElementById('centerpic').style.width=w+'px';

    if(w>=picwidth){
        document.getElementById('centerpic').style.width=(picwidth+10)+'px';
        resizecenterh()
        return 0;
    }
    w+=100;
    setTimeout("resizecenterw()",50);
}

function resizecenterh(){
    document.getElementById('centerpic').style.height=h+'px';

    if(h>=picheight){
        document.getElementById('centerpic').style.height=picheight+'px';
        display();
        return 0;
    }
    h+=100;
    setTimeout("resizecenterh()",50);
}

function display(){
         if(showslide){
             document.getElementById('centerpic').innerHTML="<div id='pslide'>"+sllink+"</div>";
         }

         document.getElementById("centerpic").innerHTML+="<a href='javascript:closefpic()'><img src='"+pic+"' id='bpic' style='filter:alpha(opacity=0); -moz-opacity:0;' border='0'></a>";

         //if(comm!=0) Requestcomm();
         inc=0;
         fade('bpic');
         picleft=(larg-picwidth)/2;
         if(previd){
             document.getElementById("centerpic").innerHTML+="<div id='prev' style='position:absolute;background-image:url(images/rect-precsuiv.png);left:0;top:100;width:73;height:20;z-index:11;filter:alpha(opacity=60);-moz-opacity:0.6;' onmouseover=\"this.style.fontWeight=800;\" onmouseout=\"this.style.fontWeight=400;\"><a href='javascript:showpid("+previd+")' class='white12'>Précédent</a></div>";
             document.getElementById("prev").style.left=picleft+"px";
         }
         if(nextid){
             document.getElementById("centerpic").innerHTML+="<div id='next' style='position:absolute;background-image:url(images/rect-precsuiv.png);left:700;top:100;width:73;height:20;z-index:11;filter:alpha(opacity=60);-moz-opacity:0.6;' onmouseover=\"this.style.fontWeight=800;\" onmouseout=\"this.style.fontWeight=400;\"><a href='javascript:showpid("+nextid+")' class='white12'>Suivant</a></div>";
             document.getElementById("next").style.left=(picleft+picwidth-73)+"px";
         }
}

function fade(div){
         if(document.getElementById(div)==null) return 0;
         if(document.all){
             if ( window.opera )
                document.getElementById(div).style.opacity = inc*0.01;
             else
                 document.getElementById(div).filters.alpha.opacity=inc;

             inc+=5;
             if ( inc<= 100 ){
                timer=setTimeout("fade('"+div+"')", 100);
                return 0;
             }
        }else{
            document.getElementById(div).style.setProperty("-moz-opacity", inc, "");
            inc+=0.1;
            if ( inc<= 1 ){
               timer=setTimeout("fade('"+div+"')", 100);
               return 0;
            }
        }
}


function closefpic(){
         if(showslide) stopslide();
         document.getElementById("backpic").style.visibility='hidden';
    document.getElementById("centerpic").innerHTML='';
    fadeout();
}

function fadeout(){
    document.getElementById('centerpic').style.width=w+'px';
    document.getElementById('centerpic').style.height=h+'px';
    h-=200;
    w-=200;
    if(h<=0 || w<=0){
        document.getElementById("dpic").style.visibility='hidden';
        pclosed=true;
        return 0;
    }
    setTimeout("fadeout()",50);
}     

function GetScrollPage(){
  var DocRef;

  if( window.innerWidth){
    toppage=window.pageYOffset;

  }
  else{ // Cas Explorer a part
    if( document.documentElement && document.documentElement.clientWidth)
      DocRef = document.documentElement;
    else
      DocRef = document.body;
      toppage=DocRef.scrollTop;

  }
}

function fadeback(div){
         if(document.all){
        if ( window.opera )
            document.getElementById(div).style.opacity = incback*0.01;
        else
            document.getElementById(div).style.filter="alpha(opacity="+incback+")";
            //document.getElementById(div).style.opacity=incback;
        incback+=5;
        if ( incback<= 80 ){
            timer=setTimeout("fadeback('"+div+"')", 100);
            return 0;
        }
    }else{
        document.getElementById(div).style.setProperty("-moz-opacity", incback, "");
        incback+=0.1;
        if ( incback<= 0.8 ){
            timer=setTimeout("fadeback('"+div+"')", 100);
            return 0;
        }
    }
}

function slideshow(){
   nextid=aindex+1;
   if(nextid==picids.length) nextid=0;
   sllink=slstop;
   document.getElementById('pslide').innerHTML=slstop;
   showpid(picids[nextid]);
   sltimer=setTimeout("slideshow()",5000);
}

function stopslide(){
         sllink=slplay;
         document.getElementById('pslide').innerHTML=slplay;
         clearTimeout(sltimer);
}

function Requestcomm() {

        var httpRequest = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // Voir la note ci-dessous à propos de cette ligne
            }
        }
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {}
            }
        }

        if (!httpRequest) {
            return false;
        }

        httpRequest.onreadystatechange = function() { responsecomm(httpRequest); };
        httpRequest.open('GET', 'http://www.clubbingbynight.com/js/commentaires.php?id='+comm, true);
        httpRequest.send(null);

    }

    function responsecomm(httpRequest) {

        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
               document.getElementById('centerpic').innerHTML+=httpRequest.responseText;
            }
        }

    }

    function findindex(value){
             for(i=0;i<picids.length;i++){
                 if(picids[i]==value) return i;
             }
             return 0;
    }
    
    function pkey(e){
      var evtobj=window.event? event : e //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
      //var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode
      var unicode=evtobj.keyCode;
      var actualkey=String.fromCharCode(unicode)
      if(previd && (unicode==40 || unicode==37)) showpid(previd);
      if(nextid && (unicode==38 || unicode==39)) showpid(nextid);
      if(unicode==27) closefpic();

     //alert(unicode);
    }