/*------------------------------------------------
Fotoschauprogramm von Martin Schmidt
Begonnen Februar 2007
Stand 01.11.2009
Verwendung nur unter Angabe dieses Programmkopfes
--------------------------------------------------*/

var sta_stopp = 0 ;
var zeit_clip ;
var zeit_aufbau = 4000 ; // 4000
var zeit_durchlauf = 12000 ;// 12000
var zeit_beginn = 1000 ;
var zeit_pause = 5000 ;
var bild_rand_breite = 2 ;
var bild_rand = bild_rand_breite+"px solid #8888cc" ;
var div_1 ;
var div_2 ;
var bild_1 ;
var bild_2 ;
var max_nr ;
var schau_art ;
var schau_art_muster ;
var schritt ;
var bilder_ordner ;
var bilder_ordner_53x40 = "/bilder/mitglieder/passfotos" ;
var namens_zusatz = "" ;
var set_iv ;
var set_tout ;
var pos_lox_1 ;
var pos_loy_1 ;
var pos_lox_2 ;
var pos_loy_2 ;
var r_x, r_y, l_x, l_y ;
var breite ;
var hoehe ;
var index = 1 ;
var bild_nr ;
var bild_nr_zweist ;
var anfangs_nr ;
var zufalls_zahl_ind ;

var muster = new Array () ;
muster [0] = new Array () ;
muster [0][0] = "roe" ;
muster [0][1] = "uoe" ;
muster [0][2] = "ld" ;
muster [0][3] = "ooe" ;
muster [0][4] = "moe" ;
muster [1] = new Array () ;
muster [1][0] = "roe" ;
muster [1][1] = "uoe" ;
muster [1][2] = "ls" ;
muster [1][3] = "ooe" ;
muster [1][4] = "moe" ;

function zufallszahl ()
{
   zufalls_zahl = Math.round (Math.random () * max_nr) ;
   if (zufalls_zahl == 0)
      zufalls_zahl = 1 ;
   zufalls_zahl_einst = zufalls_zahl ;
   if (zufalls_zahl < 10)
      zufalls_zahl = "0"+zufalls_zahl ;
   return zufalls_zahl ;
}


function breite_hoehe (bild_2)
{
   if (bilder_ordner == bilder_ordner_53x40)
   {
      breite = bild_2.width ;
      hoehe = bild_2.height ;
   }
   else
   {
      breite = bild_2.width + (bild_rand_breite * 2) ;
      hoehe = bild_2.height + (bild_rand_breite * 2) ;
   }
   //window.status = "Breite: "+breite+" Höhe: "+hoehe ;
}

