var apps4Prostyle = "<style>.apps4pexts1 { display: inherit; position: relative;left: 150px; } .apps4pexts1 a { text-align:center; color:#ffffff;margin-right:10px } .apps4pexts1 img { width:16px;height:16px;vertical-align:top;margin-right:3px } </style>";

setTimeout(function () {
  if(GetExtensionLocation('apps4pro_gantt_ext_script')) {
  LoadApps4ProGanttBtnInPortalHeader();
  }
  if(GetExtensionLocation('apps4pro_pm_ext_script')) {
  LoadApps4Pro_PM_MenuInPortalHeader();
  }
}, 2000);


var apps4pro_bt_loaded = false;
function LoadApps4ProGanttBtnInPortalHeader() {
  var i = 0;
  var myInterval = setInterval(function () {
    var portalHeader =  $("#topLevelRegion").children().eq(1);
    
    if (portalHeader && portalHeader.length) {
        try {
          var div_apps4_pro_gantt_ph = $("#div_apps4_pro_gantt_ph");
          if (!div_apps4_pro_gantt_ph.length) {
            var extnLoca = GetExtensionLocation('apps4pro_gantt_ext_script');
            var $newdiv1 = $(apps4Prostyle+"<div id='div_apps4_pro_gantt_ph' class='apps4pexts1'><div><button style='background-color: #008CBA;border:none;margin-right:2px;margin-top:10px;padding:7px;'><a title='Gantt' onmouseover='' style='cursor: pointer;' onclick='return GanttView( " + '"'+extnLoca+'"' +" )'><span><img src='" + extnLoca + "/assets/gantt_view.png'/><span>Gantt</span></span></a></button></div></div>");        
            portalHeader.prepend($newdiv1);
          }
        } catch (error) {};
        apps4pro_bt_loaded = true;
      if (apps4pro_bt_loaded) {
        clearInterval(myInterval);
      }
      if (i > 10) {
        clearInterval(myInterval);
      }
      i = i + 1;
    }
  }, 1000);
}

var apps4pro_pm_menu_loaded = false;
function LoadApps4Pro_PM_MenuInPortalHeader() {
  var i = 0;
  var myInterval = setInterval(function () {
    var portalHeader =  $("#topLevelRegion").children().eq(1);
    if (portalHeader && portalHeader.length) {
      try {
        var div_apps4pro_planner_manager_ph = $("#div_apps4pro_planner_manager_ph");
        if (!div_apps4pro_planner_manager_ph.length) {
          var extnLoca = GetExtensionLocation('apps4pro_pm_ext_script');;          
          var $newdiv1 = $(apps4Prostyle+"<div id='div_apps4pro_planner_manager_ph'  class='apps4pexts1'><div><button style='background-color: #008CBA;border:none;margin-right:2px;margin-top:10px;padding:7px;'><a title='Export Tasks' onmouseover='' style='cursor: pointer;' onclick='return ExportTasks( " + '"'+extnLoca+'"' + " )'><span><img  src='" + extnLoca + "/assets/file_export.png'/><span>Export Tasks</span></span></a></button><button style='background-color: #008CBA;border:none;margin-right:2px;margin-top:10px;padding:7px;'><a title='Import Tasks' onmouseover='' style='cursor: pointer;' onclick='return ImportTasks( " + '"'+extnLoca+'"' +" )'><span><img  src='" + extnLoca + "/assets/import.png'/><span>Import Tasks</span></span></a></button></div></div>");
          portalHeader.prepend($newdiv1);
        }
      } catch (error) {};
      apps4pro_pm_menu_loaded = true;

      if (apps4pro_pm_menu_loaded) {
        clearInterval(myInterval);
      }
      if (i > 10) {
        clearInterval(myInterval);
      }
      i = i + 1;
    }
  }, 1000);
}

function GanttView(ext_location) {
  var popuppage = ext_location + '/index.html#/homepage?query=gantt';
  // window.open(popuppage, '_blank',
  //   ',toolbar=no,menubar=no,scrollbars=no,location=no,directories=no');
   windowpop(popuppage, 1100, 650);
}

function ExportTasks(ext_location) {
  var popuppage = ext_location + '/index.html#/homepage?query=export';
  // window.open(popuppage, '_blank',
  //   ',toolbar=no,menubar=no,scrollbars=no,location=no,directories=no');
   windowpop(popuppage, 1100, 650);
}

function ImportTasks(ext_location) {
  var popuppage = ext_location + '/index.html#/homepage?query=import';
  // window.open(popuppage, '_blank',
  //   ',toolbar=no,menubar=no,scrollbars=no,location=no,directories=no');
  windowpop(popuppage, 1100, 650);
}

function windowpop(url, width, height) {
  var leftPosition, topPosition;
  //Allow for borders.
  leftPosition = (window.screen.width / 2) - ((width / 2) + 10);
  //Allow for title and status bars.
  topPosition = (window.screen.height / 2) - ((height / 2) + 50);
  //Open the window.
  // window.open(url, '_blank',
  //   ',toolbar=no,menubar=no,scrollbars=no,location=no,directories=no');
  window.open(url, "Window2", "status=no,height=" + height + ",width=" + width + ",resizable=yes,left=" + leftPosition + ",top=" + topPosition + ",screenX=" + leftPosition + ",screenY=" + topPosition + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no");
}

function GetExtensionLocation(apps4Profile) {
  try {
  var currentJsLoc = $('.'+apps4Profile)[0].src
  var extnLoca = currentJsLoc.substring(0, currentJsLoc.lastIndexOf('/'));
  extnLoca = extnLoca.substring(0, extnLoca.lastIndexOf('/'));
  return extnLoca.replace("assets", "")
  } catch{}
  return '';
}
