var AddToCartNotification = Behavior.create({

  initialize: function () {
    this.elem_overlay = this.create_overlay();
    this.elem_notification = this.element;
    this.elem_checkout_button = $('atc-button-checkout');
    this.elem_continue_button = $('atc-button-continue');

    this.elem_continue_button.observe('click', this.finish.bindAsEventListener(this));
    this.start();
  },

  start: function () {
    this.display_special_elements(false);
    this.show_overlay();
    this.show_notification();
  },

  finish: function (evt) {
    if (evt) evt.stop();
    this.elem_notification.hide();
    this.elem_overlay.hide();
    this.display_special_elements(true);
  },

  create_overlay: function () {
    var elem_body = document.body;
    var elem_overlay = new Element('div', {id: 'atc-overlay'}).hide();
    elem_body.appendChild(elem_overlay);
    return elem_overlay;
  },

  display_special_elements: function (s) {
    // some elements cannot be overlayed reliably, regardless of z-index
    $$('select', 'object', 'embed').each(function (elem) {
      elem.style.visibility = (s ? 'visible' : 'hidden');
    });
  },

  show_overlay: function () {
    var page_size_array = this.getPageSize();
    this.elem_overlay.setStyle({
      width:  page_size_array[0]+'px',
      height: page_size_array[1]+'px'
    });
    new Effect.Appear(this.elem_overlay, {duration: 0.2, from: 0.0, to: 0.8});
  },

  show_notification: function () {
    var page_scroll_array = document.viewport.getScrollOffsets();
    this.elem_notification.setStyle({
      top: (page_scroll_array[1] + (document.viewport.getHeight() * 0.4)) + 'px',
      left: page_scroll_array[0] + 'px'
    }).show();
  },

  // got this from lightbox2 by Lokesh Dhakar
  getPageSize: function() {
    var xScroll, yScroll;

        if (window.innerHeight && window.scrollMaxY) {
            xScroll = window.innerWidth + window.scrollMaxX;
            yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }

        var windowWidth, windowHeight;

        if (self.innerHeight) {	// all except Explorer
            if(document.documentElement.clientWidth){
                windowWidth = document.documentElement.clientWidth;
            } else {
                windowWidth = self.innerWidth;
            }
            windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        }

        // for small pages with total height less then height of the viewport
        if(yScroll < windowHeight){
            pageHeight = windowHeight;
        } else {
            pageHeight = yScroll;
        }

        // for small pages with total width less then width of the viewport
        if(xScroll < windowWidth){
            pageWidth = xScroll;
        } else {
            pageWidth = windowWidth;
        }

        return [pageWidth,pageHeight];
    }

});


Event.addBehavior({
  '#atc-notification': AddToCartNotification
});

