// Copyright (C) 2008 Xtensive LLC.
// All rights reserved.
// For conditions of distribution and use, see license.
// Created by: Alexey Lobov
// Created:    2009.01.19

Ext.namespace("Xtensive");

Xtensive.LoadingIndicator = function() {
}

Xtensive.LoadingIndicator.prototype.initialize = function(element) {
  this.element = this.obtainElement(element);
  this.splash = document.createElement('div');
  this.splash.style.visibility = "hidden";

  this.message = document.createElement('div');
  this.message.className = "loadingindicator-message";
  this.splash.appendChild(this.message);

  this.label = document.createElement('div');
  this.label.className = "loadingindicator-label";
  this.splash.appendChild(this.label);

  this.topLine = document.createElement('div');
  this.topLine.className = "loadingindicator-top_line";
  this.splash.appendChild(this.topLine);

  this.bottomLine = document.createElement('div');
  this.bottomLine.className = "loadingindicator-bottom_line";
  this.splash.appendChild(this.bottomLine);

  this.leftLine = document.createElement('div');
  this.leftLine.className = "loadingindicator-left_line";
  this.splash.appendChild(this.leftLine);

  this.rightLine = document.createElement('div');
  this.rightLine.className = "loadingindicator-right_line";
  this.splash.appendChild(this.rightLine);

  this.leftUpCorner = document.createElement('div');
  this.leftUpCorner.className = "loadingindicator-left_up_corner";
  this.splash.appendChild(this.leftUpCorner);

  this.leftDownCorner = document.createElement('div');
  this.leftDownCorner.className = "loadingindicator-left_down_corner";
  this.splash.appendChild(this.leftDownCorner);

  this.rightUpCorner = document.createElement('div');
  this.rightUpCorner.className = "loadingindicator-right_up_corner";
  this.splash.appendChild(this.rightUpCorner);

  this.rightDownCorner = document.createElement('div');
  this.rightDownCorner.className = "loadingindicator-right_down_corner";
  this.splash.appendChild(this.rightDownCorner);

  document.body.insertBefore(this.splash, null);
}

Xtensive.LoadingIndicator.prototype.obtainElement = function(elementOrId) {
  var element = null;
  if (!elementOrId)
    element = document.body;
  else {
    if (typeof elementOrId == 'string')
      element = document.getElementById(elementOrId);
    if (elementOrId.nodeName)
      if (elementOrId.nodeType == 1)
        element = elementOrId;
  }
  return element;
}

Xtensive.LoadingIndicator.prototype.show = function(msgText) {

  var leftEdge = this.element.offsetLeft+(this.element.offsetWidth-this.message.offsetWidth)/1.01;
  var rightEdge = this.message.offsetWidth+this.element.offsetLeft+(this.element.offsetWidth-this.message.offsetWidth)/1.01;
  var topEdge = this.element.offsetTop+(this.element.offsetHeight-this.message.offsetHeight)/1.01;
  var bottomEdge = this.message.offsetHeight+this.element.offsetTop+(this.element.offsetHeight-this.message.offsetHeight)/1.01;

  this.message.style.left = leftEdge;
  this.message.style.top = topEdge;

  this.label.style.left = leftEdge+24;
  this.label.style.top = topEdge+4;
  this.label.style.width = this.message.offsetWidth-28;
  this.label.style.height = this.message.offsetHeight-8;
  this.label.style.lineHeight = this.message.offsetHeight-8+"px";
  this.label.innerHTML = msgText;

  this.topLine.style.left = leftEdge;
  this.topLine.style.top = topEdge;
  this.topLine.style.width = this.message.offsetWidth;

  this.bottomLine.style.left = leftEdge;
  this.bottomLine.style.top = bottomEdge-4;
  this.bottomLine.style.width = this.message.offsetWidth;

  this.leftLine.style.left = leftEdge;
  this.leftLine.style.top = topEdge;
  this.leftLine.style.height = this.message.offsetHeight;
  this.leftLine.style.backgroundPosition = "0px 50%";

  this.rightLine.style.left = rightEdge-4;
  this.rightLine.style.top = topEdge;
  this.rightLine.style.height = this.message.offsetHeight;
  this.rightLine.style.backgroundPosition = "0px 50%";

  this.leftUpCorner.style.left = leftEdge;
  this.leftUpCorner.style.top = topEdge;

  this.leftDownCorner.style.left = leftEdge;
  this.leftDownCorner.style.top = bottomEdge-4;

  this.rightUpCorner.style.left = rightEdge-4;
  this.rightUpCorner.style.top = topEdge;

  this.rightDownCorner.style.left = rightEdge-4;
  this.rightDownCorner.style.top = bottomEdge-4;

  this.splash.style.visibility = "visible";
}

Xtensive.LoadingIndicator.prototype.hide = function() {
  this.splash.style.visibility = "hidden";
}

Xtensive.LoadingIndicator.prototype.destroy = function() {
  this.splash.removeChild(this.message);
  message = null;
  document.body.removeChild(this.splash);
  splash = null;
}

Xtensive.LoadingIndicator.prototype.monitorAjaxRequests = function() {
  var pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
  var context = this;
  pageRequestManager.add_beginRequest(function(sender, args) { context.show("Loading..."); });
  pageRequestManager.add_endRequest(function(sender, args) { context.hide(); });
}
if(typeof(Sys)!=='undefined')
    Sys.Application.notifyScriptLoaded();