﻿var IsIe;
var _IsMouseDown = false;
var _srcElementId = null;
var _LastX,_LastY;
var LastStX = 265;
var LastStY = 240;

var WindowsFrameCssClass = 'WindowsFrame';
var WindowsTitleCssClass = 'WindowsTitle';
var WindowsBodyCssClass = 'WindowsBody';
var WindowsTextTitleCssClass = 'WindowsTitleText';

var FocusBodyCssClass = 'FocusWindowsBody';
var FocusTextTitleCssClass = 'FocusWindowsTextTitle';

var _WindowsList = new Array();
var _UniqueId = 0;
var WindowsTopFocus = 100;

var WindowsStatusBar = null;

var StatusCssClass = 'StatusBarItem';
var FocusStatusCssClass = 'FocusStatusBarItem';
var Max_Window_Title_Size = 40;
var Max_Status_Title_Size = 35;


if(window.navigator.appName.indexOf("Explorer") != -1)
 IsIe = true;
else
 IsIe = false; 

function _OnMousedown(e)
{
  var em = (e)?e:window.event;
  var s;
  s = (em.srcElement)?em.srcElement:e.target;
  if(s == null)
   return;
  var i = s.id;
    if(i.indexOf("Wnd_Title_") == -1)
      return;
  var _id = i.replace('Wnd_Title_','');
  _srcElementId = 'Wnd_' + _id;
    if(document.getElementById(_srcElementId) == null)
      return;
  document.body.style.cursor = 'move';
  _IsMouseDown = true;
  document.onmousemove = _OnMouseMove;
  document.onmouseup = _OnMouseUp;
  
  _LastX = parseInt(em.clientX);
  _LastY = parseInt(em.clientY);
  _setFocus(parseInt(_id));
  document.onselectstart = _OnSelectStart;
}

function _OnMouseUp(e)
{
  document.onmousemove = null;
  document.onmouseup = null;  
  _IsMouseDown = false;
  document.body.style.cursor = '';
  _srcElementId = null;
  document.onselectstart = null;
}

function _OnMouseMove(e)
{
 if(_IsMouseDown == true && _srcElementId != null)
 {
   var em;
   em = (e)?e:window.event;
   
   var s;
   s = document.getElementById(_srcElementId);
   
   var wx,wy;
   wx = parseInt(s.style.right);
   wy = parseInt(s.style.top);
   
   var mx,my;
   mx = parseInt(em.clientX);
   my = parseInt(em.clientY);   
 
   wx = wx - (mx - _LastX);
   _LastX = mx;
   wy = wy + (my - _LastY);
   _LastY = my;
   
   s.style.right = wx+'px';
   s.style.top =  wy+'px';  
 }
}

function _OnSelectStart(e)
{
  em = (e)?e:window.event;
  em.returnValue = false;
}

document.onmousedown = _OnMousedown;

function _MakeTitle(h,text)
{
  var str;

  str = "<table style=\"width:100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
  str = str + "<td style=\"width:10px\"><img height=\"25\" src=\"Files/fa/Images/right-top-unfocus.png\" width=\"10\" /></td>";
  str = str + "<td style=\"width:23px\"><img height=\"25\" src=\"Files/fa/Images/button-close-focus.gif\" width=\"23\" onclick=\"_Close(" + h + ");\" /></td>";
  str = str + "<td style=\"width:23px\"><img height=\"25\" src=\"Files/fa/Images/button-min-focus.gif\" width=\"23\"onclick=\"_Hide(" + h + ");\" /></td>";
  str = str + "<td class=\"" + WindowsTitleCssClass + "\" id=\"Wnd_Title_" + h + "\"><span id=\"Wnd_Title_Text_" + h + "\" class=\"" + WindowsTextTitleCssClass + "\">" + text + "</span></td>";
  str = str + "<td style=\"width:10px\"><img height=\"25\" src=\"Files/fa/Images/left-top-unfocus.png\" width=\"10\" /></td>";
  str = str + "</tr></table>";
  return str;
}
function MakeWindow(h)
{
 var main = document.createElement("div");
 var title;
 var body;

 main.id = "Wnd_" + h;
 main.className = WindowsFrameCssClass;
 main.innerHTML = "<div></div><div></div>";
 main.style.right = LastStX + "px";
 main.style.top = LastStY + "px";
 main.style.position = "absolute";
 main.style.width = "438px";
 main.style.visibility = "visible";
 main.style.zIndex = WindowsTopFocus;
 
 title = main.childNodes[0];
 body = main.childNodes[1];
 document.body.appendChild(main);
 LastStX += 30;
 LastStY += 30;
if(LastStX > 480) 
{
 LastStX = 265;
 LastStY = 240;
}
 return {"main":main,"title":title,"body":body};
}
/********************************************************************/
function _StatusBar(id,op,title)
{
  if(WindowsStatusBar == null)
    return;
  if(op == 'a')
  {
    var strtitle;
    if(title.length > Max_Status_Title_Size)
      strtitle = title.substring(0,Max_Status_Title_Size) + " ... ";
    else
      strtitle = title;

    WindowsStatusBar.innerHTML = WindowsStatusBar.innerHTML + "<div id=\"status_"+id+"\" class=\"" + StatusCssClass +"\" onclick=\"_Show("+id+");\">"+strtitle+"</div>";
  }
  else
  {  
  var e = document.getElementById("status_"+id);
  if(e == null)
    return;

  if(op == 'r')
        WindowsStatusBar.removeChild(e);
  else if(op == 'lf')
       e.className = StatusCssClass;
  else if(op == 'gf')
       e.className = FocusStatusCssClass;
  else if(op == 'st')
  {
    var strtitle;
    if(title.length > Max_Status_Title_Size)
      strtitle = title.substring(0,Max_Status_Title_Size) + " ... ";
    else
      strtitle = title;

       e.innerHTML = strtitle;
  }
  }  
}

