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()
{
    $("a.olk_lightbox").lightBox();
    $("a.olk_fancybox").fancybox();
    $("a.olk_zoom").jqueryzoom();

    /*  $(".olk_accordion").accordion({
     active: false,
     alwaysOpen: false,
     autoheight: false,
     header: 'h3',
     animated: 'bounceslide' // slide, easeslide
     }); */
    $("a.olk_flyout").flyout();
    $("img[src$='.png']").ifixpng();

    $("ul.olk_tabs").tabs("div.olk_panes > div");
    $("div.olk_accordion").tabs(".olk_accordion div.pane", {tabs: 'h3', effect: 'slide', initialIndex: null});
    $("div.olk_slideshow div.tabs").tabs(".slides > div", {effect: 'fade',fadeOutSpeed: "slow",rotate: true}).slideshow();
    $("div.gallery").slideshow_1();
    $("a.olk_movie").media();
});


