/*
  Javascript Slideshow V 0.1
  Datum: 29.07.2004

*/


function Slideshow(){
    this.name = 'slideshow';

    // Eigenschaften
    this.resize = true;
    this.width = 400;
    this.height = 400;
    this.backgroundcolor = '#eee';

    this.start  = 1;
    this.max = 50;
    this.aktuell = this.start - 1;
    this.nr = this.start - 1;
    this.dir = 1;
    this.loop = true;

    this.error = 0;

    this.fix = 3;
    this.type = 'jpg';
    this.path = '';
    this.before = '';
    this.timeout=0;

    this.textNext = '';


    this.obj = null;
    this.w = this.h = 0;
    this.showInfo = false;

    this.win = window;
}
//-----------------------------------------------
Slideshow.prototype.anzahl = function()
//-----------------------------------------------
{
    return( this.max - this.start - this.error + 1);
}
//-----------------------------------------------
Slideshow.prototype.checkNext = function()
//-----------------------------------------------
{
    if(this.aktuell > this.max)
    {
        if(!this.loop ) { alert('Fertig'); return ; }
    }
    this.next();
}

//-----------------------------------------------
Slideshow.prototype.fertig = function(img)
//-----------------------------------------------
{
    this.m.style.display = 'none';
    // this.img.style.display = 'inline';

    this.w = img.width;
    this.h = img.height;

    if (typeof this.img.style.filter != 'undefined'){
         this.img.style.filter = "blendTrans(duration=1)";
         this.img.filters.blendTrans.apply();
         this.img.filters.blendTrans.play();
    }
    this.resizeImg();
    this.img.src = img.src;

    this.nr += this.dir;
    this.infoText();
    return false;

}
//-----------------------------------------------
Slideshow.prototype.resizeImg = function(){
//-----------------------------------------------
    var r = this.h / this.w;

    if(!this.resize)
    {
         this.img.width = this.w;
         this.img.height = this.h;
         return;
    }

    var d_w = this.width / this.w;
    var d_h = this.height / this.h;
    if(Math.abs(d_h) < Math.abs(d_w))
    {
         this.img.height = this.height;
         this.img.width = this.height / r;
    }
    else
    {
         this.img.width = this.width;
         this.img.height = this.width * r;
    }
}
//-----------------------------------------------
Slideshow.prototype.next = function()
//-----------------------------------------------
{
	  if(this.nr > this.anzahl() && this.dir > 0) {  this.nr = this.aktuell  = this.start; }

    this.aktuell += this.dir;
    this.show();
    if(this.aktuell > this.max  )  { this.nr = this.aktuell  = this.start - 1; }
    if(this.aktuell < (this.start - 1)  ) { this.nr = this.anzahl() + 1; this.aktuell = this.max + 1; }
}

//-----------------------------------------------
Slideshow.prototype.show = function() {
//-----------------------------------------------
    this.m.style.display = 'block';
    // this.img.style.display = 'none';

    var file = this.getFile(this.aktuell);

    var img = new Image();

    img.parent = this;
    img.onload = function() { this.parent.fertig(this) }
    img.onerror = function() { this.parent.checkNext() }
    img.src = file;
}

