/** Install Easing Plugin: http://gsgd.co.uk/sandbox/jquery/easing/ */
jQuery.easing['jswing'] = jQuery.easing['swing'];
jQuery.extend(jQuery.easing, {

  def: 'easeOutQuad',
  swing: function (x, t, b, c, d) {
    //alert(jQuery.easing.default);
    return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
  },
  easeInQuad: function (x, t, b, c, d) {
    return c*(t/=d)*t + b;
  },
  easeOutQuad: function (x, t, b, c, d) {
    return -c *(t/=d)*(t-2) + b;
  },
  easeInOutQuad: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t + b;
    return -c/2 * ((--t)*(t-2) - 1) + b;
  }
});

(function(jQuery) {
  jQuery.fn.storckSlider = function(options) {

    /* "this" ist das jQuery-Objekt, auf das das Plugin angewendet wird.
     *  Z.B. $('#slideShow').slider(); -> this == $('#slideShow')
     *
     *  Das jQuery-Object wird "return"t, damit das Chaining weiterhin möglich ist:
     *  $('#slideShow').slider().css('color', 'red');
     *
     *  "each" wird verwendet, da ja mit einem Selektor mehrere Elemente angesprochen
     *  werden können: $('div.slideShow').slider();
     *
     *  "options" sind die Optionen für die Initialisierung des Plugins:
     *  $('#slideShow').slider({
     *    direction: 'left',
     *    speed: 1000,
     *    onComplete: function(param){ alert('Fertig'); }
     *  });
     *  (Siehe auch unten die "defaults".)
     */
    return this.each(function() {
      init($(this), options);
    });
  };

  /**
   * Plugin-weite Variabeln
   */
  var opts = {};

  /**
   * Private functions
   */
  function init($obj, o) {
    // Merge options mit defaults
    mergeOptions(o);

    opts.count = $obj.find(opts.child_selector).length;
    
    // Show bigger images, if less or equal big_limit items are available
    if (opts.count <= opts.big_limit) {
      $obj.addClass("big");
      opts.width = opts.big_width;
    }

    // only proceed, if there are more elements, then displayslots
    if( opts.count <= opts.visible ){
      // MouseOver effect for product images if enabled and linked
      jQuery.storckSlider._mouseOver($obj);
      return true;
    }

    // remove empty nodes from the list
    $obj.find(opts.slides_selector).each(function(){
      if( $(this).children().length == 0 )
        $(this).detach();
    });

    // add clickhandler to right arrow
    $obj.find(opts.next_selector).click(function() {
      jQuery.storckSlider._moveTo($obj, "right");
      return false;
    });

    // add clickhandler to left arrow
    $obj.find(opts.prev_selector).click(function() {
      jQuery.storckSlider._moveTo($obj, "left" );
      return false;
    });

    // hide slider first
    if (opts.direction == 'left') {
      $obj.find(opts.slider_selector).css( 'marginLeft', parseInt( $obj.css('width') ) );
      var position = opts.visible;
    }
    else {
      $obj.find(opts.slider_selector).css('marginLeft', opts.count * opts.width * -1);
      var position = 0;
    }
    // Slide in upon window load
    $(window).load(function() {
      if (opts.direction == 'left') {
        var init_pos = -opts.width * ($obj.find(opts.slides_selector).length - position);
      }
      else {
        var init_pos = 0;
      }
      $obj.find(opts.slider_selector).delay(600).animate({
        'marginLeft': init_pos
      }, 2000, 'easeOutQuad', function() {
        jQuery.storckSlider._checkArrows($obj);
        // MouseOver effect for product images if enabled and linked
        jQuery.storckSlider._mouseOver($obj);
      });
    });
  }

  // Merge options mit defaults
  function mergeOptions(o) {
    opts = jQuery.extend( {}, jQuery.fn.storckSlider.defaults, o);
  }

  // Public function base
  jQuery.storckSlider = {};

  /**
   * PLUGIN PART
   */

  /* doSomeThing kann auch außerhalb des Plugins aufgerufen werden:
   * jQuery.storckFlyoutMenu.doSomeThing();
   * (Sinnvoll, wenn man auch Tests für das Plugin schreiben möchte.)
   */
  jQuery.storckSlider._checkArrows = function($obj, position) {
    if (position == undefined) {
      position = parseInt($obj.find(opts.slider_selector).css("marginLeft")) / -opts.width;
    }
    if (opts.direction == 'left') {
      // we need the 'prev' button (<)
      if ( position > 0 || opts.endless)
        $obj.find(opts.prev_selector).removeClass( 'disabled' );
      else
        $obj.find(opts.prev_selector).addClass( 'disabled' );
      // we  need 'next' button (>)
      if( position < opts.visible -1 || opts.endless)
        $obj.find(opts.next_selector).removeClass( 'disabled' );
      else
        $obj.find(opts.next_selector).addClass( 'disabled' );
    }
    else {
      if (position <= 0 && !opts.endless) {
        $obj.find(opts.prev_selector).addClass("disabled");
      }
      else {
        $obj.find(opts.prev_selector).removeClass("disabled");
      }
       
      if (position >= opts.count - opts.visible && !opts.endless) {
        $obj.find(opts.next_selector).addClass("disabled");
      }
      else {
        $obj.find(opts.next_selector).removeClass("disabled");
      }
    }
  },

  // moves the slider by one slide in the given direction
  jQuery.storckSlider._moveTo = function($obj, dir ) {
    // For the zoom of big packs
    $obj.find("ul a").unbind('mouseenter');
    
    var $slider = $obj.find(opts.slider_selector);
    var $slides = $obj.find(opts.slides_selector);
    var sliderLeft = parseInt($slider.css("marginLeft"))
    var position = sliderLeft / -opts.width;
    if (dir == 'left') {
      position = Math.floor(position - 1);
    }
    else {
      position = Math.ceil(position + 1);
    }
    jQuery.storckSlider._checkArrows($obj, position);
    if (opts.endless) {
      if (position < 0) {
        sliderLeft = -opts.width;
        $slides.last().prependTo($slider);
        $slider.css({'marginLeft': sliderLeft});
        position = 0;
      }
      if (position > $slides.length - opts.visible) {
        position = $slides.length - opts.visible;
        sliderLeft = (position - 1) * -opts.width;
        $slides.first().appendTo($slider);
        $slider.css({'marginLeft': sliderLeft});
      }
    }
    $slider.stop(true);
    $slider.animate({
      'marginLeft': -opts.width * position
    }, function() {
      jQuery.storckSlider._mouseOver($obj);
    });
  }
  
  
  jQuery.storckSlider._mouseOver = function($obj) {
    if (opts.mouseOver) {
      // To hover the image, we place a big image over it with the same link and hide the original.
      var hover = $obj.find('a.hover');
      if (hover.length == 0) {
        hover = $('<a href="" class="hover"><img src="" alt="" /></a>').hide();
      }
      $obj.append(hover);
      // On mouseover over a product, make it big size!
      $obj.find("ul a").mouseenter(function() {
        var elem = $(this);
        var hover = $obj.find('a.hover');
        hover.attr('href', elem.attr('href'));
        var org_src = elem.find('img').attr('src');
        hover.find("img").attr({'src': org_src.replace("brands", "brands/big"), 'lowsrc' : org_src}).css({width: '100%', height: '100%'});
        // Hide the original image, but keep it's space occupied
        $obj.find("ul a").css('visibility', 'visible');
        elem.css('visibility', 'hidden');
        // Move the hover to the final position and show it.
        var left = elem.offset().left;
        hover.stop();
        hover.css({left : left - 220, bottom: 0, position: 'absolute', width: 146, height: 97, display: 'block'}).animate({left : left - 232, width: 169, height: 112}, 200, 'easeInOutQuad');
        // Hide the left resp right arrow, if the left or right product is hovered
        if (left == 235) {
          $obj.find(opts.prev_selector).addClass('disabled');
        }
        if (left == 839) {
          $obj.find(opts.next_selector).addClass('disabled');
        }
      }).each(function() {
        // Cache all images
        if (typeof cache == "undefined") {
          cache = [];
        }
        cache.push($('<img>').attr('src', $(this).find("img").attr('src').replace("brands", "brands/big")));
      });
      // On mouseout of the big size image, restore the original state.
      hover.mouseleave(function() {
        if( opts.count > opts.visible ){
          jQuery.storckSlider._checkArrows($obj, opts.position );
        }
        $(this).hide();
        $obj.find("ul a").css('visibility', 'visible');
      });
    }
  }

  /**
   *  Plugin default options
   */
  /* Die Standardkonfiguration des Plugins kann auch von extern verändert werden:
   * jQuery.fn.storckSlider.defaults.option1 = false;
   * */
  jQuery.fn.storckSlider.defaults = {
    width: 120,
    visible: 6,
    // If less or equal than big_limit items are available, show bigger images (add class big to elem)
    big_limit: 0,
    big_width: 146,
    // Direction of the initial animation. Will stop at the last element
    direction: 'left',
    slider_selector: 'ul',
    slides_selector: 'ul li',
    child_selector: 'ul li img',
    next_selector: 'a[rel=next]',
    prev_selector: 'a[rel=prev]',
    endless: false
  };

})(jQuery);

