/*28.08.2008 // 13:34*/
var isIE=/MSIE (5\.5|\d+)/.test(navigator.userAgent);
var trans=!/MSIE (5\.5|6)/.test(navigator.userAgent);

function domID(id) {
	return document.getElementById(id);
}
function domCE(elm) {
	return document.createElement(elm);
}
function byTAG(e,tag) {
	if (!e) return;
	return e.getElementsByTagName(tag);
}
function debug(str) {
	String(window.location.search).indexOf('debug')>-1 ? alert(str) : null;
}

var metaColor="#FFFFFF";
var newMetaColor=metaColor;
var currentMainMenuItem;

function preloadIcon(e) {
	e.onload=null;
	if(!e.originalImage) (e.originalImage=e.originalSrc ? e.originalSrc : e.src);
	e.overImage=new Image();
	e.overImage.src=returnColoredURL(e.originalImage);
}

function iconRollOut(e) {
	e.oiv=setTimeout(function() {iconRollOver(e,false)},1);
}
function iconRollOver(e,over) {
	if(e.oiv) clearTimeout(e.oiv);
	if (e.tagName.toLowerCase() !== 'img') e=byTAG(e,'img')[0];
	if (!e.originalImage) (e.originalImage=e.originalSrc ? e.originalSrc : e.src);
	e.src=over ? returnColoredURL(e.originalImage) : e.originalImage;
}

function logoRollOver(e,over) {
	e.src=over ? "fileadmin/templates/grafikelemente/logoNRWKultur.png" : "fileadmin/templates/grafikelemente/logoNRWKultur_BW.png";
}

function showIconText(e,over) {
	var t=byTAG(e.parentNode.parentNode,'div');
	if (t.length) {
		t[0].style.visibility=over ? 'visible' : 'hidden';
		t[0].style.color=metaColor;
	}
}

function returnColoredURL(orgURL,debug) {
	debug ? alert(orgURL) : null;
	return 'fileadmin/templates/php/colorize.php?theImage='+orgURL+'&color='+metaColor.split("#")[1];
}

function execFunctions() {
	var a=arguments;
	for (var i=0;i<a.length;i++) {
		var f=a[i].shift();
		thisObj=this;
		if(typeof(f)=='string'){
			var objArray=f.split(".");
			if(objArray.length>1){
				thisObj=eval(objArray.slice(0,-1).join('.'));
				f=eval(f);
			}
		}
		f.apply(thisObj,a[i]);
	}
}

function hideRoundLayer(e,layerName) {
	var l=domID(layerName);
	l.otv=setTimeout(function() {showRoundLayer(e,layerName,false)},1);
}
function showRoundLayer(e,layerName,over,changed) {
	var l=domID(layerName);
	if(l.otv) clearTimeout(l.otv);
	if(over) l.style.visibility="visible";
	if(!over && !trans) l.style.visibility="hidden";
	var f=domID('bigFocus');
	if(over && !isIE) f.style.visibility="visible";
	
	var off;
	var currentNode;
	if(over && (!l.over || changed)) {
		l['wasOver']=true;
		var mName;
		var tName;
		if(layerName=='langLayer') {
			mName='langMenu';
			tName='SPAN'
		}else if(layerName=='sizeLayer'){
			mName='sizeMenu';
			tName='A'
		}else{
			mName='searchMenu';
			tName='DIV'
		}
		var aNodes=byTAG(l,tName);
		var newWidth=0;
		domID(mName).style.width="1000px";
		newWidth=aNodes[aNodes.length-1].offsetWidth+aNodes[aNodes.length-1].offsetLeft-aNodes[0].offsetLeft;
		domID(mName).style.width=newWidth+"px";
	}
	
	if((!l.originalWidth || l.originalWidth!=l.offsetWidth) && over) {
		var realOffsetLeft=e.parentNode.offsetLeft+e.offsetLeft;
		l.originalWidth=l.offsetWidth;
		var xMax=1200-realOffsetLeft;
		l.style.left=Math.min(xMax-l.offsetWidth,0)+"px";
		var d=byTAG(l,'div');
		var m=false;
		for (var i=0;i<d.length;i++) {
			if(d[i].className == "RRmarker") {
				m=d[i];
				break;
			}
		}
		if (m) m.style.left=(-parseInt(l.style.left)+25-m.offsetWidth/2)+"px";
	}
	if(over) {
		if(!isIE){
			f.style.left=(e.offsetLeft+parseInt(l.style.left)+l.offsetWidth/2)+"px";
			f.style.top=(l.offsetTop+l.offsetHeight/2)+"px";
			f.style.width=(1200-f.parentNode.offsetLeft-f.offsetLeft)+"px";
		}
		if(layerName=='searchLayer') domID('tx_indexedsearch[sword]').focus();
	}else{
		if(layerName=='searchLayer') domID('tx_indexedsearch[sword]').blur();
	}
	if(trans) {
		startFade(l,over ? 100 : 0);
	}else{
		l.style.filter='';
	}
	if(!isIE) {
		if (over) {
			l.fto=setTimeout(function() {startFade(f,100)},1);
		} else {
			if(l.fto) clearTimeout(l.fto);
			startFade(f,0);
		}
	}
	l.over=over;
};



