var libnextpub="";
var delaiRotation=10000;

function chPub() {
  if($("#z1 div").length>1) animY('z1', 201);
  if(document.getElementById("z2")) {
    if($("#z2 div").length>1) animY('z2', 100, 10);
  } else {
    if($("#z21 div").length>1) animY('z21', 48, 5);
    if($("#z22 div").length>1) animY('z22', 48, 5);
    if($("#z23 div").length>1) animY('z23', 48, 5);
    if($("#z24 div").length>1) animY('z24', 48, 5);
    if($("#z25 div").length>1) animY('z25', 48, 5);
    if($("#z26 div").length>1) animY('z26', 48, 5);
  }
  setTimeout("chPub();", delaiRotation);
}
function animY(id, to, step) {
  if(!step) step=20;
  var timer=50;
  var nt=$("#"+id).scrollTop()+step;
  if(nt>to) nt=to;
  $("#"+id).scrollTop(nt);
  if(nt<to) setTimeout("animY('"+id+"',"+to+", "+step+");", timer);
  else {
    if(id=="z1") $("#z1txt").html(libnextpub);
    $("#"+id+" div:first").appendTo("#"+id);
    $("#"+id).scrollTop(0);
    newPub(id);
  }
}

function newPub(id) {
  if(id=="z1") {
    $("#z1 div:last").html(getPub(1));
  } else {
    $("#"+id+" div:last").html(getPub(2));
  }
}
var seqidx1=0;
var seqidx2=0;

