// JavaScript Document
imgBaseName="";
oldText="";
photog = new Array (1);
photog[0] = new Array ("2005 JIm Rakete","2009 Johannes Zacher","2009 Johannes Zacher","2009 Johannes Zacher");
document.onscroll = function(){
	if (window.pageYOffset > 90){
	var heading = document.getElementById("heading");
	heading.style.opacity = ".3";

	} else {
		var heading = document.getElementById("heading");
		heading.style.opacity = "1";
	}
	if (window.pageXOffset>5){
	
	document.getElementById("nav").style.opacity = ".3";

	} else {
	document.getElementById("nav").style.opacity = "1";
	}
	
	
	}

//e.target.scrollTop


function initVideo()
{
	
videoMax=9;videoIndex=0;
videoName = new Array ("2008_Tatort_-_Mit_ruhiger_Hand","2006_Nuclear_Race","2008_Hafenkante","2007_Kuppke","2008_SoKo_Wismar","2000_Luther","1994_bis_2000_USA","1989_Hurenglueck","1989_Hitlerjunge_Salomon");
document.getElementById("thumbBarBk").onclick = videoChange;
document.getElementById("thumbBarFw").onclick = videoChange;
var imagePreload = new Array (videoName.length);
var preCount = 0;

for (var i=0; i<videoName.length; i++)
	{ 	imagePreload[i] = new Image();
		imagePreload[i].src = "images/videoThumbs/img_"+videoName[i]+".jpg";
		imagePreload[i].onload = testingPre;
	}
	function testingPre () {preCount++; if (preCount == videoName.length) {videoSetup();}}
}
function videoInfo()

{	
var newText = this.getAttribute("title").replace(/_/g," ");
document.getElementById("videoText2").innerHTML=newText;
}

function restoreInfo()
{
	document.getElementById("videoText2").innerHTML="";
}


function video ()

{
	var title = this.getAttribute("title");
	
	var newVideo = "videos/mov_"+title+".flv";
	
	document.getElementById("videoPlayer").SetVariable("player:jsStop", "");
	document.getElementById("videoPlayer").SetVariable("player:jsUrl",newVideo);
	document.getElementById("videoPlayer").SetVariable("player:jsPlay", "");
	return false;
}

	
function thumbBarSetup (divName, amtLink, amtImg, amtTotalImg)
{	
	divElement = document.getElementById(divName);
	divElement.setAttribute("amtLink",amtLink);
	divElement.setAttribute("amtImg",amtImg);
	divElement.setAttribute("amtTotalImg",amtTotalImg);
	divElement.setAttribute("currentThumb",amtTotalImg);
	divElement.setAttribute("firstFullImage",amtTotalImg);
	divElement.setAttribute("currentThumbInBar",1);
	
	buttonTryout ("NavBk");
	buttonTryout ("NavFw");
	
	
	function buttonTryout (buttonName)
	{
	try{
	var buttonId = divName+buttonName;
	var buttonTag = document.getElementById(buttonId);
	buttonTag.onclick = fullImageChange;
	}
	catch (error){return false}
	}



	thumbBar(divElement,0,amtTotalImg);
}
	
function thumbBarChange ()
{
	direction = this.getAttribute("direction");
	divElement = this.parentNode.parentNode.parentNode;
	
	var currentThumb = parseInt(divElement.getAttribute("currentThumb"));
	thumbBar (divElement, direction, currentThumb);
}
	
