var msdown = false;
var msx = 0;
var msy = 0;
// (R)
function coords(e) {
  var posx = 0;
  var posy = 0;
  if (e.pageX || e.pageY) {
    posx = e.pageX;
    posy = e.pageY;
  } else if (e.clientX || e.clientY) {
    posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
    posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
  }
  return new Array(posx, posy);
}
// (R)
function select_frame_init(e) {
  e = e || window.event;
  msdown = true;
  var mousexy = coords(e);
  msx = mousexy[0];
  msy = mousexy[1];
  return false;
}
// (R)
function select_frame_coords(e) {
  e = e || window.event;
  var x1 = 0;
  var x2 = 0;
  var y1 = 0;
  var y2 = 0;
  var mousexy = coords(e);
  x1 = msx;
  y1 = msy;
  x2 = mousexy[0];
  y2 = mousexy[1];

  if (x1 > x2) {
    x1 = x1 + x2;
    x2 = x1 - x2;
    x1 = x1 - x2;
  }
  if (y1 > y2) {
    y1 = y1 + y2;
    y2 = y1 - y2;
    y1 = y1 - y2;
  }

  var select_frame_coordinates = {};

  select_frame_coordinates.x1 = x1;
  select_frame_coordinates.y1 = y1;
  select_frame_coordinates.x2 = x2;
  select_frame_coordinates.y2 = y2;

  select_frame_coordinates.mousex1 = msx;
  select_frame_coordinates.mousey1 = msy;
  select_frame_coordinates.mousex2 = mousexy[0];
  select_frame_coordinates.mousey2 = mousexy[1];

  return select_frame_coordinates;
}
// (R)
function msgs_selection_onmousemove(e) {
  //----------------------------------
  if (!e) e = window.event;
  //----------------------------------
  if (G_Collection_lock == true) return; // Collection is being scrolled now by dragIt(), release()
  //----------------------------------
  if (e == null) { alert('event == null'); return; }
  //----------------------------------
  var mousexy = null;

  if (!e.simulatedEvent) {
    mousexy = coords(e);
    if (G_MousemoveEvent) {
      G_MousemoveEvent.clientX = mousexy[0];
      G_MousemoveEvent.clientY = mousexy[1];
    }
  }
  //----------------------------------
  if (!e.shiftKey) {
    var sframe = document.getElementById('selectFrame');
    sframe.style.visibility = 'hidden';

    G_FirstSelectedMsgID = null;
    G_FirstSelectedMsgIndex = null;
    G_FirstSelectedCollectionID = null;
    G_LastSelectedMsgID = null;
    G_LastSelectedMsgIndex = null;
    G_LastSelectedCollectionID = null;

    G_SelectedCollectionCoordinates.top = null;
    G_SelectedCollectionCoordinates.bottom = null;
    G_SelectedCollectionCoordinates.left = null;
    G_SelectedCollectionCoordinates.right = null;

    G_SelectedCollectionLock = false;
  }
  //----------------------------------
  if ((e.shiftKey) && (msdown == true) && (G_FirstSelectedCollectionID != null)) {
    var page_size = null;
    var msgs_quantity = null;
    var fv_msg_index = null;
    var max_fv_msg_index = null;
    var msgs = external.ObjByID(G_FirstSelectedCollectionID);

    var br = null;
    var br2 = null;
    var msgs_container = msgs_container = document.getElementById(G_FirstSelectedCollectionID + '_container');
    //------------
    if (msgs_container) { // Collection can change its own height, so updated coordinates are required
      G_SelectedCollectionLock = true;

      br = getCollectionMsgsBoundingRectangle(G_FirstSelectedCollectionID);

      G_SelectedCollectionCoordinates.top = br.top;
      G_SelectedCollectionCoordinates.bottom = br.bottom;
      G_SelectedCollectionCoordinates.left = br.left;
      G_SelectedCollectionCoordinates.right = br.right;

      //----------------------------------------------------------
      // Real Select Frame coordinates calculation
      var x1 = 0;
      var x2 = 0;
      var y1 = 0;
      var y2 = 0;

      mousexy = coords(e);
      /*
						x1 = msx;
							y1 = msy;
							x2 = mousexy[0];
							y2 = mousexy[1];

							if (x1>x2){
									x1 = x1+x2;
									x2 = x1-x2;
									x1 = x1-x2;
							}

							if (y1>y2){
									y1 = y1+y2;
										y2 = y1-y2;
									y1 = y1-y2;
							}
			*/
      //----------------------------------------------------------
      // if required, do scroll collection
      // ----------------------------------------------------------
      var velocity = 0;
      if (G_SelectedCollectionCoordinates.top != null) {
        if (mousexy[1] < G_SelectedCollectionCoordinates.top) {
          if ((G_SelectedCollectionCoordinates.top - mousexy[1]) <= 20) velocity = 1;
          else velocity = 2;
          quick_collection_scrolling(G_FirstSelectedCollectionID, 'above', velocity, e);
        }
      }
      if (G_SelectedCollectionCoordinates.bottom != null) {
        if (mousexy[1] > G_SelectedCollectionCoordinates.bottom) {
          if ((mousexy[1] - G_SelectedCollectionCoordinates.bottom) <= 20) velocity = 1;
          else velocity = 2;
          quick_collection_scrolling(G_FirstSelectedCollectionID, 'below', velocity, e);
        }
      }
      //----------------------------------------------------------
      // Select Frame coordinates correction for display
      // ----------------------------------------------------------
      if (G_SelectedCollectionCoordinates.left) {
        if (mousexy[0] < G_SelectedCollectionCoordinates.left) x1 = G_SelectedCollectionCoordinates.left;
      }
      if (G_SelectedCollectionCoordinates.right) {
        if (mousexy[0] > G_SelectedCollectionCoordinates.right) x2 = G_SelectedCollectionCoordinates.right;
      }
      // ----------------------------------------------------------
      x1 = G_SelectedCollectionCoordinates.left;
      x2 = G_SelectedCollectionCoordinates.right;
      // ----------------------------------------------------------
      if (G_SelectedCollectionCoordinates.top) {
        if (mousexy[1] < G_SelectedCollectionCoordinates.top) y1 = G_SelectedCollectionCoordinates.top;
      }
      if (G_SelectedCollectionCoordinates.bottom) {
        if (mousexy[1] > G_SelectedCollectionCoordinates.bottom) y2 = G_SelectedCollectionCoordinates.bottom;
      }
      // ----------------------------------------------------------
      // if first selected msg is visible, the select frame starts from it
      if (msg_is_visible(G_FirstSelectedCollectionID, G_FirstSelectedMsgID)) { // alert('first message is visible');
        br2 = getMsgBoundingRectangle(G_FirstSelectedCollectionID, G_FirstSelectedMsgID);

        if ((mousexy[1] < br2.top) && (br2.top)) { y2 = br2.bottom; y1 = mousexy[1]; }
        if (mousexy[1] < br.top) y1 = br.top;

        if ((mousexy[1] >= br2.top) && (br2.top)) { y2 = br2.bottom; y1 = br2.top; }

        if ((mousexy[1] > br2.bottom) && (br2.bottom)) { y1 = br2.top; y2 = mousexy[1]; }
        if (mousexy[1] > br.bottom) y2 = br.bottom;


        //				if((mousexy[1] >= br2.top) && (mousexy[1] <= br2.bottom) && (br2.top) && (br2.bottom)) {
        //					if ((mousexy[1] - br2.top)  > (br2.bottom - mousexy[1])) {y1 = br2.top; y2 = mousexy[1]-3;}
        //					if ((mousexy[1] - br2.top)  < (br2.bottom - mousexy[1])) {y1 = mousexy[1]+3; y2 = br2.bottom;}
        //					if ((mousexy[1] - br2.top) == (br2.bottom - mousexy[1])) {y1 = br2.top; y2 = br2.bottom;}
        //					y1 = br2.top; y2 = br2.bottom;
        //				}
      } else { // if first selected msg is not visible
        fv_msg_index = msgs.Method0('M_GetFirstVisibleMessageIndex');
        page_size = external.Method1('M_GetGlobalOption', 'ItemsPerPage');

        mousexy = coords(e);

        br = getCollectionMsgsBoundingRectangle(G_FirstSelectedCollectionID);

        if ((fv_msg_index - G_FirstSelectedMsgIndex) >= 0) { y1 = br.top; y2 = mousexy[1] - 3; }
        if (mousexy[1] > br.bottom) y2 = br.bottom;

        if ((fv_msg_index + page_size) <= G_FirstSelectedMsgIndex) { y1 = br.bottom; y2 = mousexy[1] + 3; }
        if (mousexy[1] < br.top) y2 = br.top;
      }
      // ----------------------------------------------------------
      if (x1 > x2) {
        x1 = x1 + x2;
        x2 = x1 - x2;
        x1 = x1 - x2;
      }

      if (y1 > y2) {
        y1 = y1 + y2;
        y2 = y1 - y2;
        y1 = y1 - y2;
      }
      // ----------------------------------------------------------
      var sframe = document.getElementById('selectFrame');
      if (!sframe) alert('selectFrame is not found');
      sframe.style.top = y1;
      sframe.style.left = x1;
      sframe.style.width = Math.max(x2 - x1 + 1 - 3, 0);
      sframe.style.height = Math.max(y2 - y1 + 1 - 5, 0);
      sframe.style.visibility = msdown ? 'visible' : 'hidden';
      //----------------------------------------------------------
      // if mouse is out of collection and it is not edge of collection: do scroll collection
      // ----------------------------------------------------------

      mousexy = coords(e);
      fv_msg_index = msgs.Method0('M_GetFirstVisibleMessageIndex');
      //-----------------------------------
      msgs_quantity = msgs.Method0('M_GetCount');
      page_size = external.Method1('M_GetGlobalOption', 'ItemsPerPage');
      max_fv_msg_index = 0;
      if (msgs_quantity > page_size) { max_fv_msg_index = msgs_quantity - page_size; }
      //-----------------------------------
      if (mousexy[0]) {
        if ((mousexy[1] < G_SelectedCollectionCoordinates.top) ||
					(mousexy[1] > G_SelectedCollectionCoordinates.bottom)) {
          if ((fv_msg_index != 0) && (fv_msg_index != max_fv_msg_index)) { // if simulated event is required
            if (!G_MousemoveIsLaunched) { // if simulated event is not launched yet
              G_MousemoveIsLaunched = true;

              G_MousemoveEvent = document.createEventObject();
              G_MousemoveEvent.button = 1;
              G_MousemoveEvent.shiftKey = true;
              G_MousemoveEvent.cancelBubble = true;
              G_MousemoveEvent.simulatedEvent = true;
              G_MousemoveEvent.clientX = mousexy[0];
              G_MousemoveEvent.clientY = mousexy[1];

              G_MousemoveElement = document.body;
              if ((((G_SelectedCollectionCoordinates.bottom + 20) >= mousexy[1]) && (mousexy[1] > G_SelectedCollectionCoordinates.bottom)) ||
								(((mousexy[1] + 20) >= G_SelectedCollectionCoordinates.top) && (mousexy[1] < G_SelectedCollectionCoordinates.top))) {
                window.setTimeout('G_MousemoveElement.fireEvent("onmousemove", G_MousemoveEvent);', 90);
              } else { window.setTimeout('G_MousemoveElement.fireEvent("onmousemove", G_MousemoveEvent);', 0); }
            } else if ((G_MousemoveIsLaunched) && (e.simulatedEvent)) {	// if this sample of event is simulated event
              G_MousemoveElement = document.body;
              if ((((G_SelectedCollectionCoordinates.bottom + 20) >= mousexy[1]) && (mousexy[1] > G_SelectedCollectionCoordinates.bottom)) ||
								(((mousexy[1] + 20) >= G_SelectedCollectionCoordinates.top) && (mousexy[1] < G_SelectedCollectionCoordinates.top))) {
                window.setTimeout('G_MousemoveElement.fireEvent("onmousemove", G_MousemoveEvent);', 90);
              } else { window.setTimeout('G_MousemoveElement.fireEvent("onmousemove", G_MousemoveEvent);', 0); }
            } else if ((G_MousemoveIsLaunched) && (!e.simulatedEvent)) {	// if this sample of event is genuine event
              mousexy = coords(e);
              G_MousemoveEvent.clientX = mousexy[0];
              G_MousemoveEvent.clientY = mousexy[1];
            }
          }
        } else G_MousemoveIsLaunched = false;
      } // if simulated event is not required
    } else alert('messages container is not found');
  }
}
// (R)
function msgs_selection_onmouseup(e) {
  //----------------------------------
  if (!e) e = window.event;
  //----------------------------------
  if (G_Collection_lock == true) return; // Collection is being scrolled now by dragIt(), release()
  //----------------------------------
  if (e == null) { alert('onmouseup event == null'); return; }
  //----------------------------------
  var mousexy = coords(e);
  // ----------------------------------------------------------------
  var msgs = null;
  if (G_FirstSelectedCollectionID && G_FirstSelectedMsgID) { // if items selection has been started
    msgs = external.ObjByID(G_FirstSelectedCollectionID);
    if (msgs) G_LastSelectedCollectionID = G_FirstSelectedCollectionID;
  } else return; // if items selection has not been started
  // ----------------------------------------------------------------
  if (G_SelectedCollectionCoordinates.top) {
    if ((mousexy[1] < G_SelectedCollectionCoordinates.top) && (msgs)) { // if mouse pointer is above coordinates bar
      var msg = msgs.Method0('M_GetFirstVisibleMessage');
      if (msg) {
        G_LastSelectedMsgID = msg.ObjID;
        G_LastSelectedMsgIndex = msgs.Method0('M_GetFirstVisibleMessageIndex');
      }
    }
  }
  // ----------------------------------------------------------------
  if (G_SelectedCollectionCoordinates.bottom) {
    if ((mousexy[1] > G_SelectedCollectionCoordinates.bottom) && (msgs)) { // if mouse pointer is below coordinates bar
      var lastVisibleMsgID = null;
      var msg = msgs.Method0('M_GetFirstVisibleMessage');
      if (msg != null) lastVisibleMsgID = msg.objID;

      while (msg != null) {
        msg = msgs.Method1('M_GetNextVisibleMessage', msg);
        if (msg != null) lastVisibleMsgID = msg.objID;
        if (lastVisibleMsgID != null) G_LastSelectedMsgID = lastVisibleMsgID;
      }
      G_LastSelectedMsgIndex = get_selected_msg_index(G_FirstSelectedCollectionID, G_LastSelectedMsgID);
    }
  }
  // ----------------------------------------------------------------
  if (msdown == true) {
    if (G_LastSelectedMsgID == null) {
      G_LastSelectedMsgID = G_FirstSelectedMsgID;
      G_LastSelectedMsgIndex = G_FirstSelectedMsgIndex;
      G_LastSelectedCollectionID = G_FirstSelectedCollectionID;
    }
    if (G_LastSelectedMsgID != null) do_selection(); // mouse is located inside of collection but onmouseup event for message was not launched (selectFrame does not allow this event)
    msdown = false;
  }
  document.getElementById('selectFrame').style.visibility = 'hidden';

  G_FirstSelectedMsgID = null;
  G_FirstSelectedMsgIndex = null;
  G_FirstSelectedCollectionID = null;
  G_LastSelectedMsgID = null;
  G_LastSelectedMsgIndex = null;
  G_LastSelectedCollectionID = null;

  G_SelectedCollectionCoordinates.top = null;
  G_SelectedCollectionCoordinates.bottom = null;
  G_SelectedCollectionCoordinates.left = null;
  G_SelectedCollectionCoordinates.right = null;

  G_SelectedCollectionLock = false;
  // ----------------------------------------------------------------
}