function initPub() {
  var nb1=(jsonpub["A"]?jsonpub["A"].length:0)+(jsonpub["B"]?Math.ceil(jsonpub["B"].length/3.0):0)+(jsonpub["C"]?Math.ceil(jsonpub["C"].length/3.0):0)+(jsonpub["D"]?jsonpub["D"].length:0);
  if(nb1!=0) {
    $("#z1").html("<div>"+getPub(1)+"</div>");
    $("#z1txt").html(libnextpub);
  }
  if(nb1>1) {
    //plusieurs zones à gérer... 2eme div
    $("<div></div>").appendTo("#z1");
    newPub("z1");
  }
  if($("#z2").length>0) {
    $("#z2").html("<div>"+getPub(2)+"</div>");
    if(jsonpub["P"].length>1) {
      $("<div></div>").appendTo("#z2");
      newPub("z2");
    }
  } else {
    $("#z21").html("<div>"+getPub(2)+"</div><div></div>");
    $("#z22").html("<div>"+getPub(2)+"</div><div></div>");
    $("#z23").html("<div>"+getPub(2)+"</div><div></div>");
    $("#z24").html("<div>"+getPub(2)+"</div><div></div>");
    if($("#z25").length>0) {
      $("#z25").html("<div>"+getPub(2)+"</div><div></div>");
      $("#z26").html("<div>"+getPub(2)+"</div><div></div>");
      newPub("z25");
      newPub("z26");
    }
    newPub("z21");
    newPub("z22");
    newPub("z23");
    newPub("z24");
  }
  setTimeout('chPub();', delaiRotation);
}
var chosenPubZ2=[];
function getPub(zone) {
  if(zone==1) {
    if(jsonpub["seq1"]=="") {
      jsonpub["seq1"]+=jsonpub["A"]?"A":"";
      jsonpub["seq1"]+=jsonpub["B"]?"B":"";
      jsonpub["seq1"]+=jsonpub["D"]?"D":"";
      jsonpub["seq1"]+=jsonpub["A"]?"A":"";
      jsonpub["seq1"]+=jsonpub["C"]?"C":"";
      jsonpub["seq1"]+=jsonpub["A"]?"A":"";
      jsonpub["seq1"]+=jsonpub["B"]?"B":"";
      jsonpub["seq1"]+=jsonpub["D"]?"D":"";
    }
    var h="";
    if(jsonpub["seq1"] && jsonpub["seq1"]!="") {
      var niv=jsonpub["seq1"].charAt(seqidx1);
      seqidx1=(seqidx1+1)%(jsonpub["seq1"].length);
      var idx=Math.floor(Math.random()*jsonpub[niv].length);
      switch(niv) {
        case 'A':
        case 'D':
        h=getHtmlImg(jsonpub[niv][idx], "z1"+niv);
        break;
        case 'B':
        case 'C':
        var tmp=jsonpub[niv].slice(0, jsonpub[niv].length);
        for(var i=0 ; i<3 ; i++) {
          if(tmp.length>0) {
            h+=getHtmlImg(tmp[idx], "z1"+niv);
            if(i!=2) {
              var exc=tmp[idx].excl;
              tmp.splice(idx,1);
              if(exc.length>0) {
                for(var k=exc.length-1 ; k>=0 ; k--) {
                  for(var j=tmp.length-1 ; j>=0 ; j--) {
                    if(tmp[j].id==exc[k]) tmp.splice(j,1);
                  }
                }
              }
              idx=Math.floor(Math.random()*tmp.length);
            }
          } else {
            h+="<img src=\"/images/pix.gif\" class=\"z1"+niv+"\" />";
          }
        }
        break;
      }
      if(niv=="D") libnextpub=jsonpub[niv][idx].txt;
      else libnextpub=libpub[niv];
    }
    return h;
  } else { //zone 2
    var h="";
    if(!jsonpub["seq2"] && jsonpub["seq2"]=="") {
      if(jsonpub["P"]) jsonpub["seq2"]="P";
      else {
        jsonpub["seq2"]+=jsonpub["M"]?"MMMM":"";
        jsonpub["seq2"]+=jsonpub["N"]?"NNNN":"";
        jsonpub["seq2"]+=jsonpub["O"]?"OOOO":"";
      }
    }
    if(jsonpub["seq2"] && jsonpub["seq2"]!="") {
      var niv=jsonpub["seq2"].charAt(seqidx2);
      var tmp=jsonpub[niv].slice(0, jsonpub[niv].length);
      for(var k=chosenPubZ2.length-1 ; k>=0 ; k--) { //Epuration des pubs déjà prêtes...
        for(var j=tmp.length-1 ; j>=0 ; j--) {
          if(tmp[j].id==chosenPubZ2[k]) tmp.splice(j,1);
        }
      }
      
      var idx=Math.floor(Math.random()*tmp.length);
      switch(niv) {
        case 'M':
        case 'P':
        if(tmp.length>0) {
          h=getHtmlImg(tmp[idx], "z2"+niv);
        } else {
          h+="<img src=\"/images/pix.gif\" class=\"z2"+niv+"\" border=\"1\" />";
        }
        break;
        case 'N':
        case 'O':
        var or=tmp.length>0?tmp[idx].orientation:'V';
        for(var i=0 ; i<2 ; i++) {
          if(tmp.length>0) {
            h+=getHtmlImg(tmp[idx], "z2"+niv+or);
            if(i!=1) {
              chosenPubZ2[chosenPubZ2.length]=tmp[idx].id;
              var exc=tmp[idx].excl;
              tmp.splice(idx,1);
              for(var j=tmp.length-1 ; j>=0 ; j--) {
                if(tmp[j].orientation!=or) tmp.splice(j,1);
                else if(exc.length>0) {
                  for(var k=exc.length-1 ; k>=0 ; k--) {
                    if(tmp[j].id==exc[k]) tmp.splice(j,1);
                    chosenPubZ2[chosenPubZ2.length]=exc[k];
                  }
                }
              }
              idx=Math.floor(Math.random()*tmp.length);
            }
          } else {
            h+="<img src=\"/images/pix.gif\" class=\"z2"+niv+or+"\" />";
          }
        }
        break;
      }
      seqidx2=(seqidx2+1)%(jsonpub["seq2"].length);
      if(seqidx2%($("#z25").length>0?6:4)==0) chosenPubZ2=[];
      else if(tmp.length>0) chosenPubZ2[chosenPubZ2.length]=tmp[idx].id;
    }
    return h;
  }
}
function getHtmlImg(o, cl) {
  var h="";
  switch(o.typeLien) {
    case 'I':
      //h+="<a href=\"page.php?page="+o.lien+"\" >";
      h+="<a href=\"/page/"+o.lien+"/"+o.nom+".html\" >";
      break;
    case 'PI':
      h+="<a href=\"javascript:popIn("+o.lien+")\" >";
      break;
    case 'E':
      h+="<a href=\""+o.lien+"\" target=\"_blank\" >";
      break;
    case 'B':
      h+="<a href=\"javascript:afficheProd("+o.lien+")\" >";
      break;
    
  }
  h+="<img ";
  h+="src=\"/"+o.img+"\" class=\""+cl+"\"";
  h+="/>";
  if(o.typeLien!="") h+="</a>";
  return h;
}

