// Hilfsfunktion fuers Entwickeln  +++++++++++++++++++++++++
function initDragObj() {
	$$(".draggableFlag").each(function(i){
		new Draggable(i, {change: moveDragObj});
		i.setStyle({border: "1px dashed red;"});
	});
}
function moveDragObj(i) {
	var e = i.element;
	document.title = "left: " + e.getStyle("left") + ", top: " + e.getStyle("top") + ", id: " + e.id;
}

// IE6 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Highslide im IE6 ueber hs-Dummy-Objekt deaktivieren
function noHighslideInIe6() {
 var hsTmp = Class.create({expand: function() {}});
 if($browser=="IE 6") {
    hs = new hsTmp;
  }
}

// Volltextsuche Popup-Box +++++++++++++++++++++++++++++++++++++
function initPopupSearchBox() {
	if($("popup-top-right-search")) {
		$("popup-top-right-search").observe("click", function(e) {
			//Effect.BlindUp('loginPopupBox', { duration: 0.5 });
			$("loginSearchBox").style.visibility="hidden";
			if (smPlay) {smPlay = false; soundManager.stop('mySound');}
		} );
	}

	if($("popupSearchClick")) {
		$("popupSearchClick").observe("click", function(e) {
			$("loginSearchBox").style.visibility="visible";
		} );
	}
}

// Newswechsel im unteren Bereich ++++++++++++++++++++++++++++++
function initNewsSlider() {
	if(!$("newsSliderContent")) return; // hier gibt's keine News;
	$("newsSliderContent").setStyle({color:"#fab91b"});
	setSliderNextNews($("newsSliderContent").readAttribute("href"));
}
function setSliderNextNews(path) {
	if ( typeof setSliderNextNews.num == 'undefined' ) {
		setSliderNextNews.num = 0;
	}

	$("newsSliderContent").innerHTML = newsTitles[setSliderNextNews.num][0];
	$("newsSliderContent").writeAttribute("href", path + "?id=" + newsTitles[setSliderNextNews.num][1]);
	$("newsSliderContent").morph("color:#7f5e0d", {duration: "1.0"});	
	$("newsSliderContent").morph("color:#fab91b", {duration: "1.0", delay:3, queue: "end", afterFinish:function(){setSliderNextNews(path)} });
	//Effect.Appear('newsSliderContent', { duration: "1.0", queue: "end" });
	//Effect.Fade('newsSliderContent', { duration: "1.0", delay:3, queue: "end", afterFinish:function(){setSliderNextNews(path)}});
	setSliderNextNews.num++;
	if(setSliderNextNews.num==newsTitles.length) setSliderNextNews.num=0;
}


// +++++++++++++++ Glossar-Ajax-Suche ++++++++++++++++++++++++
function initGlossary() {
	if($('fieldSearch')) {
		$('fieldSearch').observe('keyup', function(e)
		{
			updateGlossaryResult();
		});
		$('fieldSearch').observe('click', function(e)
		{
			if(e.target.value==e.target.readAttribute("alt")) {
				e.target.value="";
			}
			
		});
	}
}
function updateGlossaryResult() {
	if ( typeof updateGlossaryResult.numChars == 'undefined' ) {
		updateGlossaryResult.numChars = 0;
	}

	if ( ($('fieldSearch').value.length > 2) && ($('fieldSearch').value.length!=updateGlossaryResult.numChars) ) {
		$('glossarResult').innerHTML = ixTransLoading;
		new Ajax.Updater($('glossarResult'), '/libPhp/ajax/ajaxGlossary.php', {
		method:'post',
		parameters:
		{
			str: $('fieldSearch').value
		}});
	}
	updateGlossaryResult.numChars = $('fieldSearch').value.length;
}