function msg_selection_onmouseup(e) {
  //= ==================================================
  if (!e) e = window.event;
  //----------------------------------
  if (G_Collection_lock == true) return; // Collection is being scrolled now by dragIt(), release()
  //= ==================================================
  if (e == null) { alert('onmouseup event == null'); return; }
  //= ==================================================
  var msg_id = null;
  var collection_id = null;

  if (e.shiftKey) {
    var el = event.srcElement;
    var p = el;
    while (p.nodeName.toLowerCase() != 'tbody') { p = p.parentNode; }
    msg_id = p.id;
    collection_id = p.CollectionID;
  }
  //= ==================================================
  if ((msg_id != null) && (collection_id != null)) {
    if ((collection_id == G_FirstSelectedCollectionID)) {
      G_LastSelectedMsgID = msg_id;
      G_LastSelectedMsgIndex = get_selected_msg_index(collection_id, msg_id);
      G_LastSelectedCollectionID = collection_id;

      if (msdown == true) {
        if (G_LastSelectedMsgID != G_FirstSelectedMsgID) do_selection(); // two or more messages are required
        msdown = false;
      }

      document.getElementById('selectFrame').style.visibility = 'hidden';

      G_FirstSelectedMsgID = null;
      G_FirstSelectedMsgIndex = null;
      G_FirstSelectedCollectionID = null;
      G_LastSelectedMsgID = null;
      G_LastSelectedMsgIndex = null;
      G_LastSelectedCollectionID = null;

      G_SelectedCollectionCoordinates.top = null;
      G_SelectedCollectionCoordinates.bottom = null;
      G_SelectedCollectionCoordinates.left = null;
      G_SelectedCollectionCoordinates.right = null;

      G_SelectedCollectionLock = false;
    } else { // another function will be called: msgs_selection_onmouseup();
      msgs_selection_onmouseup(e);
    }
  } else {
    msdown = false;
    document.getElementById('selectFrame').style.visibility = 'hidden';

    G_FirstSelectedMsgID = null;
    G_FirstSelectedMsgIndex = null;
    G_FirstSelectedCollectionID = null;
    G_LastSelectedMsgID = null;
    G_LastSelectedMsgIndex = null;
    G_LastSelectedCollectionID = null;

    G_SelectedCollectionCoordinates.top = null;
    G_SelectedCollectionCoordinates.bottom = null;
    G_SelectedCollectionCoordinates.left = null;
    G_SelectedCollectionCoordinates.right = null;

    G_SelectedCollectionLock = false;
  }
  //= ==================================================
}

// ///////////////////////////////////////////////////////////////////////////////////////////
//                                   body mouse events
// ///////////////////////////////////////////////////////////////////////////////////////////

function body_onmousedown(e) {
  if (!e) e = window.event;
  //	window.event.cancelBubble = true;
  if (G_Selected_lock == true) return;
  if (G_Collection_lock == true) return; // Collection is being scrolled now by dragIt(), release()
}

// ///////////////////////////////////////////////////////////////////////////////////////////

function body_onmousemove(e) {
  if (!e) e = window.event;
  //	window.event.cancelBubble = true;
  if (G_Selected_lock == true) return;
  if (G_Collection_lock == true) return; // Collection is being scrolled now by dragIt(), release()

  msgs_selection_onmousemove(e);
}

// ///////////////////////////////////////////////////////////////////////////////////////////

function body_onmouseup(e) {
  if (!e) e = window.event;
  G_ScrollButtonIsPressed = false; // if any collection  is being scrolled, and the mouse is not located above the scroll button
  //	window.event.cancelBubble = true;
  if (G_Selected_lock == true) return;
  if (G_Collection_lock == true) return; // Collection is being scrolled now by dragIt(), release()

  if (e.shiftKey) {
    msgs_selection_onmouseup(e);
  }
}

// ///////////////////////////////////////////////////////////////////////////////////////////
// (R)
function msg_is_visible(collection_id, msg_id) {
  var visibility = false;

  var msgs = external.ObjByID(collection_id);

  var msg = msgs.Method0('M_GetFirstVisibleMessage');
  var msgID = null;
  if (msg != null) msgID = msg.objID;

  if ((msg != null) && (msgID != null)) {
    while (msg_id != msg.objID) {
      msg = msgs.Method1('M_GetNextVisibleMessage', msg);
      msgID = null;
      if (msg != null) {
        msgID = msg.objID;
        if (msg_id == msg.objID) visibility = true;
      } else break;
    }
  }

  return visibility;
}

// (R)
function get_selected_msg_index(collection_id, msg_id) {
  var index = null;

  var msgs = external.ObjByID(collection_id);

  var msg = msgs.Method0('M_GetFirstVisibleMessage');
  var msg_index = msgs.Method0('M_GetFirstVisibleMessageIndex');

  if (msg != null) {
    if (msg_id == msg.objID) index = msg_index;
  }

  if ((msg != null) && (msg.objID != null)) {
    while (msg_id != msg.objID) {
      msg = msgs.Method1('M_GetNextVisibleMessage', msg);
      msg_index++;
      if (msg != null) {
        if (msg_id == msg.objID) index = msg_index;
      } else break;
    }
  }

  return index;
}

// (R)
function getMsgBoundingRectangle(collection_id, msg_id) {
  var msg_coordinates = {};
  var br = null;
  var msg = null;
  var El = null;
  var msgs = document.getElementById(collection_id + '_container');

  if (msgs) {
    var descendants = msgs.getElementsByTagName('*');
    for (var i = 0; i < descendants.length; i++) {
      El = descendants[i];
      if ((El.tagName.toLowerCase() == 'tbody') && (El.id == msg_id)) {
        msg = El;
        i = descendants.length; // alert('msg is found');
      }
    }

    if (msg) {
      br = msg.getBoundingClientRect();

      var shift_x = document.body.scrollLeft + document.documentElement.scrollLeft;
      var shift_y = document.body.scrollTop + document.documentElement.scrollTop;

      msg_coordinates.top = br.top + shift_y;
      msg_coordinates.bottom = br.bottom + shift_y;
      msg_coordinates.left = br.left + shift_x;
      msg_coordinates.right = br.right + shift_x;
    }
  }
  return msg_coordinates;
}
// //
// ///////////////////////////////////////////////////////////////////////////////////////
// (R)
function getCollectionMsgsBoundingRectangle(collection_id) {
  var br = null;
  var br2 = null;
  var collection = document.getElementById(collection_id + '_container');
  var collection_coordinates = {};

  if (collection) {
    br = collection.getBoundingClientRect();

    var shift_x = document.body.scrollLeft + document.documentElement.scrollLeft;
    var shift_y = document.body.scrollTop + document.documentElement.scrollTop;

    collection_coordinates.top = br.top + shift_y;
    collection_coordinates.bottom = br.bottom + shift_y;
    collection_coordinates.left = br.left + shift_x;
    collection_coordinates.right = br.right + shift_x;


    var collection_header = document.getElementById(collection_id + '_header_line_container');
    if (!collection_header) collection_header = document.getElementById('SelectedMenu_' + collection_id);
    if (collection_header) br2 = collection_header.getBoundingClientRect();
    if (br2.top) collection_coordinates.top += (br2.bottom - br2.top + 1);
    /*
				msgs = external.ObjByID(collection_id);
				msg = msgs.Method0('M_GetFirstVisibleMessage');
				br2 = getMsgBoundingRectangle (collection_id, msg.objID);
				// first collection element is a header element
				collection_coordinates.top += (br2.bottom-br2.top+1);
		*/
  }
  return collection_coordinates;
}
// (R)
function getElementBoundingRectangle(element_id) {
  var br = null;
  var element = document.getElementById(element_id);
  var element_coordinates = {};

  if (element) {
    br = element.getBoundingClientRect();

    var shift_x = document.body.scrollLeft + document.documentElement.scrollLeft;
    var shift_y = document.body.scrollTop + document.documentElement.scrollTop;

    element_coordinates.top = br.top + shift_y;
    element_coordinates.bottom = br.bottom + shift_y;
    element_coordinates.left = br.left + shift_x;
    element_coordinates.right = br.right + shift_x;
  }
  return element_coordinates;
}
// (R)
function do_selection() {
  if ((G_FirstSelectedCollectionID) && (G_FirstSelectedMsgID) && (G_LastSelectedMsgID)) {
    var msgs = external.ObjByID(G_FirstSelectedCollectionID);
    // msg_node_onmousedown() --> msgs.Method1('M_StartSelection', msg_id)
    G_LastSelectedMsgIndex = msgs.Method1('M_InvertSelectedByRange', G_LastSelectedMsgID);
  }
}

// int collection::MGetVisibleMessageIndexByID(id)
// int collection::MStartSelection(id)
// int collection::MInvertSelectedByRange(id)

/*
	var msgs = external.ObjByID(G_FirstSelectedCollectionID);
	var CollectionChildrenCount = msgs.Method0('M_GetCount');
	var msg_selected = null;

	var msg = msgs.GetFirst();
	var msg_id = null;
	var next = null;
	do {
		next = false;
   		if (msg) msg_id = msg.ObjID;
			if ((msg_id != null) && (msg_id != G_FirstSelectedMsgID) && (msg_id != G_LastSelectedMsgID)) {
				msg = msgs.GetNext(msg);
				next = true;
			}
	}
	while (next);

	if (msg.ObjID == G_LastSelectedMsgID) G_LastSelectedMsgID = G_FirstSelectedMsgID;

	do {
		next = false;
   		if (msg) msg_id = msg.ObjID;
		if ((msg_id != null) && (msg_id != G_LastSelectedMsgID)) {
			msg_selected = msg.Method0('M_GetSelectedState');
			if (msg_selected != null) {
				if (msg_selected == false) 	msg_selected = true;
				else 						msg_selected = false;
				msg.Method1('M_SetSelectedState', msg_selected);
			}
			msg = msgs.GetNext(msg);
			next = true;
		}
	}
	while (next);

	msg_selected = msg.Method0('M_GetSelectedState');
	if (msg_selected != null) {
		if (msg_selected == false) 	msg_selected = true;
		else 						msg_selected = false;
		msg.Method1('M_SetSelectedState', msg_selected);
	}
}

*/

// ///////////////////////////////////////////////////////////////////////////////////////////
// (R)
function quick_collection_scrolling(CollectionID, Direction, Velocity, Event) {
  var msgs = external.ObjById(CollectionID);
  //	var msgs = G_Assocs[G_ActiveAssocID].TreeView._collections[CollectionID].obj;
  var first_visible_msg_index = msgs.Method0('M_GetFirstVisibleMessageIndex');
  //-------------------------
  var msgs_quantity = msgs.Method0('M_GetCount');
  var page_size = external.Method1('M_GetGlobalOption', 'ItemsPerPage');
  var max_first_visible_msg_index = 0;

  if (msgs_quantity > page_size) { max_first_visible_msg_index = msgs_quantity - page_size; }

  //-------------------------
  if ((Direction == 'below') && (Velocity == 1) && (first_visible_msg_index < max_first_visible_msg_index)) go_next_message(CollectionID);
  else if ((Direction == 'above') && (Velocity == 1) && (first_visible_msg_index > 0)) go_prev_message(CollectionID);

  if ((Direction == 'below') && (Velocity == 2) && (first_visible_msg_index < max_first_visible_msg_index)) { go_next_message(CollectionID); } // go_next_page(CollectionID);
  else if ((Direction == 'above') && (Velocity == 2) && (first_visible_msg_index > 0)) { go_prev_message(CollectionID); } // go_prev_page(CollectionID);
  //-------------------------
}
// /////////////////////////////////////////////////////////////////////////////////////////
// 									msg node mouse events
function msg_node_onclick(e) {
  //= ======================================================
  e = e || event;
  //= ======================================================
  if (G_Selected_lock == true) return; 	// Selection is forbidden
  if (G_Collection_lock == true) return; 	// Collection is being scrolled now by dragIt(), release()
  //= ======================================================
  if (e.ctrlKey) {
    //= ==================================================
    var msg_id = null;
    var msg_obj = null;
    var msg_selected = null;
    //= ==================================================
    var el = e.srcElement;
    var p = el;
    while (p.nodeName.toLowerCase() != 'tbody') { p = p.parentNode; }
    msg_id = p.id;
    collection_id = p.CollectionID;
    //= ==================================================
    if (msg_id != null) {
      msg_obj = external.ObjByID(msg_id);
      msg_selected = msg_obj.Method0('M_GetSelectedState');
    }
    //= ==================================================
    if (msg_selected != null) {
      if (msg_selected == true) msg_selected = false;
      else msg_selected = true;

      msg_obj.Method1('M_SetSelectedState', msg_selected);
    }
    //= ==================================================
  }
}

// ///////////////////////////////////////////////////////////////////////////////////////////
// (R)
function msg_node_onmousedown(e) {
  //-------------------------------------------------------
  if (!e) e = window.event;
  window.event.cancelBubble = true;
  if (G_Selected_lock == true) { msdown = false; return; } 		// Selection is forbidden
  if (G_Collection_lock == true) { msdown = false; return; } 	// Collection is being scrolled now by dragIt(), release()
  //-------------------------------------------------------
  if (e.shiftKey) {
    //---------------------------------------------------
    msdown = true;
    var mousexy = coords(e);
    msx = mousexy[0];
    msy = mousexy[1];
    //---------------------------------------------------
    var el = event.srcElement;
    var p = el;
    while (p.nodeName.toLowerCase() != 'tbody') { p = p.parentNode; }
    var msg_id = p.id;
    var collection_id = p.CollectionID;
    // ---------------------------------------------------
    if ((msg_id != null) && (collection_id != null)) {
      //-----------------------------------------------
      var msgs = external.ObjByID(collection_id);

      G_FirstSelectedMsgID = msg_id;
      G_FirstSelectedCollectionID = collection_id;
      G_FirstSelectedMsgIndex = msgs.Method1('M_StartSelection', msg_id); // get_selected_msg_index(collection_id, msg_id);
      G_LastSelectedMsgID = null;
      G_LastSelectedMsgIndex = null;
      G_LastSelectedCollectionID = null;
      //-----------------------------------------------
      var br = getCollectionMsgsBoundingRectangle(collection_id);
      G_SelectedCollectionLock = true;
      G_SelectedCollectionCoordinates.top = br.top;
      G_SelectedCollectionCoordinates.bottom = br.bottom;
      G_SelectedCollectionCoordinates.left = br.left;
      G_SelectedCollectionCoordinates.right = br.right;
      //-----------------------------------------------
    }
  }
}

