var flowstep = (58*4);
var flowtime = 0.8;

function initCoverFlow(){
    $$('.imageflowNext').each(function(element) {
        Event.observe(element, 'click',picsNext, false);
        });
    $$('.imageflowPrev').each(function(element) {
      Event.observe(element, 'click',picsPrev, false);
      });
}

function picsNext(event){
    movePics(event, 1);
}

function picsPrev(event){
    movePics(event, -1);
}

function movePics(event, amount){
    imagecontainer = Event.element(event).up('.imageflow').down(1);
    position = parseFloat(imagecontainer.style.left || '0');
    desiredpos = position+(amount*flowstep);

    maxLeft = imagecontainer.up().getWidth()-calcWidth(imagecontainer)-flowstep;

    if((desiredpos <= 0) && (desiredpos > maxLeft)){
        moveByPix(imagecontainer, (amount*flowstep));
    }  else {
        moveByPix(imagecontainer, 0);
    }

}

function calcWidth(imagecontainer){
    var width = 0;
     imagecontainer.immediateDescendants().each(
                    function(el) {
                            width = width+el.getWidth();
                   });
      return  width;
}


function moveByPix(imagecontainer, pixels){
    new Effect.Move(imagecontainer, {x:pixels, y:0,duration:flowtime,
        beforeStart:deactivate,
        afterFinish:initCoverFlow,
        transition:Effect.Transitions.sinoidal 
    });
}


function deactivate(){
    $$('.imageflowNext').each(function(element) {Event.stopObserving(element, 'click', false);});
    $$('.imageflowPrev').each(function(element) {Event.stopObserving(element, 'click', false);});
}

var naviMargins = new Array(0,-5,10,30,5,30,20,0,0,0,0,0,0,0,0,0,0,0);
function moveNavi(){
 var navizaehler = 0;
  if($('submenu')){
    $$('.sub-navi-item').each(function(element) {
          if(element.tagName == "LI") {          
              element.style.marginLeft = naviMargins[navizaehler]+"px";
              navizaehler++;
          }
      }); 
      
   if (window.navigator.userAgent.indexOf("MSIE 7") < 0 && window.navigator.userAgent.indexOf("MSIE 6") > -1){
        new $('submenu').show();  // no animation in ie6
   } else {
         new Effect.BlindDown('submenu',{
                duration:0.5,
                afterFinish:function(){
                      if (window.navigator.userAgent.indexOf("MSIE 7") > -1)  
                          $('submenu').style.overflow='hidden'; // ie7 won't work without                                              
                      }
                }); 
  }
      
   }  

  
  
}

function attachOnclicks(event) {
      $$('.news-list-morelink').each(function(element) {
          if(element.firstChild.tagName == "A") {
            Event.observe(element, "click", showNews, false);
          }
      });
      
      $('content-right').descendants().each(function(element) {
          if(element.tagName == "H2") {
            Event.observe(element, "click", klippKlapp, false);
          }
      });     
      
}

function klippKlapp(event){

   button = Event.element(event);
   if('IMG' == button.tagName) button = button.up();
  content = button.nextSiblings()[0];
  if(button.className == 'graphical-header'){
      button.className = 'graphical-header-closed';
      new Effect.SlideUp(content,{
                duration:0.5,
                beforeStart: function(){
                  // deactivate browsing to prevent flicker
                  Event.stopObserving(button, "click", klippKlapp, false);
                },
                afterFinish: attachOnclicks       
      });
  } else {
      button.className = 'graphical-header';
      new Effect.SlideDown(content, {
                duration:0.5,
                beforeStart: function(){
                  // deactivate browsing to prevent flicker
                  Event.stopObserving(button, "click", klippKlapp, false);
                },
                afterFinish: attachOnclicks
      });    
  }
  
  
}

function wait(element){
    // boring, huh? ....
}


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

function loadScripts(){
          attachOnclicks();
          moveNavi();
          initCoverFlow();         
}  

Event.observe(window, "load",  loadScripts , false);