var state=0;
var next=0;
var oldWidth=0;
var step=1;
var stepfac=1.3;
var origWidth=0, origHeight=0;
var img, img4, nextimg, linkimg;
var nextslide;
var slideA, slideB;
var menu, xtbl, msie;
var lastdir=2;
var col, maxcol, aktuell;
var site='pip';

function clip() {
  var tbl = document.getElementById('tbl');
  if (!tbl)
    return;

  var left = tbl.style.left ? parseInt(tbl.style.left) : 1;
  var contHeight = 300;
  var contWidth = 600;

  switch (state) {
    case -2:  // einmal nach links
      if (left<=-contWidth+2) {
        state=0;
        slideA.innerHTML = slideB.innerHTML;
        tbl.style.left=1;
      } else {
        if (contWidth+left<14 && step>1)
          step/=2;
        tbl.style.left = (left - step) + "px";
        if (contWidth+left>14 && step<8)
          step*=2;
      }
      break;

    case -1:  // endlos nach links
      tbl.style.left = ((left == -contWidth) ? 0 : (left - 1)) + "px";
      break;

    case 1:   // endlos nach rechts
      tbl.style.left = ((left == 0) ? (-contWidth) : (left + 1)) + "px";
      break;

    case 2:   // einmal nach rechts
      if (left>=1) {
        state=0;
        tbl.style.left=1;
      } else {
        if (left>-14 && step>1)
          step/=2;
        tbl.style.left = (left + step) + "px";
        if ( left < -14 && step<8)
          step*=2;
      }
      break;

    case 12: // Bild vergrössern
      if (img.height==contHeight)
        state=22;
      else {
        if (!origWidth) {
          origWidth=img.width;
          origHeight=img.height;
          step=1;
        }
        if (contHeight-img.height<30 && step>1)
          step/=2;
        var w=slideA.clientWidth;
        img.width=origWidth*(img.height+=2*step)/origHeight;
        if (parseInt(img.id)>2)
          tbl.style.left=left-(slideA.clientWidth-w)+"px";
        if (contHeight-img.height>30 && step<5)
          step*=2;

      }
      break;

    case 22:
      if (next) {
        state = 32;
        step = 1;
      }
      break;

    case 32:  // Bild verkleinern
      if (img.height==origHeight)
        state=0;
      else {
        if (img.height-origHeight<30 && step>1)
          step/=2;
        var w=slideA.clientWidth;
        img.width=origWidth*(img.height-=2*step)/origHeight;
        if (parseInt(img.id)>2)
          tbl.style.left=left-(slideA.clientWidth-w)+"px";
        if (img.height-origHeight>30 && step<5)
          step*=2;

      }
      break;

    default:
      switch (next) {
        case -2:
          state=next;
          loadSlide( slideB, nextslide, line );
          next=0;
          break;

        case 0:
          tbl.style.left = left+"px";
          state = 0;
          break;

        case 2:
          slideB.innerHTML = slideA.innerHTML;
          tbl.style.left = -contWidth +"px";
          loadSlide( slideA, nextslide, line);
          state=next;
          next=0;
          break;

        case 3:
          next=lastdir*=-1;
          break;

        case 12:
          img=nextimg;
          state=next;
          next=0;
          break;

        default:
          if (next>=100 && next<120 && next!=menu) {
            document.getElementById( "m"+next ).style.color=_style[site].color;
            if (menu)
              document.getElementById( "m"+menu ).style.color="black";

            // lösche Seitenzähler der Kollektionen
            for (var x=110; x<120; x++) {
              var el=document.getElementById('p'+x);
              if (el)
                el.innerHTML = '';
            }

            // hole Informationen über die nächste Seite
            menu=next;
            var n=_next['m'+next] || _next[site+next];
            if (n) {
              if (n.nextslide=='c') {
                next = aktuell;
                clip();
                return;
              }
              nextslide = n.nextslide;
              next      = n.next;
              col       = n.col;
              maxcol    = n.maxcol;
              line      = n.line;
            }
          }
        }
  }
  document.getElementById('slideCont').style.clip = 'rect(auto,'+ contWidth +'px,' + contHeight +'px,auto)';
  document.getElementById('collection').style.left=menu>108 ? document.getElementById('m109').offsetLeft + xtbl.offsetLeft+2 : -500;
  var dx=xtbl.offsetLeft % 121+(msie ? -43 : 21);
  var dy=(msie ? -44 : 27);
  document.body.style.backgroundPosition=dx + "px " + dy + "px";
  setTimeout(clip,15);
}

