var SHRINK_INTERVAL = 10;    // Schrittweite beim Scrollen
var SHRINK_DIF = 25;        // Interval beim Scrollen (je groesser, desto schneller geht das Scrollen)
var PAUSE_TEXT = "pause";   // Beschriftung des Pause-Buttons
var PLAY_TEXT = "play";     // Beschriftung des Play-Buttons
var NEXT_PAGE_INTERVAL = 4000;  // Interval für das anzeigen des nächsten Inhalts in Millisekunden
//var NEXT_NEWS_INTERVAL = 8000; // Interval für das anzeigen des nächsten News Teasers in Millisekunden
var NEXT_TEASER_INTERVAL = 5000;

var CURRENT_PAGE = 0;
var NUMBER_OF_PAGES = 0;
var PREVIOUS_PAGE;
var PLAY_STATUS = "play";
var DIRECTION = 1; 
var INTERVAL_OBJEKT = 0;
var BUTTONS_DEACTIVATED = false;
var INTERVAL_OBJEKT_NEWS = 0;
var INTERVAL_OBJECT_TEASER = 0;
var SWITCH_PAGE = 3;
//var PAGE_NEWS = "noes";

function show(message)
  {
  document.getElementById("output").innerHTML = message;
  }

function deactive_buttons()
  {
  //document.getElementById("previous_button").disabled = true;
  //document.getElementById("next_button").disabled = true;
  BUTTONS_DEACTIVATED = true;
  }

function active_buttons()
  {
  //document.getElementById("previous_button").disabled = false;
  //document.getElementById("next_button").disabled = false;
  BUTTONS_DEACTIVATED = false;
  }

function read_divs()
  {
  var main = document.getElementById("scrollerID");
  try {
  if(document.getElementById("box2")) document.getElementById("box2").style.display = "inline";
  if(document.getElementById("box3")) document.getElementById("box3").style.display = "inline";
  if(document.getElementById("box4")) document.getElementById("box4").style.display = "inline";
  if(document.getElementById("box5")) document.getElementById("box5").style.display = "inline";
  if(document.getElementById("box6")) document.getElementById("box6").style.display = "inline";
  } catch(e){}
  
  pages = new Array();
  
  next_child = main.firstChild;
  do
    {
    if (next_child.className=="content_div")
      pages[pages.length] = next_child;
    }
  while(next_child = next_child.nextSibling);
  
  NUMBER_OF_PAGES = pages.length - 1;

  for(var i=0;i<pages.length;i++)
    {
    pages[i].style.zIndex = 0;
    pages[i].setAttribute("size_to_restore",pages[i].offsetWidth);
    pages[i].style.display = "none";
    }

  pages[CURRENT_PAGE].style.display = "inline";
  document.getElementById("play_pause_button").innerHTML = PAUSE_TEXT;
  
  build_thumbnails();
  
  INTERVAL_OBJECT = window.setInterval("play_content()",NEXT_PAGE_INTERVAL);
//  INTERVAL_OBJEKT_NEWS = window.setInterval("play_news()",NEXT_NEWS_INTERVAL);
  INTERVAL_OBJECT_TEASER = window.setInterval("play_teaser()",NEXT_TEASER_INTERVAL);
  
  build_switchbox();
  }
  
function build_thumbnails()
  {
  var thumbnail_array = new Array();
  var iLen = pages.length;
  //if(iLen>4) iLen = 5;
  for(var i=0;i<iLen;i++)
    {
    thumbnail_array[thumbnail_array.length] = "<span id='slider_thumb_container_"+i+"'><a onfocus='this.blur();' id='slider_thumb_a_" + i + "' href='javascript: show_content_page(" + i + ")'><img width='92' height='55' id='slider_thumb_img_" + i + "' src='" + pages[i].getAttribute("thumbnail") + "' class='thumbnail' border=0></a></span>";
    }
  document.getElementById("thumbnails_border").innerHTML = thumbnail_array.join("<br>");
  }

function show_content_page(page)
  {
  if (!BUTTONS_DEACTIVATED)
    {
    stop_interval();
    if (page!=CURRENT_PAGE)
      {
      PREVIOUS_PAGE = CURRENT_PAGE; 
      CURRENT_PAGE = page;
      DIRECTION = 1;
      
      show_content();
      } 
    start_interval();
    }   
  }

var check_switch_counter = 0
function check_switch(page){
  //if(page == SWITCH_PAGE) switch_page_5(SWITCH_PAGE);
  //if(page == 5) switch_page_5(SWITCH_PAGE);
  if(++check_switch_counter>3){
    switch_page_5(SWITCH_PAGE);
    check_switch_counter = 0;
  }
}

