﻿Type.registerNamespace('Moasis.Common');

Moasis.Common.ModalDialog = function() { 
	Moasis.Common.ModalDialog.initializeBase(this);

	this._popup = null;
	this._pnlTitle = null;
	this._pnlContent = null;

	this._UsePostBack = null;
	this._PostbackTargetID = null;
	this._IsShown = null;
}

Moasis.Common.ModalDialog.prototype = {
  initialize: function() {
    Moasis.Common.ModalDialog.callBaseMethod(this, 'initialize');

    this._pnlTitle = this.$control('pnlTitle');
    this._pnlContent = this.$control('pnlContent');
    this._pnlProgress = this.$control('pnlProgress');

    this.add_ContentReady(Type.createDelegate(this, this._onContentReady));
    this.add_ContentResized(Type.createDelegate(this, this._onContentResized));

    Sys.Application.add_load(Type.createDelegate(this, function(sender, args) {
      this._popup = this.$component('ModalPopupExtender1');
    }));

    window._modalDialog = this;
  }, // end of 'initialize' method

  dispose: function() {
    if (this._pnlContent && this._ifrmContent) {
      this._pnlContent.removeChild(this._ifrmContent);
      this._ifrmContent = null;
    }
  }, // end of 'dispose' method

  get_UsePostBack: function() {
    return this._UsePostBack;
  }, // End of 'PostbackTargetID' poroperty's get indexer

  set_UsePostBack: function(value) {
    this._UsePostBack = value;
  }, // End of 'PostbackTargetID' poroperty's set indexer

  get_PostbackTargetID: function() {
    return this._PostbackTargetID;
  }, // End of 'PostbackTargetID' poroperty's get indexer

  set_PostbackTargetID: function(value) {
    this._PostbackTargetID = value;
  }, // End of 'PostbackTargetID' poroperty's set indexer

  get_IsShown: function() {
    return this._IsShown;
  }, // End of 'IsShown' poroperty's get indexer

  set_IsShown: function(value) {
    this._IsShown = value;
  }, // End of 'IsShown' poroperty's set indexer

  add_ContentReady: function(handler) {
    this.get_events().addHandler('ContentReady', handler);
  }, // End of 'ContentReady' event's add handler

  remove_ContentReady: function(handler) {
    this.get_events().removeHandler('ContentReady', handler);
  }, // End of 'ContentReady' event's remove handler

  add_ContentResized: function(handler) {
    this.get_events().addHandler('ContentResized', handler);
  }, // End of 'ContentResized' event's add handler

  remove_ContentResized: function(handler) {
    this.get_events().removeHandler('ContentResized', handler);
  }, // End of 'ContentResized' event's remove handler

  show: function() {
    this._popup.show();
    this._IsShown = true;
  }, // end of 'show' method

  hide: function() {
    this._popup.hide();
    this._IsShown = false;
    if (this._IsLoading)
      this._IsLoading = false;
  }, // end of 'hide' method

  _layout: function() {
    var _containerDisplay = this._pnlContent.style.display;
    this._pnlContent.style.width = null;
    this._pnlContent.style.display = '';
    if (this._ifrmContent) {
      this._ifrmContent.style.width = null;
      this._ifrmContent.style.height = null;
      this._ifrmContent.style.position = 'static';
      this._ifrmContent.style.top = null;
      this._ifrmContent.style.left = null;
    }
    var contentSize = getContentSize(this._ifrmContent.contentWindow.document);
    this._pnlContent.style.display = _containerDisplay;

    if (this._ifrmContent) {
      this._ifrmContent.style.width = contentSize.width + 'px';
      this._ifrmContent.style.height = contentSize.height + 'px';
    }
    this._popup._layout();

    if (this._ifrmContent) {
      if (this._pnlContent.offsetWidth > contentSize.width) {
        this._pnlContent.style.width = this._pnlContent.offsetWidth + 'px';
        this._ifrmContent.style.position = 'relative';
        this._ifrmContent.style.top = '0px';
        this._ifrmContent.style.left = Math.floor((this._ifrmContent.offsetParent.offsetWidth - contentSize.width) / 2) + 'px';
      }
      this._popup._layout();
    }
  }, // end of '_layout' method

  loadContent: function(url) {
    if (this._IsLoading)
      return;

    this._pnlContent.style.width = null;
    $css(this._pnlProgress).remove('hidden');
    $css(this._pnlTitle).add('hidden');
    if (this._ifrmContent) {
      this._pnlContent.removeChild(this._ifrmContent);
      this._ifrmContent = null;
    }

    this.show();

    this._IsLoading = true;

    var _iframe_id = this.get_id() + '_iframe';
    this._ifrmContent = $createElement('iframe', this._pnlContent, { id: _iframe_id, frameBorder: '0', scrolling: 'no', marginWidth: '0', marginHeight: '0', className: 'hidden' });
    this._ifrmContent.contentWindow.location.href = url;
  }, // end of 'loadContent' method

  redirectToLoginPage: function() {
    this.hide();
    var url = '/login/login.aspx?ReturnUrl={0}&Redirect=1';
    location.href = String.format(url, encodeURIComponent(location.href));
  }, // end of 'redirectToLoginPage' method

  _onContentReady: function() {
    if (!this._IsLoading)
      return;

    this._IsLoading = false;

    $css(this._pnlProgress).add('hidden');
    var _title = this._ifrmContent.contentWindow.document.title;
    if (typeof (_title) == 'string' && _title.length > 0) {
      this._pnlTitle.innerText = _title;
      $css(this._pnlTitle).remove('hidden');
    } else
      $css(this._pnlTitle).add('hidden');

    $css(this._ifrmContent).remove('hidden');
    this._layout();

    this._ifrmContent.contentWindow.closePopup = Type.createDelegate(this, this.hide);
    this._ifrmContent.contentWindow.popup = {};
    this._ifrmContent.contentWindow.popup._layout = Type.createDelegate(this, this._layout);
  }, // end of '_onContentReady' method

  _onContentResized: function() {
    this._layout();
  }, // end of '_onContentReady' method

  raiseEvent: function(event_id) {
    var handler = this.get_events().getHandler(event_id);
    if (handler)
      handler(this);
  }, // end of 'raiseEvent' method

  refresh: function() {
    if (this._UsePostBack && this._PostbackTargetID)
      __doPostBack(this._PostbackTargetID, '')
    else
      window.location.href = window.location.href;
  }, // end of 'raiseEvent' method

  $control: function(control_id) {
    return $(this.get_id() + '_' + control_id);
  }, // end of '$control' method

  $component: function(component_id) {
    return $find(this.get_id() + '_' + component_id);
  } // end of '$component' method

} // end of members