(function(jQuery) {
  jQuery.fn.storckFlyoutMenu = function(options) {
    return this.each(function() {
      init($(this), options);
    });
  };

  /**
   * Plugin-weite Variabeln
   */
  var opts = {};

  /**
   * Private functions
   */
  function init($obj, o) {
    // Merge options mit defaults
    mergeOptions(o);
    
    var container = $obj.find(opts.containerSelector);
    // Exit, if no submenu is available or if I'm a IE 7 or earlier.
    if (container.length == 0 || ($.browser.msie && $.browser.version <= 7)) {
      return true;
    }
    
    $obj.hover(jQuery.storckFlyoutMenu.show, jQuery.storckFlyoutMenu.triggerHide);
    $('#connector, #flyout').hover(jQuery.storckFlyoutMenu.clearHide, jQuery.storckFlyoutMenu.triggerHide);
  }

  // Merge options mit defaults
  function mergeOptions(o) {
    opts = jQuery.extend( {}, jQuery.fn.storckFlyoutMenu.defaults, o);
  }

  // Public function base
  jQuery.storckFlyoutMenu = {};

  /**
   * PLUGIN PART
   */
  jQuery.storckFlyoutMenu.show = function() {
    jQuery.storckFlyoutMenu.clearHide();
    var obj = $(this);
    var connector = $('#connector');
    var menu = $('#flyout');
    
    connector.css({
      width: obj.width() + opts.padding * 2,
      left: obj.position().left + opts.offsetLeft
    }).show();
    menu.html(obj.find(opts.containerSelector).html()).css({
      left: obj.position().left + opts.offsetLeft
    }).show();
  }
  
  // Clear the timeout, since we entered another element
  jQuery.storckFlyoutMenu.clearHide = function() {
    window.clearTimeout(jQuery.storckFlyoutMenu.timeout);
  }
  
  jQuery.storckFlyoutMenu.triggerHide = function() {
    jQuery.storckFlyoutMenu.clearHide();
    jQuery.storckFlyoutMenu.timeout = window.setTimeout(jQuery.storckFlyoutMenu.hide, 500);
  }
  
  jQuery.storckFlyoutMenu.hide = function() {
    $('#connector, #flyout').hide();
  }
  
  /**
   *  Plugin default options
   */
  /* Die Standardkonfiguration des Plugins kann auch von extern verändert werden:
   * jQuery.fn.storckFlyoutMenu.defaults.option1 = false;
   * */
  jQuery.fn.storckFlyoutMenu.defaults = {
    containerSelector : 'div.flyout',
    padding : 4,
    offsetLeft : 10
  };

})(jQuery);