// ///////////////////////////////////////////////////////////////////////////////////////////

function msg_node_onmousemove(e) {
  //= ==================================================
  if (!e) e = window.event;
  if (G_Selected_lock == true) { return; } 		// Selection is forbidden
  if (G_Collection_lock == true) { return; } 	// Collection is being scrolled now by dragIt(), release()
  //= ==================================================

  if (e.shiftKey) {
    var el = window.event.srcElement;
    var p = null;

    if (el) p = el;
    if (p) {
      if (p.nodeName) {
        var node_name = p.nodeName.toLowerCase();
        var step_is_required = true;
        if (p.nodeName.toLowerCase() == 'tbody') step_is_required = false;

        while (step_is_required) {
          p = p.parentNode;
          node_name = null;
          if (p) {
            if (p.nodeName) node_name = p.nodeName.toLowerCase();
            if (node_name) {
              if (p.nodeName.toLowerCase() == 'tbody') step_is_required = false;
            } else step_is_required = false;
          } else step_is_required = false;
        }
      }
    }
  }
  //= ==================================================
  msgs_selection_onmousemove(e);
  //= ==================================================
}

// ///////////////////////////////////////////////////////////////////////////////////////////
// (R)
function msg_node_onmouseup(e) {
  if (!e) e = window.event;
  window.event.cancelBubble = true;

  G_ScrollButtonIsPressed = false; // if any collection  is being scrolled, and the mouse is not located above the scroll button

  if (G_Selected_lock == true) { return; } 		// Selection is forbidden
  if (G_Collection_lock == true) { return; } 	// Collection is being scrolled now by dragIt(), release()

  if (e.shiftKey) {
    var el = window.event.srcElement;
    var p = el;
    while (p.nodeName.toLowerCase() != 'tbody') { p = p.parentNode; }

    msg_selection_onmouseup(e);
  } else {
    var sframe = document.getElementById('selectFrame');
    sframe.style.visibility = 'hidden';

    G_FirstSelectedMsgID = null;
    G_FirstSelectedMsgIndex = null;
    G_FirstSelectedCollectionID = null;
    G_LastSelectedMsgID = null;
    G_LastSelectedMsgIndex = null;
    G_LastSelectedCollectionID = null;

    G_SelectedCollectionCoordinates.top = null;
    G_SelectedCollectionCoordinates.bottom = null;
    G_SelectedCollectionCoordinates.left = null;
    G_SelectedCollectionCoordinates.right = null;

    G_SelectedCollectionLock = false;
  }
}
// (R)
function msg_node_onmousewheel(e) {
  //= ======================================================
  if (!e) e = window.event;
  if (G_Selected_lock == true) { return; } 		// Selection is forbidden
  if (G_Collection_lock == true) { return; } 	// Collection is being scrolled now by dragIt(), release()
  //= ======================================================
  if ((G_Shown_message.msg_id != null) && (G_Shown_message.collection_id != null)) {
    G_Above_message.msg_id = null;
    G_Above_message.collection_id = null;

    var msg_id = G_Shown_message.msg_id;
    var collection_id = G_Shown_message.collection_id;

    CloseMenu(msg_id, collection_id);
    CloseSelectedMenu(msg_id, collection_id);
  }
  //= ======================================================
  if (e.shiftKey) {
    //= ==================================================
    var msg_id = null;
    var msg_obj = null;
    var msg_selected = null;
    var collection_id = null;
    //= ==================================================
    var el = window.event.srcElement;
    var p = el;
    while (p.nodeName.toLowerCase() != 'tbody') { p = p.parentNode; }
    msg_id = p.id;
    collection_id = p.CollectionID;
    //= ==================================================
    msg_obj = external.ObjByID(msg_id);
    msg_selected = msg_obj.Method0('M_GetSelectedState');
    //= ==================================================
    if (!G_Assocs[G_ActiveAssocID]) return true;
    if (!G_Assocs[G_ActiveAssocID].TreeView._collections[collection_id]) return true;
    if (G_Assocs[G_ActiveAssocID].TreeView._collections[collection_id].obj) { var collection_message_list = G_Assocs[G_ActiveAssocID].TreeView._collections[collection_id].obj; } else return true;
    var message_counter_value = collection_message_list.Method0('M_GetCount');
    var items_per_page = external.Method1('M_GetGlobalOption', 'ItemsPerPage');

    if (message_counter_value > items_per_page) {
      var collection_node = G_Assocs[G_ActiveAssocID].TreeView.root._children[collection_id];
      var first_msg_index = collection_message_list.Method0('M_GetFirstVisibleMessageIndex');

      wheel_lock = true;
      event.cancelBubble = true;

      if (msg_selected != null) {
        if (msg_selected == false) msg_selected = true;
        else msg_selected = false;
        msg_obj.Method1('M_SetSelectedState', msg_selected);
      }

      if ((event.wheelDelta > 0) && (first_msg_index > 0) && (collection_node._expanded == true)) {
        go_prev_message(collection_id);
        wheel_lock = false;
        return false;
      } else if ((event.wheelDelta < 0) && (first_msg_index < message_counter_value - items_per_page) && (collection_node._expanded == true)) {
        go_next_message(collection_id);
        wheel_lock = false;
        return false;
      }
      wheel_lock = false;
      return true;
    }
    wheel_lock = false;
    return true;
  }
  // ---------------------------------------------------
}

// /////////////////////////////////////////////////////////////////////////////////////////
/*
function ExtSelectMsg(MsgCollectionObjID, MsgObjID) {
	ExtChangeMsg(MsgCollectionObjID, MsgObjID);
	SelectCounterRefresh(MsgCollectionObjID);
	UpdateSelectedMenu(MsgCollectionObjID, null);
}
*/
// /////////////////////////////////////////////////////////////////////////////////////////

function CreateSelectControls(CollectionID, SelectControlsExist) {
  if (!SelectControlsExist) {
    this.top_counter = '<div class="SelectTopCounter" ID="SelectCounter_' + CollectionID +
			'" title="' + external.LS('tooltip_select_counter') + '" nowrap>' +
			'</div>';

    this.top_menu = '<div ID="SelectMenu_container_' + CollectionID + '">' +
			'<img src="Pics/Dot.gif">' +
			'</div>';

    this.top_menu_full = '<div ID="SelectMenu_container_' + CollectionID + '">' +
			'<img ID="SelectMenu_' + CollectionID +
			'" onmousedown="SelectMenu_Down(this)" onclick="SelectMenu_OnClick(\'' + CollectionID + '\');" ' +
			'ondblclick="SelectMenu_OnDblClick(\'' + CollectionID + '\');" onmouseenter="SelectMenu_OnMouseEnter(this)" ' +
			'onmouseleave="SelectMenu_OnMouseLeave(this)" onmouseup="SelectMenu_OnMouseUp(this)" ' +
			'src="Pics/btn_group_actions.gif" style="cursor:hand; margin-left: 1px; margin-right: 0px;" title="' +
			external.LS('tooltip_select_menu') + '">' +
			'</div>';

    this.top_button_select_all = '<div ID="SelectAll_container_' + CollectionID + '">' +
			'<img ID="SelectAll_' + CollectionID + '" ' +
			'onclick="SelectAll_OnClick(\'' + CollectionID + '\');" ' +
			'ondblclick="SelectAll_OnDblClick(\'' + CollectionID + '\');" ' +

			'onmouseenter="ButtonView(this, \'selected\');" ' +
			'onmousedown="ButtonView(this, \'used\');" ' +
			'onmouseup="ButtonView(this, \'abandoned\');" ' + // selected
			'onmouseleave="ButtonView(this, \'abandoned\');" ' +

			'src="Pics/btn_group_select_all.gif" style="cursor:hand; margin-left: 1px; margin-right: 0px;" title="' +
			external.LS('tooltip_button_select_all') + '">' +
			'</div>';

    this.top_button_deselect_all = '<div ID="DeselectAll_container_' + CollectionID + '">' +
			'<img ID="DeselectAll_' + CollectionID + '" ' +
			'onclick="DeselectAll_OnClick(\'' + CollectionID + '\');" ' +
			'ondblclick="DeselectAll_OnDblClick(\'' + CollectionID + '\');" ' +

			'onmouseenter="ButtonView(this, \'selected\');" ' +
			'onmousedown="ButtonView(this, \'used\');" ' +
			'onmouseup="ButtonView(this, \'abandoned\');" ' + // selected
			'onmouseleave="ButtonView(this, \'abandoned\');" ' +

			'src="Pics/btn_group_deselect_all.gif" style="cursor:hand; margin-left: 1px; margin-right: 5px;" title="' +
			external.LS('tooltip_button_deselect_all') + '">' +
			'</div>';
  } else {
    var Collection = external.ObjById(CollectionID);
    var select_counter = Collection.Method0('M_GetSelectedItemsCount');

    this.top_counter = '<div class="SelectTopCounter" ID="SelectCounter_' + CollectionID +
			'" title="' + external.LS('tooltip_select_counter') + '" nowrap>' +
			' ' + external.LS('str_select_counter') +
			' (' + select_counter + ')' + '</div>';

    this.top_menu = '<div ID="SelectMenu_container_' + CollectionID + '">' +
			'<img src="Pics/Dot.gif">' +
			'</div>';

    this.top_menu_full = this.top_menu;

    this.top_button_select_all = '<div ID="SelectAll_container_' + CollectionID + '">' +
			'<img ID="SelectAll_' + CollectionID + '" ' +
			'onclick="SelectAll_OnClick(\'' + CollectionID + '\');" ' +
			'ondblclick="SelectAll_OnDblClick(\'' + CollectionID + '\');" ' +

			'onmouseenter="ButtonView(this, \'selected\');" ' +
			'onmousedown="ButtonView(this, \'used\');" ' +
			'onmouseup="ButtonView(this, \'abandoned\');" ' +  // selected
			'onmouseleave="ButtonView(this, \'abandoned\');" ' +

			'src="Pics/btn_group_select_all.gif" style="cursor:hand; margin-left: 1px; margin-right: 0px;" title="' +
			external.LS('tooltip_button_select_all') + '">' +
			'</div>';

    this.top_button_deselect_all = '<div ID="DeselectAll_container_' + CollectionID + '">' +
			'<img ID="DeselectAll_' + CollectionID + '" ' +
			'onclick="DeselectAll_OnClick(\'' + CollectionID + '\');" ' +
			'ondblclick="DeselectAll_OnDblClick(\'' + CollectionID + '\');" ' +

			'onmouseenter="ButtonView(this, \'selected\');" ' +
			'onmousedown="ButtonView(this, \'used\');" ' +
			'onmouseup="ButtonView(this, \'abandoned\');" ' + // selected
			'onmouseleave="ButtonView(this, \'abandoned\');" ' +

			'src="Pics/btn_group_deselect_all.gif" style="cursor:hand; margin-left: 1px; margin-right: 5px;" title="' +
			external.LS('tooltip_button_deselect_all') + '">' +
			'</div>';
  }

  if (G_Selected_lock == true) {
    this.top_counter = '<div class="SelectTopCounter" ID="SelectCounter_' + CollectionID + '" >' + '</div>';
    this.top_menu = '<div ID="SelectMenu_container_' + CollectionID + '">' + '<img src="Pics/Dot.gif">' + '</div>';
    this.top_menu_full = '<div ID="SelectMenu_container_' + CollectionID + '">' + '<img src="Pics/Dot.gif">' + '</div>';
    this.top_button_select_all = '<div ID="SelectAll_container_' + CollectionID + '">' + '<img src="Pics/Dot.gif">' + '</div>';
    this.top_button_deselect_all = '<div ID="DeselectAll_container_' + CollectionID + '">' + '<img src="Pics/Dot.gif">' + '</div>';
  }

  return this;
}

function SelectCounterRefresh(MsgCollectionObjID, Counter) {
  if (MsgCollectionObjID === undefined) return;
  if (Counter === undefined) Counter = MsgCollectionObj.Method0('M_GetSelectedItemsCount');

  var MsgCollectionObj = external.ObjById(MsgCollectionObjID);
  var SelectedItemsQuantity = Counter; // MsgCollectionObj.Method0('M_GetSelectedItemsCount');

  var select_counter_element = document.getElementById('SelectCounter_' + MsgCollectionObjID);
  if (select_counter_element) {
    if (SelectedItemsQuantity > 0) select_counter_element.innerHTML = ' ' + external.LS('str_select_counter') + ' (' + SelectedItemsQuantity + ')';
    else {
      select_counter_element.innerHTML = '';
      G_Assocs[G_ActiveAssocID].TreeView.redraw_collection(MsgCollectionObjID);
      UpdateSelectedMenu(MsgCollectionObjID, null); // SelectedWinningTreeID == null means that selected menu is closed
    }
  }
}

function ButtonView(control_element, mode) {
  var control_src = control_element.src;
  if (control_src == null) return;

  if (mode == 'selected') {
    control_src = control_src.replace(/.gif/, '_hi.gif');
  }
  if (mode == 'used') {
    control_src = control_src.replace(/(_hi)?.gif/, '.gif');
    control_src = control_src.replace(/.gif/, '_gray.gif');
  }
  if (mode == 'abandoned') {
    control_src = control_src.replace(/(_hi)?.gif/, '.gif');
    control_src = control_src.replace(/(_gray)?.gif/, '.gif');
  }
  control_element.src = control_src;
}

function SelectAll_OnClick(CollectionID) {
  G_Select_All = true;
  //	var e = window.event;
  //	e.cancelBubble = true;

  var Collection = external.ObjById(CollectionID);
  Collection.Method0('M_SelectAllItems');
}

function SelectAll_OnDblClick(CollectionID) {
  // var Collection = external.ObjById(CollectionID);
  // Collection.Method0('M_DeselectAllItems');
}

// /////////////////////////////////////////////////////////////////////////////////////////

function DeselectAll_OnClick(CollectionID) {
  G_Select_All = true;

  var Collection = external.ObjById(CollectionID);
  Collection.Method0('M_DeselectAllItems');
}

function DeselectAll_OnDblClick(CollectionID) {
  // var Collection = external.ObjById(CollectionID);
  // Collection.Method0('M_SelectAllItems');
}

// /////////////////////////////////////////////////////////////////////////////////////////

function getOffsetSum(elem) {
  var top = 0;
  var left = 0;
  while (elem) {
    top = top + parseInt(elem.offsetTop);
    left = left + parseInt(elem.offsetLeft);
    elem = elem.offsetParent;
  }
  return {
    top: top,
    left: left
  };
}

function GetInfoAttrs(elementID) {
  var info = document.getElementById(elementID);
  var message = '';
  for (var i = 0; i < info.attributes.length; i++) {
    var attr = info.attributes[i];
    if (attr.specified) {
      message += attr.name + ' = ' + attr.value + '<br/>';
    }
  }
  return message;
}

