var highlightColor = '#C0B5AF';
var activeImage = 1;
var openContainer = null;
var opening = false;
var loading = false;
var orgColor = '';
var scrollToPx = 0;
var scrollPxOld = 0;
var openNext = false;
var slideLoaded = false;
var fading = false;
var topValueGlobal = 0;	
	
Object.extend(Element, {
	nextSib: function(element,cntTag) {
		if (!isNaN(parseInt(cntTag))) {
			for (i=0;i<cntTag;i++) {	
				element = element.nextSibling; 	
				while ((element) && (element.nodeType!=1)) {
					element = element.nextSibling;
				}	
			}
		}
		else {
				element = element.nextSibling; 	
				while ((element) && (element.nodeType!=1)) {
					element = element.nextSibling;
				}			
		}
		return element;
	},
	prevSib: function(element,cntTag) {
		if (!isNaN(parseInt(cntTag))) {
			for (i=0;i<cntTag;i++) {
				element = element.previousSibling; 	
				while ((element) && (element.nodeType!=1)) {
					element = element.previousSibling;
				}	
			}
		}
		else {
				element = element.previousSibling; 	
				while ((element) && (element.nodeType!=1)) {
					element = element.previousSibling;
				}				
		}
		return element;
	},
	getParentNode: function(element,cntTag) {
		if (!isNaN(parseInt(cntTag))) {
			for (i=0;i<cntTag;i++) {
				if (element.parentNode)
					element = element.parentNode;
				else
					break;
			}			
		}
		else {
			element = element.parentNode; 	
			while ((element) && (element.tagName!=cntTag) && (element.nodeType==1)) {
				element = element.parentNode;
			}			
		}
		
	
		return element;
	}
});


function UnCryptMailto( s, sh ) {
		var n = 0;
		var r = "";
		for( var i = 0; i < s.length; i++)
		{
				n = s.charCodeAt( i );
				if( n >= 8364 )
				{
						n = 128;
				}
				r += String.fromCharCode( n - sh );
		}
		return r;
}
	
function lnkTrans(s, sh)	{
	location.href = 'mailto:'+UnCryptMailto(s, sh);
}


function showImages(j) {
	opening = true;
	if (!j)
		j = 0;
	i = (j+1);	
	imgArr = $$('div.series_images img');
	if (imgArr.length>0) {
		imgArr[j].style.visibility = 'visible';
		if (j<(imgArr.length-1))
			setTimeout(function() {showImages(i)},100);
		else {
			loading = false;
			opening = false;
		}	
	}
}

function highlightSeries(series,highlight,typ) {
	if (loading)
		return;
		
	tmp = $$('.'+series);
	for (i=0;i<tmp.length;i++) {
		if (highlight==1)
			tmp[i].style.backgroundColor=highlightColor;
		else if (typ==8)
			tmp[i].style.backgroundColor='#000000';		
		else
			tmp[i].style.backgroundColor='#ffffff';	
		
			
	}
	return;
}


function showSeries(obj,series) {
	var nr = 1;
	if (opening)
		return;		
		tmp = $$('.'+series);
		for (i=0;i<tmp.length;i++) {
			if (i==0)
				objFirst = tmp[i];	
			if (obj==tmp[i]) {
				nr += i ;
				/*objFirst = tmp[i];*/
				activeImage = nr;
			}	
		}	
	
		objLast = tmp[i-1];
		sId = series.substr(6);
		params = '&series='+sId+'&nr='+nr;
		new Ajax.Updater('', "ajax/getPortfolio.php5", {parameters: params, method:"post",evalScripts:true, onComplete:function (req) {showPortfolio(objFirst,objLast,req,sId)}, onFailure:show_failure, onException:show_exception });			
	return;
}


