if (!Migros) var Migros = {};
if (!Migros.SpecialOfferFlash) Migros.SpecialOfferFlash = {};
Migros.SpecialOfferFlash.flashHover = false;
Migros.SpecialOfferFlash.imgHover   = false;
Migros.SpecialOfferFlash.setHoverExpiration = function (e) {
  setTimeout(function() {
    if (!(Migros.SpecialOfferFlash.flashHover || Migros.SpecialOfferFlash.imgHover))
    {
      e.hide();
    }
  }, 150);
}
Migros.SpecialOfferFlash.eFlashContainer = $('<div></div>');

Migros.SpecialOfferFlash.initialize = function (x, y, w, h, url) {
  var eFlashExtendImage = $('<img id="flash-more" style="display:none;cursor:pointer;" src="http://www.migros.fr/special-offer/ban.png" />');
  var eFlashZone = $('<div style="cursor:pointer"></div>');

  Migros.SpecialOfferFlash.eFlashContainer
    .appendTo('body')
    .flash({
      src: 'http://www.migros.fr/special-offer/ad.swf',
      height: h,
      width: w,
      AllowScriptAccess: 'always',
      flashvars: {
        height: h,
        width: w,
        url: url
      }
    }, {
      expressInstall: true
    })
    .css({
      position: 'absolute',
      height: '20px',
      width: ''+w+'px',
      left: x,
      top: y,
      'z-index': 5000
    })
    .hover(function () {
      eFlashExtendImage.show();
      Migros.SpecialOfferFlash.flashHover = true;
    }, function () {
      Migros.SpecialOfferFlash.flashHover = false;
      Migros.SpecialOfferFlash.setHoverExpiration(eFlashExtendImage);
    })
    .click(function() {
      window.location = url;
    });

  eFlashExtendImage
    .appendTo('body')
    .css({
      position: 'absolute',
      left: x+(w/2)-90,
      top: y+Migros.SpecialOfferFlash.eFlashContainer.height(),
      'z-index': 5000
    })
    .hover(function () {
      Migros.SpecialOfferFlash.imgHover = true;
    }, function () {
      Migros.SpecialOfferFlash.imgHover = false;
      Migros.SpecialOfferFlash.setHoverExpiration(eFlashExtendImage);
    })
    .click(function() {
      window.location = url;
    });

  Migros.SpecialOfferFlash.eFlashContainer.appendTo('body');

  $(window).resize(function() {
    Migros.SpecialOfferFlash.resize();
  });
}

Migros.SpecialOfferFlash.resize = function () {
  var ref        = Migros.SpecialOfferFlash.getReference();
  var flashWidth = parseFloat(Migros.SpecialOfferFlash.eFlashContainer.css('width'));
  var position   = ref - flashWidth;

  Migros.SpecialOfferFlash.eFlashContainer
    .css('left', position);
}

