var open = false;
var backgroundholder = 'background-image';
var timeout = null;
function openc() {
  if (!open) {
    // schatten
    // evt. generisch generieren
    pictures = get_shadow_picture_array();
    picture = select_randomly_from_array(pictures, 'shadow');
    open_content(picture[0], picture[1])
  }
}

function open_content(width, shadow_number){
  grow_demo_img = 'grow_demo_img'+shadow_number+'';
  close_url = '/images/button-schliessen'+shadow_number+'.png';

  $('schliessen').setStyle({backgroundImage: 'url('+close_url+')'});
  new Effect.Morph('grow_demo', {
    style: 'width:'+width+'px;',
    duration:0.1,
    afterFinish:function(effect){
      new Effect.Parallel([
        new Effect.Morph(grow_demo_img, {style: 'width:'+width+'px; height:469px;'}),
        new Effect.Morph('grow_demo_img_w', {style: 'width:'+width+'px; height:469px;'}),
        new Effect.Morph('content_area', {style: 'width:615px;height:362px;'})], { 
          duration: 0.7,
          afterFinish:function(effect){
            new Effect.Appear('content_area',{duration:0.2});
          }
        }
      );
    }
  });
  set_last(get_index_of_shadow(shadow_number), 'shadow')
  open = true;     
}

function close_content() {
	$('content_area').hide();
	new Effect.Parallel([
	  new Effect.Morph('grow_demo_img1', {style: 'width:0px; height:0px;'}),
	  new Effect.Morph('grow_demo_img2', {style: 'width:0px; height:0px;'}),
	  new Effect.Morph('grow_demo_img3', {style: 'width:0px; height:0px;'}),
	  new Effect.Morph('grow_demo_img4', {style: 'width:0px; height:0px;'}),
	  new Effect.Morph('grow_demo_img5', {style: 'width:0px; height:0px;'}),
	  new Effect.Morph('grow_demo_img6', {style: 'width:0px; height:0px;'}),
	  new Effect.Morph('grow_demo_img7', {style: 'width:0px; height:0px;'}),
		new Effect.Morph('grow_demo_img_w', {style: 'width:0px; height:0px;'}),
		new Effect.Morph('content_area', {style: 'width:0px; height:0px;'})
	], { 	
		duration: 0.5
	});
	open = false;
}

function get_shadow_picture_array() {
  return Array(
    Array(774, 1),
    Array(750, 2),
    Array(727, 3),
    Array(704, 4),
    Array(680, 5),
    Array(656, 6),
    Array(633, 7)
    );
}

function change_background(image, tg) {
  turn_green = tg || false;
  if (turn_green) {
    logo_url = 'url(\'/images/logo_k2_hell.png\')';
  } else {
    logo_url = 'url(\'/images/logo_k2_dunkel.png\')';
  }
  color_navigation();
  var im = image;
  new Effect.Parallel([
    new Effect.Fade(backgroundholder),
    new Effect.Fade('logo')
    ], {
      duration:0.3,
      afterFinish:function(image){
        image_url = 'url(\'/images/'+ im + '\')'; 
        new Effect.Parallel([
          new Effect.Appear(backgroundholder),
          new Effect.Appear('logo')
          ], {
            duration:0.6,
            beforeStart:function(image){
              $(backgroundholder).setStyle({background: image_url});
              $('logo').setStyle({background: logo_url});
            }
          }
        );
      }
    }
  );
  set_last(get_index_of_image(image), 'background');
  reset_countdown();
}
  
var last = new Hash();
last.set('shadow', 0);
last.set('background', 0);
function select_randomly_from_array(given_array, scope) {
  do {
    random = Math.floor(given_array.length*Math.random())
  } while(last.get(scope) == random);
  return given_array[random];
}
function set_last(number, scope) {
  last.set(scope, number);
}
function get_index_of_shadow(shadow_number) {
  i= 0;
  get_shadow_picture_array().each(function(item){
    if (item[1] == shadow_number) result = i;
    i++;
  });
  return result;
}
function get_index_of_image(name) {
  i= 0;
  get_background_pictures_array().each(function(item){
    if (item[0] == name) result = i;
    i++;
  });
  return result;
}

function change_background_randomly() {
  backgrounds = get_background_pictures_array();
  background = select_randomly_from_array(backgrounds, 'background');
  change_background(background[0], background[1]);
} 
function reset_countdown() {
  clearTimeout(timeout);
  timeout = startCountdown();
}
function countdown() {
  change_background_randomly();
  reset_countdown();
}
function startCountdown() {
  return setTimeout('countdown()', 20000);
}





function clock(){
  today = new Date();
  hour = today.getHours();
  minutes = today.getMinutes();
  seconds = today.getSeconds();
  month = today.getMonth() + 1;
  year = today.getYear();
  day = today.getDate();
  if (seconds <=9) seconds="0" + seconds;
  if (minutes <=9) minutes="0" + minutes;
  if (hour <=9) hour="0" + hour;
  if (day <=9) day="0" + day;
  if (month <=9) month="0" + month;
  year = 1900+year;
  $('date').replace('<span id="date"><span style="margin-right:13px">'+ day +'.'+month+'.'+year+'</span>'+hour+':'+minutes+':'+seconds+' Uhr</span>');
  setTimeout('clock()', 1000);
}
var turn_green = true;
var active_link = 'full_service_link'
function color_active_link(){
   $(active_link).setStyle('color:#f6a800;');
}
function color_all_to_given_background() {
  if (turn_green) {
    color = 'color:#878810;';
  } else {
    color = 'color:#ffffff;';
  }
  $('navigation').setStyle(color);
  $$('#navigation a').each(function(s){s.setStyle(color);});
}
function color_navigation(){
  color_all_to_given_background();
  color_active_link();
}
