/// <reference name "MicrosoftAjax.js" assembly="System.Web.Extensions" />

function PopupWindow(popupWindowID, popupParentIDHiddenFieldID, roundedContentID, width, height, roundedWidthDifference, roundedHeightDifference) {

  this.popupWindow = $get(popupWindowID);
  
  this.popupParent = null;
  
  if (this.popupWindow) {
    //when you call this function instead of all the code below, popupWindow.clientWidth and Height are zero)
    //this.popupParent = this.getPopupParent(popupParentIDHiddenFieldID);

    var popupParentIDHiddenField = $get(popupParentIDHiddenFieldID);

    //The class is loaded for the first time
    if(popupParentIDHiddenField.value == '') {
    
      //The popup is hidden, and its parent node is the parent we need
      this.popupParent = this.popupWindow.parentNode;
      if(this.popupParent.id == '') {
        //Generate the id automatically. If the popupWindow is updated through ajax, 
        //the popupParent won't be re-renderred with ajax,
        //because the popupWindow is situated in the custom update panel, when it's visible; so the id will remain
        this.popupParent.id = this.generatePopupParentID();
      }
      
      //Save the parent id to the hidden field
      popupParentIDHiddenField.value = this.popupParent.id;
    }
    //The class is loaded after postback
    else {
      //The hidden field has runat="server" attribute, so its value is saved in roundtrips.
      this.popupParent = $get(popupParentIDHiddenField.value);
      
      //The popupParent with automatically generated id was updated through ajax, so its id was lost.
      if(this.popupParent == null) {
         //But it means that the popup is hidden, and its parent node is the parent we need
         this.popupParent = this.popupWindow.parentNode;
         this.popupParent.id = this.generatePopupParentID();
         //Save the parent id to the hidden field 
         //(actually, the generated id is always the same after postbacks, 
         //so the hidden field alredy contains this id, but the generation algorithm may be changed)
         popupParentIDHiddenField.value = this.popupParent.id;
      }
    }
    //MS AJAX function
    $addHandler(window, 'resize', new this.ResizeEventHandler(this).invoke);
    $addHandler(window, 'scroll', new this.ResizeEventHandler(this).invoke);
  }
  
  this.roundedContent = $get(roundedContentID);
  
  this.popupBackgroundID = 'popupWindowBackground';
  
  this.popupPlaceHolderID = 'popupPlaceHolder';
  
  this.popupHideFrameID = 'popupHideFrame';
  
  this.popupBackground = $get(this.popupBackgroundID); 
  
  this.popupPlaceHolder = $get(this.popupPlaceHolderID); 
  
  this.popupHideFrame = $get(this.popupHideFrameID); 
   
  this.initialSize = new iFramework.Dimension(width, height);
  
  this.minSize = new iFramework.Dimension(0, 0);
  
  this.minPadding = 0;
  
  this.roundedWidthDifference = roundedWidthDifference || 0;
  
  this.roundedHeightDifference = roundedHeightDifference || 0;
  
  this.size = null;
  
  this.setSize(new iFramework.Dimension(width, height));
}

PopupWindow.prototype.generatePopupParentID = function () {
  return this.popupWindow.id + '_AutomaticallyGeneratedPopupParentID';
};

PopupWindow.prototype.showHide = function () {
  if (this.popupWindow && this.popupBackground) {
    if (!$(this.popupWindow).is(':visible')) {
      this.show();
    }
    else {
      this.hide();
    }
  }
};

PopupWindow.prototype.moveToPlaceHolder = function () {
  if (this.popupWindow && this.popupPlaceHolder && this.popupParent) {
    $(this.popupWindow).remove().appendTo(this.popupPlaceHolder);
  }
};

PopupWindow.prototype.moveToParent = function () {
  if (this.popupWindow && this.popupPlaceHolder && this.popupParent) {
    $(this.popupWindow).remove().appendTo(this.popupParent);
  }
};

PopupWindow.prototype.show = function () {
  if (this.popupWindow && this.popupBackground) {
    
    $(this.popupBackground).show();
    this.moveToPlaceHolder();
    $(this.popupWindow).show();
    $(this.popupWindow).css('visibility', 'visible');
    window.scrollTo( 0, 0);
    this.setBackgroundSize();
    this.setBounds(); 
    if (iFramework.Browser.isIE6() && this.popupHideFrame) {
      $(this.popupHideFrame).show();
    }
    iFramework.Browser.setScrollEnabled(false);
  }
};

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).css({
        left : newPosition.getX() + 'px',
        top : newPosition.getY() + 'px'
    });
    if (iFramework.Browser.isIE6() && this.popupHideFrame) {
        $(this.popupHideFrame).css({
            left : newPosition.getX() + 'px',
            top : newPosition.getY() + 'px',
            width : this.getSize().getX() + 'px',
            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();
  }
};
