var activeImage = 0;
var posOld = 0;
var scrolling = false;
var projectsToClose = 0;
var projectsClosed = 1;
var lastNr = 0;
var oldValue = '';
var fileRemoved = false;
var dragStart = false;
var scroll = false;
var scrollTop = true;
var user_agent = navigator.userAgent.toLowerCase();

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 checkMail(s) {
	var a = false;
	var res = false;
	if(typeof(RegExp) == 'function') {
		var b = new RegExp('abc');
		if(b.test('abc') == true){a = true;}
	}

	if(a == true)	{
		reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+
								 '(\\@)([a-zA-Z0-9\\-\\.]+)'+
								 '(\\.)([a-zA-Z]{2,5})$');
		res = (reg.test(s));
	}
	else {
		res = (s.search('@') >= 1 &&
			s.lastIndexOf('.') > s.search('@') &&
			s.lastIndexOf('.') >= s.length-5)
	}
	return(res);
}

function pageOffset(win)
{
    if(!win) win = window;
    var pos = {left:0,top:0};

    if(typeof win.pageXOffset != 'undefined')
    {
         // Mozilla/Netscape
         pos.left = win.pageXOffset;
         pos.top = win.pageYOffset;
    }
    else
    {
         var obj = (win.document.compatMode && win.document.compatMode == "CSS1Compat") ?
         win.document.documentElement : win.document.body || null;

         pos.left = obj.scrollLeft;
         pos.top = obj.scrollTop;
    }
    return pos;
}

function getClientHeight() {

	if (window.innerWidth) { 
		h = window.innerHeight; 
		w = window.innerWidth; 
	} 	
	else if(document.body) {
		h = document.body.clientHeight;
		w = document.body.clientWidth;
		if (document.body.offsetHeight == h && document.documentElement && document.documentElement.clientHeight)  {
			h = document.documentElement.clientHeight;
			w = document.documentElement.clientWidth;
		}
	}	
	return Array(w,h);	
}