// ++++++++++++++ FAQ (vergrößerbare Bereiche) +++++++++++++++
// id: des zu morphenden Fensters
// flagForceClosing: true = auf jeden Fall schliessen
function toggleUpDown(id, flagForceClosing, dauer) {
	if(!id) return;
	if( ($(id).getHeight()>2) || flagForceClosing) {
		height=0;
		$(id).previous().style.backgroundImage="url(/libImages/global/faqPlus.png)";
		$(id).morph("height: "+height+"px", {duration:dauer, afterFinish:function() {$(id).setStyle({visibility:'hidden'});}});
	} else {
		height=$(id).readAttribute("rel");
		$(id). previous().style.backgroundImage="url(/libImages/global/faqMinus.png)";
		$(id).morph("height: "+height+"px", {duration:dauer, beforeStart:function() {$(id).setStyle({visibility:'visible'});}});

	}
	//$(id).morph("height: "+height+"px", {duration:dauer});
	
}

function showAllFaq() {
	$("faq").setStyle({visibility:"visible"});
}

function initFAQ() {
	$$(".faqObserver").each( function(item) {
		var id = item.next().identify();

		item.observe("click", function(e){
			var liObj = item.up();
			toggleUpDown(id, false, 0.7);
			if(idOpenOverviewBox!=id) toggleUpDown(idOpenOverviewBox, true, 0.7);
			idOpenOverviewBox = id; 

		});
		// init
		$(id).writeAttribute({rel: $(id).getHeight() });
		toggleUpDown(id, true, 0);
		window.setTimeout("showAllFaq()", 1000);
	} );
}

// ++++++++++++++ contactPerson (vergrößerbare Bereiche) +++++++++++++++
// id: des zu morphenden Fensters
// flagForceClosing: true = auf jeden Fall schliessen
function toggleUpDownContactPerson(id, flagForceClosing, dauer) {
	if(!id) return;
	//var dauer = 0.7; 
	if( ($(id).getHeight()>2) || flagForceClosing) {
		height=0;
		$(id).previous().style.backgroundImage="url(/libImages/global/bgHlContactPersonPlus.jpg)";
		if($browser!="IE 7") { $(id).morph("height: "+height+"px", {duration:dauer, afterFinish:function() {$(id).setStyle({visibility:'hidden'});}});
		} else { $(id).setStyle({height:'0px'});$(id).setStyle({visibility:'hidden'}); }
	} else {
		height=$(id).readAttribute("rel");
		$(id). previous().style.backgroundImage="url(/libImages/global/bgHlContactPersonMinus.jpg)";
		if($browser!="IE 7") { $(id).morph("height: "+height+"px", {duration:dauer, beforeStart:function() {$(id).setStyle({visibility:'visible'});}});
		} else { $(id).setStyle({height:height+'px'});$(id).setStyle({visibility:'visible'}); }
	}
	//$(id).morph("height: "+height+"px", {duration:dauer});
}

function showAllContactPerson() {
	$("contactPerson").setStyle({visibility:"visible"});
}

function initContactPerson() {
	$$(".contactPersonObserver").each( function(item) {
		var id = item.next().identify();

		item.observe("click", function(e){
			var liObj = item.up();
			toggleUpDownContactPerson(id, false, 0.7);
			if(idOpenOverviewBoxFoo!=id) toggleUpDownContactPerson(idOpenOverviewBoxFoo, true, 0.7);
			idOpenOverviewBoxFoo = id; 

		});
		// init
		$(id).writeAttribute({rel: $(id).getHeight() });
		toggleUpDownContactPerson(id, true, 0);
		window.setTimeout("showAllContactPerson()", 1000);
	} );
}


// ++++++++++++++ NEWS (vergrößerbare Bereiche) +++++++++++++++
// class: newsText.tmpl
function initNewsResizer(minSize, truncateSize) {
	var pic1 = '<img src="/libImages/global/newsReadMorePlus.png" />';
	var pic2 = '<img src="/libImages/global/newsReadLess.png" />';

	$$(".mainNewsText").each(function(item) {
		var s = item.innerHTML;
		pufferNews.set(item.identify(), s);
		item.writeAttribute({rel: item.getHeight() });
		item.innerHTML = s.truncate(truncateSize).stripTags();
		toggleUpDownNews(item.identify(), true, 0.0, minSize, truncateSize);
	});
	
	
	$$(".swapNewsText").each(function(item) {
		item.observe("click", function(e){
			toggleUpDownNews(item.previous().identify(), false, 0.7, minSize, truncateSize);
			if(item.innerHTML.startsWith(moreText)) item.innerHTML = lessText+pic2;
			else item.innerHTML = moreText+pic1;
		});
	});

	if($("newsCostum"))$("newsCostum").setStyle({visibility: 'visible'});
}

