Relaxer = function(config) {
  this.mask = null;
  this.win = null;
  this.maximized = false;
  this.normal_height = 600;
  this.hidden = true;
  
  this.addEvents({
    show: true,
    hide: true
  });
  
  Relaxer.superclass.constructor.call(this, config);
  
  this.init();
}

utils.extend(Relaxer, utils.Observable, {
  init: function() {
    $(window).resize(this.onUpdateViewFrame.createDelegate(this))
             .scroll(this.onUpdateViewFrame.createDelegate(this));
  },
  
  show: function() {
    this.showMask();
    this.showWin();
    this.hidden = false;

    this.onShow();
  },
  
  hide: function() {
    this.hideWin();
    this.hideMask();
    this.hidden = true;

    this.onHide();
  },
  
  isHidden: function() {
    return this.hidden;
  },
  
  showWin: function() {
    this.getWin().show();
    this.getWin().setFrame(this.calcWinFrame());
  },
  
  calcWinFrame: function() {
    var view_frame = Magnifier2.Utils.getViewFrame();
    var win_size = view_frame.size.getCopy();
    if(!this.getWin().isMaximized() && this.normal_height < win_size.height)
      win_size.height = this.normal_height;
      
    win_size.width = win_size.height * 1.24;
    win_size = Magnifier2.Utils.scaleRectangleIntoRectangle(win_size, view_frame.size);
    var win_offset = Magnifier2.Utils.getViewFrameCenter().sub(win_size.width / 2, win_size.height / 2);
    
    return new Frame(win_offset, win_size);
  },
  
  hideWin: function() {
    this.getWin().hide();
  },
  
  showMask: function() {
    this.getMask().show();
  },
  
  hideMask: function() {
    this.getMask().hide();
  },
  
  getMask: function() {
    if(!this.mask) {
      this.mask = new Magnifier2.Mask({
        listeners: {
          click: this.onMaskClick,
          scope: this
        }
      });
    }
    return this.mask;
  },
  
  getWin: function() {
    if(!this.win) {
      this.win = new Relaxer.Win({
        listeners: {
          close: this.onClose,
          fullscreen: this.onFullScreen,
          scope: this
        }
      });
    }
    return this.win;
  },
  
  onMaskClick: function() {
    this.hide();
  },
  
  onClose: function() {
    this.hide();
  },
  
  onFullScreen: function() {
    this.getWin().setFrame(this.calcWinFrame());
  },
  
  onUpdateViewFrame: function() {
    if(this.getWin().isShown())
      this.getWin().setFrame(this.calcWinFrame());
  },
  
  onShow: function() {
    this.fireEvent('show', [this]);
  },
  
  onHide: function() {
    this.fireEvent('hide', [this]);
  }
});

Relaxer._instance = null;
Relaxer.instance = function() {
  if(!Relaxer._instance) {
    Relaxer._instance = new Relaxer();
  }
  return Relaxer._instance;
}


/**
 * Relaxer window
 * @param {Object} config
 */
Relaxer.Win = function(config) {
  this.flash_id = null;
  this.maximized = false;
  
  this.addEvents({
    close: true,
    fullscreen: true
  });
  
  Relaxer.Win.superclass.constructor.call(this, config);
}

utils.extend(Relaxer.Win, Magnifier2.Win, {
  calcBodySize: function(win_size) {
    return new Size(
      win_size.width,
      win_size.height
    );
  },
  
  setupFlash: function() {
    this.flash_id = $('<div></div>').insertBefore(this.getCloseButton()).generateId().attr('id');
//    swfobject.embedSWF(
//       "/flash/relaxer/relaxer.swf", this.flash_id,
//        "100%", "100%", "9", false,
//       {}, 
//       { /*allowScriptAccess: "always", */wmode: 'transparent', scale: 'exactfit' }, 
//       {}
//    );
    swfobject.embedSWF(
      "/slideshow/monoslideshow.swf", this.flash_id,
      "100%", "100%", "9", false,
      { dataFile: "/relax.xml" },
      { allowScriptAccess: "always", wmode: 'transparent' },
      {}
    );
  },
  
  getCloseButton: function() {
    return $('.button.close', this.getBodyEl());
  },
  
  getFullScreenButton: function() {
    return $('.button.full_screen', this.getBodyEl());
  },
  
  removeFlash: function() {
    $('#' + this.flash_id).remove();
  },
  
  isMaximized: function() {
    return this.maximized;
  },
  
  _initBody: function($el) {
    $('<div class="button close">[ закрыть ]</div>').appendTo($el).click(this.onClose.createDelegate(this));
    $('<div class="button full_screen">[ на весь экран ]</div>').appendTo($el).click(this.onFullScreenClick.createDelegate(this));    
  },
  
  _createTitle: function($el) {
  },
    
  onShow: function() {
    Relaxer.Win.superclass.onShow.apply(this, arguments);
    
    this.setupFlash();
  },
  
  onHide: function(callback, scope) {
    Relaxer.Win.superclass.onHide.apply(this, arguments);
    
    this.removeFlash();
  },
  
  onClose: function() {
    this.fireEvent('close', [this]);
  },
  
  onFullScreenClick: function() {
    this.maximized = !this.maximized;
    this.fireEvent('fullscreen', [this, this.maximized]);
    this.getFullScreenButton().html(this.maximized ? '[ уменьшить ]' : '[ на весь экран ]');
  }
});