function rand( min, max ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Leslie Hoare
    // +   bugfixed by: Onno Marsman
    // *     example 1: rand(1, 1);
    // *     returns 1: 1
    var argc = arguments.length;
    if (argc == 0) {
        min = 0;
        max = 2147483647;
    } else if (argc == 1) {
        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function setCenter(obj) {
	hw = getClientHeight();
	
	pageH = hw[1]
	pageW = hw[0];
	
	if (pageW>1008)
		pageW = 1008;
		
	obj.style.display = 'block';	
	obj.style.top = ((pageH/2)-(parseInt(obj.offsetHeight)/2))+'px';
	obj.style.left = ((pageW/2)-(parseInt(obj.offsetWidth)/2))+'px';
	
}

	
function nav_mo(objImg){

	if (objImg.src.indexOf('_active.')>0 && objImg.parentNode.parentNode.className.indexOf('navSelected')<0 && (objImg.parentNode.parentNode.className.indexOf('selected')<0 || objImg.parentNode.parentNode.className.indexOf('unselected')>=0))
		objImg.src = objImg.src.replace(/_active\./,".");
	else if (objImg.parentNode.parentNode.className.indexOf('navSelected')<0 && (objImg.parentNode.parentNode.className.indexOf('selected')<0 || objImg.parentNode.parentNode.className.indexOf('unselected')>=0))
		objImg.src = objImg.src.replace(/\.gif/,"_active.gif");

	return true;
}
		
function imgMO(objImg){

	if (!objImg.src)	 
		objImg = objImg.getElementsByTagName('IMG')[0];

	if (objImg.src.indexOf('_01.')>0)
		objImg.src = objImg.src.replace(/_01\./,"_02.");
	else if (objImg.src.indexOf('_02.')>0)
		objImg.src = objImg.src.replace(/_02\./,"_01.");
	

	return true;
}

function imgTitle_mo(obj,over) {
	tmp = obj.getElementsByTagName('span')[0];
	if (tmp) 
		if (over)
			tmp.style.display='block';
		else
			tmp.style.display='none';	
}
	
	
function switchImages() {

	if ($('imageList') && $('imageList').childNodes.length>1) {
		setTimeout('nextImage()',1000);
		setInterval('nextImage()',4000);
	}
}


function nextImage() {
		
	for (i=0;i<$('imageList').childNodes.length;i++) {		
		if (i==(activeImage+1)) {
		
			//$('imageList').childNodes[i].appear();
			new Effect.Appear($('imageList').childNodes[i]);
			activeImage++;	
			break;
		}	
		else {
			if (i==(activeImage)) {
				
				//$('imageList').childNodes[i].fade();
				new Effect.Fade($('imageList').childNodes[i]);
				if ((i+1)==$('imageList').childNodes.length) {
					$('imageList').childNodes[0].appear();
						activeImage=0;						
				}					 
			}					
		}			
	}

}

function showStep(obj,step) {
	tmp = $$('li.step_1');
	for (i=0;i<tmp.length;i++) 
		tmp[i].style.display='none';		
	tmp = $$('div.step_2');
	for (i=0;i<tmp.length;i++) 
		tmp[i].style.display='none';
	
	tmp = $$('div.step_'+step);
	tmp2 = $$('li.step_'+step);
	tmp = tmp.concat(tmp2);
	for (i=0;i<tmp.length;i++) 
		tmp[i].style.display='block';
	
	for (i=0;i<obj.parentNode.childNodes.length;i++) 	
		obj.parentNode.childNodes[i].className = obj.parentNode.childNodes[i].className.replace(/nlTabActive/,'');
	obj.className = obj.className+' nlTabActive';

}
	
	
	
function clearField( obj ) {
	if (obj.value == obj.getAttribute('org')) {
		//oldValue = obj.name.replace(/cmt/,'');
		if (obj.getAttribute('name')=='password' && !Prototype.Browser.IE)
			obj.setAttribute('type','password');
		oldValue = obj.getAttribute('org');
		obj.value = "";  
		obj.focus();
	}
}  
 	
function clearFieldDefault( obj ) {
	if (!obj.cleared) {
		oldValue = obj.value;		
		obj.value = "";  
	}
} 

function resetField( obj ) {
  
	if (oldValue != "" && obj.value == "") {
		if (obj.getAttribute('name')=='password' && !Prototype.Browser.IE)
			obj.setAttribute('type','text');	
		obj.value = oldValue;
		obj.cleared = false;		
	}   
}  

 
function checkForm(frmId) {
	elems = $(frmId).getElements();
	errCnt = 0;
	error = '';
	for (i=0;i<elems.length;i++) {
		if (elems[i].className.indexOf('validate')>=0) {
		
			tmp = elems[i].getAttribute('label');
			
			if ($F(elems[i])=='' || $F(elems[i])==tmp) {
				
				error += 'Please fill in "'+tmp+'".'+"\n";
				errCnt++;
			
				elems[i].style.backgroundColor = '#ffdddd';			
			}
		}
		if (elems[i].className.indexOf('email')>=0 && ($F(elems[i])!='' && $F(elems[i])!='E-Mail')) {
			if (!checkMail($F(elems[i]))) {
				errCnt++;
				elems[i].style.backgroundColor = '#ffdddd';
				error += 'E-mail invalid.'+"\n";
			}
		}
	}
	return error;
}
 
 	function addEvent( obj, type, fn ) {
	  if ( obj.attachEvent ) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
		obj.attachEvent( 'on'+type, obj[type+fn] );
	  } else
		obj.addEventListener( type, fn, false );
	}	
	
	function resize() {
		
		var sizes = getClientHeight();	
    
		image_resize = $('image_resize_inner').getElementsByTagName('IMG')[0];
		imageWidth = parseInt(image_resize.getAttribute('width'),10);
		imageHeight = parseInt(image_resize.getAttribute('height'),10);
		widthDyn = 900-imageWidth;

		swidth = Math.max(sizes[0]-460,550);	
		$('page').style.width = swidth+'px';
		
		
		//$('image_resize_container').style.width = $('image_resize_container').parentNode.offsetWidth+'px';
		$('image_resize_container').style.width = swidth+'px';
		
		if (imageWidth>imageHeight) {
		
			var newWidth = imageWidth;
			var newHeight = imageHeight;
      if (newHeight>sizes[1]-230) {
        newHeight = Math.max(sizes[1]-230,340);
        newWidth = newHeight/imageHeight*imageWidth;
      }
      while (newWidth>swidth) {
				newWidth--;  
				newHeight = newWidth/imageWidth*imageHeight;        
      }      
      image_resize.style.width = newWidth+'px';	
      image_resize.style.height = newHeight+'px';	     
			/*if (imageWidth>swidth) {
		
				var newWidth = swidth;
				var newHeight = newWidth/imageWidth*imageHeight;
				if (newHeight>Math.max(sizes[1]-230,340)) {
					newHeight = Math.max(sizes[1]-230,340);
					var newWidth = newHeight/imageHeight*imageWidth;
				}
				image_resize.style.width = newWidth+'px';	
				image_resize.style.height = newHeight+'px';		
			
			}
			if (newHeight>sizes[1]-230) {
				if (newHeight>Math.max(sizes[1]-230,450)) {	
					var newHeight = Math.max(sizes[1]-230,450);
					var newWidth = newHeight/newHeight*imageWidth;
					
					image_resize.style.width = newWidth+'px';	
					image_resize.style.height = newHeight+'px';	
				}
			
			}	*/
      
			$('image_resize_inner').style.width = newWidth+'px';
			$('image_resize_inner').style.height = newHeight+'px';				
			$('image_resize_container').style.height = (Math.max(sizes[1]-230,340))+'px';

		}
		else {			
			if (sizes[1]<=(image_resize.offsetHeight+230) && sizes[1]>550 && (sizes[1]-230<imageHeight)) {
				var newHeight = (sizes[1]-230);
				var newWidth = newHeight/imageHeight*imageWidth;			
			}	
			else if (sizes[1]<(image_resize.offsetHeight+230) && sizes[1]<=550) {						
				var newHeight = 340;
				var newWidth = newHeight/imageHeight*imageWidth;		
			}	
			else if (sizes[1]>(image_resize.offsetHeight+230) && image_resize.offsetHeight<imageHeight && (sizes[1]-230<imageHeight)) {
				var newHeight = (sizes[1]-230);
				var newWidth = newHeight/imageHeight*imageWidth;			
			}			
			else {
				var newHeight = imageHeight;
				var newWidth = imageWidth;							
			}

			image_resize.style.height = newHeight+'px';
			image_resize.style.width = newWidth+'px';						
			$('image_resize_container').style.height = (Math.max(sizes[1]-220,360))+'px';	
			$('image_resize_inner').style.width = newWidth+'px';
			$('image_resize_inner').style.height = newHeight+'px';			
		}
		
		/*if (image_resize.getAttribute('src').indexOf('.jpg')>0) {	
			if (sizes[0]<=1000 && image_resize.getAttribute('src').indexOf('/small/')<0) 
				image_resize.setAttribute('src',image_resize.getAttribute('src').substr(0,image_resize.getAttribute('src').lastIndexOf('/'))+'/small'+image_resize.getAttribute('src').substr(image_resize.getAttribute('src').lastIndexOf('/')));	
			if (sizes[0]>=1050 && image_resize.getAttribute('src').indexOf('/small/')>0) 
				image_resize.setAttribute('src',image_resize.getAttribute('src').replace(/\/small\//,'/'));	
		}*/
		image_resize.style.visibility = 'visible';
	}
	function resize3() {
		
		var sizes = getClientHeight();	
		
		swidth = Math.max(sizes[0]-460,550);	
		$('page').style.width = swidth+'px';
		$('news_entry').style.width = $('news_entry').parentNode.offsetWidth+'px';
		
		imgObjArr = $$('.news_image img');
		
		for (i=0;i<imgObjArr.length;i++) {
			imageWidth = imgObjArr[i].getAttribute('width');
			imageHeight = imgObjArr[i].getAttribute('height');
		
			var newWidth = imageWidth;
			var newHeight = imageHeight;
			if (sizes[0]<1260  && sizes[0]>=1000) {
				if (imgObjArr[i].offsetWidth>=550) {
					var newWidth = (imageWidth-((1260-sizes[0])*((150-(700-imageWidth))/260)));
					var newHeight = newWidth/imageWidth*imageHeight;	
				}
			}	
			else if (sizes[0]<=1000  && imgObjArr[i].offsetWidth>=550) {
				var newWidth = 550;	  
				var newHeight = newWidth/imageWidth*imageHeight;	
			}	
			else if (sizes[0]>=1260  && imgObjArr[i].offsetWidth<700) {
				var newWidth = imageWidth;
				var newHeight = newWidth/imageWidth*imageHeight;	
			}	

			imgObjArr[i].style.width = newWidth+'px';	
			imgObjArr[i].style.height = newHeight+'px';
		}
	}
	
	function resize2() {
		var sizes = getClientHeight();	
		//if (sizes[0]<1260  && sizes[0]>=1000  && $('list_resize').offsetWidth>=500) 
		if (sizes[0]>=1000  && $('list_resize').offsetWidth>=500) 
			$('list_resize').setAttribute('style','width:'+(Math.round(760-((1260-sizes[0])*(200/200))))+'px');	
		if (sizes[0]>=1260  && $('list_resize').offsetWidth<760) 
			$('list_resize').setAttribute('style','width:760px');			
	}	
	addEvent(window, 'resize', function(event) {		
		if ($('image_resize_inner'))
			resize();
		else if ($('list_resize'))	
			resize2();
		else if ($('news_entry')) {		
			resize3();		
				
		}	
		else if ($('page')) {
			var sizes = getClientHeight();	

			$('page').style.width = Math.max(sizes[0]-460,550)+'px';
		}				
	});		
	
	if ($('image_resize_inner'))
		resize();
	else if ($('list_resize'))	
		resize2();
	else if ($('news_entry')) {		
		resize3();		
			
	}	
	else if ($('page')) {
		var sizes = getClientHeight();	
		swidth = Math.max(sizes[0]-460,550);	
		$('page').style.width = swidth+'px';
	}	
		
	if (!$('image_resize_inner') && !$('list_resize') && !$('news_entry') && $('page')) {
		var sizes = getClientHeight();	
		$('page').style.width = Math.max(sizes[0]-460,550)+'px';
	}	
	
	function commitDrop(dragged,cnt) {
								
		$('dropbox_5').update($('dropbox_4').innerHTML);
		$('dropbox_4').update($('dropbox_3').innerHTML);
		$('dropbox_3').update($('dropbox_2').innerHTML);
		$('dropbox_2').update($('dropbox_1').innerHTML);

		if (dragged.tagName=='IMG')
			var small = imgSrc = dragged.getAttribute('src');
		else {	
			imgSrc = dragged.getElementsByTagName('IMG')[0].getAttribute('src');
			var small = imgSrc.substr(0,imgSrc.lastIndexOf('/'))+'/thumb'+imgSrc.substr(imgSrc.lastIndexOf('/'));					
		}	
		var img = new Image();
		img.src = imgSrc;
		var orgWidth = img.width;
		var orgHeight = img.height;
		img.src = small;

		if (orgWidth>orgHeight) {
			var newHeight = 43;
			var newWidth = Math.round(newHeight/orgHeight*orgWidth);							
		}
		else {
			var newWidth = 53;
			var newHeight = Math.round(newWidth/orgWidth*orgHeight);			
		}

	
		if (newHeight>43) {
			img.width = newWidth;
			img.height = newHeight;
			img.setAttribute('style','position:absolute;top:-'+((newHeight-43)/2)+'px');
		}					
		else if (newWidth>53) {
			img.height = newHeight;
			img.width = newWidth;
			img.setAttribute('style','position:absolute;left:-'+((newWidth-53)/2)+'px');
		}	
		else {
			img.height = newHeight;
			img.width = newWidth;
		}				
		$('dropbox_1').update(img);	
		

    
		if (!$('lbCnt')) {
			var counter = document.createElement('span');		
			counter.setAttribute('id','lbCnt');
			var counterInner = document.createElement('span');
			counterInner.style.letterSpacing = '0px';
			var count = document.createTextNode(' / '+cnt);
			counterInner.appendChild(count);
			counter.appendChild(counterInner);
			$('lightbox_link').appendChild(counter);	
      
		}	
		else 
			$('lbCnt').update(' / <span style="letter-spacing:0px">'+cnt+'</span>');
		$('lightbox_link').addClassName('lightbox_wide');
		new Effect.Highlight('dropbox_empty',  { 
			startcolor: '#ade98d',
			endcolor: '#ffffff',
			duration:0.8
		});		
	}
	
	function commitDropRemove(dragged,req) {
		//dragged.parentNode.parentNode.removeChild(dragged.parentNode);		
		dragged.parentNode.removeChild(dragged);
	}
	
	function addDrop(remove) {
		if (!remove)
			var remove = false;
		
		Droppables.add('lightbox', {
			accept: 'lbDrag',
			hoverclass: 'dropHover',
			onDrop: function(dragged, dropped, event) {

				if (dragged.tagName=='IMG')
					params = 'id='+dragged.id.split('_')[1];
				else 	
					params = 'id='+dragged.getElementsByTagName('IMG')[0].id.split('_')[1];						
				if (remove) {
					params += '&remove=1';
					dragged.style.visibility='hidden';
				}
				$('dropbox_empty').style.backgroundImage = 'url(fileadmin/template/img/loading.gif)';
				new Ajax.Updater('', '/projects/hiepler/site/ajax/lightbox.php', {  
					method: 'post',
					parameters: params, 
					onSuccess:function(req) { 
						$('dropbox_empty').style.backgroundImage = 'none';
						if (parseInt(req.responseText)>0 && !remove) 
							commitDrop(dragged,req.responseText);
						else if (parseInt(req.responseText)>=0 && remove) 
							commitDropRemove(dragged,req.responseText);							
						else {
							new Effect.Highlight('dropbox_empty',  { 
								startcolor: '#ff9177',
								endcolor: '#ffffff',
								duration:0.8
							});
							if (remove)
								//dragged.parentNode.style.visibility='visible';
								dragged.style.visibility='visible';
						}
						
					}, 
					onFailure:show_failure, 
					onException:show_exception
				});

	
			}
		});	
	}
	
	function doSort(obj) {
					
	
		param = Sortable.serialize(obj);	
		new Ajax.Updater('', "/projects/hiepler/site/ajax/resort.php", {parameters: param, method:"post",evalScripts:true});		

	}
	
	
	function navKeys(event) {
		if (event.keyCode==37 && $('pagPrev')) {
			tmpPress = $$('#pagPrev a');
			tmpPress[0].addClassName('focus');
			window.location.href = tmpPress[0].getAttribute('href').replace(/&back/,'');		
		}
		else if (event.keyCode==39  && $('pagNext')) {
			tmpPress = $$('#pagNext a');
			tmpPress[0].addClassName('focus');
			window.location.href = tmpPress[0].getAttribute('href').replace(/&next/,'');		
		}
	}

    document.observe('keyup', navKeys);

	
  
if (navigator.appVersion.indexOf('MSIE 6')>0) {

  tmp1 = document.createTextNode('Sie benutzen den Internet Explorer 6, dieser stammt aus dem Jahr 2001 und wird nicht weiter unterstützt. Bitte verwenden Sie ');
  tmp2 = document.createTextNode('Mozilla Firefox');
  tmp3 = document.createTextNode(' oder ');
  tmp4 = document.createTextNode(' Internet Explorer 8 ');

  a1 = document.createElement('A');
  a1.setAttribute('href','http://www.getfirefox.com');
  a1.setAttribute('target','_blank');
  a1.appendChild(tmp2);
  
  a2 = document.createElement('A');
  a2.setAttribute('href','http://www.microsoft.com/windows/internet-explorer/default.aspx');
  a2.setAttribute('target','_blank');
  a2.appendChild(tmp4);
  
  
  div = document.createElement('DIV');
  div.setAttribute('id','ie6');
  div.appendChild(tmp1);
  div.appendChild(a1);
  div.appendChild(tmp3);
  div.appendChild(a2);

  document.getElementsByTagName('BODY')[0].insertBefore(div,document.getElementsByTagName('BODY')[0].firstChild);
}	
	
function show_exception( requestObject, ex) {
	if (Prototype.Browser.IE) {
		alert('Bitte aktivieren Sie "ActiveX-Steuerlemente" in ihren Browsereinstellungen.');
	}
	else
		alert("Keine AJAX-Anfrage an " + requestObject.url + " moeglich: " + ex);
}

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


var c_canvas = $("x");
if (c_canvas) {
	if (typeof G_vmlCanvasManager != 'undefined')
		c_canvas = G_vmlCanvasManager.initElement(c_canvas);
	var context = c_canvas.getContext("2d");

	context.strokeStyle = '#aaa'; 
	context.lineWidth   = 0.5;

	context.moveTo(3, 3);
	context.lineTo(50, 40);

	context.lineWidth   = 1;
	context.moveTo(3, 40);
	context.lineTo(50, 3);
	context.stroke();
	context.stroke();
}

function isTouchDevice(){
	try{
		document.createEvent("TouchEvent");
		return true;
	}catch(e){
		return false;
	}
}	


ico = new Image();
ico.src = '/fileadmin/template/img/pfeil_05.ico';


Event.observe(window, 'load', function() {
	
  if (isTouchDevice()) {
	$$('body')[0].addClassName('touch');
	//$$('body')[0].setStyle({height: Math.max($('page_container').getHeight(),($('page').getHeight()+105))+'px'});
	//$('nav_footer').setStyle({top:(-100)+'px'});
	if ($('image_resize_description'))  {
		aTL = document.createElement('div');
		aTL.setAttribute('id','addToLightbox');
		aTL.onclick = function() {

			dragged = $(this).up('DIV#image_resize_inner').down('IMG');
			
			if (dragged.tagName=='IMG')
				params = 'id='+dragged.id.split('_')[1];
			else 	
				params = 'id='+dragged.getElementsByTagName('IMG')[0].id.split('_')[1];						
	
			$('dropbox_empty').style.backgroundImage = 'url(fileadmin/template/img/loading.gif)';
			new Ajax.Updater('', '/projects/hiepler/site/ajax/lightbox.php', {  
				method: 'post',
				parameters: params, 
				onSuccess:function(req) { 
				
					$('outer_container').removeClassName('lbActive');
					tmpLinkLB = $('lightbox_link');
					tmpLinkLB.active = false;					
					tmpLinkLB.onclick();
					
					$('dropbox_empty').style.backgroundImage = 'none';
					if (parseInt(req.responseText)>0) 
						commitDrop(dragged,req.responseText);					
					else {
						new Effect.Highlight('dropbox_empty',  { 
							startcolor: '#ff9177',
							endcolor: '#ffffff',
							duration:0.8
						});

					}					
				}, 
				onFailure:show_failure, 
				onException:show_exception
			});		
		

		}
		aTL.update('add to lightbox');
		
		new Insertion.Bottom('image_resize_description', aTL);
	}
  }	

});
Event.observe(window, 'scroll', function() {
	if (isTouchDevice()) {
		hw = getClientHeight();
		tmpScroll = pageOffset(window);
		$('nav_footer').setStyle({top:(hw[1]+tmpScroll.top-130)+'px'});
	}
});