function bewegen ()
{
   div_2.style.visibility = "visible" ;
   if (index == 1)
   {
      div_1.parentNode.style.right = Math.floor (breite / 2)+"px" ;
      a = div_1.style.zIndex ;
      div_1.style.zIndex = div_2.style.zIndex ;
      div_2.style.zIndex = a ;
   }
   switch (schau_art)
   {
   case "rd" :
        div_2.style.left = (pos_lox_1 + breite - index)+"px" ;
        div_2.style.clip = "rect(0px, "+index+"px, "+hoehe+"px, 0px)" ;
        break ;
   case "ld" :
        div_2.style.left = (pos_lox_1 - breite + index)+"px" ;
        div_2.style.clip = "rect(0px, "+breite+"px,"+hoehe+"px, "+(breite - index)+"px)" ;
        break ;
   case "od" :
        div_2.style.top = (pos_loy_1 - hoehe + index)+"px" ;
        div_2.style.clip = "rect("+(hoehe - index)+"px, "+breite+"px, "+hoehe+"px, 0px)" ;
        break ;
   case "ud" :
        div_2.style.top = (pos_loy_1 + hoehe - index)+"px" ;
        div_2.style.clip = "rect(0px, "+breite+"px, "+index+"px, 0px)" ;
        break ;

   case "roe" :
        div_2.style.clip = "rect(0px, "+breite+"px,"+hoehe+"px, "+(breite - index)+"px)" ;
        break ;
   case "loe" :
        div_2.style.clip = "rect(0px,"+index+"px,"+hoehe+"px, 0px)" ;
        break ;
   case "ooe" :
        div_2.style.clip = "rect(0px, "+breite+"px, "+index+"px, 0px)" ;
        break ;
   case "uoe" :
        div_2.style.clip = "rect("+(hoehe - index)+"px, "+breite+"px, "+hoehe+"px, 0px)" ;
        break ;
   case "moe" :
        div_2.style.clip = "rect("+(r_y-index)+"px, "+(r_x+index)+"px, "+(l_y+index)+"px, "+(l_x-index)+"px)" ;
        break ;

   case "rs" :
        div_1.style.left = (pos_lox_1 - index)+"px" ;
        div_1.style.clip = "rect(0px,"+breite+"px,"+hoehe+"px, "+(breite - index)+"px)" ;
        div_2.style.left = (pos_lox_1 + breite - index)+"px" ;
        div_2.style.clip = "rect(0px,"+index+"px,"+hoehe+"px, 0px)" ;
        break ;
   case "ls" :
        div_2.style.left = (pos_lox_1 - breite + index)+"px" ;
        div_2.style.clip = "rect(0px, "+breite+"px,"+hoehe+"px, "+(breite - index)+"px)" ;
        break ;
   case "os" :
        div_2.style.top = (pos_loy_1 - hoehe + index)+"px" ;
        div_2.style.clip = "rect("+(hoehe - index)+"px, "+breite+"px, "+hoehe+"px, 0px)" ;
        break ;
   case "us" :
        div_2.style.top = (pos_loy_1 + hoehe - index)+"px" ;
        div_2.style.clip = "rect(0px, "+breite+"px, "+index+"px, 0px)" ;
        break ;
   }
   if (index == abbruch)
   {
      clearInterval (set_iv) ;
      index = 1 ;

      div_1.style.left = pos_lox_1+"px" ;
      bild_1.src = bild_2.src ;

      a = div_1.style.zIndex ;
      div_1.style.zIndex = div_2.style.zIndex ;
      div_2.style.zIndex = a ;

      if (bilder_ordner == bilder_ordner_53x40)     //Titelbox beschriften
         div_1.title = bilder_namen [zufalls_zahl_einst] ;
      else
         div_1.title = "Bild Nr. "+bild_nr ;
      //document.getElementById ("bild_nr").innerHTML = "Bild Nr. "+bild_nr ;

      switch (reihenfolge)
      {
      case "go" :
           bild_nr ++ ;
           if (bild_nr > max_nr)
              bild_nr = 1 ;
           bild_nr_zweist = bild_nr ;
           if (bild_nr < 10)
              bild_nr_zweist = "0"+bild_nr ;
           break ;
      case "zf" :
           bild_nr_zweist = zufallszahl () ;
           bild_nr = bild_nr_zweist ;
           break ;
      }
      if (bilder_ordner == bilder_ordner_53x40)
         bild_nr_zweist = bilder_namen [zufalls_zahl_einst] ;
      bild_2.src = bilder_ordner+"/"+bild_nr_zweist+namens_zusatz+".jpg" ;   // Nächstes Bild vorladen
      div_2.style.visibility = "hidden" ;

      setTimeout ("breite_hoehe (bild_2)", (zeit_pause - 1000)) ;

      if (sta_stopp == 1)
         setTimeout ("durchlauf ()", zeit_pause) ;
      return ;
   }
   index ++ ;

}

