$(document).ready(function() {
   /*
   alert('WAAAARGH!11einseinself!1!!');   // das musste ich mal rauslassen
   return false;                          // und tschüss ;)
   */

   // headerImage hover effect 
   $('.headerImage').hover(function() {
      $(this).children('.layerText').stop().css({'opacity':'0.5'})
         .animate({'height':'32px','opacity':'1'}, 250).end()
         .children('.layerBackground').stop().animate({'height':'32px'}, 250)
         .end().children('.button').stop().animate({'opacity':'1'}, 250);
   }, function() {
      $(this).children('.layerText').stop()
         .animate({'height':'0px','opacity':'0.5'}, 500).end()
         .children('.layerBackground').stop().animate({'height':'0px'}, 500)
         .end().children('.button').stop().animate({'opacity':'0.5'}, 500);
   }).children('.layerBackground').css({'opacity':'0.5'}).end()
      .children('.button').css({'opacity':'0.5'});

   // event search form
   $('.extra-links .week').click(function() {
      $('#cb-search-form input[name="startDate"]').remove();
      $('#cb-search-form input[name="endDate"]').remove();
      $('#cb-search-form input[name="event_datetime"]').val($(this).attr('ref'));
      $('#cb-search-form').submit();
   });
   
   // search forms helper
   jQuery.fn.defaultValuedInput = function(default_value) {
      jQuery(this).each(function() {
         jQuery(this).focus(function() {
            if (jQuery(this).val() == default_value || jQuery(this).val() == '') {
               jQuery(this).val('');
            }
            jQuery(this).addClass('active');
         }).focus().blur(function() {
            if (jQuery(this).val() == default_value || jQuery(this).val() == '') {
               jQuery(this).val(default_value).removeClass('active');
            }
         }).blur();
      });
   }
   
   // search forms
   $('.searchInput').defaultValuedInput($('.searchInputDefaultValue').html());
   
   // fix services
   $('.press_check > .entry > .entry').each(function() {
      var text = $(this).children('.name').children('.text');
      $(this).children('.children').prepend(text.clone());
      text.remove();
   });
   
   // fix search form
   $('.go-button').click(function() {
      $(this).parent().parent()[0].submit();
      return false;
   });
   
   /* 
    * cb-player
    * 
    * */
   
   if (window.PLAYER_CONFIG) {
      (function () {
         var AgdokPlayerWindow = CbSimplePlayerWindow.extend({
               constructor: function (element, options) {
                  this.base(element, $.extend({
                     config:                    'agdok_inline',
                     layerColor:                '#000000',
                     id_type:                   '',
                     versions_autoplay:         true,
                     slides_width:              130,
                     slides_height:             96,
                     slides_separator:          6,
                     slides_video_trigger_icon: '/player40/media/agdok_inline/play_icon_slides.png',
                     play_icon:                 $(document.createElement('img')).attr('src', '/player40/media/agdok_inline/play_icon.png')
                  }, PLAYER_CONFIG, options || {}));
               }
            }),

            AgdokInlinePlayerWindow = AgdokPlayerWindow.extend({
               constructor: function (options) {
                  var self = this,
                  viewport = $('#player-viewport');

                  viewport.find('.expand-button').unbind('click.player').bind('click.player', function (e) {
                     e.preventDefault();
                     self.reset();
                     var player = new AgdokLayerPlayerWindow();
                     player.open();
                  });
                  
                  // click on pic bar player resets inline player
                  $('.logo_box .thickbox2').click(function(){
                     self.reset();
                     return false;
                  });

                  this.base(viewport, $.extend({
                     modal:  false
                  }, PLAYER_CONFIG, options || {}));
               }
            }),

            AgdokLayerPlayerWindow = AgdokPlayerWindow.extend({
               constructor: function (options) {
                  this.base(null, $.extend({
                     width:              810,
                     height:             530,
                     modal:              true,
                     closeButtonElement: $(document.createElement('a')).attr('href', '#')
                  }, PLAYER_CONFIG, options || {}));

                  // Add version selector.
                  $(document.createElement('div')).addClass('versions')
                     .append($(document.createElement('select')).addClass('__CbUiPlayerVersions'))
                     .appendTo(this.element().children('div'));

                  // Add additional images.
                  $(document.createElement('div')).addClass('__CbUiPlayerSlides')
                     .appendTo(this.element().children('div'));
               },

               handleReady: function (options) {
                  /* Add layer padding. */
                  this.resizeX(options.width + 2 * 30);
                  this.centerX();

                  return this.base(options);
               }
            });

         new AgdokInlinePlayerWindow().open();
      }());
   }
});

$(window).load(function() {
   positionZoomIcon();
});

function positionZoomIcon() {
   $('.portrait').each(function() {
      if ($(this)[0].height > 20) {
         $(this).parent().children('.zoom').css('top',($(this).offset().top + $(this)[0].height - 11)+'px');
      }
   });
}

function closeThickbox() {
 jQuery("body, html").css("overflow","");
   $('#TB2_overlay, #TB2_window').remove();
   $('#TB2_overlay, #TB2_window, #TB2_HideSelect').remove();
}

function styleCbSearch() {
   $('.visitMenuLabel').each(function() {
      var label = $(this).parent().children('.cbSearchLink').html().replace(/^.*http:\/\/web1\.heimat\.de\/agdok\/[a-z]{2}_[A-Z]{2}\/([^\/]+)\/.*$/, '$1').replace(/^.*http:\/\/www\.agdok\.de\/[a-z]{2}_[A-Z]{2}\/([^\/]+)\/.*$/, '$1').split('_')[0];
      var caption = $('.cbSearchLabel2Caption').children('.'+label).html();
      $(this).html(caption);
   });
}

function submitform(myfield) {
   if (window && window.event && window.event.keyCode == 13) {
      myfield.form.submit();
      return false;
   }
}


