//* *****************************************************************************************************
// 							Functions that are being called from core

function ExtActiveAssociationStopped(AssocID) {
  UpdateAssocStatistics(AssocID);
}

function ExtOnStatusAttributeChanged(AssocID, StatusAttribute) {
  //------------------------------------------------------------------------------------------------
  var AssocObj = external.ObjByID(AssocID);
  var Status = AssocObj.Method0('M_GetAggregatedGeneralStatus');
  //------------------------------------------------------------------------------------------------
  if (StatusAttribute == STATUS_ATTRIBUTE_ANALYZE_ENABLED ||
		StatusAttribute == STATUS_ATTRIBUTE_SYNCHRONIZE_ENABLED ||
		StatusAttribute == STATUS_ATTRIBUTE_STOP_ENABLED) { UpdateButtons(AssocID); } else { UpdateAssocStatus(AssocID, Status); }
  //------------------------------------------------------------------------------------------------
  if (AssocID == G_ActiveAssocID) { ExtUpdateProgressBar(AssocID, external.Method0('M_GetProgressState'), external.Method0('M_GetProgressPercent')); } // New !!
  //------------------------------------------------------------------------------------------------
  if (AssocID == G_ActiveAssocID) { ExtRefreshAllFoldersTraffic(AssocID); }
  //------------------------------------------------------------------------------------------------
  var g_selected_lock_old = G_Selected_lock;
  if (external.Method0('M_GetSelectItemMode') == true) G_Selected_lock = false;
  else G_Selected_lock = true;
  if (g_selected_lock_old != G_Selected_lock) redraw_all_collections('redraw all');
  // redraw_all_collections('redraw all');
  // ------------------------------------------------------------------------------------------------
}

function ExtRefreshAllFoldersTraffic(AssocID) {
  // var AssocObj = external.ObjByID(AssocID);
  if (AssocID == G_ActiveAssocID) {
    if (G_Assocs[AssocID].Folders_is_ready == true) {
      var FolderArray_length = G_Assocs[AssocID].Folder_is_ready_array.length;
      for (var i = 0; i < FolderArray_length; i++) {
        //------------------------------------------
        var SyncTree_elem = document.getElementById('Assoc_' + G_ActiveAssocID).all['SyncTree' + i];
        var SyncTreeID = SyncTree_elem.synctree_id;
        //------------------------------------------
        var SyncTreeObject = external.ObjByID(SyncTreeID);
        if (SyncTreeObject != null) {
          //------------------------------------------
          var del_panel = document.getElementById(SyncTreeID + '_traffic_del');
          del_panel.innerHTML = '';
          var temp = external.Method1('M_GetDeletedDataInfo', SyncTreeID);
          if (temp) del_panel.innerHTML = external.Method1('M_GetDeletedDataInfo', SyncTreeID);
          //--
          var mov_panel = document.getElementById(SyncTreeID + '_traffic_mov');
          mov_panel.innerHTML = '';
          temp = external.Method1('M_GetChangedDataInfo', SyncTreeID);
          if (temp) mov_panel.innerHTML = external.Method1('M_GetChangedDataInfo', SyncTreeID);
          //------------------------------------------
        }
      }
    }
  }
}

function ExtPauseStatusChanged(State) {
  ExtUpdateProgressBar(G_ActiveAssocID, State, 101);
}

