$(document).ready(function(){
   // --------------------------------------------------------------------------
   // This is a more efficient virtual wall than in films_detail.php, since it
   // REALLY protects me against most of the bullsh*t that lives here.
   // JavaScript-closures FTW.
   
   if (window.PLAYER_CONFIG) {
      (function () {
         var GDocsPlayerWindow = CbSimplePlayerWindow.extend({
               constructor: function (element, options) {
                  this.base(element, $.extend({
                     config:                    'german_documentaries',
                     layerColor:                '#000000',
                     id_type:                   '',
                     versions_autoplay:         true,
                     slides_width:              130,
                     slides_height:             96,
                     slides_separator:          6,
                     slides_video_trigger_icon: '/player40/media/german_documentaries/play_icon_slides.png',
                     play_icon:                 $(document.createElement('img')).attr('src', '/player40/media/german_documentaries/play_icon.png')
                  }, PLAYER_CONFIG, options || {}));
               }
            }),

            GDocsInlinePlayerWindow = GDocsPlayerWindow.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 GDocsLayerPlayerWindow();
                     player.open();
                  });

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

            GDocsLayerPlayerWindow = GDocsPlayerWindow.extend({
               constructor: function (options) {
                  this.base(null, $.extend({
                     width:              810,
                     height:             530,
                     modal:              true,
                     closeButtonElement: $(document.createElement('a')).attr('href', '#')
                  }, 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 GDocsInlinePlayerWindow().open();
      }());
   }
   
   // Do not go below this line. It is scary out there.
   // --------------------------------------------------------------------------
   
   (function () {
      $('img').ifixpng();
      if (typeof(window.onload) != "undefined")
         var oldFunc = window.onload;
      window.onload = function() {
         Shadowbox.init();
         $(window).resize(function(){
            $('#content, #newsbox').css('height','auto');
            $('#content').height(document.body.clientHeight-164+'px');
            if ($('#content').height() > $('#newsbox').height())
               $('#newsbox').height($('#content').height());
            var i = 0;
            while ($('#main').height() > $('#newsbox').height()) {
               if (i == 10)
                  break;
               $('#newsbox').height($('#main').height());
               i++;
            }
            $('.resizeMeToo').height(window.innerHeight-166);
         });
         $('.searchBox').focus(function(){
            if ($(this).val() == $(this).attr('alt'))
               $(this).val('');
            $(this).css('color','#000000');
         }).blur(function(){
            if ($(this).val() == '') {
               $(this).val($(this).attr('alt'));
               $(this).css('color','#c0c0c0');
            }
         });
         if ($("#imagesBox").length > 0) {
            $("#imagesBox").css("height",$("#imagesBox img")[0].height);
            $("#imagesBox img.smallImage").css("margin-top",$("#imagesBox img")[0].height-55);
            $(window).resize(function(){
               if ($("#imagesBox img.smallImage").length > 0) {
                  var firstTop = $("#imagesBox img.smallImage")[0].offsetTop;
                  var lastTop = $("#imagesBox img.smallImage")[$("#imagesBox img.smallImage").length-1].offsetTop;
                  if (firstTop != lastTop) {
                     $("#showImageMore").show();
                  } else {
                     $("#showImageMore").hide();
                  }
               }
            });
            $("#imagesBox img").each(function(){
               var targetThumb = $('#forPic_'+$(this).attr('id').replace('thumb_for',''))[0];
               var linkElement = this;
               $(this).mouseover(function(){
                  $('.picFrame').hide();
                  $(targetThumb).show().css({
                     'top': ((linkElement.offsetTop+linkElement.height)-$(targetThumb).height())+105,
                     'left': this.offsetLeft+15
                  });
               });
               $(targetThumb).hover(function(){},function(){
                  $(this).hide();
               }).click(function(){
                  $(linkElement).click();
               });
            });
         }
         $(window).resize();
         if (typeof(oldFunc) == "function")
            oldFunc();
      }
   }());
});
function searchForFilms() {
   var url = document.location.href.split('films')[0]+'films/search';
   url += '/'+$('select[name=search_categ]').val();
   url += '/'+$('select[name=search_topic]').val();
   url += '/'+$('input[name=search_from]').val();
   url += '/'+$('input[name=search_to]').val();
   url += '/'+$('input[name=search_freetext]').val();
   url += '/1'; //paging
   document.location.href = url;
}
function relatedVideoClick(id){
   id = parseInt(id);
   var url = document.URL.replace(/\d+$/,id);
   window.location.href = url;
}

