var imgs=["/images/btnImgPrev.png", "/images/btnImgPrev_f2.png", "/images/btnImgPrev_f3.png", "/images/btnImgNext.png", "/images/btnImgNext_f2.png", "/images/btnImgNext_f3.png"];

function galPrepare()
{
  //Blder nebeneinander	
  $gallery=$("#gallery");
  $gallery.css("white-space", "nowrap").hide();
  //Preload der Abdunklung
  $.each(imgs, function(elem, elemindex)
  {
    img=new Image();
	img.src=elem; 
  });
  //Bilder einhängen
  $galcon=$("#gallerycontainer");
  $galcon.css("height", "340px");
  $("<img id='btnprev' border=0 src='"+imgs[0]+"' />").prependTo($galcon);
  $("<img id='btnnext' border=0 src='"+imgs[3]+"' />").prependTo($galcon);
  
}


$().ready(function(){
 $("#btnprev").hover(function(){$(this).attr("src", imgs[1]); fSpeedSoll=4;}, function(){$(this).attr("src", imgs[0]); fSpeedSoll=0;});
 $("#btnnext").hover(function(){$(this).attr("src", imgs[4]); fSpeedSoll=-4;}, function(){$(this).attr("src", imgs[3]); fSpeedSoll=0;});
 $("#btnnext").mousedown(function(){fSpeedSoll=-18;}).mouseup(function(){fSpeedSoll=-4;});
 $("#btnprev").mousedown(function(){fSpeedSoll=18}).mouseup(function(){fSpeedSoll=4;});
 $gallery.show();
 setInterval(moveIt, 20);
});


var iGalWidth=0;
function galWidth()
{
	//Wert endgültig? Dann diesen zurückgeben
	if(iGalWidth>0) return iGalWidth;
	//Wert ermiteln
	var iWidth=0;
	var bComplete=true;
	$("#gallery a img").each(function()
    {
		iWidth+=parseInt(this.width);
		if(!this.complete) bComplete=false;
    });
	if(bComplete) iGalWidth=iWidth;
	return iWidth;
}

var fSpeedSoll=0.0;
var fSpeed=0.0;
var fOffset=0.0;

function moveIt()
{
	if(fSpeed!=fSpeedSoll)
	{
		var fDelta=(fSpeed-fSpeedSoll)*0.05;
		fSpeed-=fDelta;
	}
	//Kein Scroll?
	if(fSpeed==0.0) return;
	//Bewegung stoppen bein Minmalbewegung
	if((Math.abs(fSpeed)<0.2)&&(fSpeedSoll==0)) fSpeed=0;
	//Scrollen
	fOffset+=fSpeed;
	if(fOffset>0) {fOffset=0; fSpeed=0;}
	i=galWidth();
	i2=parseInt($("#gallerycontainer")[0].offsetWidth);
	i=i-i2;
	if(fOffset<-i) {fOffset=-i; fSpeed=0;}
	$gallery.css("left", parseInt(fOffset)+"px");
}

