$(document).ready(function() {
  
  var running = false;
  
  var offer = {
    title:   $('<div class="offer-title">' + $('.tpl-footer-wrapper .offer-box .HP-pageblock-title:first').text() + '</div>'),
    items:   $('<div class="offer-items" />').append($('.tpl-footer-wrapper .offer-box .page')),
    content: $('<div class="offer-content" />')
  };
  
  
  $('.tpl-footer-wrapper .offer-box').remove();
  $('.tpl-footer-wrapper .offer-switcher').append(offer.title);
  $('.tpl-footer-wrapper .offer-switcher').append(offer.content.append(offer.items));
  
  offer.items.css({
      'position': 'absolute',
      'width': offer.items.children('.page:first').outerWidth(true) * offer.items.children('.page').length
  });
  
  $('.tpl-footer-wrapper .offer-switcher .button.next').click(function() {
    if(running) return;
    var pos = offer.items.position();
    if(offer.items.width() >= (-1*pos.left) + (2*560)) {
      running = true;
      offer.items.animate({'left': pos.left - 560}, function() { running = false });
    } else if(offer.items.width() > (-1*pos.left) + 560) {
      running = true;
      offer.items.animate({'left': (-1*(offer.items.width() - 560))}, function() { running = false });
    }
  });
  
  $('.tpl-footer-wrapper .offer-switcher .button.prev').click(function() {
    if(running) return;
    var pos = offer.items.position();
    if((pos.left + 560) < 0) {
      running = true;
      offer.items.animate({'left': pos.left + 560}, function() { running = false });
    } else if(pos.left != 0) {
      running = true;
      offer.items.animate({'left': 0}, function() { running = false });
    }
  });
  
  /* SlideShow  */
  var imgs = $('.tpl-category-picture img');
                
  if(imgs.length > 1) {
    
    var i  = 0;
    var no = 1;
    var zIndex = 50;
    var first = $(imgs.get(0));
    var next = first.next();
    var tmp = null;
    var timeoutThread = null;
    var effectThread = null;
    var switchRadio = function() {
      
      var currentSwitcher = $('.tpl-category-picture .tpl-switcher a.active');
      var nextSwitcher = currentSwitcher.next();
      
      if(nextSwitcher.length == 0)
        nextSwitcher = $('.tpl-category-picture .tpl-switcher').children().next().next();
      
      $(currentSwitcher[0]).removeClass('active');
      $(nextSwitcher[0]).addClass('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).text(no++);
      
      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).addClass('active');
        
      });
      switchers.push(switcher);
      $('.tpl-category-picture .tpl-switcher').append(switcher);
      
    });
    
    $(switchers).addClass("number");
    
    if(switchers.length > 0)
      $(switchers[0]).addClass('active');
    
    func();
    
    $('a.next').click(function() {
      if($('.number.active').next('.number').length > 0)
        $('.number.active').next('.number').click();
      else
        $('.number.active').parent().children('.number').first().click();
    });
    
    $('a.prev').click(function() {
      if($('.number.active').prev('.number').length > 0)
        $('.number.active').prev('.number').click();
      else
        $('.number.active').parent().children('.number').last().click();
    });
    
  } else {
    $('.tpl-category-picture a.next, .tpl-category-picture a.prev').remove();
  }
  
  
  /* Angebote Slider */
  
  var first = true;
  $('.tpl-content .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-content .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-content .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');
  
  room_images.attr(
    {
      onClick: "return hs.expand(this);",
      'class': "highslide"
    });
  
  room_images.first().addClass("orig");
  
  
});