function switch_page_5(page){
  try{
    if(document.getElementById('slider_thumb_container_5')){
      var a_href = document.getElementById('slider_thumb_a_'+page).href;
      var img_src = document.getElementById('slider_thumb_img_'+page).src;
      document.getElementById('slider_thumb_a_'+page).href = document.getElementById('slider_thumb_a_5').href;
      document.getElementById('slider_thumb_img_'+page).src = document.getElementById('slider_thumb_img_5').src;
      document.getElementById('slider_thumb_a_5').href = a_href;
      document.getElementById('slider_thumb_img_5').src = img_src;
    }
  }catch(e){}
}

function play_content()
  {
  if (PLAY_STATUS == "play")
    {
    if (DIRECTION>0) next_content();
    else previous_content();
    }
  }
  
/*function play_news()
  {
  if (PAGE_NEWS=="enew")
    {
	  document.getElementById("enew").style.display = "none";
	  document.getElementById("noes").style.display = "inline";
	  PAGE_NEWS = "noes";
	}
  else if (PAGE_NEWS=="nint")
    {
	  document.getElementById("nint").style.display = "none";
	  document.getElementById("enew").style.display = "inline";
	  PAGE_NEWS = "enew";
	}
  else
    {
	  document.getElementById("noes").style.display = "none";
	  document.getElementById("nint").style.display = "inline";
	  PAGE_NEWS = "nint";
	}
  }*/
  
function play_pause()
  {
  stop_interval();
  if (PLAY_STATUS=="play") 
    {
    PLAY_STATUS = "pause";
    document.getElementById("play_pause_button").innerHTML = PLAY_TEXT;
    }
  else
    {
    PLAY_STATUS = "play";
    document.getElementById("play_pause_button").innerHTML = PAUSE_TEXT;
    start_interval();
    }
  }

function stop_interval()
  {
  window.clearInterval(INTERVAL_OBJECT);
  INTERVAL_OBJECT = 0;
  }

function start_interval()
  {
  INTERVAL_OBJECT = window.setInterval("play_content()",NEXT_PAGE_INTERVAL);
  }

function previous_content()
  {
  if (!BUTTONS_DEACTIVATED)
    {
    stop_interval();
    PREVIOUS_PAGE = CURRENT_PAGE; 
    if (CURRENT_PAGE==0) CURRENT_PAGE = NUMBER_OF_PAGES;
    else CURRENT_PAGE = CURRENT_PAGE - 1;
    
    DIRECTION = -1;
    
    show_content();
    start_interval();
    }    
  }
  
function next_content()
  {
  if (!BUTTONS_DEACTIVATED)
    {
    stop_interval();
    PREVIOUS_PAGE = CURRENT_PAGE; 
    if (CURRENT_PAGE==NUMBER_OF_PAGES) CURRENT_PAGE = 0;
    else CURRENT_PAGE = CURRENT_PAGE + 1;
    
    DIRECTION = 1;
    
    show_content(); 
    start_interval();
    }   
  }
  
function show_content()
  {
  deactive_buttons();
  
  if (CURRENT_PAGE > PREVIOUS_PAGE) var action = "forward";
  else var action = "back";
  
  if (CURRENT_PAGE==NUMBER_OF_PAGES && PREVIOUS_PAGE==0)
    action = "back";
    
  if (CURRENT_PAGE==0 && PREVIOUS_PAGE==NUMBER_OF_PAGES)
    action = "forward";
  
  if (pages[CURRENT_PAGE].getAttribute("xl"))
    enlarge_screen(document.getElementById("scrollerID"));
  
  if (pages[PREVIOUS_PAGE].getAttribute("xl"))
    shrink_screen(document.getElementById("scrollerID"));
  
  check_switch(CURRENT_PAGE);
      
  switch(action)
    {
    case "forward":
      pages[CURRENT_PAGE].style.zIndex = 1;
      pages[PREVIOUS_PAGE].style.zIndex = 2;
      pages[CURRENT_PAGE].style.display = "inline";
      shrink_to_the_left(pages[PREVIOUS_PAGE],1);
      break;
    case "back":
      pages[CURRENT_PAGE].style.zIndex = 2;
      pages[PREVIOUS_PAGE].style.zIndex = 1;
      obj_resize(pages[CURRENT_PAGE],1);
      pages[CURRENT_PAGE].style.display = "inline";
      grow_to_the_right(pages[CURRENT_PAGE],1);
      break;
    }
    
  
  /*for(var i=0;i<pages.length;i++)
    {
    if (i==CURRENT_PAGE) pages[i].style.display = "inline";
    else pages[i].style.display = "none";
    }
  */
  }
  
