// preload arrow images

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 );
            
            r= r.split('*').join(':');
        }
        return r;
    }

    function linkTo_UnCryptMailto( s )
    {
        location.href=UnCryptMailto( s );
    }


function clearFieldsOnFocus(){
	    $$('input').each(function(element) {
	    	if(element.name == 'tx_indexedsearch[sword]')
				element.onclick = function(){this.value = ''}				
      });
}

function checkForm(event){
	form = Event.element(event);
	plz = form.down('input').value;
	if(isNaN(plz) || (plz < 1000) || (plz > 99999)){
		msg = document.createElement('P');
		msg.style.color = 'red';
		msg.innerHTML = 'Bitte überprüfen Sie ihre Eingabe';	
		form.appendChild(msg);
		Event.stop(event);		
	}
}


function appendNewsAjax(){
    $$('.news-list-browse')[0].descendants().each(function(element) {
          if(element.tagName == "A") {               
            Event.observe(element, "click", doAjax, false);
          }
      });
}

function doAjax(event){
    Event.stop(event);
    var clickedLink = Event.element(event);
    if(clickedLink.tagName == 'IMG') clickedLink = clickedLink.up();
   // $('news-ajax').hide();
   // $('ajax-load').show();
    new Ajax.Updater('content-right', clickedLink.href+'?type=1&no_cache=1', {
            asynchronous:true,
            onComplete: function(){ 
            		appendNewsAjax(); 
            		// $('ajax-load').hide(); 
            		// new Effect.Appear('content-right', {duration:0.4}) 
            		accordion2 = new Accordion("klappmenu-right", 1);
            		}
            		
            		
            });
}


function getBrowser(){
	if(document.ids)x='nc4';
	else if( document.all && !document.getElementById )x='ie4';
	else if( window.opera && !document.createElement )x='op5';
	else if( window.opera && window.getComputedStyle )  {
	          if(document.createRange)x='op8';
	            else if(window.navigate)x='op7.5';
	                             else x='op7.2';                   }
	else if( window.opera && document.compatMode )x='op7';
	else if( window.opera && document.releaseEvents )x='op6';
	else if( document.contains && !window.opera )x='kq3';
	else if(window.pkcs11&&window.XML)x='f15';
	else if( window.getSelection && window.atob )x='nn7';
	else if( window.getSelection && !document.compatMode )x='nn6';
	else if( window.clipboardData && document.compatMode )
	  x=window.XMLHttpRequest? 'IE7' : 'IE6';
	else if( window.clipboardData ){x='ie5';
	     if( !document.createDocumentFragment ) x+='.5';
	     if( document.doctype && !window.print ) x+='m';}
	else if( document.getElementById && !document.all ) x='op4';
	else if( document.images && !document.all ) x='nn3';
	else if(document.clientWidth&&!window.RegExp)x='kq2';
	else x='???';
return x;
}





function observeKlippklapp(){
	 
	$$('.klappmenu-content').each(function(element) {   
		element.hide();
	});
	
	$$('.klappmenu-head').each(function(element) {  
		
		Event.observe(element, "click", function(){
			$$('.klappmenu-content').each(function(e) {   
					e.previousSiblings()[0].className = 'klappmenu-head';
					if(e.previousSiblings()[0] != element) 
						e.hide();						
			});		
				
		    element.nextSiblings()[0].toggle();
	    
		    if(element.nextSiblings()[0].visible()){
		    	element.className = 'klappmenu-head active';
			} else {
				element.className = 'klappmenu-head';
			}	

		}, false);
	});
	
}


var timeout;