function SelectMenu_Down(Obj) {

}

function SelectMenu_OnClick(CollectionID) {
  window.event.cancelBubble = true;
  var head_el_1 = document.getElementById(CollectionID + '_sort_by_name_1');
  var head_el_2 = document.getElementById(CollectionID + '_sort_by_time_2');

  var br_1 = head_el_1.getBoundingClientRect();
  var br_2 = head_el_2.getBoundingClientRect();

  // alert("Top:"+br_1.top+", Left:"+br_1.left+", Right:"+br_1.right+", Bottom:"+br_1.bottom);

  var menu_width = (br_2.right - br_1.left);
  var button_container_element = document.getElementById(CollectionID + '_sort_by_winner');
  var selected_menu_html = '<table id="' + CollectionID + '_selected_menu_table" collection_id="' + CollectionID + '" ' +
		'width="' + menu_width + 'px"' +
		'border="0" cellspacing="0" cellpadding="0" class="MessageList">' +
		decodeURIComponent(button_container_element.selected_menu_outerHtml) +
		'</table>';

  //		var container_element = document.getElementById(CollectionID+'_message_list_container');
  //		var container_element = document.getElementById(CollectionID+'_winner_item_container');
  var container_element = document.getElementById(CollectionID + '_winner_item_container');
  var scroll_el = document.getElementById(CollectionID + '_ScrollBar');
  var scroll_br = scroll_el.getBoundingClientRect();
  var scroll_width = (scroll_br.right - scroll_br.left);

  var div_node = document.createElement('div');

  //----------------

  div_node.id = CollectionID + '_selected_menu_container';
  //		div_node.style.position = "absolute";
  //		div_node.style.position = "relative";
  //		div_node.style.left = -200; //-(br_2.right - br_1.left)/2 - 50;
  //		div_node.style.top = 20;
  div_node.style.height = (br_2.bottom - br_1.top);
  div_node.style.width = (br_2.right - br_1.left);

  var h_shift = Math.round((br_2.right - br_1.left) / 2 - scroll_width); alert(h_shift);

  div_node.style.visibility = 'visible'; // "hidden";
  //		div_node.style.display = 'block'; //"inline"; //"none"; // "block";
  //		div_node.style.zIndex = '13';
  div_node.style.border = 'thick solid #FFFFFF';
  div_node.style.cssText = 'position: absolute; z-index:1; left:-' + h_shift + '; top:_2;'; // 'left:100; top:0; width:100%; height:100%;';

  div_node.innerHTML = selected_menu_html;

  //		var container_element = document.getElementById('context_menu_Frame');
  /*
			container_element.style.position = "absolute";
			container_element.style.left = br_1.left; //br_1.left;
			container_element.style.top = 0; //br_1.top;
	//		container_element.style.height = (br_2.bottom - br_1.top);
	//		container_element.style.width = (br_2.right - br_1.left);
			container_element.style.visibility = 'visible'; //"hidden";
	//		container_element.style.display = 'block'; //"inline"; //"none"; // "block";
			container_element.style.zIndex = '1';
	*/

  //		if (container_element.children[0]) container_element.removeChild(container_element.children[0]);
  //		container_element.appendChild(div_node);
  container_element.insertBefore(div_node, container_element.children[0]);

  //		 alert(container_element.children[0].id);
  // 		 alert(container_element.children[0].style.position);
  //		 alert(container_element.children[0].style.position);
  //		menu_element.style.left = -200; //-(br_2.right - br_1.left)/2 - 50;
  //		menu_element.style.top = 20;
  //----------------

  //		var info = GetInfoAttrs (CollectionID+'_container');
  //		alert("element:   " + info);

  //	container_element.appendChild(div_node);
}

function SelectMenu_OnDblClick(CollectionID) {

}

function SelectMenu_OnMouseEnter(Obj) {

}

function SelectMenu_OnMouseLeave(Obj) {

}

function SelectMenu_OnMouseUp(Obj) {

}

function CreateSelectMenuObject(AccosID) {
  this.path = {};
  this.count = 0;
  this.ID = {};

  var AssocObj = external.ObjByID(AssocID);
  var SuperTreeList = AssocObj.GetM('m_SuperTreesPtr');
  SuperTree_Obj = SuperTreeList.GetFirst();
  var i = 0;
  while (SuperTree_Obj != null) {
    SyncTree_ID = SuperTree_Obj.objID;
    SyncTree_path = SuperTree_Obj.Method0Own('M_ToViewFilename');

    this.path[i] = SyncTree_path;
    this.ID[SyncTree_path] = SyncTree_ID;
    this.count = i;

    SuperTree_Obj = SuperTreeList.GetNext(SuperTree_Obj);
    i++;
  }

  return this;
}

// ----------------------------------------------------------------------

function CreateSelectedMenuRootFolderHTML(SyncItemCharacteristics, Counter) {
  //
  var name = '';
  var size = '';
  var size_title = '';
  var time = '';
  var time_title = '';
  var state = '';
  var state_style = '';

  var root_name = '';
  var sync_type_name = '';
  var sync_type_picture = '';

  var Item_html = '';
  var Root_Item_html = '';

  var root_name_html = '';
  var sync_type_name_html = '';

  if (Counter == -1) return Item_html;

  if (SyncItemCharacteristics != null) {
    root_name = SyncItemCharacteristics.root_name;
    sync_type_name = SyncItemCharacteristics.sync_type_name;
    sync_type_picture = SyncItemCharacteristics.sync_type_picture;

    if (SyncItemCharacteristics.state == '') {
      name = SyncItemCharacteristics.name;
      size = SyncItemCharacteristics.size;
      size_title = external.LS('tooltip_file_size');
      time = SyncItemCharacteristics.time;
      time_title = external.LS('tooltip_file_time');
    }
    if (SyncItemCharacteristics.state != '') {
      name = SyncItemCharacteristics.name;
      state = SyncItemCharacteristics.state;
      if (SyncItemCharacteristics.if_missing == true) state_style = 'style="color:red"';
      if (SyncItemCharacteristics.if_syncability == false) state_style = 'style="color:gray"';
      if (SyncItemCharacteristics.if_available == false) state_style = 'style="color:gray"';
    }
  }
  var name_html = '<td width="50%" class="SyncAttrName">' + name + '</td>';
  var size_html = '<td title="' + size_title + '" class="SyncAttrSize" ><span style="width:40px">' + size + '</span></td>';
  var time_html = '<td nowrap class="SyncAttribute" title="' + time_title + '" ><span style="width:90px">' + time + '</span></td>';
  var state_html = '<td class="SyncAttrState" colspan="2" valign="middle" align="center" nowrap ' + state_style + ' >' +
		'<span style="width:137px">' + state + '</span></td>';

  //	var empty_td_html = '<td width="50%" nowrap>' + '</td>';
  //	var root_name_html = '<td width="1%" class="SyncAttrName" nowrap>' + root_name + '</td>';
  //	var sync_type_name_html = '<td width="1%" class="SyncAttribute" nowrap>' + sync_type_name + '</td>';
  //	var type_picture_html = '<span style=" width:22px; height:22px; display:inline-block; '+
  //							'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+
  //							sync_type_picture+'\', sizingMethod=\'scale\');" >'+'</span>';
  //	var sync_type_picture_html = '<td width="1%" class="SyncAttribute" nowrap>' + type_picture_html + '</td>';

  root_name_html = '<span>' + root_name + '</span>';
  sync_type_name_html = '<span>' + sync_type_name + '</span>';
  'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' +
		sync_type_picture + '\', sizingMethod=\'scale\');" >' + '' + '</span>';
  // opacity: 0.1; background: rgba(34,35,35,0.1);


  if (SyncItemCharacteristics == null) {
    Root_Item_html += '<td width="50%" nowrap align="right" valign="middle" colspan="3" class="SyncAttrRootNameHead" style="border-bottom: solid 1px #787864; border-top: solid 1px #787864;">';
    Root_Item_html += root_name_html;
    Root_Item_html += sync_type_name_html;
    //	Root_Item_html += sync_type_picture_html;
    Root_Item_html += '</td>';
  } else if (Counter == 0) {
    Root_Item_html += '<td width="50%" nowrap align="right" valign="middle" colspan="3" class="SyncAttrRootNameHead" style="border-bottom: solid 1px #787864; border-top: solid 1px #787864;">';
    Root_Item_html += root_name_html + ' - ';
    Root_Item_html += '(' + sync_type_name_html + ') ';
    //	Root_Item_html += sync_type_picture_html;
    Root_Item_html += '</td>';
  } else {
    Root_Item_html += '<td width="50%" nowrap align="left" valign="middle" colspan="3" class="SyncAttrRootNameHead" style="border-bottom: solid 1px #787864; border-top: solid 1px #787864;">';
    //	Root_Item_html += sync_type_picture_html;
    Root_Item_html += '(' + sync_type_name_html + ')' + ' - ';
    Root_Item_html += root_name_html;
    Root_Item_html += '</td>';
  }
  if (state != '') Item_html = name_html + state_html;
  else Item_html = name_html + size_html + time_html;

  //	Item_html = '<td width="1%" class="SyncAttrName" nowrap>' + type_picture_html + sync_type_name + root_name + '</td>';

  //	if ((SyncItemCharacteristics == null) || (Counter == 0)) Item_html = empty_td_html + Item_html;
  //	else  Item_html = Item_html + empty_td_html;
  //	if ((SyncItemCharacteristics == null) || (Counter == 0)) Item_html = empty_td_html + sync_type_picture_html + sync_type_name_html + root_name_html;
  //	else  Item_html = sync_type_picture_html + sync_type_name_html + root_name_html + empty_td_html;

  //	if (SyncItemCharacteristics == null) Item_html = empty_td_html + Item_html;
  //	else 	if( Counter == 0) Item_html = empty_td_html + Item_html;
  //			else Item_html = Item_html + empty_td_html;

  Item_html = Root_Item_html; // if( SyncItemCharacteristics==null) alert(Item_html);

  return Item_html;
}

function OpenSelectedMenu(msg_id, CollectionID) {
  var Collection = external.ObjById(CollectionID);
  var SelectedItemsQuantity = Collection.Method0('M_GetSelectedItemsCount');
  if (SelectedItemsQuantity == 0) return;
  var menu_msg_id = 'SelectedMenu_' + CollectionID;
  // --------------------------------------------------------------------
  if ((G_Above_message.collection_id == CollectionID) && (G_Above_message.msg_id == menu_msg_id) &&
		(G_Above_message.mustshown == true) && (G_Shown_message.msg_id != menu_msg_id)) {
    CloseSelectedMenu(G_Shown_message.msg_id, G_Shown_message.collection_id);
    CloseMenu(G_Shown_message.msg_id, G_Shown_message.collection_id);

    var Msg_element = document.getElementById('SelectedMenu_' + CollectionID); if (Msg_element == null) return;
    Msg_element.oldClassName = Msg_element.className;
    Msg_element.className = Msg_element.className + ' Exp'; // alert(Msg_element.outerHTML);

    // if item must be shown  ---------------------------------------------------------------------------------------------

    var SelectedWinningTreeID = Collection.Method0('M_GetSelectedStatus'); // Vovas function
    UpdateSelectedMenu(CollectionID, SelectedWinningTreeID);

    var Base_up_element = '';
    var Base_down_element = '';
    if (Msg_element.msg_type == '2') {
      Base_up_element = Msg_element.all['Base_' + menu_msg_id];
      Base_down_element = Base_up_element;
    } else {
      Base_up_element = Msg_element.all['Base_up_' + menu_msg_id];
      Base_down_element = Msg_element.all['Base_down_' + menu_msg_id];
    }

    // if it must to do nothing	---------------------------------------------------------------------------------------------
    var picture = '';
    if (Msg_element.if_dont_sync == 'true')  picture = 'Pics/Radio_no_active.gif';
    else  picture = 'Pics/Radio_no_unactive.gif';

    if (SelectedWinningTreeID == CONST_CID) {  picture = 'Pics/Radio_no_unactive.gif'; } // if selection was changed after last action

    var Dont_sync_pic = '<img ID="Radio_no_' + CollectionID + '" src="' + picture + '" onclick=OnSelectedArrowClick("' + menu_msg_id + '","' +
			CollectionID + '","' + CONST_WIN_NO_SYNC + '") style="cursor:hand" title="' + external.LS('hint_Selected_Nothing') + '">';

    // if it must to delete all	---------------------------------------------------------------------------------------------

    if (Msg_element.if_del_sync == 'true')  picture = 'Pics/Radio_del_active.gif';
    else  picture = 'Pics/Radio_del_unactive.gif';

    if (SelectedWinningTreeID == CONST_CID) {  picture = 'Pics/Radio_del_unactive.gif'; } // if selection was changed after last action

    var Del_sync_pic = '<img ID="Radio_del_' + CollectionID + '" src="' + picture + '" onclick=OnSelectedArrowClick("' + menu_msg_id + '","' +
			CollectionID + '","' + CONST_WIN_DEL_SYNC + '") style="cursor:hand" title="' + external.LS('hint_Selected_Delete_All') + '">';

    //  -------------------------------------------------------------------------------------------------------------------------

    var Up_plate = document.createElement('div');
    var Down_plate = document.createElement('div');

    Base_down_element.className = 'Back_plate1';
    Base_up_element.appendChild(Up_plate);
    Base_down_element.appendChild(Down_plate);

    Up_plate.id = 'Up_plate';
    Up_plate.className = 'Up_plate_style_' + Msg_element.msg_type + '_for_selected_menu';
    Up_plate.title = external.LS('tooltip_select_menu');

    var CaptionClass = 'SelectedMenuPopupCaption';

    Up_plate.innerHTML = '<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td class="' +
			CaptionClass + '" width="100%" align="center">' +
			external.LS('popup_menu_selected') + '</td></tr></table>';

    Down_plate.id = 'Down_plate';
    Down_plate.className = 'Down_plate_style_' + Msg_element.msg_type + '_for_selected_menu';

    // if (MsgObj.Method0('M_GetSelectedState') == true) MoreRefClass +="_selected";

    Down_plate.innerHTML = '<table width="100%" cellspacing="0" cellpadding="0">' +
			'<tr>' +
			//										'<td width="34%" class="Down_plate_cell" style="padding-left:5px;">'+
			//											'<span class="'+MoreRefClass+
			//												'" onclick="OnSelectedMenuMoreButtonClick(\''+msg_id+'\', \''+CollectionID+'\')">'+
			//												external.LS('popup_menu_more')+
			//											'</span>'+
			//										'</td>'+
			'<td width="50%" class="Down_plate_cell">' +
			'<img src="Pics/Dot.gif">' +
			Dont_sync_pic +
			'</td>' +
			'<td width="50%" class="Down_plate_cell" style="padding-right:7px;">' +
			Del_sync_pic + '</td>' +
			'</tr>' +
			'</table>';
    //	-------------------------------------------------------------------------------------------------------------------------
    G_Shown_message.msg_id = menu_msg_id;
    G_Shown_message.collection_id = CollectionID;
  }
}