function $createElement(tag, parentElement, prop){
	var elm = document.createElement(tag);
	for (var key in prop)
		elm[key] = prop[key];
	parentElement.appendChild(elm);
	return elm;
} // end of '$createElement' method

function getClientSize(doc){
	var size = { width: 0, height: 0 };
	if (doc.compatMode == "CSS1Compat") {
			size.width = doc.documentElement.clientWidth;
			size.height = doc.documentElement.clientHeight;
	} else {
			size.width = doc.body.clientWidth;
			size.height = doc.body.clientHeight;
	}
	return size;
} // end of 'getClientSize' method

function getContentSize(doc){
	var clientSize = getClientSize(doc);
	var size = { width: 0, height: 0 };
	if (Sys.Browser.agent == Sys.Browser.InternetExplorer && doc.compatMode != "CSS1Compat") {
			size.width = doc.body.scrollWidth;
			size.height = doc.body.scrollHeight;
	}
	else {
			size.width = Math.max(Math.max(doc.documentElement.scrollWidth, doc.body.scrollWidth), clientSize.width);
			size.height = Math.max(Math.max(doc.documentElement.scrollHeight, doc.body.scrollHeight), clientSize.height);
	}
	return size;
} // end of 'getContentSize' method


Moasis.Common.ModalDialog.registerClass('Moasis.Common.ModalDialog', Sys.Component);

if (typeof(Sys) !== 'undefined')
	Sys.Application.notifyScriptLoaded();