function grow_to_the_right(obj)
  {
  var me = this;
  destination_width = obj.getAttribute("size_to_restore");
  if (getRealWidth(obj)<destination_width)
    {
    width = getRealWidth(obj) + SHRINK_DIF;
    obj_resize(obj,width);
    window.setTimeout(function() { me.grow_to_the_right(obj); },SHRINK_INTERVAL);
    }
  else
    {
    obj_resize(obj,destination_width);
    pages[PREVIOUS_PAGE].style.display = "none";
    active_buttons();
    }
  }

  
function enlarge_screen(obj)
  {
  var me = this;
  destination_width = obj.getAttribute("xl_size");
  thumb_obj = document.getElementById("thumbnails_border");
  if (getRealWidth(obj)<destination_width)
    {
    width = getRealWidth(obj) + SHRINK_DIF;
    obj_resize(obj,width);
    width = getRealWidth(thumb_obj) - SHRINK_DIF;
    obj_resize(thumb_obj,width);
    window.setTimeout(function() { me.enlarge_screen(obj); },SHRINK_INTERVAL);
    }
  else
    {
    obj_resize(obj,destination_width);
    obj_resize(thumb_obj,0);
    }
  }

function shrink_screen(obj)
  {
  var me = this;
  destination_width = obj.getAttribute("default_size");
  thumb_obj = document.getElementById("thumbnails_border");
  if (getRealWidth(obj)>destination_width)
    {
    width = getRealWidth(obj) - SHRINK_DIF;
    obj_resize(obj,width);
    width = getRealWidth(thumb_obj) + SHRINK_DIF;
    obj_resize(thumb_obj,width);
    window.setTimeout(function() { me.shrink_screen(obj); },SHRINK_INTERVAL);
    }
  else
    {
    obj_resize(obj,destination_width);
    obj_resize(thumb_obj,thumb_obj.getAttribute("default_size"));
    }
  }

function shrink_to_the_left(obj)
  {
  var me = this;
  width = getRealWidth(obj);
  if (width>1)
    {
    width = width - SHRINK_DIF;
    obj_resize(obj,width);
    window.setTimeout(function() { me.shrink_to_the_left(obj); },SHRINK_INTERVAL);
    }
  else
    {
    obj.style.display = "none";
    obj.style.width = obj.getAttribute("size_to_restore") + "px";
    active_buttons();
    }
  }
  
function obj_resize(obj,x)
	{
	if (x<1) x = 1;
	obj.style.width = x + "px";
	}
      
function getRealLeft(imgElem)
	{
	xPos = eval(imgElem).offsetLeft;
	tempEl = eval(imgElem).offsetParent;
    while (tempEl != null)
		{
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    	}
  	return xPos;
	}        
	
function getRealWidth(obj)
	{
	return obj.offsetWidth;
	}
function addEvent( obj, type, fn )
 {
 if (obj.addEventListener)
  {
     obj.addEventListener( type, fn, false );
     }
 else if (obj.attachEvent)
  {
  obj["e"+type+fn] = fn;
       obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
       obj.attachEvent( "on"+type, obj[type+fn] );
     }
 }

/*
function teaser_set_title_internal(img_b,img_c_title){
  var a = $(img_b).childElements();
  if(a[0]){
    img_b_img = $(a[0]);
    try{img_b_img.title = img_c_title;}catch(e){}
    try{img_b_img.alt = img_c_title;}catch(e){}
    try{img_b_img.writeAttribute('title',img_c_title);}catch(e){}
    try{img_b_img.writeAttribute('alt',img_c_title);}catch(e){}
  }
}

function teaser_set_title(e){
  var e = $(e);
  if(e && e.childElements){
    var a = e.childElements();
    var img_a = $(a[2]);
    var img_b = $(a[3]);
    var img_c = $(a[4]);
      var img_c_title = img_c.readAttribute('title');
      if(img_c_title && !img_c_title.blank()){
        teaser_set_title_internal(img_a,img_c_title);
        teaser_set_title_internal(img_b,img_c_title);
      }
  }
}
*/

/**
 * Teaser Mouseover Text
 **/ 

function teaser_set_title(e){
  var imgs;
  
  e = $(e);
  if(e){
    imgs = e.getElementsBySelector('img');
    if(imgs){
      if(imgs.length == 3){
        if(imgs[1].title.blank()) return;
        
        imgs[0].title = imgs[1].title;
        imgs[0].alt = imgs[1].title;
        imgs[2].title = imgs[1].title;
        imgs[2].alt = imgs[1].title;
      }
      if(imgs.length == 4){
        if(imgs[2].title.blank()) return;
        
        imgs[0].title = imgs[2].title;
        imgs[0].alt = imgs[2].title;
        imgs[1].title = imgs[2].title;
        imgs[1].alt = imgs[2].title;
        imgs[3].title = imgs[2].title;
        imgs[3].alt = imgs[2].title;
      }
    }
  }
}

