/*################################################################################
	D E B U G
################################################################################*/

var debugModus = 0	; // Debugmodus EIN = 1 || AUS = 0

function debug(txt)
{
	if(!debugModus || typeof(console) != "object") { return; }
	else { console.log(txt); }
}
debug("debugModus = true");


/*################################################################################
	$$$
################################################################################*/

// get Elements by Name

function $$$(obj)
{
	var obj = document.getElementsByName(obj);
	obj = (obj.length==1)?obj[0]:obj;
	return obj;
}


/*################################################################################
	F U N C T I O N S
################################################################################*/

function checkBrowserName(name){
	agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf(name.toLowerCase())>-1) {
		return true;
	}
	return false;
}


function mainNaviPosition() {
	var xPos = $('borderTop').getPosition().x;
	
	$('BIG_mainNavi').setStyles({
		'left': xPos+25,
		'visibility' : 'visible'
	});
}

// ## scaleBlackBoxes -------------------------------------------------------------

function scaleBlackBoxes() {
	$$('.blackBoxes').each(function(blackBox) {
		var targetWidth = blackBox.getSize().x-24;
		var targetHeight = blackBox.getElement('div[class=blackBoxesCenter]').getSize().y;
		
		blackBox.getElement('div[class*=blackTitle]').setStyle("width", targetWidth);
		blackBox.getElement('div[class*=blackBoxesCenter]').setStyle("width", targetWidth);
		blackBox.getElement('div[class*=blackFooter]').setStyle("width", targetWidth);
		blackBox.getElement('div[class*=blackBoxesLeft]').setStyle("height", targetHeight);
		blackBox.getElement('div[class*=blackBoxesRight]').setStyle("height", targetHeight);
	});
}

// ## setOpacity -------------------------------------------------------------

function setOpacity() {
	$('memberDropContent').set("opacity", 0);
}

// ## stretchMainPage -------------------------------------------------------------

function stretchMainPage() {
	if($('searchResult') === null && ($$('.cmt') === null || $$('.cmt').length === 0)) {
		var rightSlotHeight = $('rightSlot').getSize().y;
		var contentHeight = $('BIG_mainContent').getSize().y;
		
		/* if(contentHeight < 445 && ($('big_videoPlayer') === null)) {
			if($('big_top5voting') === null) { $('BIG_mainContent').setStyle('height', 445); }
		} else */ if(rightSlotHeight > contentHeight && ($('big_videoPlayer') === null) ) {
			$('BIG_mainContent').setStyle('height', rightSlotHeight+50);
		}
	}
}

// ## showNavigation --------------------------------------------------------

function showNavigation(naviId,layerId) {
	
	$('big_searchLayer').setStyle("display","none");
	activeZone = $(layerId).getElement('div[class*=BIG_naviBody]');
	
	var layerPosition = $(naviId).getPosition().x-15;
	var navList = $$('.BIG_firstLevel');
	var navPunkte = navList[0].getElements('a');
	var title = $(layerId).getElement('h3');
	var killzone = $('serviceNavigation');
	var killzone2 = $(layerId).getElement('img[class*=hn_TL]');
	
	navPunkte.each(function(el) {
		el.setStyle('visibility', 'visible');			    
	});
	
	$$('.BIG_navigation').setStyle('display','none');
	$(naviId).setStyle('visibility','hidden');
	$(layerId).setStyles({
		'left' : layerPosition,
		'display' : 'block'
	});
	
	var targetWidth = 357 - ($(layerId).getElement('h3').getSize().x + 36);
	$(layerId).getElement('div[class*=spacer]').setStyle('width', targetWidth);
	
	killzone.addEvent("mouseenter", function() { setTimeout(function() { hideNavi(layerId,naviId) },350); });
	killzone2.addEvent("mouseenter", function() { setTimeout(function() { hideNavi(layerId,naviId) },350); });
	
	activeZone.addEvent("mouseleave", function() {
		//activeZone.removeClass("ACTIVE");
		setTimeout(function() {hideNavi(layerId,naviId)},350);
		
	});	
}

function hideNavi(layer,navi) {
	$('BIG_thirdLevelDIV_'+layer).setStyle('display', 'none');
	$$('.BIG_mainNavi').removeClass('Over');
	$(layer).setStyle('display', 'none'); 
	$(navi).setStyle('visibility','visible');
}

// ## showThirdLevel --------------------------------------------------------