function thumbBar (divElement,direction,currentThumb)
{	
	var divName = divElement.getAttribute("id");
	//var index = parseInt(divName.substr(divName.length-1))-1;
	var amtLink= divElement.getAttribute("amtLink");
	var amtImg = divElement.getAttribute("amtImg");
	var amtTotalImg = divElement.getAttribute("amtTotalImg");
	
	
	
	//document.getElementById("infoText").innerHTML=currentThumb;
	
	
	
	if 	(direction !== 0) 
	{
	
	currentThumb += (amtLink*amtImg*direction);
	
	
	
				if (currentThumb < 1) {currentThumb=amtTotalImg; } 
				if (currentThumb > amtTotalImg) 
				{   
				 	var rest =amtTotalImg%(amtLink*amtImg);
					 if (rest>0) {currentThumb= rest; 
					 
					
					 
					 } else {currentThumb=amtLink*amtImg; }
					 
				
				}
	}
	
	
	// delete all thumbs: 
		if ( divElement.hasChildNodes() )
		{
			while (divElement.childNodes.length >= 1 )
		  {
			  divElement.removeChild(divElement.firstChild );       
		  } 
		}
	divElement.setAttribute("currentThumb",currentThumb);
	
		
	var newLink;
	var amtTotal = currentThumb;
	var firstFullImage;
	var newList;
	
		newList = document.createElement("ul");
		
	for (var j=0; j < amtImg; j++){
			newLink = document.createElement("a");
			firstFullImage = amtTotal; // before amtTotal gets higher the start thumb is stored
			for (var k=0; k < amtLink; k++)
			{	var newImage = document.createElement("img");
				newImage.src = "images/"+divName+"/thumbs/img"+(amtTotal)+".jpg";
				newLink.appendChild(newImage);
				amtTotal--;
				if (amtTotal<1) {break;}
			}
			newLink.href = "#"+j;
			newLink.title = "Klick mich!";
			
			/*ph=(amtTotal/amtLink);
			if ( !photog[index][ph] ) {
				photog[index][ph]="nicht angegeben";}
			newLink.setAttribute("photog",photog[index][ph]);*/
			newLink.setAttribute("fullImg",firstFullImage);
			newLink.onclick = fullImage ;// for IE!
			newList.appendChild(newListElement(newLink));
			if (amtTotal<1) {break;}
			
	}
	if (amtImg*amtLink < amtTotalImg) {
			newLink = document.createElement("a");
			newLink.href="#weiter";
			newLink.title="mehr Bilder!";
			newLink.setAttribute("id","navButton");
			newLink.setAttribute("direction","-1");
			
			newLink.onclick = thumbBarChange;
			
			var text = document.createTextNode(">");
			newLink.appendChild(text);
			newList.appendChild(newListElement(newLink));
			
			newLink = document.createElement("a");
			newLink.href="#weiter";
			newLink.title="mehr Bilder!";
			newLink.setAttribute("id","navButton");
			newLink.setAttribute("direction","1");
			
			newLink.onclick = thumbBarChange;
			
			
			var text = document.createTextNode("<");
			newLink.appendChild(text);
			newList.insertBefore(newListElement(newLink),newList.firstChild);
	}
	divElement.appendChild(newList);
	
	
}
function newListElement(listElement){
	var newListElement = document.createElement("li")
	newListElement.appendChild(listElement);
	return newListElement;
}

function fullImage () { // THUMB GETS CLICKED
	
	var firstFullImage = parseInt(this.getAttribute("fullImg")); // imgage reference stored in actual thumb tag
	var thumbDiv = this.parentNode.parentNode.parentNode// get the enclosing div, which is the div that holds the thumbBar
	var divName = thumbDiv.getAttribute("id");// get the name of the thumbBar (to refer to the location of the full images
	var amtLink = thumbDiv.getAttribute("amtLink");
	var amtImg = thumbDiv.getAttribute("amtImg");
	var amtTotalImg = thumbDiv.getAttribute("amtTotalImg");
	fullImageDisplay ( amtLink,amtImg, divName, firstFullImage,amtTotalImg);
	
	var currentThumbInBar = calcThumbIndex (firstFullImage, amtLink, amtImg, amtTotalImg); // set ThumbBar for next and back
	divElement.setAttribute("currentThumbInBar",currentThumbInBar);
	
}
	
function fullImageDisplay ( amtLink, amtImg, divName, firstFullImage,amtTotalImg)
{	var preCount = 0;
	var imagePreload = new Array(amtLink);
	for (var i=0; i<amtLink;i++){
		imageId = divName+"_"+(i+1);
		imagePreload[i] = new Image();
		imagePreload[i].src = "images/"+divName+"/MarcoHofschneider_"+parseInt(firstFullImage-i)+".jpg";
		imagePreload[i].onload = preloadCount;
		}
		
		function preloadCount() {
		preCount++;
		if (preCount == amtLink) {
			
	for (var i=0; i<amtLink;i++){
		imageId = divName+"_"+(i+1);
		document.getElementById(imageId).src = "images/"+divName+"/MarcoHofschneider_"+parseInt(firstFullImage-i)+".jpg";
	}}}
	
	var index = parseInt(divName.substr(divName.length-1))-1;
	var photogIndex = Math.floor(firstFullImage/2)-1;
	
			if ( !photog[index][photogIndex] ) {
				photog[index][photogIndex]="nicht angegeben";}
			
		
	document.getElementById("copyrightInfo").innerHTML='&copy;'+photog[index][photogIndex];
	document.getElementById(divName).setAttribute("firstFullImage", firstFullImage);
}



