var isIE=/MSIE (5\.5|\d+)/.test(navigator.userAgent);
var trans=!/MSIE (5\.5|6)/.test(navigator.userAgent);
var $j = jQuery.noConflict();


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;
}

function hideDownloadPreview(e) {
	var l=domID('downloadPreviewImageLayer');
	l.keepDirection=1;
	l.otv=setTimeout(function() {showDownloadPreview(e,false)},1);
}

function showDownloadPreview(e,over,newImage) {
	if(over && newImage.split('\/').pop()=='') return;
	var l=domID('downloadPreviewImageLayer');
	var i=domID('downloadPreviewImage');
	if(l.otv) clearTimeout(l.otv);
	if(over) {
		l.invoker=e;
		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('downloadPreviewImageLayer');
	//var m=domID('verticalMarkerBar');
	var p=getMousePos(e);
	var newX=p[0];
	//m.style.backgroundImage = 'url(fileadmin/templates/grafikelemente/roundRectLeftMarker.png)';
	//m.style.left = -m.offsetWidth+"px";
	
	//l.style.left = newX-l.parentNode.parentNode.offsetLeft+20+"px";
	//l.style.top = p[1]+domID('contentDiv').scrollTop-l.parentNode.parentNode.parentNode.offsetTop+2+"px";
	
	l.style.left = l.invoker.parentNode.offsetLeft-134+"px";
	l.style.top = Math.min(l.invoker.offsetTop+2, l.invoker.parentNode.offsetHeight-122)+"px";
}

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;
}

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);


$j(document).ready(function() {
	try{initResizer()}catch(e){};
	try{initNews()}catch(e){};
	try{checkBackground()}catch(e){};

	$j('#logoRandom').crossSlide({ sleep:7, fade:2, shuffle:true },
	   [{ src: 'fileadmin/templates/sceneLogos/01.png' },
		{ src: 'fileadmin/templates/sceneLogos/02.png' },
		{ src: 'fileadmin/templates/sceneLogos/03.png' },
		{ src: 'fileadmin/templates/sceneLogos/04.png' },
		{ src: 'fileadmin/templates/sceneLogos/05.png' },
		{ src: 'fileadmin/templates/sceneLogos/06.png' },
		{ src: 'fileadmin/templates/sceneLogos/07.png' },
		{ src: 'fileadmin/templates/sceneLogos/08.png' },
		{ src: 'fileadmin/templates/sceneLogos/09.png' },
		{ src: 'fileadmin/templates/sceneLogos/10.png' },
		{ src: 'fileadmin/templates/sceneLogos/11.png' },
		{ src: 'fileadmin/templates/sceneLogos/12.png' },
		{ src: 'fileadmin/templates/sceneLogos/13.png' },
		{ src: 'fileadmin/templates/sceneLogos/14.png' },
		{ src: 'fileadmin/templates/sceneLogos/15.png' },
		{ src: 'fileadmin/templates/sceneLogos/16.png' },
		{ src: 'fileadmin/templates/sceneLogos/17.png' },
		{ src: 'fileadmin/templates/sceneLogos/18.png' },
		{ src: 'fileadmin/templates/sceneLogos/19.png' },
		{ src: 'fileadmin/templates/sceneLogos/20.png' }]);
	
	$j('#Meta_KKLogoWort').animate({opacity: "hide"}, "fast");
	$j("#Meta_KKLogo").hover(
		function(){ $j('#Meta_KKLogoWort').animate({opacity: "show"}, "normal"); }, function(){ $j('#Meta_KKLogoWort').animate({opacity: "hide"}, "normal"); }
	);
	$j('#Meta_GoetheLogoWort').animate({opacity: "hide"}, "fast");
	$j("#Meta_GoetheLogo").hover(
		function(){ $j('#Meta_GoetheLogoWort').animate({opacity: "show"}, "normal"); }, function(){ $j('#Meta_GoetheLogoWort').animate({opacity: "hide"}, "normal"); }
	);
	
	var randomLogo = Math.round(Math.random());
	randomLogo==1 ? $j('#Meta_GoetheLogo').show() : $j('#Meta_KKLogo').show();
	
	$j('.tx-indexedsearch-res').infinitescroll({
	  navSelector  : "ul.browsebox",
	  nextSelector : ".browsebox li:last a:last",    
	  itemSelector : ".tx-indexedsearch-res div.shortImageBox",          
	  debug        : false,                        
	  loadingImg   : "",          
	  loadingText  : " ",      
	  animate      : false,      
	  extraScrollPx: 40,      
	  donetext     : "I think we've hit the end, Jim" ,
	  bufferPx     : 20,
	  errorCallback: function(){},                 
	  localMode    : false
					},function(){
	});
	
	$j('#blogPosts').infinitescroll({
	  navSelector  : "#blogPageBrowser .blogNavigation",
	  nextSelector : ".blogNavigation a",    
	  itemSelector : "#blogPosts div.startBox",          
	  debug        : false,                        
	  loadingImg   : "",          
	  loadingText  : " ",      
	  animate      : false,      
	  extraScrollPx: 40,      
	  donetext     : "I think we've hit the end, Jim" ,
	  bufferPx     : 20,
	  errorCallback: function(){},                 
	  localMode    : false
					},
	function(){
		setupMoreText();
	});
	
	setTimeout('setupMoreText()', 100);

	$j('h1:empty').each(function() { $j(this).remove(); });
	$j('h2:empty').each(function() { $j(this).remove(); });
	$j('h3:empty').each(function() { $j(this).remove(); });
	$j('h4:empty').each(function() { $j(this).remove(); });
	$j('h5:empty').each(function() { $j(this).remove(); });
	$j('h6:empty').each(function() { $j(this).remove(); });
	$j('div.footerText:empty').each(function() { $j(this).remove(); });
});


function onLoadInit() {


}

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 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};	
		}
	}
}

/* prototype.js */
if (!document.myGetElementsByClassName) {
	document.myGetElementsByClassName = function(className) {
		var children = document.getElementsByTagName('*') || document.all;
		var elements = new Array();
	  
		for (var i = 0; i < children.length; i++) {
			var child = children[i];
			var classNames = child.className.split(' ');
			for (var j = 0; j < classNames.length; j++) {
				if (classNames[j] == className) {
					elements.push(child);
					break;
				}
			}
		}
		return elements;
	}
}

function setupMoreText() {
	var anchors = document.myGetElementsByClassName('fillTextDummyImage');
	for (var a=0;a<anchors.length;a++) {
		try {
			var theAnchor = anchors[a].parentNode;
			var theParent = theAnchor.parentNode.parentNode;
			var theWidth = theParent.offsetWidth;
			var textDiv = theAnchor.parentNode.nextSibling;
			var underlineDiv = textDiv.lastChild;

			if (!underlineDiv) {
				continue;
			}
			underlineDiv.style.width = (textDiv.offsetLeft-theAnchor.offsetLeft)+"px";
			textDiv.style.marginTop = '-20px';
			textDiv.style.zIndex = 100;
		} catch(e) {
			
		}
		anchors[a].className = 'fillTextDummyImageDone';
	}
}