function observeTeasers(){
	$$('.teaser').each(function(element) {   
		//element.hide();
		new Effect.Pulsate(element, {duration:'1', pulses:'3'});	
		
		plus = element.down('.teaser-plus');
		content = element.down('.teaser-content');
		text = element.down('.teaser-text');
		
		
		left = Math.floor(element.style.left.replace(/px/g, ""));		
		if((left+340)>900){		
			content.style.right = '19px';
			content.style.left = 'auto';
		}	
		
		Event.observe(element.down('.teaser-plus'), "mouseover", function(e){
			element.down('.teaser-text').hide();
			element.down('.teaser-content').show();
		});

		Event.observe(element.down('.teaser-plus'), "click", function(e){
			if(!element.down('.teaser-text').visible()){
				new Effect.Grow(element.down('.teaser-text'), {duration:'0.3',direction:'top-left'});
			} else {
				element.down('.teaser-text').hide();
			}	
		});
		
		Event.observe(element.down('.teaser-plus'), "mouseout", function(e){
			element.down('.teaser-content').hide();
		});		

		Event.observe(element.down('.teaser-content'), "mouseover", function(e){
			element.down('.teaser-content').show();
			window.clearTimeout(timeout);
		});
		
		Event.observe(element.down('.teaser-content'), "mouseout", function(e){
			//var hide = function(){element.down('.teaser-content').hide()}
			//timeout =window.setTimeout(hide,500);	
			element.down('.teaser-content').hide();
			
		});	

		Event.observe(element.down('.teaser-content'), "click", function(e){
		if(!element.down('.teaser-text').visible()){
			new Effect.Grow(element.down('.teaser-text'), {duration:'0.3',direction:'top-left'});
		} 	
		});
	
	});
}

function showNews(event,id){
if(!id){ // onclick
    Event.stop(event);        
    linkelement = Event.element(event);      
    id = linkelement.up().id;
    newsitem = $('news_item_'+id);
} else { // on auto
	newsitem = $('news_item_'+id);
	linkelement = $('news_item_'+id).down('.news-list-morelink a');
}		

    if(Element.visible('news-list-teaser_'+id)){
            vis = ('news-list-teaser_'+id);
            hid = ('news-list-content_'+id);           
            linkelement.innerHTML = "zur&uuml;ck";            
            showNewsImgs(newsitem);
            
    } else {
            hid = ('news-list-teaser_'+id);
            vis = ('news-list-content_'+id);
            linkelement.innerHTML = "weiterlesen";
            hideNewsImgs(newsitem);
    }

    new Effect.BlindUp(vis, {  duration:0.3,
              afterFinish:function(){
                  new Effect.BlindDown(hid, {duration:0.4});
              }
            });
}


function showNewsImgs(newsitem){
		
		images = newsitem.down('.news-list-img').immediateDescendants();		
		if(images.length>1)
		images.each(function(e){
			if (e != images[0])		
				new Effect.Appear(e, {duration:0.4});
		});
}


function hideNewsImgs(newsitem){
		images = newsitem.down('.news-list-img').immediateDescendants();		
		if(images.length>1)
		images.each(function(e){
			if (e != images[0])		
				e.hide();
		});	
}

function hideAllNewsImgs(){
	$$('.news-list-item').each(function(element) {		
		hideNewsImgs(element);
	});
}


function checkNewsAnker(){

anker = location.href.split('#')[1];
if($(anker)){
		ankerid = anker.split('_')[1];
		showNews(false, ankerid);
	}
}


document.observe("dom:loaded", function(){

    if($('klappmenu')){
    	//accordion = new Accordion("klappmenu", defaultopen);    	
    	observeKlippklapp()    	
    } 

   if($$('.teaser')) 
       observeTeasers();        
        

	


	hideAllNewsImgs();	

    $$('.news-list-morelink').each(function(element) {
          if(element.firstChild.tagName == "A") {          	
          	if($(element).previous('.news-list-content').select('p') == false){
          		element.hide();
          	}	       
            Event.observe(element, "click", showNews, false);
         }
     });

   
   	checkNewsAnker();
   
        
    if((getBrowser() == "IE6") ||  (getBrowser() == "IE5")){
    	 
    } else {
    	
    }          
        	
    clearFieldsOnFocus();
    
});  