// ///////////////////////////////////////////////////////////////////////////////////////////////////////

function CloseSelectedMenu(msg_id, CollectionID) {
  var menu_msg_id = 'SelectedMenu_' + CollectionID;
  Aboveflag = (G_Above_message.collection_id == CollectionID) && (G_Above_message.msg_id == menu_msg_id);

  if ((!Aboveflag || (G_Above_message.mustshown == false)) && (G_Shown_message.msg_id == menu_msg_id) &&
		(G_Shown_message.collection_id == CollectionID) && (G_Shown_message.msg_id != null)) {
    var Msg_element = document.getElementById('SelectedMenu_' + CollectionID); if (Msg_element == null) return;
    Msg_element.className = Msg_element.oldClassName;
    Msg_element.className = Msg_element.className.replace(/ Exp/, ''); // alert(Msg_element.outerHTML);

    var Up_plate = Msg_element.all.Up_plate; // alert(Up_plate.innerHTML); //alert(Up_plate.outerHTML);

    if (Up_plate == null) return;
    Up_plate.outerHTML = '';// Up_plate.removeNode(true);
    //	find Down_plate in message
    var Down_plate = Msg_element.all.Down_plate;
    if (Down_plate == null) return;
    Down_plate.outerHTML = '';// Down_plate.removeNode(true);

    //	change to normal state
    if (Msg_element.msg_type == '2') Base_down_element = Msg_element.all['Base_' + menu_msg_id];
    else Base_down_element = Msg_element.all['Base_down_' + menu_msg_id];

    Base_down_element.className = 'Back_plate';

    G_Shown_message.msg_id = null;
    G_Shown_message.collection_id = null;
  }
}

// ///////////////////////////////////////////////////////////////////////////////////////////////////////

function ShowSelectedFM() {
  if (window.event.shiftKey) return;
  if (window.event.ctrlKey) return;

  var Src_cell = window.event.srcElement;
  G_Above_message.msg_id = Src_cell.msg_id;
  G_Above_message.collection_id = Src_cell.collection_id;
  window.setTimeout("OpenSelectedMenu(\'" + Src_cell.msg_id + "\', \'" + Src_cell.collection_id + "\')", 250);
}

// ///////////////////////////////////////////////////////////////////////////////////////////////////////

function HideSelectedFM() {
  if (G_Above_message.latch == false) {
    var Src_cell = window.event.srcElement;
    G_Above_message.msg_id = null;

    G_Above_message.collection_id = null;

    if (G_Above_message.mustshown == false) {
      window.setTimeout("SelectedLeadUp(\'" + Src_cell.msg_id + "\', \'" + Src_cell.collection_id + "\')", 10);
    }
    window.setTimeout("CloseSelectedMenu(\'" + Src_cell.msg_id + "\', \'" + Src_cell.collection_id + "\')", 500);
  }
}

// ///////////////////////////////////////////////////////////////////////////////////////////////////////

function SelectedLeadUp(msg_id, CollectionID) {
  if ((G_Above_message.msg_id != msg_id) || (G_Above_message.collection_id != CollectionID)) {
    G_Above_message.mustshown = true;
  }
}

// ///////////////////////////////////////////////////////////////////////////////////////////////////////

function OnSelectedArrowClick(MsgID, CollectionID, TreeID) {
  //----------------------------------------------------------------------------------
  UpdateSelectedMenu(CollectionID, TreeID);
  // ----------------------------------------------------------------------------------

  var Msg_element = document.getElementById('SelectedMenu_' + CollectionID);
  if (Msg_element == null) return;
  //	change view to normal
  Msg_element.className = Msg_element.className.replace(/ Exp/, '');
  //	find Up_plate in message
  var Up_plate = Msg_element.all.Up_plate;
  if (Up_plate == null) return;
  Up_plate.outerHTML = '';// Up_plate.removeNode(true);
  //	find Down_plate in message
  var Down_plate = Msg_element.all.Down_plate;
  if (Down_plate == null) return;
  Down_plate.outerHTML = '';// Down_plate.removeNode(true);
  //	change to normal state
  if (Msg_element.msg_type == '2') Base_down_element = Msg_element.all['Base_' + MsgID];
  else Base_down_element = Msg_element.all['Base_down_' + MsgID];

  Base_down_element.className = 'Back_plate';

  G_Shown_message.msg_id = null;
  G_Shown_message.collection_id = null;
  G_Above_message.mustshown = false;

  //----------------------------------------------------------------------------------
  var CollectionObj = external.ObjByID(CollectionID);
  CollectionObj.Method1ID('M_AlternativeSelected', TreeID);
  //----------------------------------------------------------------------------------
  var if_dont_sync = 'false';
  var if_del_sync = 'false';
  // --------------------------------------------------------
  if (TreeID == CONST_WIN_NO_SYNC) if_dont_sync = 'true';
  if (TreeID == CONST_WIN_DEL_SYNC) if_del_sync = 'true';
  // --------------------------------------------------------
  var Menu_element = document.getElementById('SelectedMenu_' + CollectionID);
  if (Menu_element != null) {
    Menu_element.if_dont_sync = if_dont_sync;
    Menu_element.if_del_sync = if_del_sync;
  }
}

// ///////////////////////////////////////////////////////////////////////////////////////////////////////

function OnSelectedMenuMoreButtonClick(MessageID, CollectionID) {
  window.event.cancelBubble = true;
  G_Above_message.latch = true;
  //	alert("OnSelectedMenuMoreButtonClick");
  //	var MessageObj = external.ObjByID(MessageID);
  //	var Solution = MessageObj.GetM('m_SolutionWPtr');
  //	external.ShowContextMenu(Solution, CollectionID);
  G_Above_message.latch = false;
  return;
}

// ///////////////////////////////////////////////////////////////////////////////////////////////////////

function GetSelectedMenuStateObject() {
  //	this.path={};
  this.count = 0;
  this.TreeID = {};
  this.radio_arrow = {};
  this.arrow_class = {};
  this.radio_del = 'Radio_del_';
  this.radio_no = 'Radio_no_';
  //-----------------------
  var AssocObj = external.Method0('M_GetSelectedAssoc');
  var SuperTreeList = AssocObj.GetM('m_SuperTreesPtr');
  //-----------------------
  SuperTree_Obj = SuperTreeList.GetFirst();

  var i = 0;
  while (SuperTree_Obj != null) {
    SyncTree_ID = SuperTree_Obj.objID;

    //			var WizardType = SuperTree_Obj.Method0('M_GetDeviceType'); if (WizardType) alert(WizardType);
    // 			var SuperTreeURL = SuperTree_Obj.Method0Own('M_ToFilename'); //if (SuperTreeURL) alert(SuperTreeURL);

    //			SyncTree_path = SuperTree_Obj.Method0Own('M_ToFilename'); if (SyncTree_path) alert(SyncTree_path);

    //			this.path[i] = SyncTree_path;
    this.TreeID[i] = SyncTree_ID;

    if (i == 0) {
      this.radio_arrow[i] = 'Pics/Radio_left_';
      this.arrow_class[i] = 'Arrow_left_';
    } else {
      this.radio_arrow[i] = 'Pics/Radio_right_';
      this.arrow_class[i] = 'Arrow_right_';
    }

    SuperTree_Obj = SuperTreeList.GetNext(SuperTree_Obj);
    i++;
  }

  this.count = i;
  return this;
}

// ////////////////////////////////////////////////////////////////////////////////////////////////////

function UpdateSelectedMenu(CollectionID, WinningTreeID) {
  // --------------------------------------------------------
  var Collection = external.ObjById(CollectionID);
  var SelectedItemsQuantity = Collection.Method0('M_GetSelectedItemsCount');
  var SelectedWinningTreeID = Collection.Method0('M_GetSelectedStatus'); // Vovas function
  //--------------------------------------------------------
  var SelectedMenu = document.getElementById('SelectedMenu_' + CollectionID);
  if (SelectedMenu == null) return;
  //--------------------------------------------------------
  if (WinningTreeID == null) {// if menu is closed
    var descendants = SelectedMenu.getElementsByTagName('*');
    for (var i = 0; i < descendants.length; i++) {
      var El = descendants[i];

      if (El.src_stealth) {
        if (SelectedItemsQuantity > 0) { El.src = El.src_normal; } else { El.src = El.src_stealth; }
      }

      if (El.class_stealth) {
        if (SelectedItemsQuantity > 0) { El.className = El.class_normal; } else { El.className = El.class_stealth; }
      }
    }
  }
  if (SelectedItemsQuantity == 0) return;
  //--------------------------------------------------------
  if (WinningTreeID == null) WinningTreeID = SelectedWinningTreeID; // if menu is closed and selected items exist
  if (WinningTreeID === undefined) WinningTreeID = SelectedWinningTreeID;
  //--------------------------------------------------------
  var MenuState = new GetSelectedMenuStateObject();
  //--------------------------------------------------------
  var WinnerIsFound = 0;
  SelectedMenu.if_dont_sync = 'false';
  SelectedMenu.if_del_sync = 'false';
  // --------------------------------------------------------
  if (WinningTreeID == CONST_WIN_NO_SYNC) {
    MenuState.radio_no += 'active.gif';
    SelectedMenu.if_dont_sync = 'true';
    SelectedMenu.if_del_sync = 'false';
    WinnerIsFound = 1;
  } else MenuState.radio_no += 'unactive.gif';

  if (WinningTreeID == CONST_WIN_DEL_SYNC) {
    MenuState.radio_del += 'active.gif';
    SelectedMenu.if_dont_sync = 'false';
    SelectedMenu.if_del_sync = 'true';
    WinnerIsFound = 2;
  } else MenuState.radio_del += 'unactive.gif';

  if (WinningTreeID == CONST_CID) {  // in this case all radio items are unactive
    SelectedMenu.if_dont_sync = 'false';
    SelectedMenu.if_del_sync = 'false';
    WinnerIsFound = 1;
  }
  //--------------------------------------------------------
  for (i = 0; i < MenuState.count; i++) {
    if (WinnerIsFound == 1) {
      MenuState.arrow_class[i] += 'no';
      MenuState.radio_arrow[i] += 'unactive.gif';
    }
    if (WinnerIsFound == 2) {
      MenuState.arrow_class[i] += 'del';
      MenuState.radio_arrow[i] += 'unactive.gif';
    }
    if (WinnerIsFound == 0) {
      if (WinningTreeID != null) {
        if (MenuState.TreeID[i] == WinningTreeID) {
          MenuState.arrow_class[i] += 'out';
          MenuState.radio_arrow[i] += 'active.gif';
        } else {
          MenuState.arrow_class[i] += 'in';
          MenuState.radio_arrow[i] += 'unactive.gif';
        }
      } else {
        MenuState.arrow_class[i] += 'no';
        MenuState.radio_arrow[i] += 'unactive.gif';
      }
    }
  }
  // --------------------------------------------------------

  //	var img_id = "Radio_no_"+CollectionID; // alert(img_id);
  //	var img = document.getElementById(img_id);
  //	if (img) img.src = MenuState.radio_no; // if (img) alert(MenuState.radio_image[i]);

  // --------------------------------------------------------

  //	var img_id = "Radio_del_"+CollectionID; // alert(img_id);
  //	var img = document.getElementById(img_id);
  //	if (img) img.src = MenuState.radio_del; //if (img) alert(MenuState.radio_image[i]);

  // --------------------------------------------------------

  for (var i = 0; i < MenuState.count; i++) {
    var img_id = 'Radio_' + CollectionID + '_' + i;
    var div_id = 'Arrow_' + CollectionID + '_' + i;

    var img = document.getElementById(img_id);
    var div = document.getElementById(div_id);

    if (img) img.src = MenuState.radio_arrow[i];
    if (div) div.className = MenuState.arrow_class[i];
    if (div) div.class_changed = MenuState.arrow_class[i];
  }
}

// ////////////////////////////////////////////////////////////////////////////////////////////////////

function OnSelectedPopupClick() {
  if (G_Above_message.mustshown == true) {
    G_Above_message.mustshown = false;
    CloseSelectedMenu(G_Above_message.msg_id, G_Above_message.collection_id);
  } else {
    G_Above_message.mustshown = true;
    OpenSelectedMenu(G_Above_message.msg_id, G_Above_message.collection_id);
  }
}

// ///////////////////////////////////////////////////////////////////////////////////////////////////////

function GetSyncTreeIdWithNumber(num, AccosID) {
  // if (!AccosID) AccosID = G_ActiveAssocID;
  // alert("AccosID: " +AccosID);
  // var AssocObj = external.ObjByID(AssocID);
  // var AssocObj = external.ObjByID(G_ActiveAssocID);
  var AssocObj = external.Method0('M_GetSelectedAssoc');
  var SuperTreeList = AssocObj.GetM('m_SuperTreesPtr');
  var SuperTreeObj = SuperTreeList.GetFirst();

  for (var i = 0; i < num + 1; i++) {
    if (SuperTreeObj) SyncTreeId = SuperTreeObj.objID;
    SuperTreeObj = SuperTreeList.GetNext(SuperTreeObj);
  }
  return SyncTreeId;
}
// ///////////////////////////////////////////////////////////////////////////////////////////////////////

// function BuildCollectionSortSelectHeaderMenu(MsgObj, MsgEvenness, CollectionID, TreeID, IsVisible)
// function BuildCollectionSortHeaderMenu(CollectionID)
// function GetRootFolderName(FolderNumber)
// function GetSyncTreeId(SyncTreeNumber)

// ///////////////////////////////////////////////////////////////////////////////////////////////////////

