﻿if (typeof(AC) == 'undefined') { AC = {}; }



AC.OverlayScreen = Class.create();
Object.extend(AC.OverlayScreen.prototype, Event.Listener);
Object.extend(AC.OverlayScreen.prototype, {

    initialize: function(overlay) {
        this.listenForEvent(AC.ViewMaster, 'ViewMasterWillShowNotification', false, this.willShow);
        this.listenForEvent(overlay, 'beforeClose', false, this.willClose);
    },

    screen: function(overlayPanel) {
        var body = $(document.documentElement);
        var dimensions = body.getDimensions();

        var width = dimensions.width;
        if (document.body.clientWidth>width) width = document.body.clientWidth;

        var height = dimensions.height;
        if (document.body.clientHeight>height) height = document.body.clientHeight;

        this._screen = new Element('div', { id:'OverlayPanelScreen', className:'overlaypanelscreen',
            style: 'position:absolute; display:none; top:0; left:0; width:'+width+'px; height:'+height+'px;'
        });
        document.body.appendChild(this._screen);

        this._screen.observe('click', overlayPanel.close.bindAsEventListener(overlayPanel));
    },

    escape: function(overlayPanel) {
        this._escape = Event.observe(window, 'keyup', function(evt) {
            if (evt.keyCode == Event.KEY_ESC) {
                if (overlayPanel.currentSection) {
                    overlayPanel.close();
                }
            }
        }.bind(overlayPanel));
    },

    willShow: function(evt) {
        var overlayPanel = evt.event_data.data.sender;
        var incoming = evt.event_data.data.incomingView;

        if (overlayPanel.overlay) {
            if (!this._escape) this.escape(overlayPanel);

            if (incoming) {
                if (!this._screen) this.screen(overlayPanel);
                new Effect.Appear(this._screen, { duration:1, from:0, to:.85 });
            }
        }
    },

    willClose: function(evt, outgoing, incoming) {
        var overlayPanel = evt.event_data.data;

        if (overlayPanel.overlay) {
            new Effect.Fade(this._screen, { duration:.3,
                afterFinish: function(effect) {
                    effect.element.setOpacity(0);
                }
            });
        }
    }
});




Event.onDOMReady(function() {
    AC.OverlayPanel.overlayScreen = new AC.OverlayScreen(AC.OverlayPanel.overlay);
});

var RollOver = Class.create();
RollOver.prototype = {
initialize: function(option) {
  var props = { //外部から指定可能に
    hclass : "rollover", //class=rolloverの要素が対象
    suffix : "_ovr" //オーバー画像はファイル名に_ovrが付いてること
  };
  if(option) for(var key in option) props[key] = option[key];
  $$('img.'+props.hclass,'input.'+props.hclass).each(
    function(el){
      var osrc = el.getAttribute('src');
      if (!osrc) return;
      var hsrc = osrc.replace(/(\.gif|\.jpg)/,props.suffix+'$1');
      (new Image()).src = hsrc; //キャッシュに読込む
      el.observe("mouseover",function(){
        el.setAttribute('src', hsrc);
      }).observe("mouseout",function(){
        el.setAttribute('src', osrc);
      });
    }
  );
}
}//prototype
Event.observe(window,'load',function(){new RollOver();});

