//System.Gadget.Flyout.onShow = showFlyout;
System.Gadget.Flyout.onHide = closeFlyout;

var lngID = System.Gadget.document.parentWindow.lngID;

var size = System.Gadget.Settings.read("SetSize");

function closeFlyout(){
 System.Gadget.Flyout.show = false;
}

function playSound(){
 if (!System.Gadget.document.parentWindow.disableflyoutsoundsflag) System.Sound.playSound("media/tone.wav");
}

function showFlyout(){
 gtitle.innerText = flylng[lngID][0];
 nacnamestr.innerText = flylng[lngID][1];
 macaddrstr.innerText = flylng[lngID][2];
 ipv4str.innerText = flylng[lngID][3];
 ipv6str.innerText = flylng[lngID][4];
 subnetmaskstr.innerText = flylng[lngID][5];
 defgwstr.innerText = flylng[lngID][6];
 dhcpserverstr.innerText = flylng[lngID][7];
 dnsserverstr.innerText = flylng[lngID][8];
 winsstr.innerText = flylng[lngID][9];
 pcnamestr.innerText = flylng[lngID][10];
 domainstr.innerText = flylng[lngID][11];
 speedstr.innerText = flylng[lngID][12];
 nacname.title = flylng[lngID][13];
 macaddrname.title = flylng[lngID][13];
 ipv4.title = flylng[lngID][13];
 ipv6.title = flylng[lngID][13];
 subnetmask.title = flylng[lngID][13];
 defgw.title = flylng[lngID][13];
 dhcpserver.title = flylng[lngID][13];
 dnsserver.title = flylng[lngID][13];
 wins.title = flylng[lngID][13];
 pcname.title = flylng[lngID][13];
 domain.title = flylng[lngID][13];
 speed.title = flylng[lngID][13];
 closeflyouta.innerText = flylng[lngID][14];

 gtitle.style.color = System.Gadget.Settings.read("colServStr");

 nacnamestr.style.color = System.Gadget.Settings.read("colMenu");
 macaddrstr.style.color = System.Gadget.Settings.read("colMenu");
 ipv4str.style.color = System.Gadget.Settings.read("colMenu");
 ipv6str.style.color = System.Gadget.Settings.read("colMenu");
 subnetmaskstr.style.color = System.Gadget.Settings.read("colMenu");
 defgwstr.style.color = System.Gadget.Settings.read("colMenu");
 dhcpserverstr.style.color = System.Gadget.Settings.read("colMenu");
 dnsserverstr.style.color = System.Gadget.Settings.read("colMenu");
 winsstr.style.color = System.Gadget.Settings.read("colMenu");
 pcnamestr.style.color = System.Gadget.Settings.read("colMenu");
 domainstr.style.color = System.Gadget.Settings.read("colMenu");
 speedstr.style.color = System.Gadget.Settings.read("colMenu");

 nacname.style.color = System.Gadget.Settings.read("colextIP");
 macaddrname.style.color = System.Gadget.Settings.read("colextIP");
 ipv4.style.color = System.Gadget.Settings.read("colextIP");
 ipv6.style.color = System.Gadget.Settings.read("colextIP");
 subnetmask.style.color = System.Gadget.Settings.read("colextIP");
 defgw.style.color = System.Gadget.Settings.read("colextIP");
 dhcpserver.style.color = System.Gadget.Settings.read("colextIP");
 dnsserver.style.color = System.Gadget.Settings.read("colextIP");
 wins.style.color = System.Gadget.Settings.read("colextIP");
 pcname.style.color = System.Gadget.Settings.read("colextIP");
 domain.style.color = System.Gadget.Settings.read("colextIP");
 speed.style.color = System.Gadget.Settings.read("colextIP");

 iplookupa.style.color = System.Gadget.Settings.read("colCopyright");
 closeflyouta.style.color = System.Gadget.Settings.read("colCopyright");

 //body
 background.src = "images/flyout.png";
 document.body.style.width = parseInt(300 * size);
 background.style.width = parseInt(300 * size);
 document.body.style.height = parseInt(204 * size);
 background.style.height = parseInt(204 * size);
 mainsquare.style.top = parseInt(5 * size);
 mainsquare.style.left = parseInt(5 * size);
 mainsquare.style.width =  parseInt(290 * size);
 mainsquare.style.height = parseInt(194 * size);
 mainsquare.style.background = System.Gadget.Settings.read("colBgColor");
 //top
 gtitle.style.top = parseInt(6 * size);
 nacnamestr.style.top = parseInt(20 * size);
 nacname.style.top = parseInt(20 * size);
 macaddrstr.style.top = parseInt(34 * size);
 macaddrname.style.top = parseInt(34 * size);
 ipv4str.style.top = parseInt(48 * size);
 ipv4.style.top = parseInt(48 * size);
 ipv6str.style.top = parseInt(62 * size);
 ipv6.style.top = parseInt(62 * size);
 subnetmaskstr.style.top = parseInt(76 * size);
 subnetmask.style.top = parseInt(76 * size);
 defgwstr.style.top = parseInt(90 * size);
 defgw.style.top = parseInt(90 * size);
 dhcpserverstr.style.top = parseInt(104 * size);
 dhcpserver.style.top = parseInt(104 * size);
 dnsserverstr.style.top = parseInt(118 * size);
 dnsserver.style.top = parseInt(118 * size);
 winsstr.style.top = parseInt(132 * size);
 wins.style.top = parseInt(132 * size);
 pcnamestr.style.top = parseInt(146 * size);
 pcname.style.top = parseInt(146 * size);
 domainstr.style.top = parseInt(160 * size);
 domain.style.top = parseInt(160 * size);
 speedstr.style.top = parseInt(174 * size);
 speed.style.top = parseInt(174 * size);
 iplookup.style.top = parseInt(186 * size);
 closeflyout.style.top = parseInt(186 * size);
 //width
 nacnamestr.style.width = parseInt(56 * size);
 nacname.style.width = parseInt(237 * size);
 macaddrstr.style.width = parseInt(102 * size);
 ipv4str.style.width = parseInt(102 * size);
 ipv6str.style.width = parseInt(102 * size);
 subnetmaskstr.style.width = parseInt(102 * size);
 defgwstr.style.width = parseInt(102 * size);
 dhcpserverstr.style.width = parseInt(102 * size);
 dnsserverstr.style.width = parseInt(102 * size);
 winsstr.style.width = parseInt(102 * size);
 pcnamestr.style.width = parseInt(102 * size);
 domainstr.style.width = parseInt(102 * size);
 speedstr.style.width = parseInt(102 * size);
 //left
 nacname.style.left = parseInt(58 * size);
 macaddrname.style.left = parseInt(104 * size);
 ipv4.style.left = parseInt(104 * size);
 ipv6.style.left = parseInt(104 * size);
 subnetmask.style.left = parseInt(104 * size);
 defgw.style.left = parseInt(104 * size);
 dhcpserver.style.left = parseInt(104 * size);
 dnsserver.style.left = parseInt(104 * size);
 wins.style.left = parseInt(104 * size);
 pcname.style.left = parseInt(104 * size);
 domain.style.left = parseInt(104 * size);
 speed.style.left = parseInt(104 * size);
 iplookup.style.left = parseInt(12 * size);
 //font-size
 gtitle.style.fontSize = parseInt(12 * size);
 nacnamestr.style.fontSize = parseInt(12 * size);
 nacname.style.fontSize = parseInt(12 * size);
 macaddrstr.style.fontSize = parseInt(12 * size);
 macaddrname.style.fontSize = parseInt(12 * size);
 ipv4str.style.fontSize = parseInt(12 * size);
 ipv4.style.fontSize = parseInt(12 * size);
 ipv6str.style.fontSize = parseInt(12 * size);
 ipv6.style.fontSize = parseInt(12 * size);
 subnetmaskstr.style.fontSize = parseInt(12 * size);
 subnetmask.style.fontSize = parseInt(12 * size);
 defgwstr.style.fontSize = parseInt(12 * size);
 defgw.style.fontSize = parseInt(12 * size);
 dhcpserverstr.style.fontSize = parseInt(12 * size);
 dhcpserver.style.fontSize = parseInt(12 * size);
 dnsserverstr.style.fontSize = parseInt(12 * size);
 dnsserver.style.fontSize = parseInt(12 * size);
 winsstr.style.fontSize = parseInt(12 * size);
 wins.style.fontSize = parseInt(12 * size);
 pcnamestr.style.fontSize = parseInt(12 * size);
 pcname.style.fontSize = parseInt(12 * size);
 domainstr.style.fontSize = parseInt(12 * size);
 domain.style.fontSize = parseInt(12 * size);
 speedstr.style.fontSize = parseInt(12 * size);
 speed.style.fontSize = parseInt(12 * size);
 iplookup.style.fontSize = parseInt(10 * size);
 closeflyout.style.fontSize = parseInt(10 * size);

 document.getElementById("mainsquare").focus();

 try{getipv6()}
 catch (e) {}

 var WshShell = new ActiveXObject("WScript.Shell");
 var regstr = "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\"
 var locator = new ActiveXObject("WbemScripting.SWbemLocator");
 var objWMIService = locator.ConnectServer(null, "root\\cimv2");
 var objwmiWMI = locator.ConnectServer(null, "root\\WMI");
 var wbemFlagReturnImmediately = 0x10;
 var wbemFlagForwardOnly = 0x20;
 var NACdata = objWMIService.ExecQuery("SELECT Description,Index,MACAddress,IPAddress,IPSubnet,DefaultIPGateway,DHCPserver,DNSServerSearchOrder,WINSPrimaryServer FROM Win32_NetworkAdapterConfiguration Where IPenabled=True", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly);
 var dataNAC = new Enumerator(NACdata);
 for(var i=0; i!=System.Gadget.document.parentWindow.Int_Nr; i++){
  dataNAC.moveNext()
 }
 var objItem = dataNAC.item();
 try{nacname.innerText = (objItem.Description != null) ?  objItem.Description : flylng[lngID][15]}
 catch (e) {nacname.innerText = flylng[lngID][15]}
 try{macaddrname.innerText = (objItem.MACAddress != null) ? objItem.MACAddress : flylng[lngID][15]}
 catch (e) {macaddrname.innerText = flylng[lngID][15]}
 try{ipv4.innerText = (objItem.IPAddress.toArray()[0] != null) ? objItem.IPAddress.toArray()[0] : flylng[lngID][15]}
 catch (e) {ipv4.innerText = flylng[lngID][17]}
 try{subnetmask.innerText = (objItem.IPSubnet.toArray()[0] != null) ? objItem.IPSubnet.toArray()[0] : flylng[lngID][15]}
 catch (e) {subnetmask.innerText = flylng[lngID][15]}
 try{defgw.innerText = (objItem.DefaultIPGateway.toArray()[0] != null) ? objItem.DefaultIPGateway.toArray()[0] : flylng[lngID][16]}
 catch (e) {defgw.innerText = flylng[lngID][16]}
 try{dhcpserver.innerText = (objItem.DHCPserver != null) ? objItem.DHCPserver : flylng[lngID][16]}
 catch (e) {dhcpserver.innerText = flylng[lngID][15]}
 try{dnsserver.innerText = (objItem.DNSServerSearchOrder.toArray()[0] != null) ? objItem.DNSServerSearchOrder.toArray()[0] : flylng[lngID][16]}
 catch (e) {dnsserver.innerText = flylng[lngID][15]}
 try{
  if (objItem.DNSServerSearchOrder.toArray()[1] != null){
   dnsserver.innerText += ", " + objItem.DNSServerSearchOrder.toArray()[1];
   dnsserverstr.style.width = parseInt(88 * size);
   dnsserver.style.left = parseInt(92 * size);
  }
 }
 catch (e) {}
 try{wins.innerText = (objItem.WINSPrimaryServer != null) ? objItem.WINSPrimaryServer : flylng[lngID][16]}
 catch (e) {wins.innerText = flylng[lngID][15]}

 var compsys = objWMIService.ExecQuery("SELECT Domain, Name FROM Win32_ComputerSystem");
 var compsysdata = new Enumerator(compsys);
 var datacompsys = compsysdata.item();
 try{pcname.innerText = (datacompsys.Name != null) ? datacompsys.Name : flylng[lngID][16]}
 catch (e) {pcname.innerText = flylng[lngID][15]}
 try{domain.innerText = (datacompsys.Domain != null) ? datacompsys.Domain : flylng[lngID][16]}
 catch (e) {domain.innerText = flylng[lngID][15]}

 try{var inextostring = parseInt(objItem.Index,10) + ""}
 catch (e) {var inextostring = "????"}
 var endnum = 4 - inextostring.length;

 for(var i=0; i<endnum; i++){
  regstr += "0";
 }
 regstr += inextostring + "\\*SpeedDuplex";

 try{var duplexvalue = WshShell.RegRead(regstr)}
 catch (e) {var duplexvalue = "?"}
 var currentduplex;
 switch (duplexvalue){
  case "0":
   var ndisColl = new Enumerator(objwmiWMI.ExecQuery("SELECT NdisLinkSpeed " + "FROM MSNdis_LinkSpeed WHERE InstanceName='" + objItem.Description + "'"));
   currentduplex = flylng[lngID][18] + formatbytes(ndisColl.item().NdisLinkSpeed) + flylng[lngID][26];
   break;
  case "1":
   currentduplex = flylng[lngID][19];
   break;
  case "2":
   currentduplex = flylng[lngID][20];
   break;
  case "3":
   currentduplex = flylng[lngID][21];
   break;
  case "4":
   currentduplex = flylng[lngID][22];
   break;
  case "5":
   currentduplex = flylng[lngID][23];
   break;
  case "6":
   currentduplex = flylng[lngID][24];
   break;
  default:
   var ndisColl = new Enumerator(objwmiWMI.ExecQuery("SELECT NdisLinkSpeed " + "FROM MSNdis_LinkSpeed WHERE InstanceName='" + objItem.Description + "'"));
   currentduplex = flylng[lngID][25] + formatbytes(ndisColl.item().NdisLinkSpeed) + flylng[lngID][26];
   break;
 }
 speed.innerText = currentduplex;
}

