/*
 * aheadWorks Co.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://ecommerce.aheadworks.com/LICENSE-M1.txt
 *
 * @category   AW
 * @package    AW_Popup
 * @copyright  Copyright (c) 2010 aheadWorks Co. (http://www.aheadworks.com)
 * @license    http://ecommerce.aheadworks.com/LICENSE-M1.txt
 */
var Popup = {
  dialogId: null,
  onCompleteFunc: null,
  callFunc: null,
  parameters: null,

  olo: function(content, parameters) {
    // Get Ajax return before
    if (content && typeof content != "string") {
      Popup._runAjaxRequest(content, parameters, Popup.olo);
      return
    }
    content = content || "";

    parameters = parameters || {};

    //var okLabel = parameters.okLabel ? parameters.okLabel : "Ok";

    // Backward compatibility
    parameters = Object.extend(parameters, parameters.windowParameters || {});
    parameters.windowParameters = parameters.windowParameters || {};

    parameters.className = parameters.className || "alert";

    parameters.closeButton = '<button onclick="Popup.CloseCallback()">';

    return Popup._openDialog(content, parameters)
  },

  _openDialog: function(content, parameters) {

    var className = parameters.className;

    if (! parameters.height && ! parameters.width) {
      parameters.width = WindowUtilities.getPageSize(parameters.options.parent || document.body).pageWidth / 2;
    }
    if (parameters.id)
      this.dialogId = parameters.id;
    else {
      var t = new Date();
      this.dialogId = 'modal_dialog_' + t.getTime();
      parameters.id = this.dialogId;
    }

    // compute height or width if need be
    if (! parameters.height || ! parameters.width) {
      var size = WindowUtilities._computeSize(content, this.dialogId, parameters.width, parameters.height, 5, className)
      if (parameters.height)
        parameters.width = size + 5
      else
        parameters.height = size + 5
    }
    parameters.effectOptions = parameters.effectOptions ;
    parameters.resizable   = parameters.resizable || false;
    parameters.minimizable = parameters.minimizable || false;
    parameters.maximizable = parameters.maximizable ||  false;
    parameters.draggable   = parameters.draggable || false;
    parameters.closable    = parameters.closable || false;

    var win = new Window(parameters);
    win.show = function(modal){
    this.visible = true;
    if (modal) {
      // Hack for Safari !!
      if (typeof this.overlayOpacity == "undefined") {
        var that = this;
        setTimeout(function() {that.show(modal)}, 10);
        return;
      }
      Windows.addModalWindow(this);

      this.modal = true;
      this.setZIndex(Windows.maxZIndex + 1);
      Windows.unsetOverflow(this);
    }
    else
      if (!this.element.style.zIndex)
        this.setZIndex(Windows.maxZIndex + 1);

    // To restore overflow if need be
    if (this.oldStyle)
      this.getContent().setStyle({overflow: this.oldStyle});

    this.computeBounds();

    this._notify("onBeforeShow");
    if (this.options.showEffect != Element.show && this.options.showEffectOptions)
      this.options.showEffect(this.element, this.options.showEffectOptions);
    else
      this.options.showEffect(this.element);

    this._checkIEOverlapping();
    WindowUtilities.focusedWindow = this
    this._notify("onShow");

    }
    win.setTitle(parameters.title + parameters.closeButton);
    win.setAjaxContent(AjaxUrl+'&check=1',{method: 'get',onSuccess:function(){setTimeout('postFunction()',100)}});

    win.showCenter(true, parameters.top, parameters.left);
    win.setDestroyOnClose();

    win.cancelCallback = parameters.onCancel || parameters.cancel;
    win.okCallback = parameters.onOk || parameters.ok;
    return win;
  },

  _getAjaxContent: function(originalRequest)  {
      Dialog.callFunc(originalRequest.responseText, Dialog.parameters)
  },

  _runAjaxRequest: function(message, parameters, callFunc) {
    if (message.options == null)
      message.options = {}
    Dialog.onCompleteFunc = message.options.onComplete;
    Dialog.parameters = parameters;
    Dialog.callFunc = callFunc;

    message.options.onComplete = Dialog._getAjaxContent;
    new Ajax.Request(message.url, message.options);
  },

  CloseCallback: function() {
    var win = Windows.focusedWindow;
    if(win){
        if (!win.CloseCallback || win.CloseCallback(win)) {
          win.close();
        }
    }
  }

}
  
function postFunction(){
    setTimeout('Popup.CloseCallback()',autoHide*1000);//auto hide popup
    var win = Windows.focusedWindow;
    contentId = win.getId();

    //for IE
    DD_roundies.addRule('.dialog', '5px');

    var popup = document.getElementById(contentId);
    var oldHeight = popup.style.height;
    var newHeight = oldHeight.substr(0,oldHeight.length - 2)*1+10;
    popup.style.height = newHeight + 'px';


    var contentPopup = document.getElementById(contentId + '_content');
    var oldHeight = contentPopup.style.height;
    var newHeight = oldHeight.substr(0,oldHeight.length - 2)-20;
    contentPopup.style.height = newHeight + 'px';

    var dialogPopup = document.getElementById(contentId);
    var dialogHeight = dialogPopup.style.height;
    dialogHeight = dialogHeight.substr(0,dialogHeight.length - 2);
    var displayHeight = window.innerHeight;

    if(align != 2)
        alignPopup();

}
Event.observe(window, 'scroll', function() {
    if(typeof(align) !== 'undefined' && align != 2 && Windows.focusedWindow)
        alignPopup();
});

function alignPopup(){
    var win = Windows.focusedWindow;
    contentId = win.getId();
    var popup = document.getElementById(contentId);
    
    var scrollValue = getScroll();
    var topValue = popup.style.top;

    switch(align){
        case '1':
            topValue = 1 + scrollValue + 'px';
            break
        case '3':
            var displayHeight = document.documentElement.clientHeight
            var popupHeight = popup.style.height;
            popupHeight = popupHeight.substr(0,popupHeight.length - 2)*1 + 13;
            topValue = scrollValue + displayHeight - popupHeight + 'px';
            break
    }
    popup.style.top = topValue;
    
}
function getScroll() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    scrOfY = window.pageYOffset;
  } else if( document.body && document.body.scrollTop ) {
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && document.documentElement.scrollTop) {
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

