//* *****************************************************************************************************
// 							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; }
  function CloseButtonIn(position) {
    var Pos = { TL: 'left', TR: 'right' };
    document.getElementById('H' + position).innerHTML = '<div style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' +
			'(src=\'Pics/HintClose.png\'); width:15px; height:15px; position:absolute; ' + Pos[position] + ':0; bottom:0; cursor:pointer" onclick="ExtCloseHint()"></div>';
  }
  function ArrowIn(position) {
    var Arrow = {
      TL: { image: 'Hint_arrow1.png', x: 'right', y: 'bottom' },
      TR: { image: 'Hint_arrow2.png', x: 'left', y: 'bottom' },
      BL: { image: 'Hint_arrow3.png', x: 'right', y: 'top' },
      BR: { image: 'Hint_arrow4.png', x: 'left', y: 'top' }
    };
    document.getElementById('H' + position).innerHTML = '<div style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' +
			'(src=\'Pics/' + Arrow[position].image + '\'); width:25px; height:25px; position:absolute; ' + Arrow[position].x + ':0; ' + Arrow[position].y + ':5px"></div>';
  }

  if (Hint_element_id.indexOf('Syncpath_hint_') != -1) var num = Hint_element_id.charAt(14);

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

  document.getElementById('HTL').innerHTML = '';
  document.getElementById('HTR').innerHTML = '';
  document.getElementById('HBL').innerHTML = '';
  document.getElementById('HBR').innerHTML = '';
  var HintWindow = document.getElementById('HintWindow');
  HintWindow.hint_number = num;
  switch (Hint_element_id) {
  case 'Analyze_hint':
    HintedObj = document.getElementById('btnAnalyze');
    document.getElementById('HintText').innerHTML = external.LS('hint_analyze');
    ArrowIn('TL');
    CloseButtonIn('TR');
    HintFixed = true;
    break;
  case 'Synchronize_hint':
    HintedObj = document.getElementById('btnSync');
    document.getElementById('HintText').innerHTML = external.LS('hint_synchronyze');
    ArrowIn('TL');
    CloseButtonIn('TR');
    HintFixed = true;
    break;
  default:
    HintedObj = document.getElementById('Assoc_' + AssocID).all['SyncTree' + num];// document.getElementById("TreeForm"+num);
    document.getElementById('HintText').innerHTML = external.LS((WizardType == PATH_TYPE__NONE ||
				WizardType == PATH_TYPE_EMPTY) ? 'hint_syncpath_left' : 'hint_syncpath_right');
    ArrowIn('TR');
    CloseButtonIn('TL');
    HintFixed = false;
    break;
  }
  if (HintFixed) {
    HintWindow.style.left = HintedObj.offsetLeft + HintedObj.offsetWidth;
    HintWindow.style.top = HintedObj.parentNode.offsetHeight + 4;
  } else {
    // HintWindow.style.left = (HintedObj.style.posLeft+HintedObj.offsetWidth)/document.body.offsetWidth*100+'%';
    var HintX = 50;
    var HintY = HintedObj.offsetHeight;
    // HintedObj.style.border = 'solid 5px white';
    if (HintedObj.offsetParent) do { HintX += HintedObj.offsetLeft; HintY += HintedObj.offsetTop; } while ((HintedObj = HintedObj.offsetParent));
    HintWindow.style.left = HintX / document.body.offsetWidth * 100 + '%';
    HintWindow.style.top = HintY;
  }
  HintWindow.style.display = '';
}

function ExtCloseHint()// AssocID,Hint_element_id)
{
  document.getElementById('HintWindow').style.display = 'none';
  document.getElementById('HintWindow').hint_number = '';
  /*
	Hint_window.style.visibility = "hidden";
	Hint_window.hint_number = '';
	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);
}
//	-------------------------------------------------------------------
//	change name of active association for core
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 stateString = 'undefined';
  var titleString = '';
  var stateObj = document.getElementById('TBPB_percentage');

  if ((State == null) || (State == 0) || (State == 2) || (State == 4)) {
    document.getElementById('TBPB_container').style.display = 'none';
    // document.getElementById("TBPB_percentage").style.visibility = "hidden";

    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');
    }

    stateObj.innerHTML = stateString;
    stateObj.title = titleString;
    stateObj.style.width = '';
  } else {
    document.getElementById('TBPB_container').style.display = '';
    // document.getElementById("TBPB_percentage").style.visibility = "visible";
    if (Percents >= 1 && document.getElementById('TBPB_container').style.display == '' && Percents <= 100) {
      document.getElementById('TBPB').style.display = '';
      document.getElementById('TBPB').firstChild.style.width = Percents + '%';
      stateObj.innerHTML = Percents + '%';
      stateObj.style.width = '30px';
    } else { document.getElementById('TBPB').style.display = ''; }
  }

  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;
  //---------------------------------------------------------------
  if (message_element.className.match(/Msg_Odd/)) MsgEvenness = 1;
  else MsgEvenness = 0;
  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)) {
      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) && (document.getElementById('btnAnalyze').status)) {
        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();
}