function setRoundLayerTo(l,a) { // layer, align
	if(!a) a='c';
	var realOffsetLeft;
	var rightPadding;
	var e=domID('currentDeepNavigation');
	if (!e) {
		e=domID('currentSubNavigation');
		rightPadding=30;
		realOffsetLeft=e.parentNode.offsetLeft+e.offsetLeft;
	} else {
		rightPadding=16;
		realOffsetLeft=e.parentNode.parentNode.offsetLeft+e.parentNode.offsetLeft+e.offsetLeft;
	}
	if (!e) return false;

	var eWidth=(e.offsetWidth-rightPadding);
	l.originalWidth=l.offsetWidth;
	if(a=='c') {
		l.style.left=Math.max(0,realOffsetLeft-l.originalWidth/2+eWidth/2)+"px";
	} else {
		l.style.left=Math.max(0,realOffsetLeft+eWidth+20)+"px";
	}

	var d=byTAG(l,'div');
	var m=false;
	for (var i=0;i<d.length;i++) {
		if(d[i].className == "RRmarker") {
			m=d[i];
			break;
		}
	}
	if (m) m.style.left=(realOffsetLeft-parseInt(l.style.left)+eWidth/2)+"px";
}

function hideEventImageLayer(e) {
	var l=domID('eventImageLayer');
	l.keepDirection=1;
	l.otv=setTimeout(function() {showEventImageLayer(e,false)},1);
}

function showEventImageLayer(e,over,newImage) {
	if(over && newImage.split('\/').pop()=='') return;
	var l=domID('eventImageLayer');
	var i=domID('eventImage');
	if(l.otv) clearTimeout(l.otv);
	if(over) {
		l.keepDirection=0;
		l.style.visibility="visible";
		i.src=newImage;
		document.onmousemove=moveEventImageLayer;
	} else {
		document.onmousemove=null;
	}
	if(!over && !trans) l.style.visibility="hidden";
	//var f=domID('bigFocus');
	//if(over && !isIE) f.style.visibility="visible";

	if(over) {
		if(!isIE){
			//f.style.left=(e.offsetLeft+parseInt(l.style.left)+l.offsetWidth/2)+"px";
			//f.style.top=(l.offsetTop+l.offsetHeight/2)+"px";
			//f.style.width=(1200-f.parentNode.offsetLeft-f.offsetLeft)+"px";
		}
	}
	if(trans) {
		startFade(l,over ? 100 : 0);
	}else{
		l.style.filter='';
	}
	l.over=over;
};


function moveEventImageLayer(e) {
	var l=domID('eventImageLayer');
	var m=domID('verticalMarkerBar');
	var p=getMousePos(e);
	var newX=p[0]-domID('contentDiv').parentNode.offsetLeft+70;
	if(newX+l.offsetWidth<1200 || (l.keepDirection && l.direction==1)) {
		l.direction=1;
		m.style.backgroundImage = 'url(fileadmin/templates/grafikelemente/roundRectLeftMarker.png)';
		m.style.left = -m.offsetWidth+"px";
		l.style.left = newX+2+"px";
	} else {
		l.direction=-1;
		m.style.backgroundImage = 'url(fileadmin/templates/grafikelemente/roundRectRightMarker.png)';
		m.style.left = l.offsetWidth+"px";
		l.style.left = p[0]-domID('contentDiv').parentNode.offsetLeft-l.offsetWidth-70-2+"px";
	}
	l.style.top = p[1]+domID('mainDiv').scrollTop-l.offsetHeight/2+2+"px";
}