(function($) {

  $.storck = function(){};

    // start initial animation
    // this looks a little bit complicate, but it only moves the slider from right to left
    // until the rightmost item is visible
    // after animation is complete, we need to show the arrows



  /**
   *  Registers callback function, triggered on hashchange
   *
   *    "#this-is-my-tab" -> openCallback
   *    "#"               -> closeCallback
   *
   *  Usage:
   *    $.storck.hash( function( new_hash ) {
   *      console.log( "Hash has changed to" + new_hash );
   *    }, function() {
   *      console.log( "Hash has been removed" );
   *    });
   */
  $.storck.hash = function( openCallback, closeCallback ) {

    $.storck._processHash( openCallback, closeCallback );
    // register hashchange event
    $(window).hashchange( function() {
      $.storck._processHash( openCallback, closeCallback )
    });

  }
  $.storck._processHash = function( openCallback, closeCallback ) {

    if( window.location.hash && window.location.hash != '#' ) {

      closeCallback();
      openCallback( window.location.hash );

    } else closeCallback();

    return false;

  }

  /**
   * Prepare Lightboxes with javascript to reduce markup (used in inet_brand)
   *
   * example markup:
   *  <div class="lightbox">
   *    <h3>My awesome header</h3>
   *    <p>And some content, too</p>
   *  </div>
   *
   *  becomes:
   *  <div class="lightbox">
   *    <div class="header"></div>
   *    <div class="content>
   *      <h3>My awesome header</h3>
   *      <p>And some content, too</p>
   *    </div>
   *    <a href="#" class="close">Schließen</a>
   *  </div>
   *
   *  Every lighbox-container that has got the class 'video' will be skipped
   */
  $.storck.lightbox = function( selector, templates ) {

    this._tpl = {
      header: '<div class="header"></div>',
      close: '<a href="#" class="close">%text%</a>'
    }

    this._selector = selector || 'div.lightbox';

    if( templates ) $.extend( this._tpl, templates );

    var that = this;
    $( this._selector ).each(function() {
      var item = $(this).find("div.bg_middle > div");
      item.prepend( that._tpl.header );
      item.append( $.storck._render( that._tpl.close, "X" ) );

    });

  }

  /**
   *  Group multiple-lightboxes and add pagination to them
   */
  $.storck.multipage = function() {

    // only continue if there are any multipages
    if ( $('div.multi').length == 0 )
      return null;

    this._tpl = {

      nav: "<ul class='nav'></ul>",
      navPrev: "<li><a href='%link%' rel='prev'>&lt;</a></li>",
      navNext: "<li><a href='%link%' rel='next'>&gt;</a></li>",
      navLink: "<li><a href='%link%' class='number'>%name%</a></li>"

    }

    // { multipage_1: [{id: mulipage_1_4, number: 4}, {id: multipage_1_5, number: 5}] }
    this._groups = {};

    var that = this;

    // find multipages and group them by id
    //
    // i.e. multipage_1_4, multipage_1_6, multipage_1_8 belong to group
    // 'multipage_1'
    $('div.multi').each(function() {

      var parsed = ( /(multi_page_\d+)_(\d+)/ ).exec( $(this).attr('id') ),
          group = parsed[1];

      that._groups[group] = that._groups[group] || [];

      that._groups[group].push({

        id: parsed[0],
        number: parsed[2]

      });

    });
    
    if (this._groups.multi_page_0.length == 1) {
      return false;
    }

    // generate pagination toolbar for each group
    $.each(this._groups, function( i, group ) {

      var count = group.length;

      $.each(group, function( i, page ) {

        var nav = $(that._tpl.nav);

        if( i == 0 )
          var prevLink = $.storck._render( that._tpl.navPrev, '#'+page.id.replace(/multi_page/, "multipage"), true );

        else
          var prevLink = $.storck._render( that._tpl.navPrev, '#'+group[ i-1 ].id.replace(/multi_page/, "multipage"), true );

        if( i == count-1 )
          var nextLink =  $.storck._render( that._tpl.navNext, '#'+page.id.replace(/multi_page/, "multipage"), true );

        else
          var nextLink = $.storck._render( that._tpl.navNext, '#'+group[ i+1 ].id.replace(/multi_page/, "multipage"), true );

        // Add previous link to navigation (<)
        nav.append(prevLink);

        // generate numbered links for all pages
        $.each(group, function( number ) {

          var link = $.storck._render( that._tpl.navLink, {
            link: '#'+ this.id.replace(/multi_page/, "multipage"),
            name: number+1
          }, true );

          nav.append( link );

        });

        nav.append(nextLink);

        $('#'+page.id).find('div.header').first().append(nav);

      });

    });

  }

  /**
    * Internal template-rendering engine
    *
    * Usage:
    *   var my_template = "<a href='%url%'>%link_text</a>"
    *   var parameters = {
    *     url: 'http://storck.com',
    *     link_text: 'Storck - Part of Your World'
    *   }
    *   $.stock._render( my_template, parameters )
    *
    *   -> "<a href='http://storck.com'>Storck - Part of Your World</a>"
    *
    * If parameter 'make_object' is true the function returns a jquery object
    */
  $.storck._render = function( tpl, params, make_object ) {

    // param is string
    if( typeof params === typeof "" )
        tpl = tpl.replace(/%\w+%/g, params);

    // param is hashmap
    else
      $.each(params, function(key, val) {
        tpl = tpl.replace('%'+key+'%', val);
      });

    return make_object? $(tpl) : tpl;

  }

})(jQuery);

