/// <reference name "MicrosoftAjax.js" assembly="System.Web.Extensions" />

function PopupWindow(popupWindowID, roundedContentID, width, height) {
  
  this.popupWindow = $(popupWindowID);
  
  this.popupParent = null;
  
  if (this.popupWindow) {
    this.popupParent = this.popupWindow.parentNode;
    window.onresize = new this.ResizeEventHandler(this).invoke;
    window.onscroll = new this.ResizeEventHandler(this).invoke;
  }
  
  this.roundedContent = $(roundedContentID);
   
  this.popupBackgroundID = 'popupWindowBackground';
  
  this.popupPlaceHolderID = 'popupPlaceHolder';
  
  this.popupHideFrameID = 'popupHideFrame';
  
  this.popupBackground = $(this.popupBackgroundID); 
  
  this.popupPlaceHolder = $(this.popupPlaceHolderID); 
  
  this.popupHideFrame = $(this.popupHideFrameID); 
   
  this.initialSize = new iFramework.Dimension(width, height);
  
  this.minSize = new iFramework.Dimension(200, 200);
  
  this.minPadding = 40;
  
  this.roundedWidthDifference = 60;
  
  this.roundedHeightDifference = 161;
  
  this.size = null;
  
  this.setSize(new iFramework.Dimension(width, height));
}

PopupWindow.prototype.showHide = function () {
  if (this.popupWindow && this.popupBackground) {
    if (!this.popupWindow.visible()) {
      this.show();
    }
    else {
      this.hide();
    }
  }
};

PopupWindow.prototype.moveToPlaceHolder = function () {
  if (this.popupWindow && this.popupPlaceHolder && this.popupParent) {
    this.popupParent.removeChild(this.popupWindow);
    this.popupPlaceHolder.appendChild(this.popupWindow);
  }
};

PopupWindow.prototype.moveToParent = function () {
  if (this.popupWindow && this.popupPlaceHolder && this.popupParent) {
    this.popupPlaceHolder.removeChild(this.popupWindow);
    this.popupParent.appendChild(this.popupWindow);
  }
};

PopupWindow.prototype.show = function () {
  if (this.popupWindow && this.popupBackground) {
    iFramework.Browser.setScrollEnabled(false);
    this.popupBackground.show();
    this.moveToPlaceHolder();
    this.popupWindow.show();
    this.setBackgroundSize();
    this.setBounds(); 
    if (iFramework.Browser.isIE6() && this.popupHideFrame) {
      this.popupHideFrame.show();
    }
  }
};

PopupWindow.prototype.hide = function () {
  if (this.popupWindow && this.popupBackground) {    
    iFramework.Browser.setScrollEnabled(true);
    this.popupBackground.hide();
    this.popupWindow.hide();    
    this.moveToParent();
    if (iFramework.Browser.isIE6() && this.popupHideFrame) {
      this.popupHideFrame.hide();
    }    
  }
};

PopupWindow.prototype.getSize = function() {
  return this.size;
};

PopupWindow.prototype.getInitialSize = function() {
  return this.initialSize;
};

PopupWindow.prototype.getMinSize = function() {
  return this.minSize;
};

PopupWindow.prototype.setMinSize = function(newValue) {
  this.minSize = newValue;
};

PopupWindow.prototype.getMinPadding = function() {
  return this.minPadding;
};

PopupWindow.prototype.setMinPadding = function(newValue) {
  this.minPadding = newValue;
};

PopupWindow.prototype.setSize = function(newSize) {  
  if (this.popupWindow && this.roundedContent && newSize.compareTo(this.getMinSize()) == 1) {
    this.size = newSize;  
    this.popupWindow.style.width = newSize.getX() + 'px';
    this.roundedContent.style.width = newSize.getX() - this.roundedWidthDifference + 'px';
    this.roundedContent.style.height = newSize.getY() - this.roundedHeightDifference + 'px';        
  }
};

PopupWindow.prototype.setPosition = function(newPosition) {
  if (this.popupWindow != null) {
    this.popupWindow.style.left = newPosition.getX() + 'px';
    this.popupWindow.style.top = newPosition.getY() + 'px';
    if (iFramework.Browser.isIE6() && this.popupHideFrame) {
      this.popupHideFrame.style.left = newPosition.getX() + 'px';
      this.popupHideFrame.style.top = newPosition.getY() + 'px';
      this.popupHideFrame.style.width = this.getSize().getX() + 'px';
      this.popupHideFrame.style.height = this.getSize().getY() + 'px';
    }
  }
};

PopupWindow.prototype.setBounds = function() {
  if (this.popupBackground) {
    windowSize = iFramework.Browser.getActiveWindowSize();
    currentX = this.getSize().getX();
    currentY = this.getSize().getY();
    //Test minimal size.
    if (windowSize.getX() - this.getMinPadding() < currentX) {
      currentX = windowSize.getX() - this.getMinPadding();
    }    
    if (windowSize.getY() - this.getMinPadding() < currentY) {
      currentY = windowSize.getY() - this.getMinPadding();
    }
    
    //Test initial size.
    if (currentX < this.getInitialSize().getX() && windowSize.getX() - this.getMinPadding() > currentX) {
      currentX = Math.min(this.getInitialSize().getX(), windowSize.getX() - this.getMinPadding());
    }    
    if (currentY < this.getInitialSize().getY() && windowSize.getY() - this.getMinPadding() > currentY) {
      currentY = Math.min(this.getInitialSize().getY(), windowSize.getY() - this.getMinPadding());
    }    
    
    this.setSize(new iFramework.Dimension(currentX, currentY));
    
    //Set position.    
    newLeft = (windowSize.getX() - this.popupWindow.clientWidth) / 2;
    newTop = (windowSize.getY() - this.popupWindow.clientHeight) / 2;
    scrollPosition = iFramework.Browser.getScrollPosition();
    if (scrollPosition != null) {
      newLeft += scrollPosition.getX();
      newTop += scrollPosition.getY();
    }
    this.setPosition(new iFramework.Dimension(newLeft, newTop));
  }
};

PopupWindow.prototype.setBackgroundSize = function() {
  if (this.popupBackground) {
    size = iFramework.Browser.getWindowSize();
    this.popupBackground.style.width = size.getX() + 'px';
    this.popupBackground.style.height = size.getY() + 'px';
  }
};

PopupWindow.prototype.ResizeEventHandler = function(popup) {
  this.invoke = function () {
    popup.setBackgroundSize();
    popup.setBounds();
  }
};