function ExtGiveHint(AssocID, Hint_element_id) {
  //------------------------------------------------------------------------------
  if (AssocID != G_ActiveAssocID) { return; }
  var hint_element = document.getElementById('Assoc_' + AssocID).all[Hint_element_id];
  //------------------------------------------------------------------------------
  var hint_text = external.LS('hint_syncpath_left');
  var num = 0;
  if (Hint_element_id == 'Syncpath_hint_0') num = 0;
  if (Hint_element_id == 'Syncpath_hint_1') num = 1;
  if (Hint_element_id == 'Syncpath_hint_2') num = 2;
  if (Hint_element_id == 'Syncpath_hint_3') num = 3;
  if (Hint_element_id == 'Syncpath_hint_4') num = 4;
  if (Hint_element_id == 'Syncpath_hint_5') num = 5;

  var SyncTree_elem = document.getElementById('Assoc_' + AssocID).all['SyncTree' + num];
  if (SyncTree_elem) {
    var SyncTreeID = SyncTree_elem.synctree_id;
    var SyncTreeObject = external.ObjByID(SyncTreeID);
    //-----------------------------------------------------------------------------------
    var WizardType = SyncTreeObject.Method0('M_GetDeviceType');
    // var WizardObj  = SyncTreeObject.Method0Own('M_GetWizard');

    if (WizardType == PATH_TYPE__NONE ||
			WizardType == PATH_TYPE_EMPTY) {
      hint_text = external.LS('hint_syncpath_left');
    } else {
      hint_text = external.LS('hint_syncpath_right');
    }
  }
  var hint_width = 0;
  var arrow_left = 0;
  var hint_height = 0;
  if (Hint_element_id == 'Syncpath_hint_0' || Hint_element_id == 'Syncpath_hint_1') {
    hint_width = Hint_window.offsetWidth - 17 - 40;
    arrow_left = Hint_window.offsetWidth - 26;
    Hint_window.innerHTML = '<div style="position:absolute; top:0; left:' + arrow_left + '; width:25px; height:25px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'Pics/Hint_arrow2.png\'); z-index:10"></div>' +
			'<div style="position:absolute; top:18; left:40; width:' + hint_width + '; padding:20px; background:cornsilk; border:solid 1px black; color: black;">' +
			hint_text + '</div>' +
			'<img src="Pics/closehint.gif" onclick="ExtCloseHint(\'' + AssocID + '\', \'' + Hint_element_id + '\')" style="position:absolute; left:40px; top:18px;">';
    Hint_window.style.visibility = 'visible';
  } else if (Hint_element_id == 'Analyze_hint') {
    close_left = hint_element.offsetWidth + 2;
    hint_element.innerHTML = '<div style="position:relative; top:0; left:17; padding:20px; background:cornsilk; border:solid 1px black; color: black;">' +
			external.LS('hint_analyze') + '</div>' +
			'<img src="Pics/closehint.gif" onclick="ExtCloseHint(\'' + AssocID + '\', \'' + Hint_element_id + '\')" style="position:absolute; left:' + close_left + 'px; top:0px;">' +
			'<div style="position:relative; left:0; right:0; width:250px; height:18px;">' +
			'<div style="position:absolute; top:-8; left:0; width:25px; height:25px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'Pics/Hint_arrow3.png\'); z-index:10"></div></div>';
    hint_height = hint_element.offsetHeight;
    hint_element.style.top = -5 - hint_height;
    hint_element.style.visibility = 'inherit';
  } else if (Hint_element_id == 'Synchronize_hint') {
    hint_element.innerHTML = '<div style="position:relative; top:0; left:17; padding:20px; background:cornsilk; border:solid 1px black; color: black;">' +
			external.LS('hint_synchronize') + '</div>' +
			'<div style="position:relative; left:0; right:0; width:250px; height:18px;">' +
			'<img src="Pics/Hint_arrow3.gif" style="position:absolute; top:-8; left:0; z-index:10"></div>';
    hint_height = hint_element.offsetHeight;
    hint_element.style.top = -5 - hint_height;
    hint_element.style.visibility = 'inherit';
  } else {
    hint_width = Hint_window.offsetWidth - 17 - 40;
    arrow_left = Hint_window.offsetWidth - 26;
    Hint_window.innerHTML = '<div style="position:absolute; top:0; left:' + arrow_left + '; width:25px; height:25px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'Pics/Hint_arrow2.png\'); z-index:10"></div>' +
			'<div style="position:absolute; top:18; left:40; width:' + hint_width + '; padding:20px; background:cornsilk; border:solid 1px black; color: black;">' +
			external.LS('hint_syncpath_right') + '</div>' +
			'<img src="Pics/closehint.gif" style="position:absolute; left:40px; top:18px;">';
    Hint_window.style.visibility = 'visible';
  }
}

