$(document).ready(function() {
  
  var imgs = $('.tpl-header img');
                
  if(imgs.length > 1) {
  
  var i = 0;
  var zIndex = 10000;
  var first = $(imgs.get(0));
  var next = first.next();
  var tmp = null;
  var timeoutThread = null;
  var effectThread = null;
  var switchRadio = function() {
    
    var currentSwitcher = $('.tpl-header .tpl-switcher a.active');
    var nextSwitcher = currentSwitcher.next();
    
    if(nextSwitcher.length == 0)
      nextSwitcher = $('.tpl-header .tpl-switcher a:first');
    
    currentSwitcher[0].className = '';
    nextSwitcher[0].className = 'active';
    
  };
  var func = function() {
    
    timeoutThread = window.setTimeout(function() {
      
      next.hide();
      next.css('zIndex', ++zIndex);
      next.fadeIn(2000, function() {
        
        tmp = next.next();
        
        if(tmp.length != 0)
          next = tmp;
        else
          next = first;
        
        switchRadio();
        func();
        
      });
      
    }, 3000);
    
  };
  
  var switchers = [];
  
  imgs.each(function() {
    
    var img = $(this);
    
    img.css('zIndex', zIndex - i++);
    img.css('position', 'absolute');
    
    var switcher = document.createElement('a');
    switcher.appendChild(document.createTextNode(i));
    
    switcher.swoImg = img;
    
    $(switcher).click(function() {
      
      window.clearTimeout(timeoutThread);
      imgs.stop();
      imgs.hide();
      this.swoImg.show();
      this.swoImg.css('opacity', '1');
      $(switchers).removeClass('active');
      this.className = 'active';
      
    });
    switchers.push(switcher);
    $('.tpl-header .tpl-switcher').append(switcher);
    
  });
  
  if(switchers.length > 0)
    switchers[0].className = 'active';
  
  func();
  
  }
  
  /* Angebote Slider */
  
  var first = true;
  $('.tpl-cnt-left .HP_listing .pages .page div.introduction').css('display', 'none');
  $('.tpl-cnt-left .HP_listing .pages .page div.more').css('display', 'none');
  $('.tpl-cnt-left .HP_listing .pages .page').each(function() {
    if(first == true) {
      $('div.title', this).addClass('open');
      $('div.introduction', this).delay(1000).animate({
         height:'toggle'
      }, 1000);
      $('div.more', this).delay(1000).animate({
         height:'toggle'
      }, 1000);
      first = false;
    } else {
      $('div.title', this).addClass('closed');
      $('div.more', this).css('display', 'none');
      
    }
    
  });
  
  
  /* Angebote All Open/Close */
  
  $('.tpl-cnt-left .HP_listing .pages .page .title').click(function() {
    
    if($(this).hasClass('closed')) {
      $(this).removeClass('closed');
      $(this).addClass('open');
    }
    else {
      $(this).removeClass('open');
      $(this).addClass('closed');
    }
    
    $(this).next().slideToggle('fast');
    $(this).next().next().slideToggle('fast');
    return false;
    
  });
  
  
  var toggleOffers = function(status) {
    console.log(status);
    $('.tpl-cnt-left .HP_listing .pages .page .title').each(function() {
      
      if(status == 'open') {
        $(this).removeClass('closed');
        $(this).addClass('open');
        $(this).next().css('display', 'block');
        $(this).next().next().css('display', 'block');
      } else {
        $(this).removeClass('open');
        $(this).addClass('closed');
        $(this).next().css('display', 'none');
        $(this).next().next().css('display', 'none');
      }
      
    });
    
  }
  
  $('.toggle_offers').click(function() {
    if($(this).hasClass('closed')) {
       $(this).removeClass('closed');
       $(this).addClass('open');
       toggleOffers('open');
       $(this).html('alle Angebote zuklappen');
    }
    else {
      $(this).removeClass('open');
      $(this).addClass('closed');
      toggleOffers('close');
      $(this).html('alle Angebote aufklappen');
    }
  });
  
  
  /* ROOM IMAGES */
  var room_images = $('.OS_shopping_order .box-content .room-images a, .OS_shopping_order .box-content table tbody > tr td a');
  
  if(room_images.length > 0) {
    room_images.attr(
      {
	'onClick': "return hs.expand(this);",
	'class': "highslide"
      });
    
    room_images.first().addClass("orig");
  }
  
});

