document.write("<div id='wnd_container' style='position:absolute;visibility:hidden;top:1px;left:1px;width:1px;z-index:100;height:1px;'></div>");

////////////////////////////////////////////////////////////////////////////////
////
//// Создание и отрисовка окошек
//// Creating and drawing windows
////
////////////////////////////////////////////////////////////////////////////////

var HWND = new Array();

var path_to_theme="/wnd/theme_1";


var HWND_titlebar_left_img=path_to_theme+"/img/caption_left.gif";
var HWND_titlebar_right_img=path_to_theme+"/img/caption_right.gif";
var HWND_titlebar_bg_img=path_to_theme+"/img/caption_bg.gif";

var HWND_titlebar_btn_close_none_img=path_to_theme+"/img/btn_close_none.gif";
var HWND_titlebar_btn_close_over_img=path_to_theme+"/img/btn_close_over.gif";

var HWND_form_input_left_img=path_to_theme+"/img/input_field_left.gif";
var HWND_form_input_right_img=path_to_theme+"/img/input_field_right.gif";
var HWND_form_input_body_img=path_to_theme+"/img/input_field_body.gif";
var HWND_form_input_button_75_none_img=path_to_theme+"/img/input_btn_75_none.gif";
var HWND_form_input_button_75_over_img=path_to_theme+"/img/input_btn_75_over.gif";


function staticWindow(wName,wCaption,wWidth,wHeight,wPosX,wPosY)
  {
  wID=HWND.length;
  if((!isDefined(wName)) || (wName == '')){wName="undefined"}
  if((!isDefined(wCaption)) || (wCaption == '')){wCaption="undefined"}
  if((!isDefined(wWidth)) || (wWidth == '')){wWidth=300}
  if((!isDefined(wHeight)) || (wHeight == '')){wHeight=34}
  if((!isDefined(wPosX)) || (wPosX == '')){wPosX=0}
  if((!isDefined(wPosY)) || (wPosY == '')){wPosY=0}
  aWnd = new wndConstructor(wName,wCaption,wPosX,wPosY,wWidth,wHeight,wID);
  HWND.push(aWnd);
  return aWnd;
  }

function getWindowById(wID)
  {
  }