function showThirdLevel(secondLevelId) {
	var firstLevelId = $(secondLevelId).getParent('div[class*=BIG_navigation]').id;
	var thirdLevelDiv = 'BIG_thirdLevelDIV_'+firstLevelId;
	//console.log(thirdLevelDiv);
	$$('.BIG_mainNavi').removeClass('Over');
	$(secondLevelId).addClass('Over');
//	var targetPositionX = $(secondLevelId).getPosition(activeZone).x +165;
//	var targetPositionY = $(secondLevelId).getPosition(activeZone).y +59;
	
	if(checkBrowserName('MSIE 7')){
		var targetPositionX = $(secondLevelId).getPosition(activeZone).x +162;
		var targetPositionY = $(secondLevelId).getPosition(activeZone).y +68;
	} else {
		var targetPositionX = $(secondLevelId).getPosition(activeZone).x +62;
		var targetPositionY = $(secondLevelId).getPosition(activeZone).y +82;
	}
	$$('.BIG_thirdLevelContent').setStyle('display', 'none');
	
	if($(thirdLevelDiv).getStyle('display') == 'block' && $(secondLevelId + 'ThirdLevelContent') != null) {
		$(thirdLevelDiv).tween("top", targetPositionY);
		$(secondLevelId + 'ThirdLevelContent').setStyle('display', 'block');
	} else if($(secondLevelId + 'ThirdLevelContent') != null) {
		$(thirdLevelDiv).setStyles({
			'left' : targetPositionX,
			'top' : targetPositionY,
			'display' : 'block'
		});
		//console.log(secondLevelId + 'ThirdLevelContent none');
		$(secondLevelId + 'ThirdLevelContent').setStyle('display', 'block');
	} else {
		$(thirdLevelDiv).setStyle('display','none')
	}
}

// ## showThirdLevel --------------------------------------------------------

function killThirdLevel(id) {
	$$('.BIG_3rdLevel').setStyle('display', 'none')	
}

// ## showLogin -------------------------------------------------------------

function showLoginMorph() {
	var currentPosition = $('memberDropDiv').getPosition().y;
	
	var myEffect = new Fx.Morph($('memberDropDiv'), { 
		duration: 400, 
		transition: 'back:in',
		onStart: function(){
			$('member').tween("color", (currentPosition < -3) ? "#000000" : "#FFFFFF")	
			if(currentPosition > -5) {
				$('memberDropContent').tween("opacity", 0)
			}
		},
		onComplete: function() {
			if(currentPosition < -5) {
				$('memberDropContent').tween("opacity", 1)
			}
		}
	});
		myEffect.start({
		'top' : (currentPosition < -3) ? -3 : -82
	});
}


// ## scaleTeasers -------------------------------------------------------------

function scaleTeasers() {
	var getAllScalers = $('mainPage').getElements('div[class*=scale]');
	var scalerRows = (Math.ceil(getAllScalers.length/2));
	var LSscalers	= ($('BIG_leftSlot').getElements('div[class*=scale]'));
	var RSscalers	= ($('BIG_rightSlot').getElements('div[class*=scale]'));
	
//	alert(scalerRows);
//	alert("links "+LSscalers.length);
//	alert("rechts "+RSscalers.length);
//	alert("links 0" + LSscalers[0].getSize().y);
//	alert("links 1" + LSscalers[1].getSize().y);
	
//	alert("rechts 0" + RSscalers[0].getSize().y);
//	alert("rechts 1" + RSscalers[1].getSize().y);
	for(i=0;i<scalerRows;i++) {
//		alert("Zeile" + i);
		if(RSscalers[i] != null) {
		 
			if(LSscalers[i].getSize().y > RSscalers[i].getSize().y) {
			    //alert("rechts" + i + " wird " + LSscalers[i].getSize().y + "px hoch");
				  RSscalers[i].style.height = LSscalers[i].getSize().y + "px";
			} 
      
      if(RSscalers[i].getSize().y > LSscalers[i].getSize().y) {
			 // alert("links " + i +  "wird " + RSscalers[i].getSize().y + "px hoch");
			
				LSscalers[i].style.height = RSscalers[i].getSize().y + "px";
			}
		}
	
	}
}


// ## QuickNavigation -------------------------------------------------------------