(function(jQuery) {
  jQuery.fn.brandAni = function(options) {
    
    /* "this" ist das jQuery-Objekt, auf das das Plugin angewendet wird.
     *  Z.B. $('#slideShow').slider(); -> this == $('#slideShow')
     *  
     *  Das jQuery-Object wird "return"t, damit das Chaining weiterhin möglich ist:
     *  $('#slideShow').slider().css('color', 'red');
     *  
     *  "each" wird verwendet, da ja mit einem Selektor mehrere Elemente angesprochen
     *  werden können: $('div.slideShow').slider();
     *  
     *  "options" sind die Optionen für die Initialisierung des Plugins:
     *  $('#slideShow').slider({
     *    direction: 'left', 
     *    speed: 1000, 
     *    onComplete: function(param){ alert('Fertig'); }
     *  });
     *  (Siehe auch unten die "defaults".)
     */
    return this.each(function() {
      init($(this), options);
    });
  };
  
  /**
   * Plugin-weite Variabeln
   */
  var opts = {};
  var $current;
  var $container;
  
  /**
   * Private functions
   */
  function init($obj, o) {
    
    // Merge options mit defaults
    mergeOptions(o);
    
    if ($obj.children().length < 2) {
      return false;
    }
    if ($.browser.msie && $.browser.version < 9) {
      $obj.addClass("noCanvas");
      $(window).load(function() {
        $obj.find(opts.logoSelector).replaceWith(function() {
          var $img = $(this);
          $('body').append('<img class="hiddenImg" src="' + $img.attr('src') +'"');
          var $hiddenImg = $('body > img:last');
          // Manual PNG fix
          return $('<div id="div_' + $img.attr('id') + '" class="' + $img.attr('class') + '" style="width: ' + $hiddenImg.width() + 'px; height: ' + $hiddenImg.height() + 'px"><div style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + $img.attr('src') + '\', sizingMethod=\'scale\'); width: ' + $hiddenImg.width() + 'px; height: ' + $hiddenImg.height() + 'px"></div></div>');
        }).removeAttr('id');
        $(opts.logoSelector).css({opacity: 0});
      });
    }
    
    $container = $obj;
    $current = $obj.children(opts.slideSelector).first();

    window.setInterval(jQuery.brandAni.slide, opts.timeout);
  }
  
  // Merge options mit defaults
  function mergeOptions(o) {
    opts = jQuery.extend( {}, jQuery.fn.brandAni.defaults, o);
  }
  
  function logoFadeIn(bgId, imgId) {
    if (imgId == undefined) {
      return;
    }
    var animationStep = 0;
    var animationInterval = window.setInterval(function() {
      var alpha = animationStep / 23.0;
      if (alpha > 1) {
        alpha = 1;
      }
      if ($.browser.msie && $.browser.version < 9) {
        $('#' + imgId).css({opacity: alpha});
      } else {
        stackBlurImage( bgId, imgId, opts.canvasId, Math.ceil(5 - alpha * 5), true, alpha);
        $('#' + opts.canvasId).show();
      }
      
      if (animationStep > 23) {
        window.clearInterval(animationInterval);
      }
      animationStep++;
    }, 13);
  }
  
  function logoFadeOut(bgId, imgId) {
    if (imgId == undefined) {
      return;
    }
    var animationStep = 0;
    var animationInterval = window.setInterval(function() {
      var alpha = 1 - animationStep / 23.0;
      if (alpha < 0) {
        $('#' + opts.canvasId).hide();
        alpha = 0;
      }
      if ($.browser.msie && $.browser.version < 9) {
        $('#' + imgId).css({opacity: alpha});
      }
      else {
        stackBlurImage( bgId, imgId, opts.canvasId, Math.ceil(alpha * 5), true, alpha);
      }
      
      if (animationStep > 23) {
        window.clearInterval(animationInterval);
      }
      animationStep++;
    }, 13);
  }
  
  // Public function base
  jQuery.brandAni = {};

  /**
   * PLUGIN PART
   */
  
  /* Goto next slide
   */
  jQuery.brandAni.slide = function($obj) {
    var $next = $current.next(opts.slideSelector);
    
    if ($next.length == 0) {
      $next = $container.children(opts.slideSelector).first();
      $container.children(opts.slideSelector).not(':first, :last').hide();
      $current.fadeOut(1000);
    }
    else {
      $next.fadeIn(1000);
      window.setTimeout(function() {
        logoFadeIn($current.find(opts.bgSelector).attr("id"), $current.find(opts.logoSelector).attr("id"));
      }, 800);
      window.setTimeout(function() {
        logoFadeOut($current.find(opts.bgSelector).attr("id"), $current.find(opts.logoSelector).attr("id"))
      }, opts.timeout - 600);
    }
    
    $current = $next;
  },
  
  /**
   *  Plugin default options
   */
  jQuery.fn.brandAni.defaults = {
    slideSelector: "div.aniblock",
    timeout: 5000,
    canvasId: "brand_logo",
    bgSelector: "img.anibg",
    logoSelector: ".anilogo"
  };

})(jQuery);