function showPortfolio(obj,objLast,req,sId) {
	if (opening)
		return;

	opening = true;
	tout = 0;
	if (openContainer) { 		
		openNext = true;
		closePortfolio(openContainer.getElementsByTagName('A')[0]);
		tout = 700;
	}	
	openNext = false;
	setTimeout(function () {	
		new Insertion.Before(obj,req.responseText);

		openContainer = Element.prevSib(obj);
		$(openContainer).hide();
		setTimeout(function() {
			Effect.Grow(openContainer,{direction:'top-left',duration:0.5});
		},100);
		
			
		nextSib = obj;
		do {
			nextSib.style.display='none';			
		
		}	while(nextSib!=objLast && (nextSib = Element.nextSib(nextSib)));
		
		setTimeout(function () {		
			nextSib = Element.nextSib(objLast);	
			if (nextSib)	{
				nextSib.style.marginLeft = '0px';
				realignElements(nextSib,'open',sId);
			}
			else {
				opening = false;			
				showSlideInfo(sId);			
			}

		},550);
		
	},tout);
	
}

function realignElements(nextSib,open,sId) {

	do {	
		if (open=='open') {
			if (parseInt(nextSib.offsetLeft)==384  && nextSib.className.indexOf('series_last')<=0) {				
				if (nextSib = Element.nextSib(nextSib)) 
					if (nextSib.className.indexOf('row_last')<=0 )
						nextSib.className = nextSib.className + ' row_last';					
			}	
			else if (parseInt(nextSib.offsetLeft)<=512 && (nextSib.className.indexOf('row_last')>0)) {
				tmp = nextSib.className.replace(/row_last/,'');
				nextSib.className = tmp;
			}		
			
		}	
		else {
		
			if (parseInt(nextSib.offsetLeft)==0  && nextSib.className.indexOf('row_last')<=0) {

				if (nextSib = Element.prevSib(nextSib)) {			
			
					if (parseInt(nextSib.offsetLeft)==320) {					
						nextSib = Element.nextSib(nextSib);
						nextSib.className = nextSib.className + ' row_last';
					}		
					else 
						nextSib = Element.nextSib(nextSib);
				}	
			}	
			else if (parseInt(nextSib.offsetLeft)<512 && (nextSib.className.indexOf('row_last')>0)) {
				
				tmp = nextSib.className.replace(/row_last/,'');
				nextSib.className = tmp;
			}						
				
				
		}

	} while (nextSib = Element.nextSib(nextSib))
	if (open=='open') {
		opening = false;			
		showSlideInfo(sId);
	}	
	else
		hideSlideInfo();
}


function showSlideInfo(sId) {
	orgColor = $('background').style.backgroundColor;
	//$('background').style.backgroundColor = highlightColor;
	tmp = $('navigation').getStyle('background-color');
	
	if (tmp == 'rgb(255, 255, 255)' || tmp == '#fff'  || tmp == '#ffffff')
		$('highlightInfo').style.color = '#000000';
	
	topValue = topValueGlobal = parseInt(openContainer.offsetTop);
	
	if (topValue<425)
		topValue = 425;
	setTimeout(function () {$('content').style.height = parseInt($('content').offsetHeight)+topValue+'px';$('background').style.height = parseInt($('content').offsetHeight)+'px';},100);
	$('navigation_highlight').style.display = 'block';
	slideLoaded=true;
	
	params = '&entry='+sId;
	new Ajax.Updater('', "ajax/getSlideInfo.php5", {parameters: params, method:"post",evalScripts:true, onComplete:function (req) {setSlideInfo(req)}, onFailure:show_failure, onException:show_exception });			
}

function enableSlideInfo(sId) {
	if (slideLoaded) {
		//$('background').style.backgroundColor = highlightColor;
		$('navigation_highlight').style.display = 'block';
		$('navigation_inner').style.display='none';
	}
}

function disableSlideInfo(sId) {
	if (slideLoaded) {
		$('background').style.backgroundColor = orgColor;
		$('background').style.height = $('content').offsetHeight;
		$('navigation_highlight').style.display = 'none';
		$('navigation_inner').style.display='block';
	}
}

function setSlideInfo(req) {
	
	$('highlightInfo').innerHTML = '<br>'+req.responseText;
	$('highlightInfo').style.top = parseInt(openContainer.offsetTop)+30+'px';
	$('navigation_inner').style.top = parseInt(openContainer.offsetTop)-67+'px';
	$('navigation_inner').style.display='none';
	/*window.scrollTo(0,parseInt(openContainer.offsetTop)+48);*/

	moveTo(document.body.scrollTop,parseInt(openContainer.offsetTop)-57);
	
}