function toggleUpDownNews(id, flagForceClosing, dauer, minHeight, truncateSize) {
	if(!id) return;
	var text = pufferNews.get(id);

	if( ($(id).getHeight()>2+minHeight) || flagForceClosing) {
		height=minHeight;
		text = text.truncate(truncateSize).stripTags();
		$(id).morph("height: "+height+"px", {duration:dauer, afterFinish:function() {$(id).innerHTML=text;}});
	} else {
		height=$(id).readAttribute("rel");
		$(id).innerHTML=text;
		$(id).morph("height: "+height+"px", {duration:dauer});
	}
}



// +++++++++++++++++ POPUP-BOX ++++++++++++++++++++++++++++++++++++++
// den Inhalt einer existierenden Popup-Box ändern:
// siehe functions.php: getPopupBox() )
// topText bzw. mainText = null => vorhandener Wert wird nicht verändert
// style: {width:"300px"}
function setPopupBox(topText, mainText, style) {
	if( topText!=null ) $("popupBoxTop").innerHTML= topText;
	if( mainText!=null ) $("popupBoxMain").innerHTML=mainText;
	if( style!=null ) $("loginPopupBox").setStyle(style);
}

function initPopupBox() {
	if($("popup-top-right")) {
		$("popup-top-right").observe("click", function(e) {
			//Effect.BlindUp('loginPopupBox', { duration: 0.5 });
			$("loginPopupBox").style.visibility="hidden";
			if (smPlay) {smPlay = false; soundManager.stop('mySound');}
		} );
	}

	if($("popupCloseIE6")) {
		$("popupCloseIE6").observe("click", function(e) {
			//Effect.BlindUp('loginPopupBox', { duration: 0.5 });
			$("loginPopupBox").style.visibility="visible";
			if (smPlay) {smPlay = false; soundManager.stop('mySound');}
		} );
	}

	if($("popupClick")) {
		$("popupClick").observe("click", function(e) {
			$("loginPopupBox").style.visibility="visible";
			//Effect.BlindDown('loginPopupBox', { duration: 0.5 });
		} );
	}
	
	$$(".popupClickOpinion").each(function (v) {
		v.style.cursor = 'pointer';
		v.observe("click", function(e) {
			var html = '<div style="position:relative; float:left; height:320px;">'+(this.down('.imgSrc') ? '<img src="'+this.down('.imgSrc').innerHTML+'" alt="" style="position:absolute; left:0px; bottom:0px;" />' : '')+'</div>';
			html += '<div style="float:right; width:240px; height:300px; padding-right:20px; overflow-y:auto;">'+this.down('.mainText').innerHTML+'</div>';
			html += '<div class="clear"></div>';
			if (this.down('.audioSrc').innerHTML && smReady) {html += '<div class="popupPlay"><a onclick="playSound(this, \''+this.down('.audioSrc').innerHTML+'\');"><img src="/libImages/global/buttonPlay.png" alt="" /></a> '+this.down('.audioTitle').innerHTML+'</div>';}
			$("popupBoxMain").update(html);
			$("loginPopupBox").style.visibility="visible";
			//Effect.BlindDown('loginPopupBox', { duration: 0.5 });
		});
	});
}

// Popupbox anzeigen
function showPopupBox() {
	//Effect.BlindDown('loginPopupBox', { duration: 0.5 });
	$("loginPopupBox").style.visibility="visible";
}

// +++++++++++++++++++++++++ Teaser, der sich mitbewegt ++++++++++++++++++++++++++++++++
// Teaser bewegt sich beim Scrollen mit
// id: id des Teasers
// min: obere Grenze, über die der Teaser nicht bewegt wird
// maxBottom: Mindestabstand am unteren Bereich, unter den die Oberknte des Teaser nicht rutschen kann
function initTeaserMovement(id, min, maxBottom) {
	moveTeaser(id, 0.1, min, maxBottom);
	Event.observe(window, 'scroll', function() { moveTeaser(id, 0.1, min, maxBottom); });
}