function large( nr ) {
  if (state==22)
    next=32;
  else {
    nextimg=document.getElementById( nr );
    next=12;
  }
}

function preview( s ) {
  var td=document.getElementById( "preview");
  td.innerHTML = '<img src="'+s+'" />';
}

function shift( step ) {
  if (step==-1) {
    if (col==1)
      col=maxcol;
    else
      col--;
  } else if (col==maxcol)
    col=1;
  else
    col++;
  next=-2*step;
}

function loadSlide( slide, prefix, line ) {
  var html="<center><table><tr>";
  if (prefix == "news" || prefix=="contact" || prefix=="about" || prefix=="links" ) {
    var sl = document.getElementById( prefix );
    html += '<td class="content">' +(sl ? sl.innerHTML : "" ) + "</td>";
/*  } else if (prefix == '') {
    html += '<td class="content">'+
            '<iframe src="http://www.nelou.com/banner/banner.php?code=5TI2Z6X8Z5" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:485px; height:90px;" allowTransparency="false"></iframe>'+
            '</td>';
*/
  } else {
    var nr=0;
    while (++nr < 5) {
      html += '<td class="images">' +
              '  <a href="javascript:large(' + nr + ')">' +
              '     <img id="' + nr + '" src="images/' + (menu>109 || prefix=='' ? site+'/' : '')+prefix + (menu>=109 ? col :"") + nr + '.jpg" width="142" height="142">' +
              '  </a>' +
              '</td>';
    }
    if (line) {
      document.getElementById( 'p'+menu ).innerHTML='('+col+' von '+maxcol+')';
      line = _line[line];
    }
  }
  slide.innerHTML = html + "</tr></table>";
  document.getElementById( "textline" ).innerHTML=line ? line : '';
  document.getElementById( "left" ).style.display=(!line || maxcol==1 ? "none" : "inline");
  document.getElementById( "right" ).style.display=(!line || maxcol==1 ? "none" : "inline");
}

window.onload = function() {
  // pip oder fip?
  var request=window.location.search.substr( 1 );
  init(request);
};

function init(request) {
  var fipLink = document.getElementById('m106');
  if (request=='frog') {
    site='fip';
    fipLink.innerHTML = 'pip*';
    fipLink.href = 'index.htm';
    document.title = 'frog in prog';
  }

  // setze Seiten-Stil
  document.body.background = _style[site].background;
  document.getElementById('logo').src = _style[site].logo;

  // erstelle die Kollektionen
  var names = document.getElementById('collection-names');
  var html = '<a href="http://www.facebook.com/princessinprogress?sk=photos" target="_blank">archiv</a>';
  for (var m in _next) {
    if (m.substr(0,3)!=site)
      continue;
    var n = _next[m];
    if (!n.line)
      continue;
    aktuell = m.substr(3);
    html += "<br><a id='m"+m.substr(3)+"' href='javascript:void( next="+m.substr(3)+");'>";
    html += n.nextslide[0]=='f' ? 'frühling/sommer' : 'herbst/winter';
    html += ' '+n.nextslide.substr(2);
    html += '&nbsp;<span id="p'+m.substr(3)+'"></span></a>';
  }
  names.innerHTML = html;

  // Vorbereitungen
  msie = (navigator.appName!="Netscape");
  slideA = document.getElementById("slideA");
  slideB = document.getElementById("slideB");
  xtbl   = document.getElementById("xtbl");
  document.getElementById("slides").style.display="none";
  document.getElementById("m100").style.color=_style[site].color;
  document.getElementById( "left" ).src = './images/'+site+'/rechts.gif';
  document.getElementById( "right" ).src = './images/'+site+'/links.gif';
  var is = document.getElementsByTagName('i');
  for (var x=is.length-1; x>=0; x-- )
    is[x].style.color=_style[site].color;

  loadSlide( slideA, '' );
  loadSlide( slideB );

  /*
  // init Facebook
  try {
    FB.init("51581d2a05717aa7590030e9dd47c54a");
  }
  catch(e) {
  }
  */
  
  // starte mit HOME
  menu=100;
  clip();
};

