/* Global functions called by Flash Player */
function shopPopup(url) {
      window.open(url, 'cbshop', 'width=650,height=550,dependent=no,hotkeys=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
}
function relatedVideoClick(id){
   id = parseInt(id);
   var url = document.URL.replace(/\/(td)?\d+/,'/'+id);
   window.location.href = url;
}
/* CbUi */
var CbPlayerWindow = jQuery.CbWidget.window.extend({
   constructor : function(element, options) {
      this.base({element : element}, $.extend({
         showShadow : false,
         modal : false}, options));
      return this;
   },

   /**
    * shortcut to start all players in this window.
    */
   play : function() {
      var self = this;
      jQuery(document).ready(function() { // wait till all img are created
         self.element().find('.__CbUiPlayer').each(function() {
            jQuery(this).CbWidget().play();
         });
      });
      return this;
   },

   handleReady : function(options) {
      options.widgets = {};
      this.element().find('.__CbUiPlayer').each(function() {
         options.widgets.player = $(this).CbWidget();
      });
      this.element().find('.__CbUiPlayerSlides').each(function() {
         options.widgets.slides = $(this).CbWidget();
      });
      this.element().find('.__CbUiPlayerVersions').each(function() {
         options.widgets.versions = $(this).CbWidget();
      });
      this.element().find('.__CbUiPlayerControls').each(function() {
         options.widgets.controls = $(this).CbWidget();
      });
      for (var i in options.widgets) {
         options.widgets[i].trigger('ready', options);
      }
   }
}, {
   create : function(element, options, parent_window, ready_callback) {
      if (typeof parent_window === "string") parent_window = window[parent_window];
      if (!parent_window) parent_window = window.CbPlayerWindow;

      options = jQuery.extend({
         'supported_players' : jQuery.CbWidget.player.detect(),
         'options_mode' : 'ui',
         'options_format' : 'json',
         'callback' : '/player40/'
      }, options || {});
      jQuery.getJSON(options.callback, options, function (data) {
         data = jQuery.extend(data, options); // override with manual choices
         var player_window = new parent_window(element, data).open();
         if (data.autoplay) player_window.play();
         if (typeof(ready_callback) == 'function') ready_callback(player_window);
      });
   }
});

/**
 * Simplified and preconfigured CbPlayerWindow widget.
 */

// closure for jQuery shorthand notation
var CbSimplePlayerWindow = (function ($) {
   return CbPlayerWindow.extend({
      constructor: function (element, options) {
         options = $.extend({
            modal:               true,
            layerColor:          '#000000',
            layerFrame:          false,
            layerOpacity:        0.9,
            overlayClose:        true,
            delay:               500,
            autoResize:          true,
            imageTransformation: 'cropOut',
            closeButtonElement:  null,
            id_type:             ''
         }, options);

         // Automatically resize the given image to fit our needs.
         if (options.autoResize) {
            options.image = 'http://data.heimat.de/transform.php'
               +'?file='+encodeURIComponent(options.image)
               +'&width='+options.width
               +'&height='+options.height
               +'&do='+options.imageTransformation;
         }

         this.base(element, options);

         if (this.insertElement) {
            $(document.createElement('div')).addClass('__CbUiPlayer')
               .appendTo(this.element().children('div'));
         }
      },

      reset: function () {
         var playerElement = this.element().find('.__CbUiPlayer'),
            playerWidget = playerElement.CbWidget();

         playerWidget.reset();
         return this;
      },

      handleReady: function (options) {
         var baseReturnValue = this.base(options),
            self = this;

         if (this.insertElement) {
            this.autocenterX();
            this.moveToY(30);
         }

         if (this.options.closeButtonElement !== null) {
            this.options.closeButtonElement.addClass('__CbUiFrameCloseButton')
               .click(function (e) {
                  e.preventDefault();
                  self.close();
               })
               .appendTo(this.element());
         }

         return baseReturnValue;
      }
   });
}(jQuery));