function moveTeaser(id, dauer, min, maxBottom) {
	vpOffset = 150;

	if ( typeof moveTeaser.moving == 'undefined' ) {
		moveTeaser.moving = false;
	}

	var max = parseInt($(document.body).getHeight()) - maxBottom + vpOffset;

	var y = document.viewport.getScrollOffsets().top; 
	if(y<min) y=min;
	if(y>max) y=max;
	
	if(moveTeaser.moving==false) {
		moveTeaser.moving = true;
		$(id).morph("top: "+(y-vpOffset)+"px", {duration:dauer, afterFinish:function() {moveTeaser.moving = false; moveTeaser(id, 0.5, min, maxBottom);}});
	}
}


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function playSound(gObj, gSound)
{
	if (!smPlay)
	{
		smPlay = true;
		soundManager.play('mySound', gSound);
		gObj.innerHTML = '<img src="/libImages/global/buttonStop.png" alt="" />';
	}
	else
	{
		smPlay = false;
		soundManager.stop('mySound');
		gObj.innerHTML = '<img src="/libImages/global/buttonPlay.png" alt="" />';
	}
}


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function useMo(gSrc)
{
	var index = gSrc.lastIndexOf('.');
	return gSrc.substring(0, index)+'.mo'+gSrc.substring(index);
}

function refreshUploadProgress(){
	var uploadIdentifier=$("UPLOAD_IDENTIFIER").value;
	var url="/libPhp/ajax/refreshUploadProgress.php";	
	new Ajax.Request(url, {
		method: "POST",
		encoding: "UTF-8",
		parameters: "UPLOAD_IDENTIFIER="+uploadIdentifier,
		onComplete: function(transport){
alert(transport.responseText);
			if((transport.responseText>"") && (transport.responseText!="null")){
				var obj=transport.responseText.evalJSON();
				var loadingBarWidth=400;
				var newSize=(obj.bytes_uploaded*loadingBarWidth)/obj.bytes_total;
				$("uploadSize").style.width=newSize+"px";
				setTimeout("refreshUploadProgress();",1000);
			}else{
				setTimeout("refreshUploadProgress();",1000);
			}
		} 
	});
}


// Formularfelder pruefen
function chkMail() {
	var chkOK = true;
	var chkMail = true;
	var chkEmpty = true;
	var msg = "";

	$$(".chkEmpty").each( function(item){
		if($F(item)=="") {
			chkOK = false;
			chkEmpty = false;
			item.setStyle({backgroundColor: '#FF9999'});
		} else {
			item.setStyle({backgroundColor: '#FFFFFF'});
		}
	} );

	$$(".chkEmail").each( function(item){
                var reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+'(\\@)([a-zA-Z0-9\\-\\.]+)'+'(\\.)([a-zA-Z]{2,4})$');
                
		if(!reg.test($F(item)))	{
		//if($F(item)=="") {
			chkOK = false;
			chkMail = false;
			item.setStyle({backgroundColor: '#FF9999'});
		} else {
			item.setStyle({backgroundColor: '#FFFFFF'});
		}
	} );

	if(!chkEmpty) msg += ixTransJsempty +"\n";
	if(!chkMail) msg += ixTransJsmail +"\n";
	if(msg!="") alert(msg);
	return chkOK;
}

function mouseRollOver()
{
	$$('.rollOver').each(function (i)
	{
		var index = i.src.lastIndexOf('.');
		if (index > -1) {var newSrc = i.src.substring(0, index)+'.mo'+i.src.substring(index);}
		
		if (newSrc)
		{
			var img = new Image();
			img.src = newSrc;
		}
		
		i.observe('mouseover', function (e)
		{
			if (newSrc) {i.src = newSrc;}
		})
		
		i.observe('mouseout', function (e)
		{
			i.src = i.src.replace('.mo.', '.');
		})
	});

	$$('.rollOverDownImg').each(function (i)
	{
		var index = i.down('img').src.lastIndexOf('.');
		if (index > -1) {var newSrc = i.down('img').src.substring(0, index)+'.mo'+i.down('img').src.substring(index);}
		
		if (newSrc)
		{
			var img = new Image();
			img.src = newSrc;
		}
		
		i.observe('mouseover', function (e)
		{
			if (newSrc) {i.down('img').src = newSrc;}
		})
		
		i.observe('mouseout', function (e)
		{
			i.down('img').src = i.down('img').src.replace('.mo.', '.');
		})
	});
}