function wndConstructor(wName,wCaption,wPosX,wPosY,wWidth,wHeight,wID)
  {
  this.id=wID;
  this.caption = wCaption;
  this.left = wPosX;
  this.top = wPosY;
  this.height = wHeight;
  this.width = wWidth;
  this.name=wName;
  /*this.wndBorder = '';*/
  /*this.wndBorder = "<div id='wnd_" + this.id + "' "
           + "style=\""
           + "left:" + this.left +"px; "
           + "width:"+this.width+"px; "
           + "height:"+this.height+"px; "
           + "top:" + this.top +"px; "
           + "position:absolute; "
           + "visibility:hidden; "
           + "background-color:#efebe7; "
           + "\" "
           + "onclick=\"eval(document.HWND["+this.id+"].hide());return false;\" "
           + "></div>";*/
  //alert(this.id);
  //document.getElementById('wnd_container').innerHTML += this.wndBorder;
  this.wndBorder = document.createElement("div");
  this.wndBorder.id= "wnd_"+this.id;
  this.wndBorder.width=this.width+'px';
  this.wndBorder.height=this.height+'px';
  this.wndBorder.style.left=this.left;
  this.wndBorder.style.top=this.top;
  this.wndBorder.style.position="absolute";
  this.wndBorder.style.visibility="hidden";
  this.wndBorder.style.backgroundColor="";
  //this.wndBorder.onclick = function(){HWND[wID].hide();return false;};
  this.wndBorder.style.zIndex = "101";
  //+ "onmousemove = 'moveHandler(HWND["+wID+"], event);'"
  //+ "onmouseup = 'moveState = false;' "
//                                      + "onmousedown = 'initMove(HWND["+wID+"], event);' "
//  + "onmousemove = 'moveHandler(HWND["+wID+"], event);'"
  this.wndBorder.innerHTML = "<table id=\"tbl_wnd_"+this.id+"\" cellspacing=\"0\" cellpadding=\"0\" style=\"width:100%; height:100%; cursor:default;\">"
                           + "<tr>"
                             + "<td style=\"width:100%;\">"
                               + "<table id=\"header_wnd_"+this.id+"\" "
                                      + "style=\"width:100%; cursor: pointer;\" "
                                      + "cellspacing=\"0\" "
                                      + "cellpadding=\"0\" "
                                      + "border=\"0\" "
                                      + ">"
                                 + "<tr>"
                                   + "<td style=\"background-image:url("+HWND_titlebar_left_img+");background:url("+HWND_titlebar_left_img+");width:6px;height:24px;\"><img src=\""+transparent_pixel_path+"\" style=\"width:6px;height:24px;\"></td>"
                                   + "<td id=\"icon_wnd_"+this.id+"\" style=\"background-image:url("+HWND_titlebar_bg_img+");background: url("+HWND_titlebar_bg_img+");width:24px;height:24px;font-size:6px\"></td>"
                                   + "<td id=\"capt_wnd_"+this.id+"\" style=\"background-image:url("+HWND_titlebar_bg_img+");background: url("+HWND_titlebar_bg_img+");height:24px;font-size:14px;font-weight:bold;color:#ffffff;overflow:hidden;\">"+this.caption+"</td>"
                                   + "<td id=\"btn_wnd_"+this.id+"\" style=\"background-image:url("+HWND_titlebar_bg_img+");background: url("+HWND_titlebar_bg_img+");width:24px;height:24px;font-size:6px;vertical-align:middle;\"></td>"
                                   + "<td style=\"background-image:url("+HWND_titlebar_right_img+");background: url("+HWND_titlebar_right_img+");width:6px;height:24px;font-size:6px\"><img src=\""+transparent_pixel_path+"\" style=\"width:6px;height:24px;\"></td>"
                                 + "</tr>"
                               + "</table>"
                             + "</td>"
                           + "</tr>"
                           + "<tr>"
                           + "<td id=\"body_wnd_"+this.id+"\" colspan=\"5\" style=\"background-color:#efebe7;border: 1px solid #2e271f;border-top:none;text-align:left;vertical-align:top;padding:5px 5px 5px 5px;height:100%;\"></td>"
                           + "</tr>"
                           + "</table>";
                           //2e271f 584b41
  document.body.appendChild(this.wndBorder);

  this.icon = document.createElement("img");
  this.icon.style.width = 16+'px';
  this.icon.style.height = 16+'px';
  this.icon.src = transparent_pixel_path;
  document.getElementById("icon_wnd_"+this.id).appendChild(this.icon);

  this.btnClose = document.createElement("img");
  this.btnClose.id = "btn_wnd_"+this.id+"_close";
  this.btnClose.style.width = 24+'px';
  this.btnClose.style.height = 18+'px';
  this.btnClose.src = HWND_titlebar_btn_close_none_img;
  this.btnClose.onclick = function(){HWND[wID].hide();return false;};
  this.btnClose.onmouseover = function(){document.getElementById("btn_wnd_"+wID+"_close").src = HWND_titlebar_btn_close_over_img};
  this.btnClose.onmouseout = function(){document.getElementById("btn_wnd_"+wID+"_close").src = HWND_titlebar_btn_close_none_img};
  document.getElementById("btn_wnd_"+this.id).appendChild(this.btnClose);
  this.RequiredFields = new Array();

  var handle = document.getElementById("header_wnd_"+this.id);
  var root = this.wndBorder;
  Drag.init(handle, root);


  //this.wnd=document.getElementById('wnd_'+this.id);
  this.setposition = function(wPosX,wPosY)
    {
    this.left=wPosX;
    this.top=wPosY;
    this.wndBorder.left = this.left+'px';
    this.wndBorder.top = this.top+'px';
    }
  this.setcaption = function(newCaption)
    {
    this.caption=newCaption;
    }
  this.show = function()
    {
    this.wndBorder.style.visibility = 'visible';
    }
  this.showmodal = function()
    {
    document.getElementById('wnd_container').style.width=document.body.clientWidth-2+'px';
    document.getElementById('wnd_container').style.height=document.body.clientHeight-2+'px';
    document.getElementById('wnd_container').style.backgroundColor="#000000";
    opacity('wnd_container', 0, 30, 1000)
    //document.getElementById('wnd_container').style.visibility="visible";
    this.wndBorder.style.visibility = "visible";
    }
  this.hide = function()
    {
    //document.getElementById('wnd_container').style.width = 1;
    //document.getElementById('wnd_container').style.height = 1;
    //document.getElementById('wnd_container').style.backgroundColor = "";
    opacity('wnd_container', 30, 0, 1000)
    //document.getElementById('wnd_container').style.visibility = "hidden";
    this.wndBorder.style.visibility = "hidden";
    }
  this.setcaptionicon = function()
    {
    }
  this.setposatscreen = function(posTypeX,posTypeY,posDevX,posDevY)
    {
    var posX;
    var posY;
    if (posTypeX == 'center')
      {
      posX = (document.body.clientWidth/2) - (this.width/2);
      }
    else if (posTypeX == 'left')
      {
      posX = 1;
      }
    else if (posTypeX == 'right')
      {
      posX = document.body.clientWidth - this.width - 1;
      }
    else
      {
      posX = this.left;
      }
    if (posTypeY == 'middle')
      {
      posY = (document.body.clientHeight/2) - (this.height/2);
      }
    else if (posTypeY == 'top')
      {
      posY = 1;
      }
    else if (posTypeY == 'bottom')
      {
      posY = document.body.clientHeight - this.height - 1;
      }
    else
      {
      posY = this.top;
      }
    this.left=posX;
    this.top=posY;
    //alert(posX+' '+posY);
    //alert('dw:'+document.width+' dh:'+document.height+"\n"+'l:'+this.left+' t:'+this.top);
    this.wndBorder.style.left = this.left+'px';
    this.wndBorder.style.top = this.top+'px';
    }

  this.addform = function(form_action,form_method,form_enctype)
    {
    if((!isDefined(form_action)) || (form_action == '')){form_action=""}
    if((!isDefined(form_method)) || (form_method == '')){form_method="post"}
    if((!isDefined(form_enctype)) || (form_enctype == '')){form_enctype="multipart/form-data"}
    this.mainform = document.createElement("form");
    this.mainform.action = form_action;
    this.mainform.method = form_method;
    this.mainform.enctype = form_enctype;
    //this.mainform.onsubmit = function(){return false;}
    document.getElementById("body_wnd_"+this.id).appendChild(this.mainform);
    var mainformtable = document.createElement("table");
    mainformtable.id = "mainformmtable_"+this.id;
    mainformtable.width="100%";
    mainformtable.height="100%";
    mainformtable.border="0px";
    mainformtable.style.border="none";
    this.mainformbody=document.createElement("tbody")
    mainformtable.appendChild(this.mainformbody);
    this.mainform.appendChild(mainformtable);
    }

  this.addformelement = function( form_element_type, form_element_name, form_element_description, form_element_default, form_element_required )
    {
    if((!isDefined(form_element_type)) || (form_element_type == '')){form_element_type="text"}
    if((!isDefined(form_element_name)) || (form_element_name == '')){form_element_name=""}
    if((!isDefined(form_element_description)) || (form_element_description == '')){form_element_description="&nbsp;"}
    if((!isDefined(form_element_default)) || (form_element_default == '')){form_element_default=""}
    if((!isDefined(form_element_required)) || (form_element_required == '')){form_element_required="0"}
    tr = document.createElement("tr");
    td1 = document.createElement("td");
    td1.style.textAlign = "right"
    td1.style.verticalAlign = "middle"
    td1.style.padding = "0px 5px 0px 5px";
    td1.width = "30%";
    td2 = document.createElement("td");
    td2.style.textAlign = "left"
    td2.style.verticalAlign = "middle"
    td2.style.padding = "0px 5px 0px 5px";
    //td2.style.width = "50%";
    var inp;
    switch(form_element_type)
      {
      case "text":
          lInput = document.createElement("div");
          lInput.style.background = "url('"+HWND_form_input_left_img+"')";
          lInput.style.backgroundImage = "url('"+HWND_form_input_left_img+"')";
          lInput.style.backgroundRepeat = "no-repeat";
          lInput.style.backgroundPosition = "left top";
          lInput.style.width = "100%";
          rInput = document.createElement("div");
          rInput.style.background = "url('"+HWND_form_input_right_img+"')";
          rInput.style.backgroundImage = "url('"+HWND_form_input_right_img+"')";
          rInput.style.backgroundRepeat = "no-repeat";
          rInput.style.backgroundPosition = "right top";
          rInput.style.padding = "0px 14px 0px 16px";
          bInput = document.createElement("div");
          bInput.style.background = "url('"+HWND_form_input_body_img+"')";
          bInput.style.backgroundImage = "url('"+HWND_form_input_body_img+"')";
          bInput.style.backgroundRepeat = "repeat-x";
          bInput.style.backgroundPosition = "0 0";
          bInput.style.width = "100%";
          //bInput,style.border="none";
          bInput.style.height=24+'px';
          bInput.style.padding="2px 0px";
          inp = document.createElement("input");
          inp.type="text";
          inp.name=form_element_name;
          inp.value=form_element_default;
          inp.style.width = "100%";
          inp.style.background = 'none';
          inp.style.border = 'none';
          inp.style.fontSize= 16+'px';

          bInput.appendChild(inp);
          rInput.appendChild(bInput);
          lInput.appendChild(rInput);
          td2.appendChild(lInput);
          tr.appendChild(td1);
          tr.appendChild(td2);
        break;
      case "buttons":
          ok = document.createElement("input");
          ok.type="submit";
          ok.value="Да";
          ok.style.width = "75px";
          ok.style.height = "24px";
          ok.style.border='none';
          ok.style.background="url('"+HWND_form_input_button_75_none_img+"')";
          ok.style.backgroundImage = "url('"+HWND_form_input_button_75_none_img+"')";
          ok.style.fontSize= "14px";
          ok.onclick=function(){return HWND[wID].submit();}
          sp=document.createElement("img");
          sp.src=transparent_pixel_path;
          sp.width="4px";
          sp.height="4px"
          no = document.createElement("input");
          no.type="button";
          no.value="Нет";
          no.style.width = "75px";
          no.style.height = "24px";
          no.style.border='none';
          no.style.background="url('"+HWND_form_input_button_75_none_img+"')";
          no.style.backgroundImage = "url('"+HWND_form_input_button_75_none_img+"')";
          no.style.fontSize= "14px";
          no.onclick=function(){HWND[wID].hide();return false;}
          td1.innerHTML="";
          td1.width = "100%";
          td1.colSpan = 2;
          td1.appendChild(ok);
          td1.appendChild(sp);
          td1.appendChild(no);
          tr.appendChild(td1);
        break;
      case "header":
          td1.innerHTML=form_element_description;
          td1.style.textAlign = "center";
          td1.width = "100%";
          td1.colSpan = 2;
          tr.appendChild(td1);
        break;
      default:
      }
    if(isDefined(inp))
      {
      if(form_element_required != "0")
        {
        this.RequiredFields.push(inp);
        td1.innerHTML=form_element_description+"<font style=\"font-weight:bold;color:#FF0000\">*</font>";
        }
      else
        {
        td1.innerHTML=form_element_description;
        }
      }
    this.mainformbody.appendChild(tr);
    }
  this.submit = function()
    {
    for(var i = 0; i < this.RequiredFields.length; i++)
      {
      if((!isDefined(this.RequiredFields[i].value)) || (this.RequiredFields[i].value == '')){alert("Заполните все поля, помеченные '*'!");return false;}
      }
    //alert("Всо ок, можно сабмитиццо");
    //this.mainform.submit;
    return true;
    }
  }

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            if(i == opacEnd)
              {
              setTimeout("changeOpac(" + i + ",'" + id + "', 1)",(timer * speed));
              }
            else
              {
              setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
              }
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id, hide) {
    var object = document.getElementById(id).style;
    if(hide == 1){object.visibility = "hidden"}else{object.visibility = "visible"};
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

//////////////////////////////////////////////////////////////////////////////////
////
//// Перемещение окошек
//// Moving windows
////
/////////////////////////////////////////////////////////////////////////////////

//var moveState = false;
// Переменные координат мыши в начале перемещения, пока неизвестны
//var x0, y0;
// Начальные координаты элемента, пока неизвестны
//var divX0, divY0;

//var draggedWindowID;

// Объявим функцию для определения координат мыши
function mousePos(event) {
    var x = y = 0;
    if (document.attachEvent != null) { // Internet Explorer & Opera
        x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
    }
    if (!document.attachEvent && document.addEventListener) { // Gecko
        x = event.clientX + window.scrollX;
        y = event.clientY + window.scrollY;
    }
    return {x:x, y:y};
}

// Функция инициализации движения
// Записываем всё параметры начального состояния
//function initMove(HWND, event) {
//    var event = event || window.event;
//    x0 = defPosition(event).x;
//    y0 = defPosition(event).y;
//    divX0 = parseInt(HWND.wndBorder.style.left);
//    divY0 = parseInt(HWND.wndBorder.style.top);
//    moveState = true;
//    draggedWindowID=HWND.id;
//}

// Если клавишу мыши отпустили вне элемента движение должно прекратиться
//document.onmouseup = function() {
//    moveState = false;
//}
//document.onmousemove = function() {
//    moveHandler(HWND[draggedWindowID], event);
//}

// И последнее
// Функция обработки движения:
//function moveHandler(HWND, event) {
//    var event = event || window.event;
//    if (moveState) {
//        if((divX0 + defPosition(event).x - x0) > 1)
//          {
//          HWND.wndBorder.style.left = divX0 + defPosition(event).x - x0;
//          }
//        HWND.wndBorder.style.top  = divY0 + defPosition(event).y - y0;
//    }
//}

var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]),
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};