function _RegisterObject(h,df,tt,db)
{
  this.Handle = h;
  this.DivFrame = df;
  this.TitleText = tt;
  this.DivBody = db;
}

function _RegisterWindow(title,text)
{
  var lst;
  lst = MakeWindow(_UniqueId);
  var m,t,b,tt;
  var retid;
  m = lst["main"];
  t = lst["title"];
  b = lst["body"];
    
  if(m == null || t == null || b == null)
     return -1;
     
   retid = _UniqueId;
   _UniqueId++;
   b.className = WindowsBodyCssClass;
   t.innerHTML = _MakeTitle(retid,title);
   var tt = document.getElementById("Wnd_Title_Text_" + retid);
   tt.className = WindowsTextTitleCssClass;
   b.innerHTML = text;
      
   WindowsTopFocus++;      
  _WindowsList.push(new _RegisterObject(retid,m,tt,b)); 
  _StatusBar(retid,'a',title);
  _StatusBar(retid,'gf',null);
  return retid;
}

function _ChangeListFoucus(z)
{
  for(var i = 0 ; i < _WindowsList.length ; i++)
  {
    if(_WindowsList[i].DivFrame.style.zIndex > z)
      _WindowsList[i].DivFrame.style.zIndex = parseInt(_WindowsList[i].DivFrame.style.zIndex) - 1;    
  }
}

function _UnRegisterWindow(h)
{
  var d;
  for(var i = 0 ; i < _WindowsList.length ; i++)
  {
    d = _WindowsList[i].Handle;
    if(d == h)
    {
     d = _WindowsList[i];
     var z = parseInt(d.DivFrame.style.zIndex);
     document.body.removeChild(d.DivFrame);
     _WindowsList.splice(i,1);
     _ChangeListFoucus(z);
     WindowsTopFocus--;
     _StatusBar(h,'r',null);
     return true;
    }
    if(d > h)
     break;
  }
  return false;
}
/*************************************************/
function _getWindowsObject(handle)
{
  var d;
  for(var i = 0 ; i < _WindowsList.length ; i++)
  {
    d = _WindowsList[i].Handle;
    if(d == handle)
     return _WindowsList[i];
    if(d > handle)
     break;
  }
  return null;
}
var Focused = null;
function _setFocus(handle)
{
  var o;
  o = _getWindowsObject(handle);
  if(o == null)return;
  if(Focused != null)
  {
    Focused.TitleText.className = WindowsTextTitleCssClass;
    Focused.DivBody.className = WindowsBodyCssClass;    
    _StatusBar(Focused.Handle,'lf',null);
  }
  _ChangeListFoucus(o.DivFrame.style.zIndex);
  o.DivFrame.style.zIndex = WindowsTopFocus;
  
  o.DivBody.className = FocusBodyCssClass;
  o.TitleText.className = FocusTextTitleCssClass;
    
  Focused = o;
  _StatusBar(handle,'gf',null);
}

function _Close(handle)
{
  _UnRegisterWindow(handle);
}

function _setBody(handle,text)
{
 var o;
 o = _getWindowsObject(handle);
 if(o == null)
   return false;
 o.DivBody.innerHTML = text;
 return true
}

function _setTitle(handle,text)
{
  var o;
  o = _getWindowsObject(handle);
  if(o == null)
    return false;

  if(text.length > Max_Window_Title_Size)
     o.TitleText.innerHTML = text.substring(0,Max_Window_Title_Size) + " ... ";
  else  
      o.TitleText.innerHTML = text;

  _StatusBar(handle,'st',text);
  return true;
}

function _Hide(handle)
{
  var o;
  o = _getWindowsObject(handle);
  if(o == null)
    return false;
    
  if(IsIe)
  {    
    if(o.DivFrame.style.filters != null && o.DivFrame.style.filters[1] != null)
    {
      o.DivFrame.style.filters[1].apply();      
      o.DivFrame.style.visibility = 'hidden';
      o.DivFrame.style.filters[1].play();
      return;
    }
  }
  o.DivFrame.style.visibility = 'hidden';    
}

function _Show(handle)
{ 
  var o;
  o = _getWindowsObject(handle);
  if(o == null)
    return false;
  if(IsIe)
  {    
    if(o.DivFrame.style.filters != null && o.DivFrame.style.filters[0] != null)
    {
      o.DivFrame.style.filters[0].apply();      
      o.DivFrame.style.visibility = 'visible';
      o.DivFrame.style.filters[0].play();
      return;
    }
  }
  o.DivFrame.style.visibility = 'visible';
  _setFocus(handle);
}

/***************************************************/

function Window(txttitle,txtbody)
{
  this.Handle = _RegisterWindow(txttitle,txtbody); 
  if(this.Handle < 0)
    return null;
    
  this.SetTitle    = function(text){_setTitle(this.Handle,text);}
  this.SetText     = function(text){_setBody(this.Handle,text);}
  this.Foucus      = function(){_setFocus(this.Handle);}; 
  this.Show        = function(){_Show(this.Handle);}
  this.Hide        = function(){_Hide(this.Handle);}
  this.Close       = function(){_Close(this.Handle);}
}