var galleryManager=new Object();
function setupGallery(e,t) {
	e.onload=null;
	matrixRollOver(domID(t),true,false);
	galleryManager.current=domID(t);
}

function matrixRollOver(e,over,force) {
	if (galleryManager.current!=e || force){
		String(e.alpha) == 'undefined' ? e.alpha = 50 : null;
		trans ?	startFade(e,over ? 100 : 50) : l.style.filter='';
	}
};

function matrixClickThumb(e,t,i) {
	domID(t).src=i;
	if (galleryManager.current && galleryManager.current!=e) matrixRollOver(galleryManager.current,false,true);
	galleryManager.current=e;
}

function galleryScrollTo(e,d,w) {
	if(String(e.cid) == "undefined") e.cid=0;
	e.cid+=d;
	e.x=parseFloat(e.style.left);
	e.newX=-w*e.cid;
	clearInterval(e.apv);
	e.apv = setInterval(function(){animateProperty(e,"left","px","x","newX","apv",.2)},20);
}


window.onDomReady=initReady;

// Initialize event depending on browser
function initReady(fn) {
	//W3C-compliant browser
	if(document.addEventListener) {
		document.addEventListener("DOMContentLoaded",fn,false);
	} else {
		document.onreadystatechange=function() {
			readyState(fn);
		}
	}
}

//IE execute function
function readyState(func) {
	if (document.readyState == "complete") {
		func();
	}
}

window.onDomReady(onLoadInit);

function onLoadInit() {
	try{extInterfaceInit()}catch(e){};
	try{initFooterIcons()}catch(e){};
	try{initResizer()}catch(e){};
	try{initMarker()}catch(e){};
	//initSlideMenu();
	try{initSitemap()}catch(e){};
	//try{lockImages()}catch(e){};
	try{checkBackground()}catch(e){};
	try{initImageSlide()}catch(e){};
	try{initNews()}catch(e){};
	
	$('#newsList').infinitescroll({
	  navSelector  : "#newsPageBrowser",            
					 // selector for the paged navigation (it will be hidden)
	  nextSelector : "#newsPageBrowser p:last a:first",    
					 // selector for the NEXT link (to page 2)
	  itemSelector : "#newsList div.akkordeonEntry",          
					 // selector for all items you'll retrieve
	  debug        : true,                        
					 // enable debug messaging ( to console.log )
	  loadingImg   : "",          
					 // loading image.
					 // default: "http://www.infinite-scroll.com/loading.gif"
	  loadingText  : " ",      
					 // text accompanying loading image
					 // default: "<em>Loading the next set of posts...</em>"
	  animate      : false,      
					 // boolean, if the page will do an animated scroll when new content loads
					 // default: false
	  extraScrollPx: 40,      
					 // number of additonal pixels that the page will scroll 
					 // (in addition to the height of the loading div)
					 // animate must be true for this to matter
					 // default: 150
	  donetext     : "I think we've hit the end, Jim" ,
					 // text displayed when all items have been retrieved
					 // default: "<em>Congratulations, you've reached the end of the internet.</em>"
	  bufferPx     : 0,
					 // increase this number if you want infscroll to fire quicker
					 // (a high number means a user will not see the loading message)
					 // new in 1.2
					 // default: 40
	  errorCallback: function(){},
					 // called when a requested page 404's or when there is no more content
					 // new in 1.2                   
	  localMode    : false
					 // enable an overflow:auto box to have the same functionality
					 // demo: http://paulirish.com/demo/infscr
					 // instead of watching the entire window scrolling the element this plugin
					 //   was called on will be watched
					 // new in 1.2
					 // default: false
					},function(){
					 // optional callback when new content is successfully loaded in.
					 // keyword 'this' will refer to the new DOM content that was just added.
	});
	
	$('.tx-indexedsearch-res').infinitescroll({
	  navSelector  : "div.tx-indexedsearch-browsebox",
	  nextSelector : ".browsebox li:last a:last",    
	  itemSelector : ".tx-indexedsearch-res div.akkordeonEntry",          
	  debug        : true,                        
	  loadingImg   : "",          
	  loadingText  : " ",      
	  animate      : false,      
	  extraScrollPx: 40,      
	  donetext     : "I think we've hit the end, Jim" ,
	  bufferPx     : 0,
	  errorCallback: function(){},                 
	  localMode    : false
					},function(){
	});
}