/**
 * Gestion des popin... 
 */
$(function() {
  $("#popIn").dialog(
  {
      closeOnEscape: true,
      height:500,
      width:700,
      modal:true,
      autoOpen:false,
      resizable:true
  });
  $("#roll").mouseleave(function(e) { $(this).css("display", "none"); });
});

function popIn(id){
  var content=eval('('+HttpReq("/content.php","mode=getContent&id="+id+"&langue=_"+lg)+')');
  showPopIn(content.title,content.content);
}
function popInUrl(url,data,title){
  var content=HttpReq(url,data);
  showPopIn(title,content);
}
function afficheProd(id) {
  var content=eval('('+HttpReq("/boutique_details.php","id="+id)+')');
  showPopIn(content.title,content.content);
}
function showPopIn(title,content) {
  $("#popIn").html(content+"<img style='position:absolute;left:0;bottom:0' src='images/popIn_bas.jpg' >");
  $("#popIn").dialog("option", "title", title);
  $("#popIn").dialog("open");
}

function popImg(img){
  var content="<div>Loading image...</div><img src='"+img+"' onload='resize(this)' /><a href='#' onclick=\"$('#popImg').css('display', 'none');\"></a>";
  $("#popImg").html(content);
  $("#popImg").width(200);
  $("#popImg").height(30);
  centerDiv($("#popImg"));
  $("#popImg").css("display", "block");
}

function rollOver(e,c){
  $("#roll").css("height", "auto");
  $("#roll").css("width", "auto");
	$("#roll").html(c);
  $("#roll").css("height", $("#roll").height());
  $("#roll").css("width", $("#roll").width());
  $("#roll").css("display", "block");
  $("#roll").css("top", (e.pageY?e.pageY:(e.clientY+document.documentElement.scrollTop))+10);
  $("#roll").css("left", (e.pageX?e.pageX:(e.clientX+document.documentElement.scrollLeft))+10);
}
function centerDiv(o) {
  o.css("left", ($(window).width()-o.outerWidth())/2);
  o.css("top", ($(window).height()-o.outerHeight())/2);
}

function resize(img){
  var cw=$(window).width()-20;
  var ch=$(window).height()-20;
  if (img.width<cw && img.height<ch) {
    $("#popImg").width(img.width);
    $("#popImg").height(img.height);
  } else{
    var ratio=img.width/img.height;
    var ratioW=(cw+20)/img.width;
    var ratioH=(ch+20)/img.height;
    
    if (ratioW<ratioH){
      $("#popImg").width(cw);
      $("#popImg").height(cw/ratio);
      $(img).width("100%");
    } else{
      $("#popImg").width(ch*ratio);
      $("#popImg").height(ch);
      $(img).height("100%");
    }
  }
  if($("#popImg").html().substring(0,4).toLowerCase()!="<img") $("#popImg").html($("#popImg").html().substring(27));
  centerDiv($("#popImg"));
}