function ExtCloseHint(AssocID, Hint_element_id) {
  Hint_window.style.visibility = 'hidden';
  Hint_window.hint_number = '';
  var hint_element = document.getElementById('Assoc_' + AssocID).all[Hint_element_id];
  if (hint_element != null) {
    hint_element.innerHTML = '';
    hint_element.style.visibility = 'hidden';
  }
}

function ExtShowCollection(CollectionID) {
  var AssocID = G_ActiveAssocID;
  if (G_Assocs[AssocID].TreeView.root._children[CollectionID]._expanded == 0) {
    OnCollectionExpandIconClick(AssocID, CollectionID);
  }
  var CollectionElement = document.getElementById('Assoc_' + AssocID).all[CollectionID];
  CollectionElement.scrollIntoView();
}

function ExtAddNewAssoc() {
  NewAssoc();
  var ActiveProfile = external.Method0Own('M_GetProfile');
  RefreshJobNameList(ActiveProfile.objID);
}
//	-------------------------------------------------------------------
//	change active association for core
function OnActiveJobChanged(AssocID) {
  DeactivateAssoc(G_ActiveAssocID);
  ActivateAssoc(AssocID);
}

function ExtAskNewAssocName(AssocID) {
  var AssocObj = external.ObjByID(AssocID);
  var OldName = AssocObj.Method0('M_GetAssocName');
  var NewAssocName = prompt(external.LS('rename_association_prompt'), OldName);
  if (NewAssocName)
	AssocObj.Method1('M_SetAssocName', NewAssocName);
}

function ExtChangeBannerState(State) {
  var El = document.getElementById('Banner_place');
  if (State == 1) {
    El.style.display = '';
    // Banner_on_off.title=external.LS('hint_Banner_on');
  } else {
    El.style.display = 'none';
    // Banner_on_off.title=external.LS('hint_Banner_off');
  }
  // ExtChangeSyncBoardState(State);
  ResizeWindow();
}

function ExtChangeSyncBoardState(State) {
  var El = document.getElementById('Assoc_' + G_ActiveAssocID);
  var SubEl1 = El.all.AssocTemplateTable;
  var SubEl2 = SubEl1.all.SyncBoardsTableRow;
  var SubEl3 = SubEl2.all.SyncBoardsTableCell;
  var SubEl4 = SubEl3.all.SyncBoardsTableContainer;
  var ExitEl = SubEl3.all.Exit_full_screen;
  var ChangeEl = document.getElementById('Change_link');

  if (State == 1) {
    SubEl4.style.visibility = 'hidden';
    SubEl4.style.display = 'none';
    SubEl3.style.height = '10px';
    ExitEl.style.display = 'block';
    ChangeEl.style.display = 'none';

    G_Sync_board_state = 0;
  } else {
    SubEl4.style.display = 'block';
    SubEl4.style.visibility = 'visible';
    ExitEl.style.display = 'none';
    ChangeEl.style.display = 'block';

    G_Sync_board_state = 1;
  }

  ResizeWindow();
}

