// press
var Press = {
  publisher: null,
  language:null,
  current:null,
  repository:null,

  init: function(publisherID, lang) {
    this.publisher=publisherID;
    this.language=lang;
    this.current=0;
    this.repository=new Array();
    this.zone=document.getElementById("imagezone");
  },

  reset: function() {
    this.current=0;
    this.repository=new Array();
    this.zone=document.getElementById("imagezone");
    Press.removeChildren(document.getElementById("imagezone"));
    document.getElementById("imagezone").width="340";
    document.getElementById("leftarrow").src="images/spacer.gif";
    document.getElementById("rightarrow").src="images/jooi/anchor_r.gif";
  },

  removeChildren: function (c) {
    var kids=c.childNodes;
    for (j=0; j < kids.length; j++) {
      c.removeChild(kids[j]);
    }
  },

  onNext: function () {
    if(this.current+1<this.repository.length)
      this.current+=1;
    else
      return;

    img=this.repository[this.current];
    Press.removeChildren(document.getElementById("imagezone"));
    document.getElementById("imagezone").appendChild(img);
    document.getElementById("imagezone").width=img.width;

    if(this.current+1>=this.repository.length)
      document.getElementById("rightarrow").src="images/spacer.gif";
    else
      document.getElementById("rightarrow").src="images/jooi/anchor_r.gif";

    document.getElementById("leftarrow").src="images/jooi/anchor_l.gif";
  },

  onPrev: function () {
    if(this.current<1)
      this.current=0;
    else
      this.current-=1;

    img=this.repository[this.current];
    Press.removeChildren(document.getElementById("imagezone"));
    document.getElementById("imagezone").appendChild(img);
    document.getElementById("imagezone").width=img.width;

    if(this.current<1)
      document.getElementById("leftarrow").src="images/spacer.gif";
    else
      document.getElementById("leftarrow").src="images/jooi/anchor_l.gif";

    document.getElementById("rightarrow").src="images/jooi/anchor_r.gif";
  },

  pickup: function(publisherID) {
    this.publisher=publisherID;
    Press.reset();
    Press.loadImage();
  },

  loadImage: function() {

    new Ajax.Request(
      'press.php', {
        method: 'post',
        postBody:  'lang='+Press.language+'&publisher='+Press.publisher,
        onSuccess: function(t) {
          var images=new Array();
          if(t.responseText.length>0) {
            Press.reset();
            repos=t.responseText.split(",");
            for(i=0; i<repos.length; i++) {
              pressImg = new Image();
              pressImg.src="images/Press/"+repos[i];
              images[i]=pressImg;
            }

            Press.repository=images;
            document.getElementById("imagezone").appendChild(images[0]);
            document.getElementById("imagezone").width=images[0].width;
            document.getElementById("leftarrow").src="images/spacer.gif";
            document.getElementById("rightarrow").src="images/jooi/anchor_r.gif";
          }
          else {
            Press.reset();
          }
        },
        onLoading: function(t) {
          Press.reset();
          loadingImage = new Image();
          loadingImage.src = "images/jooi/loading.gif";
          loadingImage.width=16;
          loadingImage.height=16;
          document.getElementById("imagezone").appendChild(loadingImage);
        },
        onLoaded: function(t) {
          Press.reset();
        }
      }
    );
  }
}