function BuildCollectionSortHeaderMenu(CollectionID) {
  var sort_header_line_html = '';
  //----------------------------------------------------
  var New_Sort_Info = 'Sort_by_default';
  //----------------------------------------------------
  var msgs = external.ObjByID(CollectionID);
  var Last_Sort_ID = msgs.Method0('M_GetSortingCriterion');
  var TreeID = msgs.Method0('M_GetSortingTree');
  //----------------------------------------------------

  var AssocObj = external.ObjByID(G_ActiveAssocID);
  var SuperTreeList = AssocObj.GetM('m_SuperTreesPtr');
  var SuperTree_Obj = SuperTreeList.GetFirst();
  var Tree_0_ID = SuperTree_Obj.objID;
  SuperTree_Obj = SuperTreeList.GetNext(SuperTree_Obj);
  var Tree_1_ID = SuperTree_Obj.objID;
  //-----------------------------------------------------------------------------------------------
  if ((Last_Sort_ID == '646E6563736544435174726F53676E69') && (TreeID == Tree_0_ID)) { New_Sort_Info = 'Sort_by_name_down_1'; } else if ((Last_Sort_ID == '69646E6563734143464974726F53676E') && (TreeID == Tree_0_ID)) { New_Sort_Info = 'Sort_by_name_up_1'; } else if ((Last_Sort_ID == '646E6563736544436B74726F53676E69') && (TreeID == Tree_0_ID)) { New_Sort_Info = 'Sort_by_size_down_1'; } else if ((Last_Sort_ID == '69646E6563734143604974726F53676E') && (TreeID == Tree_0_ID)) { New_Sort_Info = 'Sort_by_size_up_1'; } else if ((Last_Sort_ID == '646E6563736544435F74726F53676E69') && (TreeID == Tree_0_ID)) { New_Sort_Info = 'Sort_by_time_down_1'; } else if ((Last_Sort_ID == '69646E6563734143544974726F53676E') && (TreeID == Tree_0_ID)) { New_Sort_Info = 'Sort_by_time_up_1'; }
  //-----------------------------------------------------------------------------------------------
  else if ((Last_Sort_ID == '646E6563736544435174726F53676E69') && (TreeID == Tree_1_ID)) { New_Sort_Info = 'Sort_by_name_down_2'; } else if ((Last_Sort_ID == '69646E6563734143464974726F53676E') && (TreeID == Tree_1_ID)) { New_Sort_Info = 'Sort_by_name_up_2'; } else if ((Last_Sort_ID == '646E6563736544436B74726F53676E69') && (TreeID == Tree_1_ID)) { New_Sort_Info = 'Sort_by_size_down_2'; } else if ((Last_Sort_ID == '69646E6563734143604974726F53676E') && (TreeID == Tree_1_ID)) { New_Sort_Info = 'Sort_by_size_up_2'; } else if ((Last_Sort_ID == '646E6563736544435F74726F53676E69') && (TreeID == Tree_1_ID)) { New_Sort_Info = 'Sort_by_time_down_2'; } else if ((Last_Sort_ID == '69646E6563734143544974726F53676E') && (TreeID == Tree_1_ID)) { New_Sort_Info = 'Sort_by_time_up_2'; }
  //-----------------------------------------------------------------------------------------------
  else if ((Last_Sort_ID == '646E6563736544434374726F53676E69')) { New_Sort_Info = 'Sort_by_winner_down'; } else if ((Last_Sort_ID == '69646E6563734143744974726F53676E')) { New_Sort_Info = 'Sort_by_winner_up'; }
  //-----------------------
  var NameItem1 = external.LS('sort_name_item');
  var NameItem2 = external.LS('sort_name_item');
  //-----------------------
  var SizeItem1 = '<span> ' + external.LS('sort_size_item') + '</span>';
  var SizeItem2 = '<span> ' + external.LS('sort_size_item') + '</span>';
  //-----------------------
  var TimeItem1 = '<span> ' + external.LS('sort_time_item') + '</span>';
  var TimeItem2 = '<span> ' + external.LS('sort_time_item') + '</span>';
  //-----------------------
  var WinnerItem = '<span> ' + external.LS('sort_winner_item') + '</span>';
  //-----------------------
  if (New_Sort_Info == 'Sort_by_name_up_1') NameItem1 = '<img src="Pics/Sort_Up.gif"> ' + external.LS('sort_name_item');
  else if (New_Sort_Info == 'Sort_by_name_up_2') NameItem2 = '<img src="Pics/Sort_Up.gif"> ' + external.LS('sort_name_item');
  else if (New_Sort_Info == 'Sort_by_name_down_1') NameItem1 = '<img src="Pics/Sort_Down.gif"> ' + external.LS('sort_name_item');
  else if (New_Sort_Info == 'Sort_by_name_down_2') NameItem2 = '<img src="Pics/Sort_Down.gif"> ' + external.LS('sort_name_item');
  //-----------------------
  else if (New_Sort_Info == 'Sort_by_size_up_1') SizeItem1 = '<span> <img src="Pics/Sort_Up.gif"> ' + external.LS('sort_size_item') + '</span>';
  else if (New_Sort_Info == 'Sort_by_size_up_2') SizeItem2 = '<span> <img src="Pics/Sort_Up.gif"> ' + external.LS('sort_size_item') + '</span>';
  else if (New_Sort_Info == 'Sort_by_size_down_1') SizeItem1 = '<span> <img src="Pics/Sort_Down.gif"> ' + external.LS('sort_size_item') + '</span>';
  else if (New_Sort_Info == 'Sort_by_size_down_2') SizeItem2 = '<span> <img src="Pics/Sort_Down.gif"> ' + external.LS('sort_size_item') + '</span>';
  //-----------------------
  else if (New_Sort_Info == 'Sort_by_time_up_1') TimeItem1 = '<span> <img src="Pics/Sort_Up.gif"> ' + external.LS('sort_time_item') + '</span>';
  else if (New_Sort_Info == 'Sort_by_time_up_2') TimeItem2 = '<span> <img src="Pics/Sort_Up.gif"> ' + external.LS('sort_time_item') + '</span>';
  else if (New_Sort_Info == 'Sort_by_time_down_1') TimeItem1 = '<span> <img src="Pics/Sort_Down.gif"> ' + external.LS('sort_time_item') + '</span>';
  else if (New_Sort_Info == 'Sort_by_time_down_2') TimeItem2 = '<span> <img src="Pics/Sort_Down.gif"> ' + external.LS('sort_time_item') + '</span>';
  //-----------------------
  else if (New_Sort_Info == 'Sort_by_winner_up') WinnerItem = '<span> <img src="Pics/Sort_Up.gif"> ' + external.LS('sort_winner_item') + '</span>';
  else if (New_Sort_Info == 'Sort_by_winner_down') WinnerItem = '<span> <img src="Pics/Sort_Down.gif"> ' + external.LS('sort_winner_item') + '</span>';
  // ---------------------------------------------------------------------------------------------------------------------------------------

  //	if 		(New_Sort_Info == 'Sort_by_default'  )	{
  //													//OnSortItemClick('name_1', CollectionID);
  //													NameItem1 = '<img src="Pics/Sort_Up.gif"> ' + external.LS("sort_name_item");
  //													}

  // ---------------------------------------------------------------------------------------------------------------------------------------

  var SelectControlsExist = true;

  var Select_html = new CreateSelectControls(CollectionID, SelectControlsExist);

  var Name_html_1 = '<div>' +
		'<table width="100%" border="0" cellspacing="0" cellpadding="0">' +
		'<tr>' +
		'<td class="SelectNameHead" width="100%">' + NameItem1 + '</td>' +
		'<td width="1%">' + Select_html.top_button_select_all + '</td>' +
		'<td width="1%">' + Select_html.top_button_deselect_all + '</td>' +
		'</tr>' +
		'</table>' +
		'</div>';

  var Name_html_2 = '<div>' +
		'<table width="100%" border="0" cellspacing="0" cellpadding="0">' +
		'<tr>' +
		'<td class="SelectNameHead" width="100%">' + NameItem2 + '</td>' +
		'<td width="1%">' + Select_html.top_button_select_all + '</td>' +
		'<td width="1%">' + Select_html.top_button_deselect_all + '</td>' +
		'</tr>' +
		'</table>' +
		'</div>';

  // ----------------------------------------------------
  //	alert(WinnerItem);

  sort_header_line_html += '<tbody id="' + CollectionID + '_header_line_container" ' + '>' +
		'<tr class="Single_row" id="' + CollectionID + '_header_line_string" style="height:19px;">' +
		'<td id="' + CollectionID + '_sort_by_name_1" class="SyncAttrNameHead" ' +
		'onclick="OnSortItemClick(\'name_1\',\'' + CollectionID + '\');" title="' +
		external.LS('tooltip_sort_by_name') +
		'" width="50%" style="background:#c0c0b4; border: solid 1px #787864; cursor:hand; "> ' + Name_html_1 +
		'</td>' +
		'<td id="' + CollectionID + '_sort_by_size_1" class="SyncAttrSizeHead" ' +
		'onclick="OnSortItemClick(\'size_1\',\'' + CollectionID + '\');" title="' +
		external.LS('tooltip_sort_by_size') +
		'" style="background:#c0c0b4; border: solid 1px #787864; cursor:hand; "> ' + '<div style="width:80px;">' + SizeItem1 + '</div>' +
		'</td>' +
		'<td id="' + CollectionID + '_sort_by_time_1" class="SyncAttrTimeHead" ' +
		'onclick="OnSortItemClick(\'time_1\',\'' + CollectionID + '\');" title="' +
		external.LS('tooltip_sort_by_time') +
		'" nowrap style="background:#c0c0b4; border: solid 1px #787864; cursor:hand; "> ' + '<div style="width:105px;">' + TimeItem1 + '</div>' +
		'</td>';

  sort_header_line_html += '<td id=\'' + CollectionID + '_sort_by_winner\' class=\'SyncWinnerItemHead\' ' +
		'onclick="OnSortItemClick(\'winner\',\'' + CollectionID + '\');" title=\'' +
		external.LS('tooltip_sort_by_winner') +
		'\' style=\'background:#c0c0b4; border: solid 1px #787864; cursor:hand;\' colspan=\'5\' ' +
		'>' +
		'<div id="' + CollectionID + '_winner_item_container" style="position: relative; width:100%;"> ' + WinnerItem + '</div>' +
		'</td>';

  sort_header_line_html += '<td id="' + CollectionID + '_sort_by_name_2" class="SyncAttrNameHead" ' +
		'onclick="OnSortItemClick(\'name_2\',\'' + CollectionID + '\');" title="' +
		external.LS('tooltip_sort_by_name') +
		'" width="50%" style="background:#c0c0b4; border: solid 1px #787864; cursor:hand;"> ' + Name_html_2 +
		'</td>' +
		'<td id="' + CollectionID + '_sort_by_size_2" class="SyncAttrSizeHead" ' +
		'onclick="OnSortItemClick(\'size_2\',\'' + CollectionID + '\');" title="' +
		external.LS('tooltip_sort_by_size') +
		'" style="background:#c0c0b4; border: solid 1px #787864; cursor:hand;"> ' + '<div style="width:80px;">' + SizeItem2 + '</div>' +
		'</td>' +
		'<td id="' + CollectionID + '_sort_by_time_2" class="SyncAttrTimeHead" ' +
		'onclick="OnSortItemClick(\'time_2\',\'' + CollectionID + '\');" title="' +
		external.LS('tooltip_sort_by_time') +
		'" nowrap style="background:#c0c0b4; ' + // background:#88BAC8;
		'border-top: solid 1px #787864; border-bottom: solid 1px #787864; border-left: solid 1px #787864; border-right: none; ' +
		' cursor:hand;"> ' + '<div style="width:105px;">' + TimeItem2 + '</div>' +
		'</td>' +
		'</tr>' +
		'</tbody>';
  return sort_header_line_html;
}

// ///////////////////////////////////////////////////////////////////////////////////////////////////////

// function GetRootFolderName(FolderNumber)
// function GetSyncTreeId(SyncTreeNumber)

// ///////////////////////////////////////////////////////////////////////////////////////////////////////

function GetRootFolderName(FolderNumber) {
  var SuperTreePath = SyncTreeObject.Method0Own('M_ToViewFilename');
  return SuperTreePath;
}

// ///////////////////////////////////////////////////////////////////////////////////////////////////////

function GetSyncTreeId(SyncTreeNumber) {
  var AssocObj = external.Method0('M_GetSelectedAssoc');
  var SuperTreesList = AssocObj.GetM('m_SuperTreesPtr');
  var SuperTreeObj = SuperTreesList.GetFirst();
  for (var i = 0; i < SyncTreeNumber + 1; i++) {
    if (SuperTreeObj) SyncTreeId = SuperTreeObj.objID;
    SuperTreeObj = SuperTreesList.GetNext(SuperTreeObj);
  }
  return SyncTreeId;
}

// ///////////////////////////////////////////////////////////////////////////////////////////////////////