function ExtLinkToCollectionMsg(CollectionName, MsgObjID) {
  LinkToCollectionMsg(CollectionName, MsgObjID);
}
//------------------------------------------------------------
// progress Bar block
function ExtUpdateProgressBar(AssocID, State, Percents) {
  var processName = 'undefined';
  var stateString = 'undefined';
  var titleString = '';

  if ((State == null) || (State == 0) || (State == 2) || (State == 4)) {
    document.getElementById('sliderWrapper').style.visibility = 'hidden';
    document.getElementById('sliderWrapper').firstChild.nodeValue = 0 + '%';
    document.getElementById('slider').firstChild.nodeValue = 0 + '%';

    if (State == 0) {
      var AssocObj = external.ObjByID(AssocID);
      var Status = AssocObj.Method0('M_GetAggregatedGeneralStatus');
      if (Status == STATUS_ATTRIBUTE_AUTOMATIC || Status == STATUS_ATTRIBUTE_AUTOMATIC_INTERNAL) {
        stateString = external.LS('Auto_Sync_Is_On');
        titleString = external.LS('tooltip_Auto_Sync_Is');
      } else {
        stateString = external.LS('Auto_Sync_Is_Off');
        titleString = external.LS('tooltip_Auto_Sync_Is');
      }
    } else if (State == 2) {
      stateString = external.LS('Pr_Bar_A_Is_Done');
      titleString = '';
    } else if (State == 4) {
      stateString = external.LS('Pr_Bar_Suspended');
      titleString = external.LS('tooltip_All_Is_Suspended');
    }
  } else {
    //-------------------------------------------------------------------------------------
    if (State == 1) processName = external.LS('Pr_Bar_Analyzing');
    else if (State == 3) processName = external.LS('Pr_Bar_Synchronizing');
    //-------------------------------------------------------------------------------------
    if ((Percents >= 0) && (Percents <= 100)) {
      var factor = Percents / 100;
      var pct = Math.ceil(factor * 100);
      document.getElementById('sliderWrapper').firstChild.nodeValue = processName + pct + '%';
      document.getElementById('slider').firstChild.nodeValue = processName + pct + '%';
      document.getElementById('slider').style.clip = 'rect(0px ' + parseInt(factor * 387) + 'px 16px 0px)';
      document.getElementById('sliderWrapper').style.visibility = 'visible';
    }
  }

  document.getElementById('progressBarMsg').innerHTML = stateString;
  document.getElementById('progressBarMsg').title = titleString;
  return;
}

function ExtRedrawCollection(CollectionID) {
  G_Assocs[G_ActiveAssocID].TreeView.redraw_collection(CollectionID);
}

function important_sign_winking(collection_id) {
  var winking_sign = document.getElementById('winking_sign_' + collection_id);
  var a = null;
  if (winking_sign) {
    if (winking_sign.style.opacity === undefined) winking_sign.style.opacity = 1;
    else winking_sign.style.opacity = 1 - winking_sign.style.opacity;
    a = winking_sign.style.opacity;
    winking_sign.style.filter = 'alpha(opacity=' + (100 * (1 - parseInt(a))) + ')';
  }
}

function ExtRemoveMsg(MsgCollectionObjID, MsgObjID) {
  if (G_Collection_lock == true) return;

  if ((G_Above_message.msg_id == MsgObjID) && (G_Above_message.collection_id == MsgCollectionObjID)) G_Above_message.mustshown = false;

  G_Shown_message.collection_id = null;
  G_Shown_message.msg_id = null;
  //------------------------------------
  var msg = external.ObjByID(MsgObjID);
  if (msg == null) return;
  //------------------------------------
  var AssocID = msg.GetM('m_SlnDoAssocSyncWPtr').objID;
  if (G_Assocs[AssocID].TreeView._collections[MsgCollectionObjID] == null) return;
  //------------------------------------
  var collection_container = document.getElementById('Assoc_' + AssocID).all[MsgCollectionObjID + '_container']; if (collection_container.children == null) return;
  //---------------------------------------------------------------
  var message_element = collection_container.children[MsgObjID];
  if (message_element == null) return;
  else if (message_element.exist == 'false') return;
  //---------------------------------------------------------------
  var MsgEvenness = 0;
  if (message_element.className.match(/Msg_Odd/)) MsgEvenness = 1;
  ChangeMessageIntoRemovedObject(message_element, MsgCollectionObjID, MsgObjID, MsgEvenness);
}

