﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("OS.Web");
 
OS.Web.DialogResult = function() {}
OS.Web.DialogResult.prototype = 
{
  Yes : 0,
  No : 1,
  Cancel : 2,
  Ok : 3
}

OS.Web.DialogResult.registerEnum("OS.Web.DialogResult");


OS.Web.ModalDialog = function(element) {
    OS.Web.ModalDialog.initializeBase(this, [element]);
    
    this._modalBehavior = null;    
    this._onCloseCallback = null;
    this._onCloseCallbackTemp = null;
    this._headerText = null;
    this._headerHTML = null;
}

OS.Web.ModalDialog.prototype = {
    initialize: function() {
        OS.Web.ModalDialog.callBaseMethod(this, 'initialize');
        
        this.add_Shown(Function.createDelegate(this, this._OnShown));
    },
    dispose: function() {        

        OS.Web.ModalDialog.callBaseMethod(this, 'dispose');
    },
    
    //Methods
    Show : function(onHideCallback)
    {
      if (onHideCallback != null)
        this._onCloseCallbackTemp = onHideCallback;
       
      this._modalBehavior.show();
    },
    
    Close : function()
    {
      var onHideCallback = this._onCloseCallbackTemp || this._onCloseCallback;
      
      if (onHideCallback == null || onHideCallback.apply(null, arguments) !== false)
        this._modalBehavior.hide();
      
      this._onCloseCallbackTemp = null;
    },
    
    _OnShown : function()
    {
      var footer = LastChildElement(this.get_element());
      var innerHTML = footer.innerHTML;
      footer.style.display = (innerHTML == null || innerHTML.trim().length == 0 ? "none" : "block");
    },
    
     //Events
    add_Shown: function(handler) {
        this._modalBehavior.add_shown(handler);
    },
    remove_Shown: function(handler) {
        this._modalBehavior.remove_shown(handler);
    },
    add_Hidden: function(handler) {
        this._modalBehavior.add_hidden(handler);
    },
    remove_Hidden: function(handler) {
        this._modalBehavior.remove_hidden(handler);
    },

    //Properties    
    get_HeaderText : function()
    {
      //TODO: вернуть реальный innerText
      return this._headerText;
    },
        
    set_HeaderText : function(value)
    {
      this._headerText = value;
      
      var headerContent = FirstChildElement(FirstChildElement(this.get_element()));
      SetElementInnerText(headerContent, value);
    },
    
    get_HeaderHTML : function()
    {
      //TODO: вернуть реальный innerHTML
      return this._headerHTML;
    },
        
    set_HeaderHTML : function(value)
    {
      this._headerHTML = value;
      
      var headerContent = FirstChildElement(FirstChildElement(this.get_element()));
      headerContent.innerHTML = value;
    },
    
    get_Width : function()
    {
      return this.get_element().style.width;
    },
        
    set_Width : function(value)
    {
      this.get_element().style.width = value + "px";
    },
    
     
    get_Height : function()
    {
      return this.get_element().offsetHeight;
    },
        
    set_Height : function(value)
    {
      var container = this.get_element();
      var header = FirstChildElement(container);
      var content =  NextElement(header);
      var footer =  NextElement(content);
      
      var madeVisible;
      
      if (container.style.display == "none")
      {
        container.style.display = "block";
        madeVisible = true;
      }
      var contentStyle = Sys.UI.DomElement._getCurrentStyle(content);
      var contentPaddingTop = contentStyle.paddingTop;
      contentPaddingTop = contentPaddingTop.substr(0, contentPaddingTop.length - 2);
      var contentPaddingBottom = contentStyle.paddingTop;
      contentPaddingBottom = contentPaddingBottom.substr(0, contentPaddingBottom.length - 2);
      
      //TODO: учесть бордер (и может паддинг) container'а 
      
      content.style.height = (value - header.offsetHeight - footer.offsetHeight - contentPaddingTop - contentPaddingBottom) + "px";
      
      if (madeVisible)
        container.style.display = "none";
    },
    
    get_ContentHeight : function()
    {
      //TODO:не совсем корректно - исправить
      return this.get_element().style.height;
    },
        
    set_ContentHeight : function(value)
    {
      var container = this.get_element();
      var content = NextElement(FirstChildElement(this.get_element()));
      content.style.height = value + "px"; //padding не учитывается
    },
    
    get_ModalBehavior : function()
    {
      return this._modalBehavior;
    },
        
    set_ModalBehavior : function(value)
    {
      this._modalBehavior = value;
    },
    
    get_OnHideCallback : function()
    {
      return this._onCloseCallback;
    },
    
    set_OnHideCallback : function(value)
    {
      this._onCloseCallback = value;
    }
}
OS.Web.ModalDialog.registerClass('OS.Web.ModalDialog', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
