// preload arrow images

var pfeil_up = new Image();   pfeil_up.src = 'fileadmin/layout/imgs/pfeil_rot_U.gif';
var pfeil_dn = new Image();   pfeil_dn.src = 'fileadmin/layout/imgs/pfeil_rot_D.gif';


function loadScripts(){
    countNews();
    removeEmptyPtags();
    checkCanvasSize();
    automateNews();
}


function checkCanvasSize(){
 var viewportwidth;
 var viewportheight;
 
 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
 
 if (typeof window.innerWidth != 'undefined')
 {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
 }
 
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
 }
 
 // older versions of IE
 
 else
 {
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
 }
 if(viewportwidth < 1200){
		$('wrap').style.width = (1150)+'px';
		$('wrap').style.left = '-150px';
	}
}

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 removeEmptyPtags(){
      $$('p').each(function(element) {
        if(element.innerHTML == '') element.remove();
      });
}


function observeTopLink(){

}

function observeNewsToggle(){
  if($$('.news-button')){
   Event.observe($$('.news-button')[0], "click", function(event){
            Event.stop(event);
            toggleNews();              
            }, false); 
  }
}

function toggleNews(){
  if(Element.visible('news')){
      new Effect.Fade('news');
  } else {
      new Effect.Appear('news');
  }
}


function observeTopicSwitch(){
        $$('.change-topic').each(function(element) {
           
            preload = new Image();
            preload.src = element.href;
          
            Event.observe(element, "click", function(event){
              element = Event.element(event);
              Event.stop(event);
              changeTopic(element.href);
              
              $$('.change-topic').each(function(a) {
              a.className = 'change-topic';
              });
              element.className = 'change-topic active';
              
            }, false);
        });
}

function changeTopic(url){
    $('topic-image-start').style.backgroundImage = 'url('+url+')';
}


fx.ElementScroll = Class.create();

fx.ElementScroll.prototype = Object.extend(new fx.Base(), {

      initialize: function(el, options) {
        this.el = el;
        this.iniMarginLeft = this.el.style.marginLeft;
        this.setOptions(options);
    },

    increase: function() {
        this.el.style.marginLeft = this.now + 'px';
    },

    toggle: function(){
        if (this.el.marginLeft > 0) this.custom(this.el.marginLeft, 0);
        else this.custom(0, this.iniMarginLeft);
    }

});


var curNewsId = 0;
var newsCount;
var automate = true;
var timeoutId = false;

function countNews(){
  if($('news'))
    newsCount = ($$('.news-list-item').length);
}

function scrollNews(newsid){ 


         if(curNewsId<0) curNewsId=0;  
         if(curNewsId>=newsCount) curNewsId=newsCount-1;
         
      //   $('news_direct').innerHTML = curNewsId+" "+newsid;
         
          if((curNewsId>=0) && (curNewsId < newsCount)){
            if((!newsid) && curNewsId < newsCount-1){
              newsNext();
            }else if(-1 == newsid){
              if(curNewsId>0)
                newsPrev();
            } else{
                  while ((newsid-1)>curNewsId)
                    newsNext(); 
                  while ((newsid-1)<curNewsId)
                    newsPrev();                         
            }            
          }
}

function updateNewsButtons(){
      $$('.news_direct_button').each(function(element) {
        element.className = 'news_direct_button';
      });
      $('news_button_'+curNewsId).className = 'news_direct_button active';
}

function newsNext(){
            var clientTween = new fx.ElementScroll($$('.news-list-item')[curNewsId], {duration: 400, transition: fx.sinoidal});
            clientTween.custom(0, -285);
            if(curNewsId<newsCount) curNewsId++;
}

function newsPrev(){
            curNewsId--;
            var clientTween = new fx.ElementScroll($$('.news-list-item')[curNewsId], {duration: 400, transition: fx.sinoidal});
            clientTween.custom(-285, 0);

}

function automateNews(){
	if((curNewsId < (newsCount-1)) && (automate == true))
	timeoutId = setTimeout(function(){newsNext();automateNews()}, 6000);
	if((curNewsId >= (newsCount-1)) && (automate == true))	{
		
		timeoutId = setTimeout(function(){scrollNews(1); automateNews()}, 6000);
	}
}

function stopAutomate(){
	automate = false;
	if(timeoutId) clearTimeout(timeoutId);
}

function attachOnclicks(event) {

      if($('csub_slice1'))
            Event.observe($('csub_slice1'), "click", function(){ document.location='/newsletter.html' }, false);

      $$('.news-list-morelink').each(function(element) {
          if(element.firstChild.tagName == "A") {
            Event.observe(element, "click", showNews, false);
          }
      });
}

function showNews(event){



    Event.stop(event);
    id = Event.element(event).up().id;
    
    if(Element.visible('news-list-teaser_'+id)){
            vis = ('news-list-teaser_'+id);
            hid = ('news-list-content_'+id);
            Event.element(event).innerHTML = ">> read less";
    } else {
            hid = ('news-list-teaser_'+id);
            vis = ('news-list-content_'+id);
            Event.element(event).innerHTML = ">> read more";
    }
    


    new Effect.BlindUp(vis, {  duration:0.5,
                                                    afterFinish:function(){
                                                        new Effect.BlindDown(hid, {duration:0.5});
                                                    }
            });


}

function doAjax(event){
    Event.stop(event);
    var clickedLink = Event.element(event);
    new Ajax.Updater('content_left', clickedLink.href+'&type=1', {asynchronous:true});
}


Event.observe(window, "load",  loadScripts , false);