function ExtUpdateAfterRemove(CollectionID) {
  var msg_page_shift = 1; // first line is a header line
  var collection_container_element = document.getElementById(CollectionID + '_container');
  if (collection_container_element == null) return;
  var collection_container_length = collection_container_element.children.length; // alert(collection_container_length);
  //----------------------------------------------------------------------------------------
  var msgs = G_Assocs[G_ActiveAssocID].TreeView._collections[CollectionID].obj;
  var msgs_count = msgs.Method0('M_GetCount'); // alert(msgs_count);
  //----------------------------------------------------------------------------------------
  if (msgs_count == 0) {
    var collection_node = G_Assocs[G_ActiveAssocID].TreeView.root._children[CollectionID];
    collection_node._empty = true;
    G_Assocs[G_ActiveAssocID].TreeView.redraw_collection(CollectionID);
    return;
  }
  //----------------------------------------------------------------------------------------
  var msg = msgs.Method0('M_GetFirstVisibleMessage');
  var msgID = null;
  if (msg != null) msgID = msg.objID;
  var i = 0 + msg_page_shift;
  //----------------------------------------------------------------------------------------
  while ((msg != null) && (i < collection_container_length)) {
    if (collection_container_element.children(i).id != msgID) {
      var message_element = collection_container_element.children(i);
      var MsgEvenness = 1;
      BuildMessageAsObject(message_element, CollectionID, msgID, MsgEvenness);
    }
    msg = msgs.Method1('M_GetNextVisibleMessage', msg);
    if (msg != null) msgID = msg.objID;
    i++;
  }
  //----------------------------------------------------------------------------------------
  if (msg == null) {
    while (collection_container_element.children(i)) {
      var message_container_element = collection_container_element.children(i);
      message_container_element.removeNode(true);
    }
  } else {
    while (msg != null) {
      var Assoc = msg.GetM('m_SlnDoAssocSyncWPtr');
      var AssocID = Assoc.objID;
      msgID = msg.objID;
      if (G_Assocs[AssocID].TreeView._collections[CollectionID] != null) { G_Assocs[AssocID].TreeView.add_message_node(CollectionID, msgID, -1); }

      msg = msgs.Method1('M_GetNextVisibleMessage', msg);
    }
  }
  //----------------------------------------------------------------------------------------
  i = 0 + msg_page_shift;
  var MsgEvenness = 1;
  while (collection_container_element.children(i)) {
    if (MsgEvenness == 0) collection_container_element.children(i).className = 'Msg_Even';
    else collection_container_element.children(i).className = 'Msg_Odd';
    MsgEvenness = 1 - MsgEvenness;
    i++;
  }
  //----------------------------------------------------------------------------------------
  AutoRefreshScrollers(AssocID, CollectionID);
  //----------------------------------------------------------------------------------------
}

function ExtOnAssocChanged(AssocID) {
  if (Flag_can_reload_profile == false) {
    return;
  }

  var AssocObj = external.ObjByID(AssocID);

  if ((G_AssocChangedCause == 'Propagate_set') && (AssocID == G_ActiveAssocID)) {
    If_remove_check.checked = AssocObj.Method0('M_GetMimicDeleted');
    If_rewrite_check.checked = AssocObj.Method0('M_GetMimicChanged');
    return;
  }

  if (G_AssocChangedCause == 'SyncDirectionChanged') {
    if (AssocID == G_ActiveAssocID) {
      If_remove_check.checked = AssocObj.Method0('M_GetMimicDeleted');
      If_rewrite_check.checked = AssocObj.Method0('M_GetMimicChanged');
    }

    var SyncDirection = AssocObj.Method0('M_GetSyncDirection');

    if ((typeof (SyncDirection) == 'string') && (SyncDirection == external.AbbrToHex('DIRECTION_ALL_WAY'))) {
      G_Assocs[AssocID].WinningSuperTreeID = CONST_CID;
      var NewWinningSuperTreeID = 'DIRECTION_ALL_WAY';
    } else {
      G_Assocs[AssocID].WinningSuperTreeID = SyncDirection.objID;
      var NewWinningSuperTreeID = SyncDirection.objID;
    }

    SwitchSyncDirectionArrow(AssocID, NewWinningSuperTreeID);

    return;
  }

  var LabelText = TabsLabels.cells['Label_' + AssocID].all.TabCaptionText;

  LabelText.innerText = AssocObj.Method0('M_GetAssocName');
  G_Assocs[AssocID].OnAssocChangedCalled = true;

  if (LoadAssocPaths(AssocID) == true) {
    if ((G_AnalyzeHintCallCounter > 0) && (G_OnDirectionChanged == false) && (G_filter_switched == false)) {
      var AssocElement = document.getElementById('Assoc_' + AssocID);
      if ((AssocElement != null) && (!AssocElement.all.B_ANALYZE.disabled)) {
        ExtGiveHint(AssocID, 'Analyze_hint');
        G_AnalyzeHintCallCounter--;
      }
    }

    if (AssocID == G_ActiveAssocID && (G_filter_switched == false)) {
      setTimeout("FocusToAnalyze('" + AssocID + "')", 0);
    }

    G_filter_switched = false;
  }
}

