$(document).ready(function() {
  
  
  /* SIDEBAR RIGHT */
  $('div.tpl-right-blocks div.HP-pageblock').each(function() {
    if($('div.HP-pageblock-title', this).parent().hasClass('reservation') == false) {
      $('div.HP-pageblock-title', this).addClass('closed');
      $('div.HP-pageblock-content', this).css('display', 'none');
    }
  });
  
  $('.HP-pageblock.CMS_workflow').children().first().addClass('open');
  $('.HP-pageblock.reservation').children().first().addClass('open');
  $('.HP-pageblock.reservation').children().first().prepend('<div class="bell" />');
  
  $('div.HP-pageblock-title').click(function() {
    
    if($(this).hasClass('closed')) {
      $(this).removeClass('closed');
      $(this).addClass('open');
    }
    else {
      $(this).removeClass('open');
      $(this).addClass('closed');
    }
    
    $(this).next().slideToggle('fast');
    
  });
  
  /* SlideShow  */
  var imgs = $('.tpl-category-picture img');
                
  if(imgs.length > 1) {
    
    var i  = 0;
    var no = 1;
    var zIndex = 1000;
    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();
    });
    
  }
  
  
  
  /* ONLINE RESERVATION */
  $('.RP_booking_block select[name=room_quantity]').change(function() {
    $('.RP_booking_block span.OR_select_button').text($(this).val());
  }).mouseenter(function() {
    $('.RP_booking_block span.OR_select_button').css('background-position','-120px -370px');
  }).mouseout(function() {
    $('.RP_booking_block span.OR_select_button').css('background-position','-120px -340px');
  }).mousedown(function() {
    $('.RP_booking_block span.OR_select_button').css('background-position','-120px -400px');
  });
  
  
  /* OVERLAY */
  $('div.tpl-right-blocks div.HP-pageblock div.HP-pageblock-content div.button').click(function() {
    
  });
  
});