/**
 * Teaser Mouseover Image
 **/ 

var last_teaser_switch = 0;
function try_teaser_switch_over(e){
  if(last_teaser_switch == e) return;
  if(try_teaser_switch(e)) last_teaser_switch = e;
}
function try_teaser_switch_out(e){
  try_teaser_switch(e);
  last_teaser_switch = 0;
}
function try_teaser_switch(e){
  var a,img_a,img_b,tmp;
  
  e = $(e);
  if(e && e.parentNode.childElements){
    a = e.parentNode.childElements();
    img_a = a[2];
    img_b = $(a[3]);
    if(img_b.innerHTML.blank()) return false;
    tmp = img_b.innerHTML;
    img_b.innerHTML = img_a.innerHTML;
    img_a.innerHTML = tmp;
    return true;
  }
  return false;
}

/**
 * Teaser Switch
 **/
 
var play_teaser_cnt = new Object();
play_teaser_cnt["a1"] = 0;
play_teaser_cnt["a2"] = 0;
play_teaser_cnt["a3"] = 0;
play_teaser_cnt["b1"] = 0;
play_teaser_cnt["b2"] = 0;
play_teaser_cnt["b3"] = 0;
play_teaser_cnt["c1"] = 0;
play_teaser_cnt["c2"] = 0;
play_teaser_cnt["c3"] = 0;

function play_teaser_show(base){
  var t = $(base);
  var ta = $(base + 'a');
  var tb = $(base + 'b');
  play_teaser_cnt[base]++;
  if(play_teaser_cnt[base]>2) play_teaser_cnt[base] = 0;
  if(play_teaser_cnt[base] == 1 && !ta) play_teaser_cnt[base] = 2;
  if(play_teaser_cnt[base] == 2 && !tb) play_teaser_cnt[base] = 0;
  
  if(play_teaser_cnt[base] == 0){
    if(t) {
      t.show();
      if(ta) ta.hide();
      if(tb) tb.hide();
    }
  } 
  if(play_teaser_cnt[base] == 1){
    if(ta) {
      ta.show();
      if(tb) tb.hide();
      if(t) t.hide();
    }
  }
  if(play_teaser_cnt[base] == 2){
    if(tb){
      tb.show();
      if(t) t.hide();
      if(ta) ta.hide();
    }
  }
  
}
function play_teaser(){
  play_teaser_show('a1');
  play_teaser_show('a2');
  play_teaser_show('a3');
  play_teaser_show('b1');
  play_teaser_show('b2');
  play_teaser_show('b3');
  play_teaser_show('c1');
  play_teaser_show('c2');
  play_teaser_show('c3');
}

/* Switchbox */

var switchboxes = 0;
var switchbox_data = new Object();
var switchbox_cnt = new Object();
var switchbox_interval = 0;
var g_i_tmp = 0;

function build_switchbox(){
  switchboxes = $$('div[rel="switchbox"]');
  if(switchboxes){
    switchboxes.each(
      function(val,key){
        var tmp = val.select('div');
        if(tmp){
          switchbox_data[key] = val.select('div');
          switchbox_cnt[key] = 0;
          g_i_tmp = 0;
          tmp.each(
            function(elem,k){
              if(g_i_tmp == 0) elem.show();
              else elem.hide();
              g_i_tmp++;
            }
          );
          val.show();
          if(!switchbox_interval) switchbox_interval = window.setInterval('play_switchbox()',5000);
        }
      }
    );
  }
}

function play_switchbox(){
  if(switchboxes){
    //dbg_log('-- sb init --');
    //dbg_dir(switchboxes);
    //dbg_log('-- sb start --');
    switchboxes.each(
      function(sb,key){
        var curr = switchbox_cnt[key];
        var next = curr+1;
        if(next > switchbox_data[key].length - 1) next = 0;
        //dbg_log(key + '(' + switchbox_data[key].length + ') ' + curr + ' -> ' + next);
        if(curr == next) return;
        switchbox_data[key][curr].hide();
        switchbox_data[key][next].show();
        switchbox_cnt[key] = next;
      }
    );
    //dbg_log('-- sb end --');
  }
}

function dbg_log(v){
  try{ console.log(v); } catch(e){}
}
function dbg_dir(v){
  try{ console.dir(v); } catch(e){}
}  