function initPageNavigator() {
	$$('.textLink').each(function (i)
	{
		var bild = i.previous("a").down();
		var index = bild.src.lastIndexOf('.');
		if (index > -1) {var newSrc = bild.src.substring(0, index)+'.mo'+ bild.src.substring(index);}
		
		if (newSrc)
		{
			var img = new Image();
			img.src = newSrc;
		}
		
		i.observe('mouseover', function (e)
		{
			if (newSrc) {bild.src = newSrc;}
		})
		
		i.observe('mouseout', function (e)
		{
			bild.src = bild .src.replace('.mo.', '.');
		})
	}); 
}


//**************************
//*** SPAMSCHUTZ ***  
//**************************


// UnCryptMailto() entschluesselt die Mail-Adresse, zu zuvor mit
// der PHP-Funktion encryptEmail() verschlÃƒÂ¼sselt wurde
// Anwendung:
/*
<?php $link="javascript:linkTo_UnCryptMailto('".encryptEmail("john@doe.de")."');"; ?>
<a href="<?php echo $link ?>">E-Mail an John Doe schreiben</a>
*/
 
// JS function for uncrypting spam-protected emails:
function UnCryptMailto(s) {	//
	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-(1));
	}
	return r;
}

  // JS function for uncrypting spam-protected emails:
function linkTo_UnCryptMailto(s)	{	//
	location.href="mailto:"+UnCryptMailto(s);
}

// AJAX-Stellensuche ++++++++++++++++++++++++++++++++++
function jobSearch()
{
	$(document.body).insert({'bottom': '<div id="boxSearch" '+($('thisIsTheJobTemplate') ? 'class="boxJobTemplate"' : '')+' style="display:none;"><img src="'+($('thisIsTheJobTemplate') ? '/libImages/global/boxSearchBigTop.png' : '/libImages/global/boxSearchTop.png')+'" alt="" /><div id="boxSearchHeadline">'+ixTransSearchsuggestion+':</div><div id="boxSearchContent"></div><img src="'+($('thisIsTheJobTemplate') ? '/libImages/global/boxSearchBigBottom.png' : '/libImages/global/boxSearchBottom.png')+'" alt="" /><a href="javascript:$(\'formJobSearch\').submit();" id="boxSearchBottom2"></a></div>'});
	$('ajaxJobSearch').observe('keyup', function (e)
	{
		if ($F(this).length > 2)
		{
			new Ajax.Request('/libPhp/ajax/jobSearch.php',
			{
				method: 'post',
				postBody: 'json='+$H({'searchStr': $F(this).toLowerCase()}).toJSON(),
				asynchronous: true,
				onSuccess: function (t)
				{
					try {drawSearchResult(t.responseText.evalJSON(true));}
					catch (e) {/*alert('Fehlerhafte Antwort: '+t.responseText);*/}
				}
			});
		}
		else {drawSearchResult(new Array());}
	});
}

function drawSearchResult(gArr)
{
	if (gArr.length)
	{
		var output = '';
		gArr.each(function (v, k)
		{
			if ((gArr.length > 7 && k >= 5) || k >= 7) {return;}
			output += '<div class="jobText"'+(k ? ' style="border-top:1px solid #e1ca9b;"' : '')+'><a href="/'+ixLangCurrent+'/stellenmarkt/?id='+$H(v).get('id')+'#content">'+$H(v).get('name')+'</a></div>';
		});
		$('boxSearchContent').update(output);
		$('boxSearchBottom2').update(ixTransSearchresult.replace('####', '<span>'+gArr.length+'</span>'));
		var pos = $('ajaxJobSearch').cumulativeOffset();
		$('boxSearch').style.left = (pos[0] - 23)+'px';
		$('boxSearch').style.top = (pos[1] - $('boxSearch').getHeight() + 14)+'px';
		if ($browser.indexOf('IE') != -1) {$('boxSearch').show();}
		else {$('boxSearch').appear({duration: 0.25});}
	}
	else
	{
		if ($browser.indexOf('IE') != -1) {$('boxSearch').hide();}
		else {$('boxSearch').fade({duration: 0.25});}
	}
}