function BuildCollectionSortSelectHeaderMenu(MsgObj, MsgEvenness, CollectionID, TreeID, IsVisible) {
  //----------------------------------------------------
  var New_Sort_Info = 'Sort_by_default';
  //----------------------------------------------------
  var msgs = external.ObjByID(CollectionID);
  var Last_Sort_ID = msgs.Method0('M_GetSortingCriterion');
  var TreeID = msgs.Method0('M_GetSortingTree');
  //----------------------------------------------------
  var AssocObj = external.ObjByID(G_ActiveAssocID);
  var SuperTreeList = AssocObj.GetM('m_SuperTreesPtr');
  var SuperTree_Obj = SuperTreeList.GetFirst();
  var Tree_0_ID = SuperTree_Obj.objID;
  SuperTree_Obj = SuperTreeList.GetNext(SuperTree_Obj);
  var Tree_1_ID = SuperTree_Obj.objID;
  //-----------------------------------------------------------------------------------------------
  if ((Last_Sort_ID == '646E6563736544435174726F53676E69') && (TreeID == Tree_0_ID)) { New_Sort_Info = 'Sort_by_name_down_1'; } else if ((Last_Sort_ID == '69646E6563734143464974726F53676E') && (TreeID == Tree_0_ID)) { New_Sort_Info = 'Sort_by_name_up_1'; } else if ((Last_Sort_ID == '646E6563736544436B74726F53676E69') && (TreeID == Tree_0_ID)) { New_Sort_Info = 'Sort_by_size_down_1'; } else if ((Last_Sort_ID == '69646E6563734143604974726F53676E') && (TreeID == Tree_0_ID)) { New_Sort_Info = 'Sort_by_size_up_1'; } else if ((Last_Sort_ID == '646E6563736544435F74726F53676E69') && (TreeID == Tree_0_ID)) { New_Sort_Info = 'Sort_by_time_down_1'; } else if ((Last_Sort_ID == '69646E6563734143544974726F53676E') && (TreeID == Tree_0_ID)) { New_Sort_Info = 'Sort_by_time_up_1'; }
  //-----------------------------------------------------------------------------------------------
  else if ((Last_Sort_ID == '646E6563736544435174726F53676E69') && (TreeID == Tree_1_ID)) { New_Sort_Info = 'Sort_by_name_down_2'; } else if ((Last_Sort_ID == '69646E6563734143464974726F53676E') && (TreeID == Tree_1_ID)) { New_Sort_Info = 'Sort_by_name_up_2'; } else if ((Last_Sort_ID == '646E6563736544436B74726F53676E69') && (TreeID == Tree_1_ID)) { New_Sort_Info = 'Sort_by_size_down_2'; } else if ((Last_Sort_ID == '69646E6563734143604974726F53676E') && (TreeID == Tree_1_ID)) { New_Sort_Info = 'Sort_by_size_up_2'; } else if ((Last_Sort_ID == '646E6563736544435F74726F53676E69') && (TreeID == Tree_1_ID)) { New_Sort_Info = 'Sort_by_time_down_2'; } else if ((Last_Sort_ID == '69646E6563734143544974726F53676E') && (TreeID == Tree_1_ID)) { New_Sort_Info = 'Sort_by_time_up_2'; }
  //-----------------------------------------------------------------------------------------------
  else if ((Last_Sort_ID == '646E6563736544434374726F53676E69')) { New_Sort_Info = 'Sort_by_winner_down'; } else if ((Last_Sort_ID == '69646E6563734143744974726F53676E')) { New_Sort_Info = 'Sort_by_winner_up'; }
  //-----------------------
  var NameItem1 = external.LS('sort_name_item');
  var NameItem2 = external.LS('sort_name_item');
  //-----------------------
  var SizeItem1 = '<span style="width:40px;"> ' + external.LS('sort_size_item') + '</span>';
  var SizeItem2 = '<span style="width:40px;"> ' + external.LS('sort_size_item') + '</span>';
  //-----------------------
  var TimeItem1 = '<span style="width:90px"> ' + external.LS('sort_time_item') + '</span>';
  var TimeItem2 = '<span style="width:90px"> ' + external.LS('sort_time_item') + '</span>';
  //-----------------------
  var WinnerItem = '<span style="width:98px"> ' + external.LS('sort_winner_item') + '</span>';
  //-----------------------
  if (New_Sort_Info == 'Sort_by_name_up_1') NameItem1 = '<img src="Pics/Sort_Up.gif"> ' + external.LS('sort_name_item');
  else if (New_Sort_Info == 'Sort_by_name_up_2') NameItem2 = '<img src="Pics/Sort_Up.gif"> ' + external.LS('sort_name_item');
  else if (New_Sort_Info == 'Sort_by_name_down_1') NameItem1 = '<img src="Pics/Sort_Down.gif"> ' + external.LS('sort_name_item');
  else if (New_Sort_Info == 'Sort_by_name_down_2') NameItem2 = '<img src="Pics/Sort_Down.gif"> ' + external.LS('sort_name_item');
  //-----------------------
  else if (New_Sort_Info == 'Sort_by_size_up_1') SizeItem1 = '<span style="width:40px;"> <img src="Pics/Sort_Up.gif"> ' + external.LS('sort_size_item') + '</span>';
  else if (New_Sort_Info == 'Sort_by_size_up_2') SizeItem2 = '<span style="width:40px;"> <img src="Pics/Sort_Up.gif"> ' + external.LS('sort_size_item') + '</span>';
  else if (New_Sort_Info == 'Sort_by_size_down_1') SizeItem1 = '<span style="width:40px;"> <img src="Pics/Sort_Down.gif"> ' + external.LS('sort_size_item') + '</span>';
  else if (New_Sort_Info == 'Sort_by_size_down_2') SizeItem2 = '<span style="width:40px;"> <img src="Pics/Sort_Down.gif"> ' + external.LS('sort_size_item') + '</span>';
  //-----------------------
  else if (New_Sort_Info == 'Sort_by_time_up_1') TimeItem1 = '<span style="width:90px"> <img src="Pics/Sort_Up.gif"> ' + external.LS('sort_time_item') + '</span>';
  else if (New_Sort_Info == 'Sort_by_time_up_2') TimeItem2 = '<span style="width:90px"> <img src="Pics/Sort_Up.gif"> ' + external.LS('sort_time_item') + '</span>';
  else if (New_Sort_Info == 'Sort_by_time_down_1') TimeItem1 = '<span style="width:90px"> <img src="Pics/Sort_Down.gif"> ' + external.LS('sort_time_item') + '</span>';
  else if (New_Sort_Info == 'Sort_by_time_down_2') TimeItem2 = '<span style="width:90px"> <img src="Pics/Sort_Down.gif"> ' + external.LS('sort_time_item') + '</span>';
  //-----------------------
  else if (New_Sort_Info == 'Sort_by_winner_up') WinnerItem = '<span style="width:98px"> <img src="Pics/Sort_Up.gif"> ' + external.LS('sort_winner_item') + '</span>';
  else if (New_Sort_Info == 'Sort_by_winner_down') WinnerItem = '<span style="width:98px"> <img src="Pics/Sort_Down.gif"> ' + external.LS('sort_winner_item') + '</span>';

  WinnerItem = '<div id="' + CollectionID + '_winner_item_container" style="position: relative; width:100%;"> ' + WinnerItem + '</div>';
  //---------------------------------------------------------------------------------------------------------------------------------------

  //	if 		(New_Sort_Info == 'Sort_by_default'  )	{
  //													//OnSortItemClick('name_1', CollectionID);
  //													NameItem1 = '<img src="Pics/Sort_Up.gif"> ' + external.LS("sort_name_item");
  //													}

  //---------------------------------------------------------------------------------------------------------------------------------------

  var msg_html = '';

  var TbodyClass = 'Selected_Menu';

  var SelectedMsgID = 'SelectedMenu_' + CollectionID;
  //------------------------------------------------------------
  var solution = MsgObj.GetObjM('m_SolutionWPtr');
  AssocObj = MsgObj.GetM('m_SlnDoAssocSyncWPtr');

  var AssocID = AssocObj.objID;
  // var AssocID = G_ActiveAssocID;
  // ------------------------------------------------------------
  var SelectControlsExist = true;
  var Select_html = new CreateSelectControls(CollectionID, SelectControlsExist);

  var Name_html_1 = '<div>' +
		'<table width="100%" border="0" cellspacing="0" cellpadding="0">' +
		'<tr>' +
		'<td class="SelectNameHead" width="100%">' + NameItem1 + '</td>' +
		'<td width="1%">' + Select_html.top_button_select_all + '</td>' +
		'<td width="1%">' + Select_html.top_button_deselect_all + '</td>' +
		'</tr>' +
		'</table>' +
		'</div>';

  var Name_html_2 = '<div>' +
		'<table width="100%" border="0" cellspacing="0" cellpadding="0">' +
		'<tr>' +
		'<td class="SelectNameHead" width="100%">' + NameItem2 + '</td>' +
		'<td width="1%">' + Select_html.top_button_select_all + '</td>' +
		'<td width="1%">' + Select_html.top_button_deselect_all + '</td>' +
		'</tr>' +
		'</table>' +
		'</div>';

  // ------------------------------------------------------------
  ImportantID = G_Assocs[AssocID].TreeView._collectionIdByName.Important;
  if (CollectionID == ImportantID) return msg_html;
  //------------------------------------------------------------
  Radio_del_sync = 'false';
  Radio_dont_sync = 'true';
  //------------------------------------------------------------
  var Menu_handler = 'onmouseenter="ShowSelectedFM()" onmouseleave="HideSelectedFM()" ' +
		'onclick="OnSelectedPopupClick()" oncontextmenu="StopContextMenu()" msg_id="' + SelectedMsgID + '" collection_id="' + CollectionID + '"';

  //-------------------------------------------------------------------
  var Collection = external.ObjById(CollectionID);
  var SelectedItemsQuantity = Collection.Method0('M_GetSelectedItemsCount');

  var stealth_mode = false;
  if ((SelectedItemsQuantity == 0) && (!IsVisible)) stealth_mode = true;
  var src_stealth = 'Pics/Radio_left_right_stealth.gif';
  var
    //-------------------------------------------------------------------
    SyncTreeArray = new Array();
  var i = 0;

  SuperTreeList = AssocObj.GetM('m_SuperTreesPtr');
  SuperTree_Obj = SuperTreeList.GetFirst();
  while (SuperTree_Obj != null) {
    SyncTreeArray[i] = {};
    SyncTree_Obj = SuperTree_Obj.GetFirst();
    SyncTreeArray[i].obj = SyncTree_Obj;
    SyncTreeArray[i].id = SuperTree_Obj.objID;
    SyncTreeArray[i].SyncItemCharacteristics = new CreateSyncItemCharacteristics(solution, SuperTree_Obj, CollectionID);
    // --------------------------------------------------------------------------------
    SyncTreeArray[i].arrow_direction = 'no';
    SyncTreeArray[i].radio = 'unactive';
    SyncTreeArray[i].radio_handler = 'onclick=OnSelectedArrowClick("' + SelectedMsgID + '","' + CollectionID + '","' +
			GetSyncTreeIdWithNumber(i, AssocID) + '") style="cursor:hand"';
    //--------------------------------------------------------------------------------
    SuperTree_Obj = SuperTreeList.GetNext(SuperTree_Obj);
    i++;
  }
  //---------------------------------------------------------------------------
  var SyncTreeList_length = i;
  //---------------------------------------------------------------------------
  //---------------------------------------------------------------------------

  if (SyncTreeList_length == 2) {
    var src_1 = 'Pics/Radio_left_' + SyncTreeArray[0].radio + '.gif'; if (stealth_mode == true) src_1 = src_stealth;
    var src_2 = 'Pics/Radio_right_' + SyncTreeArray[1].radio + '.gif'; if (stealth_mode == true) src_2 = src_stealth;

    var class_name_1 = 'Arrow_left_' + SyncTreeArray[0].arrow_direction; if (stealth_mode == true) class_name_1 = 'Arrow_edge_stealth';
    var class_mid_name = 'Arrow_mid_single'; if (stealth_mode == true) class_mid_name = 'Arrow_mid_single_stealth';
    var class_name_2 = 'Arrow_right_' + SyncTreeArray[1].arrow_direction; if (stealth_mode == true) class_name_2 = 'Arrow_edge_stealth';

    msg_html = '<tbody id="' + SelectedMsgID + '" class="' + TbodyClass + '" MsgEvenness="' + MsgEvenness + '" if_dont_sync="' + Radio_dont_sync +
			'" if_del_sync="' + Radio_del_sync + '" msg_type="2" exist="true" oncontextmenu="" ' +
			' >' +
			'<tr class="Single_row">' +
			//-------------------------------------------------------------------------
			'<td class="SyncAttrNameHead" id="' + CollectionID + '_sort_by_name_1" ' +
			'onclick="OnSortItemClick(\'name_1\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_name') +
			'" width="50%" > ' + Name_html_1 +
			'</td>' +
			'<td class="SyncAttrSizeHead" id="' + CollectionID + '_sort_by_size_1" ' +
			'onclick="OnSortItemClick(\'size_1\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_size') +
			'" > ' + '<div style="width:80px;">' + SizeItem1 + '</div>' +
			'</td>' +
			'<td class="SyncAttrTimeHead" id="' + CollectionID + '_sort_by_time_1" ' +
			'onclick="OnSortItemClick(\'time_1\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_time') +
			'" nowrap > ' + '<div style="width:105px;">' + TimeItem1 + '</div>' +
			'</td>' +
			//-------------------------------------------------------------------------
			'<td class="Selected_menu_left_cell" ' + Menu_handler + '>' +
			'<img ID="Radio_' + CollectionID + '_' + 0 +
			'" src="' + src_1 + '" ' +
			'" src_normal="Pics/Radio_left_' + SyncTreeArray[0].radio + '.gif" ' +
			'" src_stealth="Pics/Radio_left_right_stealth.gif" ' +
			'title="' + external.LS('hint_Selected_Winner') + '" ' +
			'class="Selected_menu_radio_control" ' + SyncTreeArray[0].radio_handler + '></td>' +
			//-------------------------------------------------------------------------
			'<td class="Selected_menu_middle_cell" height="100%" ' + Menu_handler + '>' +
			'<div class="Back_plate" id="Base_' + SelectedMsgID + '"><div ID="Arrow_' + CollectionID + '_' + 0 +

			'" class="' + class_name_1 +
			'" class_normal="Arrow_left_' + SyncTreeArray[0].arrow_direction +
			'" class_stealth="Arrow_edge_stealth' +

			'"></div></div></td>' +
			//-------------------------------------------------------------------------
			'<td class="Selected_menu_middle_cell" height="100%" ' + Menu_handler + '>' +
			'<div class="Back_plate">' +

			'<div class="' + class_mid_name + '" ' +
			'class_normal="Arrow_mid_single" ' +
			'class_stealth="Arrow_mid_single_stealth" ' +

			'></div></div></td>' +
			//-------------------------------------------------------------------------
			'<td class="Selected_menu_middle_cell" height="100%" ' + Menu_handler + '>' +
			'<div class="Back_plate"><div ID="Arrow_' + CollectionID + '_' + 1 +

			'" class="' + class_name_2 +
			'" class_normal="Arrow_right_' + SyncTreeArray[1].arrow_direction +
			'" class_stealth="Arrow_edge_stealth' +

			'"></div></div></td>' +
			//-------------------------------------------------------------------------
			'<td class="Selected_menu_right_cell" ' + Menu_handler + '>' +
			'<img ID="Radio_' + CollectionID + '_' + 1 +

			'" src="' + src_2 + '" ' +
			'" src_normal="Pics/Radio_right_' + SyncTreeArray[1].radio + '.gif" ' +
			'" src_stealth="Pics/Radio_left_right_stealth.gif" ' +

			'title="' + external.LS('hint_Selected_Winner') + '" ' +
			'class="Selected_menu_radio_control" ' + SyncTreeArray[1].radio_handler + '></td>' +
			//-------------------------------------------------------------------------
			'<td class="SyncAttrNameHead" id="' + CollectionID + '_sort_by_name_2" ' +
			'onclick="OnSortItemClick(\'name_2\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_name') +
			'" width="50%" > ' + Name_html_2 +
			'</td>' +
			'<td class="SyncAttrSizeHead" id="' + CollectionID + '_sort_by_size_2" ' +
			'onclick="OnSortItemClick(\'size_2\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_size') +
			'" > ' + '<div style="width:80px;">' + SizeItem2 + '</div>' +
			'</td>' +
			'<td class="SyncAttrTimeHead2" id="' + CollectionID + '_sort_by_time_2" ' +
			'onclick="OnSortItemClick(\'time_2\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_time') +
			'" nowrap > ' + '<div style="width:105px;">' + TimeItem2 + '</div>' +
			'</td>' +
			//-------------------------------------------------------------------------
			'</tr>' +
			'</tbody>';
  }
  if (SyncTreeList_length > 2) {
    msg_html = '<tbody id="' + SelectedMsgID + '" class="' + TbodyClass + '" MsgEvenness="' + MsgEvenness + '" if_dont_sync="' + Radio_dont_sync +
			'" if_del_sync="' + Radio_del_sync + '" msg_type="3" exist="true" oncontextmenu=""' +
			'>';
    // First row
    var src_1 = 'Pics/Radio_left_' + SyncTreeArray[0].radio + '.gif'; if (stealth_mode == true) src_1 = src_stealth;
    var src_2 = 'Pics/Radio_right_' + SyncTreeArray[1].radio + '.gif'; if (stealth_mode == true) src_2 = src_stealth;

    var class_name_1 = 'Arrow_left_' + SyncTreeArray[0].arrow_direction; if (stealth_mode == true) class_name_1 = 'Arrow_edge_stealth';
    var class_mid_name = 'Arrow_mid_top'; if (stealth_mode == true) class_mid_name = 'Arrow_mid_stealth';
    var class_name_2 = 'Arrow_right_' + SyncTreeArray[1].arrow_direction; if (stealth_mode == true) class_name_2 = 'Arrow_edge_stealth';

    msg_html += '<tr class="First_row" >' +
			//-------------------------------------------------------------------------
			'<td id="' + CollectionID + '_sort_by_name_1" class="SyncAttrNameHead" ' +
			'onclick="OnSortItemClick(\'name_1\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_name') + '" width="50%" > ' + Name_html_1 +
			'</td>' +
			'<td id="' + CollectionID + '_sort_by_size_1" class="SyncAttrSizeHead" ' +
			'onclick="OnSortItemClick(\'size_1\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_size') + '" > ' + SizeItem1 +
			'</td>' +
			'<td id="' + CollectionID + '_sort_by_time_1" class="SyncAttrTimeHead" ' +
			'onclick="OnSortItemClick(\'time_1\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_time') + '" nowrap > ' + TimeItem1 +
			'</td>' +
			//-------------------------------------------------------------------------
			'<td class="Selected_menu_left_cell" ' + Menu_handler + '>' +
			'<img ID="Radio_' + CollectionID + '_' + 0 +
			'" src="' + src_1 + '" ' +
			'" src_normal="Pics/Radio_left_' + SyncTreeArray[0].radio + '.gif" ' +
			'" src_stealth="Pics/Radio_left_right_stealth.gif" ' +
			'title="' + external.LS('hint_Selected_Winner') + '" ' +
			'class="Selected_menu_radio_control" ' + SyncTreeArray[0].radio_handler + '></td>' +
			'<td class="Selected_menu_middle_cell" height="100%" ' + Menu_handler + '>' +
			'<div class="Back_plate"><div ID="Arrow_' + CollectionID + '_' + 0 +

			'" class="' + class_name_1 +
			'" class_normal="Arrow_left_' + SyncTreeArray[0].arrow_direction +
			'" class_stealth="Arrow_edge_stealth' +

			'"></div></div></td>' +
			'<td class="Selected_menu_middle_cell" height="100%" ' + Menu_handler + '>' +
			'<div id="Base_up_' + SelectedMsgID + '" class="Back_plate">' +

			'<div class="' + class_mid_name + '" ' +
			'class_normal="Arrow_mid_top" ' +
			'class_stealth="Arrow_mid_stealth" ' +

			'></div></div></td>' +
			'<td class="Selected_menu_middle_cell" ' + Menu_handler + '><div class="Back_plate">' +
			'<div ID="Arrow_' + CollectionID + '_' + 1 +

			'" class="' + class_name_2 +
			'" class_normal="Arrow_right_' + SyncTreeArray[1].arrow_direction +
			'" class_stealth="Arrow_edge_stealth' +

			'"></div></div></td>' +
			'<td class="Selected_menu_right_cell" ' + Menu_handler + '>' +
			'<img ID="Radio_' + CollectionID + '_' + 1 +
			'" src="' + src_2 + '" ' +
			'" src_normal="Pics/Radio_right_' + SyncTreeArray[1].radio + '.gif" ' +
			'" src_stealth="Pics/Radio_left_right_stealth.gif" ' +
			'title="' + external.LS('hint_Selected_Winner') + '" ' +
			'class="Selected_menu_radio_control" ' + SyncTreeArray[1].radio_handler + '></td>' +
			//-------------------------------------------------------------------------
			'<td id="' + CollectionID + '_sort_by_name_2" class="SyncAttrNameHead" ' +
			'onclick="OnSortItemClick(\'name_2\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_name') + '" width="50%" > ' + Name_html_2 +
			'</td>' +
			'<td id="' + CollectionID + '_sort_by_size_2" class="SyncAttrSizeHead" ' +
			'onclick="OnSortItemClick(\'size_2\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_size') + '" > ' + SizeItem2 +
			'</td>' +
			'<td id="' + CollectionID + '_sort_by_time_2" class="SyncAttrTimeHead2" ' +
			'onclick="OnSortItemClick(\'time_2\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_time') + '" nowrap > ' + TimeItem2 +
			'</td>';
    // Middle rows
    for (i = 2; i < SyncTreeList_length - 1; i++) {
      var src_i = 'Pics/Radio_right_' + SyncTreeArray[i].radio + '.gif'; if (stealth_mode == true) src_i = src_stealth;

      var class_mid_name = 'Arrow_mid_mid'; if (stealth_mode == true) class_mid_name = 'Arrow_mid_stealth';
      var class_name_2 = 'Arrow_right_' + SyncTreeArray[i].arrow_direction; if (stealth_mode == true) class_name_2 = 'Arrow_edge_stealth';

      msg_html += '<tr class="Mid_row" >' +
				//-----------------------------
				'<td class="SyncAttrNameHead" id="' + CollectionID + '_sort_by_name_1" ' +
				'onclick="OnSortItemClick(\'name_1\',\'' + CollectionID + '\');" title="' +
				external.LS('tooltip_sort_by_name') +
				'" width="50%" > ' +
				'</td>' +
				'<td class="SyncAttrSizeHead" id="' + CollectionID + '_sort_by_size_1" ' +
				'onclick="OnSortItemClick(\'size_1\',\'' + CollectionID + '\');" title="' +
				external.LS('tooltip_sort_by_size') +
				'" > ' +
				'</td>' +
				'<td class="SyncAttrTimeHead" id="' + CollectionID + '_sort_by_time_1" ' +
				'onclick="OnSortItemClick(\'time_1\',\'' + CollectionID + '\');" title="' +
				external.LS('tooltip_sort_by_time') +
				'" nowrap > ' +
				'</td>' +
				//-----------------------------
				'<td class="Selected_menu_left_cell"   height="100%" ' + Menu_handler + '>' + '</td>' +
				'<td class="Selected_menu_middle_cell" height="100%" ' + Menu_handler + '>' + '<div class="Back_plate"></div></td>' +
				'<td class="Selected_menu_middle_cell" height="100%" ' + Menu_handler + '>' + '<div class="Back_plate">' +

				'<div class="' + class_mid_name + '" ' +
				'class_normal="Arrow_mid_mid" ' +
				'class_stealth="Arrow_mid_stealth"' +

				'></div></div></td>' +
				'<td class="Selected_menu_middle_cell" ' + Menu_handler + '><div class="Back_plate">' +
				'<div ID="Arrow_' + CollectionID + '_' + i +

				'" class="' + class_name_2 +
				'" class_normal="Arrow_right_' + SyncTreeArray[i].arrow_direction +
				'" class_stealth="Arrow_edge_stealth' +

				'"></div></div></td>' +
				'<td class="Selected_menu_right_cell" ' + Menu_handler + '>' +
				'<img ID="Radio_' + CollectionID + '_' + i +
				'" src="' + src_i + '" ' +
				'" src_normal="Pics/Radio_right_' + SyncTreeArray[i].radio + '.gif" ' +
				'" src_stealth="Pics/Radio_left_right_stealth.gif" ' +
				'title="' + external.LS('hint_Selected_Winner') + '" ' +
				'class="Selected_menu_radio_control" ' + SyncTreeArray[i].radio_handler + '></td>' +
				//-----------------------------
				'<td id="' + CollectionID + '_sort_by_name_2" class="SyncAttrNameHead" ' +
				'onclick="OnSortItemClick(\'name_2\',\'' + CollectionID + '\');" title="' +
				external.LS('tooltip_sort_by_name') + '" width="50%" > ' +
				'</td>' +
				'<td id="' + CollectionID + '_sort_by_size_2" class="SyncAttrSizeHead" ' +
				'onclick="OnSortItemClick(\'size_2\',\'' + CollectionID + '\');" title="' +
				external.LS('tooltip_sort_by_size') + '" > ' +
				'</td>' +
				'<td id="' + CollectionID + '_sort_by_time_2" class="SyncAttrTimeHead2" ' +
				'onclick="OnSortItemClick(\'time_2\',\'' + CollectionID + '\');" title="' +
				external.LS('tooltip_sort_by_time') + '" nowrap > ' +
				'</td>' +
				//-----------------------------
				'</tr>';
    }
    // Last row
    var src_i = 'Pics/Radio_right_' + SyncTreeArray[SyncTreeList_length - 1].radio + '.gif'; if (stealth_mode == true) src_i = src_stealth;

    var class_mid_name = 'Arrow_mid_bottom'; if (stealth_mode == true) class_mid_name = 'Arrow_mid_stealth';
    var class_name_2 = 'Arrow_right_' + SyncTreeArray[SyncTreeList_length - 1].arrow_direction; if (stealth_mode == true) class_name_2 = 'Arrow_edge_stealth';

    msg_html += '<tr class="Last_row" >' +
			//-----------------------------
			'<td class="SyncAttrNameHead" id="' + CollectionID + '_sort_by_name_1" ' +
			'onclick="OnSortItemClick(\'name_1\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_name') +
			'" width="50%" > ' +
			'</td>' +
			'<td class="SyncAttrSizeHead" id="' + CollectionID + '_sort_by_size_1" ' +
			'onclick="OnSortItemClick(\'size_1\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_size') +
			'" > ' +
			'</td>' +
			'<td class="SyncAttrTimeHead" id="' + CollectionID + '_sort_by_time_1" ' +
			'onclick="OnSortItemClick(\'time_1\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_time') +
			'" nowrap > ' +
			'</td>' +
			//-----------------------------
			'<td class="Selected_menu_left_cell" ' + Menu_handler + '>' + '</td>' +
			'<td class="Selected_menu_middle_cell" height="100%" ' + Menu_handler + '>' + '<div class="Back_plate"></div></td>' +
			'<td class="Selected_menu_middle_cell" height="100%" ' + Menu_handler + '>' + '<div id="Base_down_' + SelectedMsgID +
			'" class="Back_plate">' +

			'<div class="' + class_mid_name + '" ' +
			'class_normal="Arrow_mid_bottom" ' +
			'class_stealth="Arrow_mid_stealth" ' +

			'></div></div></td>' +
			'<td class="Selected_menu_middle_cell" ' + Menu_handler + '><div class="Back_plate">' +
			'<div ID="Arrow_' + CollectionID + '_' + (SyncTreeList_length - 1) +

			'" class="' + class_name_2 +
			'" class_normal="Arrow_right_' + SyncTreeArray[SyncTreeList_length - 1].arrow_direction +
			'" class_stealth="Arrow_edge_stealth' +

			'"></div></div></td>' +
			'<td class="Selected_menu_right_cell" ' + Menu_handler + '>' +
			'<img ID="Radio_' + CollectionID + '_' + (SyncTreeList_length - 1) +
			'" src="' + src_i + '" ' +
			'" src_normal="Pics/Radio_right_' + SyncTreeArray[SyncTreeList_length - 1].radio + '.gif" ' +
			'" src_stealth="Pics/Radio_left_right_stealth.gif" ' +
			'title="' + external.LS('hint_Selected_Winner') + '" ' +
			'class="Selected_menu_radio_control" ' + SyncTreeArray[SyncTreeList_length - 1].radio_handler + '></td>' +
			//-----------------------------
			'<td id="' + CollectionID + '_sort_by_name_2" class="SyncAttrNameHead" ' +
			'onclick="OnSortItemClick(\'name_2\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_name') + '" width="50%" > ' +
			'</td>' +
			'<td id="' + CollectionID + '_sort_by_size_2" class="SyncAttrSizeHead" ' +
			'onclick="OnSortItemClick(\'size_2\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_size') + '" > ' +
			'</td>' +
			'<td id="' + CollectionID + '_sort_by_time_2" class="SyncAttrTimeHead2" ' +
			'onclick="OnSortItemClick(\'time_2\',\'' + CollectionID + '\');" title="' +
			external.LS('tooltip_sort_by_time') + '" nowrap > ' +
			'</td>' +
			// -----------------------------
			'</tr>';
    msg_html += '</tbody>';
  }
  return msg_html;
}

// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function BuildImportantCollectionHeader(CollectionID, csName) {
    var msg, sign;

    if (csName == 'Information') {
        msg = 'hint_Information_Collection';
        sign = 'Pics/info_small.gif';
    }
    else {
        msg = 'hint_Important_Collection';
        sign = 'Pics/warning_small.gif';
    }

    var msg_html = '<span>' + external.LS(msg) + '</span>';
    var sign_html = '<span><img src=' + sign + '></span>';

    return '<tbody id="' + CollectionID + '_header_line_container">' +
		'<tr class="Single_row" id="' + CollectionID + '_header_line_string" height="19px">' +
		'<td id="' + CollectionID + '_important_header" class="SyncWinnerItemHead" ' +
		' style="background:#c0c0b4; border: solid 1px #787864; border-right: solid 0px #787864; cursor:hand;" colspan="11" >' +
		'<div style="position: relative; height:100%; width:100%;" >' +
		'<div id="winking_sign_' + CollectionID + '" style="position: absolute; left:5; top:1;">' + sign_html + '</div>' +
		'<div style="position: absolute; left:30; top:2;">' + msg_html + '</div>' +
		'</div>' +
		'</td>' +
		'</tr>' +
		'</tbody>';
}

// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function redraw_all_collections(redraw_mode) {
  //------------------------------
  var header_mode = null;
  var assoc_obj = external.Method0('M_GetSelectedAssoc'); if (!assoc_obj) return;
  var assoc_id = assoc_obj.objID;
  //------------------------------
  var collection_names = [];
  //------------------------------
  collection_names.push('Important');
  collection_names.push('Information');
  collection_names.push('Questionable');
  collection_names.push('Deleted');
  collection_names.push('New');
  collection_names.push('Changed');
  collection_names.push('Unchanged');
  collection_names.push('All');
  collection_names.push('Excluded');
  collection_names.push('OverwriteByUser');
  collection_names.push('SyncFindItems');
  //------------------------------
  var collection_quantity = collection_names.length;
  var collection_id = null;
  var msg_id = null;
  var msg = null;
  var msgs = null;
  var is_visible = false;
  var selected_counter = null;
  //----------------------------------------------------
  if (redraw_mode == 'redraw all') {
    for (var i = 2; i < collection_quantity; i++) {
      collection_id = G_Assocs[assoc_id].TreeView._collectionIdByName[collection_names[i]];
      G_Assocs[assoc_id].TreeView.redraw_collection(collection_id);
    }
  }
  //----------------------------------------------------
  if (redraw_mode == 'headers only') {
    for (var i = 2; i < collecton_quantity; i++) {
      collection_id = G_Assocs[assoc_id].TreeView._collectionIdByName[collection_names[i]];
      if (collection_id) msgs = external.ObjById(collection_id);
      if (msgs) selected_counter = msgs.Method0('M_GetSelectedItemsCount');
      if ((msgs) && (selected_counter)) header_mode = 'select';
      else header_mode = 'sort';
      selected_counter = null;
      if (msgs) msg = msgs.GetFirst();
      if (msg) msg_id = msg.objID;
      //	G_Assocs[assoc_id].TreeView.redraw_collection(collection_id);
      if (msg_id) G_Assocs[assoc_id].TreeView.redraw_header_message(collection_id, msg_id, header_mode, is_visible);
    }
  }
  //----------------------------------------------------
}