//-----------------------------------------------
Slideshow.prototype.create = function()
//-----------------------------------------------
{
    if(this.obj) return;
    this.getObj();
    if(!this.obj)
    {
         alert('Fehler, kein Objekt!');
         return;
    }

    var img = '<div title="Klicke in den Rahmen für das nächste Bild"><img galleryimg="no" src="" alt=""></div>';    
    var msg = '<div style="position:absolute">' + this.textNext  + '</div>';    		
    var text = '<div></div>'
    this.obj.innerHTML = msg + img + text;
    this.obj.style.margin = 'auto';
    this.obj.style.overflow = 'hidden';
    this.obj.style.backgroundColor =  this.backgroundcolor;

    this.setImgStyle();
    this.m = this.obj.childNodes[0];
    this.info = this.obj.childNodes[2];
    this.info.style.font = '0.8em "courier" normal';
    this.info.style.margin = '0';
    this.info.style.padding = '2px';
    this.info.style.backgroundColor = this.backgroundcolor;
    this.info.innerHTML = '<br><br><br>';
    var s = this.getSize( this.info);
    this.obj.style.width = (this.width) + 'px';
    if (this.showInfo) 
    	this.obj.style.height = (this.height + s.height + 8) + 'px';
    else
    	this.obj.style.height = (this.height) + 'px';

    var p = this.getPos(this.info);
    this.m.style.position = 'absolute';
    this.m.style.color = '#000';
    this.m.style.font = '1em bold "courier"';
    this.m.style.backgroundColor =  this.backgroundcolor;
    this.m.style.border = '0px solid black';
    this.m.style.padding = '1px 5px 1px 5px';
    this.m.style.margin = '0';
    this.m.style.top = p.top + 'px';
    this.m.style.left = p.left + 'px';
    this.m.style.width = (this.width ) + 'px';
    this.m.style.height = (s.height - 2) + 'px';

    this.next()

}
//-----------------------------------------------
Slideshow.prototype.infoText = function() {
//-----------------------------------------------
    if (this.showInfo) {
    if(!this.info) return;
    var text =  'Bild ' + this.nr + ' von ' + this.anzahl()
    + ( this.showInfo ? '<br>Orginal: ' + this.w + ' x ' + this.h + '(w x h)'
        + (this.resize ? '<br>angepaßt auf: ' + this.img.width + ' x ' + this.img.height + '(w x h)' : '')
      : ''
    )
    ;
    this.info.innerHTML = text;
  }
}
//-----------------------------------------------
Slideshow.prototype.setImgStyle = function() {
//-----------------------------------------------
    var div = this.obj.childNodes[1];
    if(!div)
    {
         alert('Kein DIV');
         return null;
    }
    div.style.border = '0px ridge #eee';
    div.style.width = this.width + 'px';
    div.style.height = this.height + 'px';
    div.style.padding = '0';
    div.style.margin = '0';
    div.style.overflow = 'hidden';
    div.style.textAlign = 'center';
    div.style.verticalAlign = 'middle';
    div.parent = this;
    div.onclick = function() { this.parent.next() }

    this.img = div.firstChild;

    if(!this.img)
    {
         alert('Kein Bild');
         return null;
    }
    return this.img;
}
//-----------------------------------------------
Slideshow.prototype.getPos = function(obj) {
//-----------------------------------------------
    var o = obj;
    var pos = {left:0, top:0};

    if(typeof o.offsetLeft != 'undefined')
    {
         while (o)
         {
              pos.left += parseInt(o.offsetLeft);
              pos.top += parseInt(o.offsetTop);
              o = o.offsetParent;
         }
    }
    else
    {
        pos.left = parseInt(o.left);
        pos.top = parseInt(o.top);
    }
    return pos;
}
//-----------------------------------------------
Slideshow.prototype.getSize = function(obj) {
//-----------------------------------------------
    var size = null;
    if(typeof document.layers != 'undefined')
    {
         size = {width :obj.clip.width, height:obj.clip.height};
    }
    else if(typeof obj.offsetWidth != 'undefined')
    {
         size = {width :parseInt(obj.offsetWidth), height:parseInt(obj.offsetHeight)};
    }
    return size;
}

Slideshow.prototype.getFile = function(nr)
{
    if(typeof nr == 'undefined') nr = this.aktuell;

    var zahl = nr;
    for(var i = 0; i < this.fix;i++) zahl = '0' + zahl;
    zahl = zahl.substring(zahl.length - this.fix);

    return this.path + '/' + this.before + zahl + '.' + this.type;
}

Slideshow.prototype.getObj = function()
{

    var win = this.win;
    var obj = win.document.getElementById ? win.document.getElementById(this.name) :
    win.document.all ? win.document.all[this.name] : win.document.layers ? win.document.layers[this.name] : null;
    if(!obj)
    {
         alert('Objekt:' + this.name + ' nicht gefunden.\nSlideshow funktioniert nicht');
         return false;
    }
    this.obj = obj;
    return this.obj;
}

var img = new Array();
Slideshow.prototype.preload = function()
{
    if(this.preloadReady) return;
    this.preloadReady = true;

    var s = this.start;
    var e = this.max;
    this.error = 0;

    for(var i = s; i < e; i++)
    {
       var idx = img.length;       
       img[idx] = new Image();
       img[idx].obj = this;
       img[idx].onerror = function() {  this.obj.error++; };       
       img[idx].src = this.getFile(i);       
    }    
}

var win = typeof document.all != 'undefined' ? window.document.body : window;

window.slideshow = new Slideshow();

win.onload = function() {

    window.slideshow.preload();
    window.slideshow.create();
    
}