function fullImageChange ()
{
var buttonId = this.getAttribute("id");
var divName = buttonId.substring(0,(buttonId.length-5)); // gets the name of the thumbBar: thumbBar1
if (buttonId.indexOf("NavBk") > -1) {direction=1;} else {direction = -1;} // if the id of the button has "bk" in it it's the back button, otherwise the forwar button
var divElement = document.getElementById(divName);
var firstFullImage = parseInt(divElement.getAttribute("firstFullImage"));

var amtLink = parseInt(divElement.getAttribute("amtLink")); // how many images in one link?
var amtTotalImg = parseInt(divElement.getAttribute("amtTotalImg"));
var amtImg = parseInt(divElement.getAttribute("amtImg")); // how many thumbs dose one thumbBar have ?

var lastBar;
var begin;


var rest = amtTotalImg%(amtLink*amtImg); 
var restLink = rest%amtLink; 
if ((rest>0) && (firstFullImage <= rest)) {lastBar = true}

firstFullImage += (direction * amtLink);
if (firstFullImage >  amtTotalImg) {
	
		
					 if (restLink>0) {firstFullImage = restLink;} 
					 
					 
				 else {firstFullImage= amtLink}}
if (firstFullImage <  1) { begin=true; firstFullImage= amtTotalImg;}

if ((rest>0) && (firstFullImage <= rest)) {lastBar = true}



fullImageDisplay ( amtLink, amtImg, divName, firstFullImage,amtTotalImg);

var currentThumb = parseInt (divElement.getAttribute("currentThumb")); 

hasToBeThumb = calcHasToBeThumb (firstFullImage, amtLink, amtImg, amtTotalImg);

if (hasToBeThumb != currentThumb) 
	{thumbBar(divElement,0, hasToBeThumb);

}





}

function calcThumbIndex (image, amtLink, amtImg, amtTotalImg)
{
	var imageNum = amtTotalImg-image;
	var firstInLink = imageNum%(amtLink*amtImg);
	firstInLink = Math.ceil((firstInLink/amtLink)) + 1;
	return firstInLink;
}

function calcHasToBeThumb (firstFullImage, amtLink, amtImg, amtTotalImg)
{	
	var thumbIndex = calcThumbIndex (firstFullImage, amtLink, amtImg, amtTotalImg);
	var hasToBeThumb = firstFullImage + ((thumbIndex-1)*amtLink);
	return hasToBeThumb;

}

function videoSetup ()
{
	var videoLink;
	var linkName;
	for (var i=0; i<4; i++)
	{
		linkName = "link" + i;
		linkImg = "thumb" + i;
		j = videoIndex + i;
		if (j>(videoMax-1)) {j -= videoMax;}
		document.getElementById(linkImg).src = "images/videoThumbs/img_"+videoName[j]+".jpg";
		linkElement = document.getElementById(linkName);
		linkElement.setAttribute("title",videoName[j]);
		linkElement.onclick = video;
		linkElement.onmouseover = videoInfo;
		linkElement.onmouseout = restoreInfo;
		
	}
	
}

function videoChange ()
{
	var direction;
	if (this.getAttribute("id").indexOf("Bk") > -1) {direction = -1;} else {direction=1;}
	videoIndex += direction;
	if (videoIndex<0) {videoIndex = videoMax-1;}
	if (videoIndex > videoMax-1) {videoIndex = 0;}
	videoSetup();
	return false;
	
	
}

function imgSetup (){
var newWidth =Math.floor( window.innerWidth/2);
var newHeight= (newWidth * 62) / 45;
//alert ("newWidth: "+newWidth);

document.getElementById("thumbBar1_1").width = newWidth;
document.getElementById("thumbBar1_2").width = newWidth;


}