// JS-Fehlermeldungen ++++++++++++++++++++++++++++++++++++++
//window.onerror=callAjaxError; // IE
//window.onError= callAjaxError; // NS

function callAjaxError(msg, url, line) {
  new Ajax.Request('/libPhp/ajax/error.php', 
 {
   method: 'post',
   postBody: 'appcode='+navigator.appCodeName+"&ver="+navigator.appVersion+"&usr="+navigator.userAgent+"&JsErrorUrl="+url+"&JsErrorLine="+line+"&JsErrorMsg="+msg,
   asynchronous: true,
   onSuccess: function (t)	{}
 });
  return false;
}

// job per ajax laden
function replaceJob(gId)
{
	new Ajax.Request('/libPhp/ajax/jobDetail.php',
	{
		method: 'post',
		postBody: 'jobId='+parseInt(gId),
		asynchronous: true,
		onSuccess: function (t)
		{
			$('stellenmarktDetail').update(t.responseText);
			addthis.button("#atbutton");
		}
	});
	return false;
}

// job hervorheben
function setActiveJob(gO)
{
	if ($browser != 'IE 5x' && $browser != 'IE 6' && $browser != 'IE 7')
	{
		if ($('activeJob')) {$('activeJob').id = null;}
		for (var i = 0; typeof($('jobTable').down('tr', i)) != 'undefined'; ++i)
		{
			$('jobTable').down('tr', i).style.backgroundImage = 'none';
			if ($('jobTable').down('tr', i).className.indexOf('even') == -1) {$('jobTable').down('tr', i).style.backgroundColor = 'transparent';}
			else {$('jobTable').down('tr', i).style.backgroundColor = '#ffe9bd';}
		}

		gO.previous().style.backgroundImage = 'url(/libImages/global/activeJobTop.png)';
		gO.previous().style.backgroundRepeat = 'repeat-x';
		gO.previous().style.backgroundPosition = 'bottom';
		gO.next().style.backgroundImage = 'url(/libImages/global/activeJobBottom.png)';
		gO.next().style.backgroundRepeat = 'repeat-x';
		gO.next().style.backgroundPosition = 'top';
		gO.style.backgroundColor = '#ffffff';
		
		if (!$('bridge')) {$(document.body).insert({'bottom': '<div id="bridge" style="position:absolute; left:0px; top:0px; width:0px;"><img src="/libImages/global/activeJobTop.png" alt="" style="display:block; width:100%; height:6px;" /><div style="height:0px; background-color:#ffffff;"></div><img src="/libImages/global/activeJobBottom.png" alt="" style="display:block; width:100%; height:6px;" /></div>'});}
		$ajOffset = gO.cumulativeOffset();
		$('bridge').down('div').style.height = gO.getHeight()+'px';
		$('bridge').style.width = ($('stellenmarktDetail').cumulativeOffset()[0] - ($ajOffset[0] + gO.getWidth()) + 8)+'px';
		$('bridge').style.left = ($ajOffset[0] + gO.getWidth())+'px';
		$('bridge').style.top = ($ajOffset[1] - 6)+'px';
	}
	else
	{
		if ($('activeJob')) {$('activeJob').id = null;}
		for (var i = 0; typeof($('jobTable').down('tr', i)) != 'undefined'; ++i)
		{
			$('jobTable').down('tr', i).style.backgroundImage = 'none';
			if ($('jobTable').down('tr', i).className.indexOf('even') == -1) {$('jobTable').down('tr', i).style.backgroundColor = 'transparent';}
			else {$('jobTable').down('tr', i).style.backgroundColor = '#ffe9bd';}
		}
		gO.style.backgroundColor = '#ffffff';
	}
}