function formatbytes(kbites){
 if (kbites >= 10000000) return (((kbites / 1000) / 1000) / 10).toFixed(1) + flylng[lngID][27];
 else return ((kbites / 1000) / 10).toFixed(1) + flylng[lngID][28];
}

function getipv6(){
 var xmlHttp = null;
 var url = "http://metric6.inetcore.com/46checker/";
 try{var xmlHttp = new XMLHttpRequest()}
 catch (e){var xmlHttp = new ActiveXObject('Microsoft.XMLHTTP')}
 xmlHttp.open("GET", url, true); //true mean call is asynchronous
 xmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); //for non cached

 xmlHttp.onreadystatechange = function(){
  // Verify that the document is ready to parse.
  if (xmlHttp.readyState == 4){  
   // Makes sure it found the file.
   if (xmlHttp.status == 200){
    // Extract information
    if(xmlHttp.responseXML.getElementsByTagName("src_addr")[0].firstChild.nodeValue){
     var ipv6addr = xmlHttp.responseXML.getElementsByTagName("src_addr")[0].firstChild.nodeValue;
     if (ipv6addr.length > 25){
      ipv6str.style.width = parseInt(36 * size);
      ipv6.style.left = parseInt(38 * size);
     }
     ipv6.innerText = ipv6addr;
    }
   }//end status
   else{
    if (xmlHttp.status==12029 || xmlHttp.status==12002 || xmlHttp.status==12007 || xmlHttp.status==12030 || xmlHttp.status==12031) ipv6.innerText = flylng[lngID][29];
    else ipv6.innerText = flylng[lngID][30] + xmlHttp.status;
   } 
  }//end readyState
 }//end function

 xmlHttp.send(null);
}

function copyText(field){
 window.clipboardData.setData('Text',field.innerText);
}