/*
function quickNavigation() {
	$$('.BIG_quickNav').each(function(qn) { 
		var qnId = qn.id;
		var quickLayerId = qnId + 'Layer';
		var topPosition = $(qnId).getPosition($(qnId).getParent('div')).y+75;
		var leftPosition = $(qnId).getPosition($(qnId).getParent('div')).x-267;
		var showHideTimer=null;
		
		qn.addEvent("mouseenter", function() {
			if(showHideTimer!=null) { clearTimeout(showHideTimer); }
			qn.src = qn.src.replace(/_/g,'Aktiv_');
			
			$(quickLayerId).setStyles({
				'left' : leftPosition,
				'top' : topPosition,
				'display' : 'block'
			});
			
			if(($(quickLayerId).getElement('h3').getSize().x) < 70) {
				($(quickLayerId).getElement('h3').setStyle('width', 65));
			}
			
			var targetWidth2 = 322 - ($(quickLayerId).getElement('h3').getSize().x);
			$(quickLayerId).getElement('div[class*=spacer]').setStyle('width', targetWidth2);
			
			$(quickLayerId).addEvent("mouseenter", function() {
				if(showHideTimer!=null) { clearTimeout(showHideTimer); }
				qn.src = qn.src.replace(/Aktiv_/g,'_');
				qn.src = qn.src.replace(/_/g,'Aktiv_');
				$(quickLayerId).setStyle('display','block');
			});
			
			$(quickLayerId).addEvent("mouseleave", function() {
				hideQuicknavigation(quickLayerId,qnId); 
				showHideTimer=null;
			});
		});
		
		qn.addEvent("mouseleave", function() {
			if (showHideTimer==null) {
				showHideTimer=setTimeout(function() { 
					hideQuicknavigation(quickLayerId,qnId); 
					showHideTimer=null;
				}, 125);
			}
		});
	});
}*/

function quickNavigation(){
	
	$$('img.BIG_quickNav').each(function(qn) { 
		var qnId = qn.id;
		qn.addEvent("mouseenter", function() {
			qn.src = qn.src.replace(/_/g,'Aktiv_');
		});
	
		qn.addEvent("mouseleave", function() {
			qn.src = qn.src.replace(/Aktiv_/g,'_');
		});
	});
}

// ## VideoBlogNavi -------------------------------------------------------------

function videoBlogNavi() {
	$$('.big_videoBlogNavi').each(function(vn){
		vn.addEvent("mouseover", function() {
			vn.src = vn.src.replace(/_/g,'Over_');
		});
		
		vn.addEvent("mouseout", function() {
			vn.src = vn.src.replace(/Over_/g,'_');	
		});
	});
}

/*
function hideQuicknavigation(layer,image) {
	$(image).src = $(image).src.replace(/Aktiv_/g,'_');
	$(layer).setStyle('display', 'none');
}
*/

// ## EventScroller -------------------------------------------------------------

/* function moveScroller() {
	$('moveDown').addEvent("click", function() {
		$('scroller').tween('top', -218);
	});
	
	$('moveUp').addEvent("click", function() {
		$('scroller').tween('top', 0);
	});
	
	$('moveRight').addEvent("click", function() {
		var myEffect = new Fx.Morph($('scroller'), { 
		duration: 400, 
		transition: 'sine:out',
		
		onComplete: function() {
			$('scroller').tween("top", 0)
		}
		});
			myEffect.start({
			'left' : -238
		});
	});
	
	$('moveLeft').addEvent("click", function() {
		var myEffect = new Fx.Morph($('scroller'), { 
		duration: 400, 
		transition: 'sine:out',
		
		onComplete: function() {
			$('scroller').tween("top", 0)
		}
		});
			myEffect.start({
			'left' : 0
		});
	});
	
}
*/


// ## EventScroller 2.0 -------------------------------------------------------------

activeStep = 0;

function eventScroller() {
	var events = $$('.big_eventOverviewLink');
	var scrollContentWidth = 210*events.length;
	$('scrollContent').setStyle('width', scrollContentWidth);
	var steps = (Math.ceil(events.length/4))-1;
		
	$('scrollRight').addEvent("click", function() {
		if(activeStep < steps) {
			var newLeft = parseInt($('scrollContent').getStyle('left'))-816;
			$('scrollContent').tween('left', newLeft);
			activeStep++;
		}
	});
	
	$('scrollLeft').addEvent("click", function() {
		if(activeStep > 0) {
			var newRight = parseInt($('scrollContent').getStyle('left'))+816;
			$('scrollContent').tween('left', newRight);
			activeStep--;
		}
	});
}

// ## SearchPosition -------------------------------------------------------------