function moveTo(scrollPx,scrollToPx) {
	scrollInt = 10;
	if (scrollPx+scrollInt>scrollToPx)
		scrollInt = scrollToPx-scrollPx;
	scrollPx+=scrollInt;	

	window.scrollBy(0,scrollInt);

	if (scrollPx>=scrollToPx) {	
		scrollPx = 0;
		return;
	}	
	
	window.setTimeout(function() {moveTo(scrollPx,scrollToPx)},10);
}

function moveUp(scrollPx,scrollToPx) {
	scrollInt = -20;
	if (scrollPx+scrollInt<scrollToPx)
		scrollInt = -scrollPx;
	scrollPx+=scrollInt;	

	window.scrollBy(0,scrollInt);

	if (scrollPx<=scrollToPx) {	
		scrollPx = 0;
		return;
	}	
	
	window.setTimeout(function() {moveUp(scrollPx,scrollToPx)},7);
}

function hideSlideInfo() {
	$('background').style.backgroundColor = orgColor;
	//setTimeout(function () {$('content').style.height = parseInt($('container').offsetHeight)+'px';$('background').style.height = $('content').offsetHeight;},50);
	redValue = 0;
	setTimeout(function () {if (topValueGlobal<425) redValue = (525-topValueGlobal); $('content').style.height = parseInt($('content').offsetHeight)-redValue-topValueGlobal+'px';$('background').style.height = $('content').offsetHeight;},50);
	
	
	$('navigation_highlight').style.display = 'none';
	$('highlightInfo').innerHTML = '';
	$('navigation_inner').style.display='block';
	$('navigation_inner').style.top='0px';
  slideLoaded = false;
}

function closePortfolio(obj) {	
	if (!openNext)
		moveUp(document.body.scrollTop,0); 

	openContainer = null;
	
	cont = Element.getParentNode(obj,6);

	pNode = Element.getParentNode(obj,7);

	new Effect.Shrink(cont, {
		direction: 'top-left',
		duration: 0.5
	});	
	
	setTimeout(function () {
		nextSib = cont;
		
		while (nextSib = Element.nextSib(nextSib)) {
			if (nextSib.style.display=='none')
				nextSib.style.display = 'block';
			else
				break;	
		}
		
	},200);
	
	setTimeout(function () {pNode.removeChild(cont)},600);
	activeImage = 1;
	if (nextSib) 
		setTimeout(function () {realignElements(Element.prevSib(nextSib),'close')},550);
	else		
		hideSlideInfo();
}

function showNextImage(obj) {
	lastImg = false;
	if (fading)
		return;
	parSib = Element.getParentNode(obj,6);
	img = parSib.getElementsByTagName('IMG');
	if (img.length<6)
		return;
		
	nextSib = Element.nextSib(Element.getParentNode(obj,5));
	i=0;

	while (i<activeImage) {
		if (nextSib.tagName=='IMG' && nextSib.parentNode.tagName!='A') {
			nextSib.style.display = 'none';
			i++;
		}	
		prevSib = nextSib;
		nextSib = Element.nextSib(nextSib);		
	}
	
	fading=true;
	setTimeout(function(){fading=false;},1000);
	
	prevSib.style.display = 'block';
	$(prevSib).fade({ duration: 0.7,from:1.0,to:0.0});	

	if (nextSib) {
		nextSib.style.display = 'block';
		nextSib.style.opacity = 0;

		$(nextSib).fade({ duration: 1.0,from:0.0,to:1.0 });
		nextSib = Element.nextSib(nextSib);		
		if (!nextSib)
			lastImg = true;
		activeImage++;
	} 
	else {
		nextSib = Element.nextSib(Element.getParentNode(obj,5));
		nextSib.style.display = 'block';
		nextSib.style.opacity = 0;
		$(nextSib).fade({ duration: 1.0,from:0.0,to:1.0 });		
		activeImage = 1;
	}
	
	
	imgObj = img[activeImage+3];
	if (imgObj.getAttribute('color')=='white') {		
		img[1].src = img[1].src.replace(/_black\./,'_white.');
		img[2].src = img[2].src.replace(/_black\./,'_white.');
		$('closeLeft').style.color = 'white';
		$('closeRight').style.color = 'white';
	}
	else {
		img[1].src = img[1].src.replace(/_white\./,'_black.');
		img[2].src = img[2].src.replace(/_white\./,'_black.');	
		$('closeLeft').style.color = 'black';
		$('closeRight').style.color = 'black';		
	}	
	if (lastImg) 
		img[2].style.display="none";
	else
		img[2].style.display="block";	
	img[1].style.display="block";				
}

