function CheckFormularFeld(s, txt)
{
  var o = document.getElementById(s);
  if (o) {
    if (o.value.length < 4) {
      alert("Bitte das Feld "+txt+" korrekt ausf"+String.fromCharCode(252)+"llen");
      return 1;
    }
  }
  return 0;
}

function CheckKontaktForm()
{
  var fehler=0;

  // Alle Angaben pruefen
  fehler += CheckFormularFeld( 'name1', 'Name');
  fehler += CheckFormularFeld( 'plzort', 'PLZ, Ort');
  fehler += CheckFormularFeld( 'email', 'E-Mail');

  if (fehler > 0) return false;
  return true;
}

var olk_gallery_timing = 1500; // milliseconds
var olk_gallery_init = false;
var olk_gallery_id = new Array();
var olk_gallery = new Array();
var currentSequence = new Array();
var currentDisplayed = new Array();
var currentEntry = new Array();

function InitGalleries() {
  currentSequence = new Array();
  currentDisplayed = new Array();
  currentEntry = new Array();
  if (olk_gallery_init || olk_gallery.length==0) return;
  if (olk_gallery.length==0) return;
  for ( var i=0; i<olk_gallery.length; i++ ) {
    for ( var j=0; j<olk_gallery[i].length; j++) {
      var image = document.createElement( "IMG" ) ;
      image.onload = function(){
        this.onload = null;
      }
      image.src = '/tn.php?id='+olk_gallery[i][j]['m_medium_id']+'&fix=1&width=121&height=87' ;
      olk_gallery[i][j]['preload']=image;
    }
    InitGallerySequence(i);
    LoopSlideshow(i);
    setTimeout( 'LoopSlideshow('+i+')', olk_gallery_timing ) ;
  }
  olk_gallery_init = true;
}

function InitGallerySequence(gallery_index) {
  var newSequence=new Array();
  for (var i=0;i<9;i++) {
    var notfound=true;
    while (notfound) {
      var newEntry=parseInt(((Math.random()*100) %9).toString(),10);
      notfound=CheckUnique(newEntry,newSequence);
    }
    newSequence[newSequence.length]=newEntry;
  }
  var newDisplayed=new Array();
  for (var i=0;i<9;i++) {
    var notfound=true;
    while (notfound) {
      newEntry=parseInt(((Math.random()*100) %olk_gallery[gallery_index].length).toString(),10);
      if (currentDisplayed[gallery_index]) {
        if (CheckUnique(newEntry,currentDisplayed[gallery_index])) continue;
      }
      notfound=CheckUnique(newEntry,newDisplayed);
    }
    newDisplayed[newDisplayed.length]=newEntry;
  }
  if (!currentSequence[gallery_index]) currentSequence[gallery_index]=new Array();
  currentSequence[gallery_index]=newSequence;
  if (!currentDisplayed[gallery_index]) currentDisplayed[gallery_index]=new Array();
  currentDisplayed[gallery_index]=newDisplayed;
  currentEntry[gallery_index]=0;
}

function CheckUnique( newEntry, sofar) {
  var notfound = false;
  for (var i=0;i<sofar.length;i++) {
    if (sofar[i]==newEntry) {
      notfound = true;
      break;
    }
  }
  return notfound;
}

function LoopSlideshow(gallery_index) {
  var current = currentEntry[gallery_index];
  if (current>8) {
    InitGallerySequence(gallery_index);
    current=0;
  }
  var newSequence = currentSequence[gallery_index][current];
  var newImageId = olk_gallery[gallery_index][currentDisplayed[gallery_index][current]]['m_medium_id'];

  if (!olk_gallery[gallery_index][current]['preload'].onload) {
    ReplaceSlide(gallery_index,newSequence,newImageId);
    currentEntry[gallery_index]+=1;
  }
  setTimeout( 'LoopSlideshow('+gallery_index+')', olk_gallery_timing ) ;
}

function ReplaceSlide(gallery_index,newSequence,newImageId) {
  var galleryDiv = olk_obj('olk_gallery_'+olk_gallery_id[gallery_index]);
  var image = galleryDiv.getElementsByTagName('IMG');
  image[newSequence].src = '/tn.php?id='+newImageId+'&fix=1&width=121&height=87';
}

(function($) {
$.fn.slideshow_1 = function() {
  InitGalleries();
};
})(jQuery);

jQuery().ready(function(){
  $(".olk_accordion").accordion({
    active: false,
    alwaysOpen: false,
    autoheight: false,
    header: 'h3',
    animated: 'bounceslide' // slide, easeslide
  });
  $("a.olk_flyout").flyout();
  $("img[@src$=.png]").ifixpng();
  $("div.olk_corner").corner();
  $("div.olk_cornertl").corner({tl:{radius:6},tr:false,bl:false,br:false});
  $("div.olk_cornertr").corner({tl:false,tr:{radius:6},bl:false,br:false});
  $("div.olk_cornerbl").corner({tl:false,tr:false,bl:{radius:6},br:false});
  $("div.olk_cornerbr").corner({tl:false,tr:false,bl:false,br:{radius:6}});
  $("div.gallery").slideshow_1();
  $("a.olk_movie").media();
});