function durchlauf ()
{
   schau_art = muster [schau_art_muster][schritt] ;
   switch (schau_art)
   {
   case "rd"  :
   case "ld"  :
   case "roe" :
   case "loe" :
   case "rs"  :
   case "ls"  :
        abbruch = breite ;
        break ;
   case "od"  :
   case "ud"  :
   case "ooe" :
   case "uoe" :
   case "os"  :
   case "us"  :
        abbruch = hoehe ;
        break ;
   case "moe" :
        var a, b, c ;
        a = Math.min (breite, hoehe) ;
        b = Math.ceil (a / 2) ;
        c = Math.floor (a / 2) ;
        if (a == hoehe)
        {
           r_y = hoehe - c ;
           r_x = breite - b ;
           l_y = hoehe - b ;
           l_x = b ;
        }
        else
        {
           r_y = b ;
           r_x = breite - b ;
           l_y = hoehe - b ;
           l_x = b ;
        }
        abbruch = b ;
        break ;
   }
   set_iv = setInterval ("bewegen ()", zeit_clip) ;

   schritt ++ ;
   if (schritt == (muster [schau_art_muster]).length)
      schritt = 0 ;
}

function bilderschau (id, ordner, max, art, folge)
{
   div_1 = document.getElementById (id) ;
   div_1.style.zIndex = "1000" ;
   bilder_ordner = ordner ;
   if (bilder_ordner == bilder_ordner_53x40)
      namens_zusatz = "_53x40" ;
   max_nr = max ;
   schau_art_muster = art ;
   reihenfolge = folge ;
   schritt = 0 ;
   if (bilder_ordner != bilder_ordner_53x40)
      div_1.style.border = bild_rand ;

   pos_lox_1 = div_1.style.left ;                                          // Linksoben_x-Wert bestimmen
   pos_lox_1 = pos_lox_1.slice (0, pos_lox_1.indexOf ("px", pos_lox_1)) ;
   pos_lox_1 = parseInt (pos_lox_1) ;

   pos_loy_1 = div_1.style.top ;                                           // Linksoben_y-Wert bestimmen
   pos_loy_1 = pos_loy_1.slice (0, pos_loy_1.indexOf ("px", pos_loy_1)) ;
   pos_loy_1 = parseInt (pos_loy_1) ;

   bild_1 = div_1.firstChild ;

   el = document.createElement ("div") ;         // Div-Element erzeugen

   div_2 = div_1.parentNode.appendChild (el) ;   // Div-Element einfügen
   div_2.style.position = "absolute" ;
   div_2.style.top = pos_loy_1+"px" ;
   div_2.style.left = pos_lox_1+"px" ;
   if (bilder_ordner != bilder_ordner_53x40)
      div_2.style.border = bild_rand ;

   el = document.createElement ("img") ;         // Img-Element erzeugen
   el.setAttribute ("border", "0px") ;
   anfangs_nr = zufallszahl () ;
   bild_nr = anfangs_nr ;
   if (bilder_ordner == bilder_ordner_53x40)
      anfangs_nr = bilder_namen [zufalls_zahl_einst] ;
   el.setAttribute ("src", bilder_ordner+"/"+anfangs_nr+namens_zusatz+".jpg") ;

   bild_2 = div_2.appendChild (el) ;             // Img-Element einfügen
   //bild_2.src = bild_1.src ;
   breite_hoehe (bild_1) ;                        // Bildmaße bestimmen
   div_1.parentNode.style.right = Math.floor (breite / 2)+"px" ;

   zeit_clip = Math.round (zeit_aufbau / breite) ;
   sta_stopp = 1 ;
   setTimeout ("durchlauf ()", zeit_beginn) ;
}

function start_stopp (id, ordner, max, art, folge)
{
   if (sta_stopp == 0)
   {
      bilderschau (id, ordner, max, art, folge) ;
      document.getElementById ("sta_sto").value = "STOPPEN" ;
   }
   else
   {
      div_2.style.visibility = "hidden" ;
      document.getElementById ("sta_sto").value = "STARTEN" ;
      sta_stopp = 0 ;
   }
}