function showPrevImage(obj) {
	parSib = Element.getParentNode(obj,6);
	img = parSib.getElementsByTagName('IMG');
	if (img.length<6)
		return;

	if (activeImage<=1) {
		activeImage = img.length-5;
		showNextImage(obj);
	}
	else {
		nextSib = Element.nextSib(Element.getParentNode(obj,5));

		i=1;
		while (i<=activeImage) {		
			nextSib = Element.nextSib(nextSib);	
			if (nextSib.tagName=='IMG') 					
				i++;
		}
		
		/*nextSib.style.display = 'none';*/		

		nextSib.style.opacity = 1;
		$(nextSib).fade({ duration: 0.7,from:1.0,to:0.0 });		
		
		
		prevSib = Element.prevSib(nextSib);
		prevSib.style.display = 'block';
		prevSib.style.opacity = 0;
		$(prevSib).fade({ duration: 1.0,from:0.0,to:1.0 });	
		
		activeImage--;
		parSib = Element.getParentNode(obj,6);
	}
	
	img = parSib.getElementsByTagName('IMG');
	imgObj = img[activeImage+3];
	if (imgObj.getAttribute('color')=='white') {		
		img[1].src = img[1].src.replace(/_black\./,'_white.');
		img[2].src = img[2].src.replace(/_black\./,'_white.');
		$('closeLeft').style.color = 'white';
		$('closeRight').style.color = 'white';
	}
	else {
		img[1].src = img[1].src.replace(/_white\./,'_black.');
		img[2].src = img[2].src.replace(/_white\./,'_black.');	
		$('closeLeft').style.color = 'black';
		$('closeRight').style.color = 'black';		
	}	
	if (activeImage==1) 
		img[1].style.display="none";
	else
		img[1].style.display="block";		

	img[2].style.display="block";					
}

function showChildren(obj) {
	/*for  (i=0;i<obj.childNodes.length;i++) 	
		if (obj.childNodes[i].nodeType==1) 	
			obj.childNodes[i].style.visibility = 'visible';		*/
	tmp = obj.getElementsByTagName('table'); 	
	tmp[0].style.visibility = 'visible';	
}

function hideChildren(obj) {
	/*for  (i=0;i<obj.childNodes.length;i++)
		if (obj.childNodes[i].nodeType==1)
			obj.childNodes[i].style.visibility = 'hidden';*/
	tmp = obj.getElementsByTagName('table'); 	
	tmp[0].style.visibility = 'hidden';				
}

function setBackground(node,path,rep) {
	if (!rep)
		rep = "no-repeat";
	$(node).style.backgroundImage = "url("+path+")";
	$(node).style.backgroundRepeat  = rep;

}


function showNavInv(obj) {
	tmp = obj.parentNode.getElementsByTagName('IMG');
	for (i=0;i<2;i++)
		tmp[i].style.visibility='visible';
}

function showNavSplitted(obj) {
	$('splitted_outer').style.visibility='visible';	
	$('splitted_inner').style.visibility='hidden';
	$('splitted_inner').style.display='none';
}

function hideNavSplitted(obj) {
	$('splitted_outer').style.visibility='hidden';	
	$('splitted_inner').style.visibility='visible';	
	$('splitted_inner').style.display='inline';
}

function hideNavInv(obj) {
	tmp = obj.parentNode.getElementsByTagName('IMG');
	for (i=0;i<2;i++) 
		tmp[i].style.visibility='hidden';
}


function show_exception( requestObject, ex) {
	//alert("Keine AJAX-Anfrage an " + requestObject.url + " moeglich: " + ex);
}

function show_failure( requestObject ) {
	//alert("Error from Server:" + requestObject.statusText);
}