function ExtCountersRefresh(CollectionID, Counter, SelectCounter) {
  //	if (CollectionID === undefined)	return;
  CounterRefresh(G_ActiveAssocID, CollectionID, Counter);
  SelectCounterRefresh(CollectionID, SelectCounter);
  if (G_Collection_lock == false) { AutoRefreshScrollers(G_ActiveAssocID, CollectionID); }
}

function ExtSelectMsg(CollectionID, MsgID) {
  //	ExtChangeMsg(CollectionID, MsgID); // Core calls this function after message selection/deselection
  //	var Collection = external.ObjById(CollectionID);
  //	var SelectedWinningTreeID = Collection.Method0('M_GetSelectedStatus');
  G_Assocs[G_ActiveAssocID].TreeView.redraw_collection(CollectionID);
  UpdateSelectedMenu(CollectionID, null); // SelectedWinningTreeID == null means that selected menu is closed
}

function ExtAddNewMsg(MsgCollectionObjID, MsgObjID, InsertPosition) {
  if (G_Collection_lock == true) return;

  var msg = external.ObjByID(MsgObjID);
  var Assoc = null;
  var AssocID = null;
  if (msg != null) {
    Assoc = msg.GetM('m_SlnDoAssocSyncWPtr');
    AssocID = Assoc.objID;
    if (G_Assocs[AssocID].TreeView._collections[MsgCollectionObjID] != null) { G_Assocs[AssocID].TreeView.add_message_node(MsgCollectionObjID, MsgObjID, InsertPosition); }
  }
}

function ExtChangeMsg(MsgCollectionObjID, MsgObjID) {
  if (G_Collection_lock == true) return;

  UpdateSelectedMenu(MsgCollectionObjID, null); // WinningTreeID == null means that selected menu is closed

  if ((G_Above_message.msg_id == MsgObjID) && (G_Above_message.collection_id == MsgCollectionObjID)) G_Above_message.mustshown = false;

  G_Shown_message.collection_id = null;
  G_Shown_message.msg_id = null;
  //------------------------------------
  var msg = external.ObjByID(MsgObjID);
  if (msg == null) return;
  //------------------------------------
  var AssocID = msg.GetM('m_SlnDoAssocSyncWPtr').objID;
  if (G_Assocs[AssocID].TreeView._collections[MsgCollectionObjID] == null) return;
  //------------------------------------
  var collection_container = document.getElementById('Assoc_' + AssocID).all[MsgCollectionObjID + '_container'];
  //------------------------------------
  var message_element = collection_container.children[MsgObjID];
  if (message_element == null) {
    return;
  } else if (message_element.exist == 'false') {
    return;
  }
  //------------------------------------
  if (message_element.className.match(/Msg_Odd/)) MsgEvenness = 1;
  else if (message_element.className.match(/Msg_Even/)) MsgEvenness = 0;
  else if (message_element.className.match(/Msg_Selected/)) MsgEvenness = message_element.MsgEvenness;
  else if (message_element.oldClassName.match(/Msg_Odd/)) MsgEvenness = 1;
  else if (message_element.oldClassName.match(/Msg_Even/)) MsgEvenness = 0;
  //------------------------------------
  BuildMessageAsObject(message_element, MsgCollectionObjID, MsgObjID, MsgEvenness);
}

function ExtRedrawTreeView(AssocID) {
  var result = GetBackAllSyncBoards(AssocID);
  if (result) return true;
  return false;
}
// ////////////////////////////////////////////////////////////////////////////////////////
//	reload profile by core call

function ExtOnProfileChanged() {
  if (Flag_can_reload_profile == false) {
    return;
  }
  RemoveAllAssocsFromScreen();
  LoadProfile();

  ActivateAssoc(G_ActiveAssocID);
  UpdateButtons(G_ActiveAssocID);
  ResizeWindow();
}