$(document).ready(function() {
  $('#nav > ul > li').storckFlyoutMenu();
  
  // Fetch matching Big8 products upon brand selection
  $('body.inet_big8_nutritions, body.inet_big8_start').find('select[name=brand]').change(function() {
    $.get(
      'get_products.php', 
      { 
        brand : $(this).val(), 
        market : $("input[name=market]").val(),
        lang : $('input[name=lang]').val(),
        select_product : $('input[name=select_product]').val()
      },
      null,
      'script'
    );
  });
  
  // Listen to hash changes and display the according tab for the big8_nutritions
  if ($('body.inet_big8_nutritions').length > 0) {
    $('div.tab').css({
      position: 'absolute',
      left: -500,
      top: 0,
      opacity: 0
    });
    $.storck.hash(
      function(hash) {
        // hash will be something like tab=info
        var tab_label = hash.match(/tab=([a-z]+)/);
        if (tab_label != null) {
          tab_label = tab_label[1];
        }
        else {
          tab_label = 'info';
        }
        var tab = $('a[href="#tab=' + tab_label + '"]').parent();
        var tab_body = $('#' + tab_label);
        if (tab.length != 1 || tab_body.length != 1){
          tab = $('a[href="#tab=info"]');
          tab_body = $('#info');
        }
        // If the clicked tab is already active, do nothing
        if (!tab.hasClass('active')) {
          var last_active_tab = $("#tabNavigation > li.active");
          
          // First access, so we do not animate
          if (last_active_tab.length == 0) {
            tab_body.css({
              opacity : 1.0,
              left : 0,
              display : 'block'
            });
          }
          // Following access, smooth animation required
          else {
            last_active_tab.removeClass("active");
            var last_active_id = last_active_tab.find("a").attr("href").split("=")[1];
            $('#' + last_active_id).animate({
              left : -500,
              opacity : 0.0
            });
            tab_body.css({display : 'block', opacity : 0.0}).delay(100).animate({
              left: 0,
              opacity: 1.0
            });
          }
          tab.addClass('active');
        }
      },
      function(){} // doNothingLoop ;-) - Ausblenden müssen wir nichts
    );
    // If nothing is selected, press the first link
    if ($('#tabNavigation li.active').length == 0) {
      window.location.hash = "tab=info";
    }
  }

  if( $('body').hasClass('inet_brand') || $('body').hasClass('inet_homepage') ) {

    new $.storck.lightbox();
    new $.storck.multipage();

    $.storck.hash(

      // if there is a hash, show matching element
      // for multipages, we need to highlight the active number
      function( element ) {
        $(element.replace(/multipage/, "multi_page")).show();
        $('div.lightbox div.header a.number[href$='+element+']').addClass('active');
        $('#stage').hide();
      },

      // hide all & remove active highlighting
      function() {
        $('div.lightbox').hide();
        $('div.lightbox div.header a.number').removeClass('active');
        $('#stage').show();
      }
    );
  }
  
  $('body.inet_brand #stage').storckSlider({visible: 5, width: 151, direction: 'right', mouseOver : true});
  $('body.inet_homepage .teaser_container').storckSlider({visible: 5, width: 184, child_selector: 'ul li img.teaser', direction: 'right', endless: true});
  $('body.inet_horizontal_scroll #content').storckSlider({visible: 2, direction: 'right', width: 330, child_selector: 'div > ul > li > *:first-child', slides_selector: 'div > ul > li', slider_selector: 'div > ul'});
  
  $('body.inet_homepage a[href=#videoplayer], body.inet_brand a[href=#videoplayer]').click(function(event) {
    var link = $(this).parent().children(':first');
    $('body.inet_brand #stage').hide();
    event.preventDefault();
    $('#videoplayer_box').show();
    swfobject.embedSWF("/inet_superglobal/flash/storckvideoplayer.swf", "videoplayer_player", "544", "371", "9","", { env: "prod", videoPath: link.attr('data-video'), bgColor: "0xcfcfcf", playerWidth: "544", playerHeight: "371"}, { allowScriptAccess: "always", wmode : 'opaque', bgcolor: "#ffffff"});
  });
    $('body.inet_homepage a[href=#swf], body.inet_brand a[href=#swf]').click(function(event) {
      var link = $(this).parent().children(':first');
      $('body.inet_brand #stage').hide();
      event.preventDefault();
      $('#videoplayer_box').show();
      swfobject.embedSWF(link.attr('data-swf'), "videoplayer_player", "544", "371", "9","", {}, { allowScriptAccess: "always", wmode : 'opaque', bgcolor: "#ffffff"});
      var close = $('<a href="#" class="close">X</a>');
      close.click(function(event) {
        event.preventDefault();
        closeVideoPlayer();
      });
      $('#videoplayer_box .bg_middle').append(close);
    });
    $('body.inet_homepage a[href=#img], body.inet_brand a[href=#img]').click(function(event) {
      var link = $(this).parent().children(':first');
      $('body.inet_brand #stage').hide();
      event.preventDefault();
      $('#videoplayer_box').show();
      $('#videoplayer_player').html($('<img src="'+ link.attr('data-img') +'" alt="" />'));
      var close = $('<a href="#" class="close">X</a>');
      close.click(function(event) {
        event.preventDefault();
        closeVideoPlayer();
      });
      $('#videoplayer_box .close').remove();
      $('#videoplayer_box .bg_middle').append(close);
    });
    
  $('body.inet_brand.previewmode a.teaser_img:empty').remove();
  $('body.inet_brand.previewmode a[href^="javascript:imperia_create_img_win"]').addClass("teaser_img");
  
  // Deeplink open Videoplayer on hash = #videoplayer
  if(location.hash == "#videoplayer"){
    $('a.play[href=#videoplayer]').first().trigger('click');
  }
  
  // Deeplink open Videoplayer on hash = #image
  if(location.hash == "#image"){
    $('a.play[href=#img]').first().trigger('click');
  }
  
  $('body.inet_homepage .brandani').brandAni();
});

function closeVideoPlayer() {
  $('body.inet_homepage').find('#videoplayer_box > div.bg_middle').html('<div id="videoplayer_player"></div>');
  $('#videoplayer_box > div.bg_middle .close').remove();
  $('body.inet_homepage, body.inet_brand').find('#videoplayer_box').hide();
  $('body.inet_brand #stage').show();
}