function searchPosition() {
	var xPosition = $('navigationContent').getPosition().x+743;
	var yPosition = $('navigationContent').getPosition().y+10;
	
	$('big_search').setStyles({
		'left'	:	xPosition,
		'top'	:	yPosition
	});
}

function showSearchLayer() {
	
	var searchField = $$('.search');
	var leftPosition = $('big_search').getPosition().x-307;
	var activeZone = $('big_searchLayer').getElement('div[class*=BIG_naviBody]');
	var killzone = $('serviceNavigation');
	var killzone2 = $('big_searchLayer').getElement('img[class*=qn_TL]');
	var killzone3 = $('big_searchLayer').getElement('img[class*=qn_TR]');
	
//	$('searchInput').addEvent("focus", function() { searchFocus = "true" } );
//	$('searchInput').addEvent("blur", function() { searchFocus = "false" } );
	
	$('big_searchLayer').setStyles({
			'left' : leftPosition,
			'display' : 'block'
	});
	
	$('searchInput').focus();
	
	$$('.BIG_navigation').setStyle('display','none');
	
//	activeZone.addEvent("mouseleave", function() { if(searchFocus == "false") { setTimeout(function() {hideSearchLayer()},750); }});
//	killzone.addEvent("mouseenter", function() { if(searchFocus == "false") { setTimeout(function() {hideSearchLayer()},750); }});
//	killzone2.addEvent("mouseenter", function() { if(searchFocus == "false") { setTimeout(function() {hideSearchLayer()},750); }});
//	killzone3.addEvent("mouseenter", function() { if(searchFocus == "false") { setTimeout(function() {hideSearchLayer()},750); }});
}

function hideSearchLayer() {
	$('big_searchLayer').setStyle("display","none");
}


function showResults(res) {
	//console.log(res);
	var heading;
	var text;
	var link;

	$('big_searchResults_content').empty();
	
	res.each(function(item){
		//console.log(item.link);
		
		heading = new Element('h5', {
			html: item.heading
		});
		
		link = new Element('a', {
			href: item.link
		});
		heading.inject(link);
		link.inject($('big_searchResults_content'));
		text = new Element('p', {
			html: item.text	
		}).inject($('big_searchResults_content'));
	});
	
	$('big_searchResults_content').style.display = "block";
	$('big_searchResults_furtherResults').style.display = "block";
}


// ## checkLen -------------------------------------------------------------

function checkLen(id,counter) {
	if($(id).value.length>counter) {
		$(id).value=$(id).value.substring(0,counter);
	}
	
	var currentCount = counter - $(id).value.length;
	$(id+'Counter').innerHTML = currentCount;
}


/*################################################################################
	Call all Function
################################################################################*/

window.addEvent("domready", function() {
	setOpacity();
	mainNaviPosition();
	window.addEvent("resize", function() {
		mainNaviPosition();		 
	});
});

window.addEvent("load", function(){
	/*if($$('.blackBoxes')) { scaleBlackBoxes(); } */
	if($('rightSlot') !== null) { 
		stretchMainPage();	
	}

	if($('scrollContent')!=null) {
		eventScroller();
	}
	quickNavigation();
	if($$('.big_videoBlogNavi').length != 0) { videoBlogNavi(); };
	window.addEvent("resize", function(){
		if($('rightSlot') != null) { 
			stretchMainPage(); 
		}
		searchPosition()
	});
	killThirdLevel();
	searchPosition();
	if($('scroller') != null) { moveScroller(); }
	
	//if(checkBrowserName('MSIE')){
		setTimeout(function() {
      if($('BIG_leftSlot') != null) { scaleTeasers(); }
    }, 200);
	//}
	//if(!checkBrowserName('MSIE')){
		//if($('BIG_leftSlot') != null) { scaleTeasers(); }
	//}
});




/*################################################################################
	CMS Alert
################################################################################*/

function cmsAlert(title, content, close, msgtype){
	Alert({
		title: title,
		content: '<p>'+content+'</p><a class="Right" style="display:block"'
	    				+' href="javascript:overlay.hide()">'
	    				+close+'</a>',
	    type: msgtype
	});
}




function strikeThrough(checked, element){
	if(checked){
		element.setStyle('text-decoration', 'line-through');
	} else {
		element.setStyle('text-decoration', 'none');		
	}
}

/*################################################################################
	reCAPTCHA
################################################################################*/

var RecaptchaOptions = {
   theme : 'custom',
   tabindex : '5',
   lang : 'de'
};