function toDoubleDigit(str) {
	return String(str).length<2 ? "0"+String(str) : String(str);
}

function backgroundLoaded() {
	var bDiv=domID('backImageDiv');
	var bImg=domID('backImg');
	bImg.orgWdt=bImg.width;
	bImg.orgHgt=bImg.height;
	if (window.onresize) window.onresize();
}

function checkBackground() {
	var bDiv=domID('backImageDiv');
	if (bDiv.style.visibility == 'hidden' && window.onresize) {
		var bImg=domID('backImg');
		bImg.orgWdt=bImg.width;
		bImg.orgHgt=bImg.height;
		window.onresize();
	}
}

function setCurrentMainNavigation(e) {
	e.id='currentMainNaviagtion';
	e.blur();
	currentMainMenuItem=e;
	var tempColor=getCSSRule('.'+currentMainMenuItem.className).style.color;
	
	if (tempColor.indexOf("#") == 0) {
		newMetaColor=tempColor;
	} else {
		var colorArray=tempColor.replace(/[^\d,]/ig,"").split(",");
		colorArray[0]=toDoubleDigit(Number(colorArray[0]).toString(16));
		colorArray[1]=toDoubleDigit(Number(colorArray[1]).toString(16));
		colorArray[2]=toDoubleDigit(Number(colorArray[2]).toString(16));
		newMetaColor="#"+colorArray.join("");
	}
	checkCurrentMainNavigation();
}


function lockImages() {
	var imgs=byTAG(document,'IMG');
	for (var i=0;i<imgs.length;i++) {
		if (imgs[i].src.indexOf('jpg') > 0) {
			imgs[i].oncontextmenu=function(){return false};	
		}
	}
}

if (!isIE) document.captureEvents(Event.MOUSEMOVE);
function getMousePos(e) {
	var m=[];
	if (isIE) {
		m=[event.clientX+document.body.scrollLeft,event.clientY+document.body.scrollTop];
	} else {
		m=[Math.max(0,e.pageX),Math.max(0,e.pageY)];
	}
	return m;
}




function setupContMenu(s,c) { //slide, current	
	if (c) {
		var maxHeight=0;
		s.offsets=new Array();
		var cur=c;
		var counter=0;
	
		s.links=byTAG(s,'A');
		while(cur!=s && cur) {
			maxHeight=Math.max(cur.offsetHeight,maxHeight);
			s.offsets.push(cur.offsetLeft);
			cur=cur.parentNode;
			counter++;
		}
		s.counter=counter;
		var newID=Math.max(0,counter-1);
		s.off=s.offsets.shift();
		
		maxHeight=Math.max(s.offsetHeight,maxHeight);
		domID('contentMenu').style.height=maxHeight+"px";
		domID('contentMenu').style.overflow="hidden";
		
		s.cid=0;
		if(window.location.hash.indexOf('#')==0){
			var tempCID=Number(window.location.hash.split('#').pop());
			if(!isNaN(tempCID)) s.cid=tempCID;
		}
		
		for(var l=0;l<s.links.length;l++){
			s.links[l].href+='#'+s.cid;
		}
		
		var startOffset=s.cid*s.off;
		s.style.left=(-startOffset)+"px";
		setTimeout(function(){contMenuScrollTo(s,newID-s.cid)},500);
	}
}

function contMenuScrollTo(e,d) {
	e.cid+=d;
	e.cid=Math.max(0,e.cid);
	
	domID('contMenuLeftArrow').style.visibility=e.cid>0 ? 'visible' : 'hidden';
	domID('contMenuRightArrow').style.visibility=e.cid<e.counter-1 && e.counter>1 ? 'visible' : 'hidden';
	
	window.location.hash=e.cid;
	for(var l=0;l<e.links.length;l++){
		e.links[l].href=e.links[l].href.split('#').shift()+'#'+e.cid;
	}
	e.x=parseFloat(e.style.left);
	e.newX=-e.cid*e.off;
	clearInterval(e.apv);
	e.apv = setInterval(function(){animateProperty(e,"left","px","x","newX","apv",.15)},20);
}