/* $Id: bildwechsel.js 9 2006-06-13 06:28:03Z sacher $ */

var intervals = new Object();
var nCurrent = new Object();
var bwIDs=new Array();
var allowBigRotate = true;

function rotate_Small_Only(interval,defer) {
	allowBigRotate = false;
	rotate(interval, defer);
}

function rotate(interval, defer){

	if ( typeof bilder == "undefined") {
		alert("Bildwechsel-Funktionalit�t noch nicht konfiguriert!");
		return;
	}

	for (var bildwechsel in bilder) {
		set_CSS(document.getElementById(bildwechsel));
		bwIDs.push(bildwechsel);
	}
	bwIDs.sort();

  if ( typeof interval == "undefined")
    interval = 3000;

	if ( typeof defer == "undefined" )
		setInterval('rotate_go()', interval);
	else
		rotate_deferred(interval)
}


function set_CSS(element) {
	if (element && element.onclick && typeof element.onclick != "undefined" && element.onclick != null)
		element.style.cursor="pointer";
}

function rotate_deferred(startinterval) {
	var i=0;
	defer = startinterval/bwIDs.length;
	while( i < bwIDs.length )
		intervals[bwIDs[i]] = setInterval('rotate_it("'+bwIDs[i]+'", '+startinterval+')', i++*defer);
}

function rotate_go() {
	for (var i = 0; i < bwIDs.length; ++i)
		rotateBanner(bwIDs[i]);
}

function rotate_it(id, milisecs) {
	clearInterval(intervals[id]);
	rotateBanner(id);
	intervals[id] = setInterval('rotateBanner("'+id+'")',milisecs);
}

function rotateBanner(id){
	if ( typeof nCurrent[id] == "undefined"  ) {
		nCurrent[id] = 0;
		var el = document.getElementById(id);
		if (el) {
      /*
      var found = false;
      for (var i=0; i<bilder[id].length; i++) {
        var img = new Image();
        img.src = bilder[id][i];
        var src = img.src;
        if (el.src == src)
          found = true;
      }
      if (!found) */
        bilder[id][bilder[id].length] = el.src;
    }
	}

	document.getElementById(id).src = bilder[id][nCurrent[id]];
	if (allowBigRotate==true && typeof pictureWindowFeverBildwechselId != "undefined"
		&& id==pictureWindowFeverBildwechselId
		&& typeof pictureWindowFever == "object"
		&& !pictureWindowFever.closed)
			pictureWindowFever.document.getElementById("pictureWindowFever").src= convertPictureName(bilder[id][nCurrent[id]]);

	if (nCurrent[id] < bilder[id].length-1)
		nCurrent[id]++;
	else
		nCurrent[id]= 0;
}

window.picture_helper = {
      pictures: [],
      duration: [],
      i: 0,
      
      init: function() {
	$('#slidewrap').css('position','relative');
	$('#slideshow').css('position','absolute').css('left','0px').css('top','0px').css('z-index','1');
	$('#loadingDiv').css('position','absolute').css('left','0px').css('top','0px').css('z-index','1');
      },
      
      rotate_picture_by_opacity: function() {
	      $('#slideshow img').remove();
	      $('#loadingDiv img').remove();
	      var next_pic_idx = picture_helper.i + 1;
	      if (picture_helper.i >= picture_helper.pictures.length -1) {
		      next_pic_idx = 0;
		      
	      }
	      $('#slideshow').append('<img src="'+picture_helper.pictures[next_pic_idx]['src']+'">');
	      $('#loadingDiv').append('<img src="'+picture_helper.pictures[picture_helper.i]['src']+'" style="opacity:1">');
	      $('#loadingDiv img').animate({opacity:0},picture_helper.duration[0]);

	      setTimeout(function(){ picture_helper.wait(next_pic_idx); }, picture_helper.duration[0]);
	      picture_helper.i = next_pic_idx;
      },

      wait: function() {
	setTimeout(function(){ picture_helper.rotate_picture_by_opacity(picture_helper.i); }, picture_helper.duration[1]);
      }
}