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

var locator = new ActiveXObject("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(null, "root\\cimv2");

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

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

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

String.prototype.removeblankbegin=function(){
 // remove blanks at begin of string
 var res=/^\s+/g;
 return this.replace(res,'');
}

function showFlyout(){
 gtitle.innerText = flylng[lngID][0];
 model_name.innerText = flylng[lngID][1];
 serialnumber.innerText = flylng[lngID][2];
 capacity.innerText = flylng[lngID][3];
 serialnumber_data.title = flylng[lngID][4];
 mediatype.innerText = flylng[lngID][5];
 interfacetype.innerText = flylng[lngID][6];
 partitions.innerText = flylng[lngID][7];
 signature.innerText = flylng[lngID][8];
 firmwarerevision.innerText = flylng[lngID][9];
 totalcylinders.innerText = flylng[lngID][10];
 totalsectors.innerText = flylng[lngID][11];
 totalheads.innerText = flylng[lngID][12];
 totaltracks.innerText = flylng[lngID][13];
 bytespersector.innerText = flylng[lngID][14];
 sectorspertrack.innerText = flylng[lngID][15];
 trackspercylinder.innerText = flylng[lngID][16];
 capabilities.innerText = flylng[lngID][17];
 closeflyouta.innerText = flylng[lngID][18];

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

 model_name.style.color = System.Gadget.Settings.read("colDiskInfo");
 serialnumber.style.color = System.Gadget.Settings.read("colDiskInfo");
 capacity.style.color = System.Gadget.Settings.read("colDiskInfo");
 mediatype.style.color = System.Gadget.Settings.read("colDiskInfo");
 interfacetype.style.color = System.Gadget.Settings.read("colDiskInfo");
 partitions.style.color = System.Gadget.Settings.read("colDiskInfo");
 signature.style.color = System.Gadget.Settings.read("colDiskInfo");
 firmwarerevision.style.color = System.Gadget.Settings.read("colDiskInfo");
 totalcylinders.style.color = System.Gadget.Settings.read("colDiskInfo");
 totalsectors.style.color = System.Gadget.Settings.read("colDiskInfo");
 totalheads.style.color = System.Gadget.Settings.read("colDiskInfo");
 totaltracks.style.color = System.Gadget.Settings.read("colDiskInfo");
 bytespersector.style.color = System.Gadget.Settings.read("colDiskInfo");
 sectorspertrack.style.color = System.Gadget.Settings.read("colDiskInfo");
 trackspercylinder.style.color = System.Gadget.Settings.read("colDiskInfo");
 capabilities.style.color = System.Gadget.Settings.read("colDiskInfo");

 model_data.style.color = System.Gadget.Settings.read("colDiskActivity");
 serialnumber_data.style.color = System.Gadget.Settings.read("colDiskActivity");
 capacity_data.style.color = System.Gadget.Settings.read("colDiskActivity");
 mediatype_data.style.color = System.Gadget.Settings.read("colDiskActivity");
 interfacetype_data.style.color = System.Gadget.Settings.read("colDiskActivity");
 partitions_data.style.color = System.Gadget.Settings.read("colDiskActivity");
 signature_data.style.color = System.Gadget.Settings.read("colDiskActivity");
 firmwarerevision_data.style.color = System.Gadget.Settings.read("colDiskActivity");
 totalcylinders_data.style.color = System.Gadget.Settings.read("colDiskActivity");
 totalsectors_data.style.color = System.Gadget.Settings.read("colDiskActivity");
 totalheads_data.style.color = System.Gadget.Settings.read("colDiskActivity");
 totaltracks_data.style.color = System.Gadget.Settings.read("colDiskActivity");
 bytespersector_data.style.color = System.Gadget.Settings.read("colDiskActivity");
 sectorspertrack_data.style.color = System.Gadget.Settings.read("colDiskActivity");
 trackspercylinder_data.style.color = System.Gadget.Settings.read("colDiskActivity");
 capabilities_data.style.color = System.Gadget.Settings.read("colDiskActivity");
 capabilitiesdescription.style.color = System.Gadget.Settings.read("colTitle");

 physdriveslist.style.color = System.Gadget.Settings.read("colDiskInfo");
 physdriveslist.style.background = System.Gadget.Settings.read("colBgColor");

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

 var size = System.Gadget.Settings.read("SetSize");
 //body
 background.src = "images/flyout.png";
 document.body.style.width = parseInt(290 * size);
 background.style.width = parseInt(290 * size);
 document.body.style.height = parseInt(292 * size);
 background.style.height = parseInt(292 * size);
 mainsquare.style.top = parseInt(5 * size); 
 mainsquare.style.left = parseInt(5 * size);  
 mainsquare.style.width =  parseInt(280 * size);
 mainsquare.style.height = parseInt(282 * size);
 mainsquare.style.background = System.Gadget.Settings.read("colBgColor");

 //top
 gtitle.style.top = parseInt(11 * size);
 physdrives.style.top = parseInt(8 * size);
 model_name.style.top = parseInt(32 * size);
 model_data.style.top = parseInt(32 * size);
 serialnumber.style.top = parseInt(46 * size);
 serialnumber_data.style.top = parseInt(46 * size);
 capacity.style.top = parseInt(60 * size);
 capacity_data.style.top = parseInt(60 * size);
 mediatype.style.top = parseInt(74 * size);
 mediatype_data.style.top = parseInt(74 * size);
 interfacetype.style.top = parseInt(88 * size);
 interfacetype_data.style.top = parseInt(88 * size);
 partitions.style.top = parseInt(102 * size);
 partitions_data.style.top = parseInt(102 * size);
 signature.style.top = parseInt(116 * size);
 signature_data.style.top = parseInt(116 * size);
 firmwarerevision.style.top = parseInt(130 * size);
 firmwarerevision_data.style.top = parseInt(130 * size);
 totalcylinders.style.top = parseInt(144 * size);
 totalcylinders_data.style.top = parseInt(144 * size);
 totalsectors.style.top = parseInt(158 * size);
 totalsectors_data.style.top = parseInt(158 * size);
 totalheads.style.top = parseInt(172 * size);
 totalheads_data.style.top = parseInt(172 * size);
 totaltracks.style.top = parseInt(186 * size);
 totaltracks_data.style.top = parseInt(186 * size);
 bytespersector.style.top = parseInt(200 * size);
 bytespersector_data.style.top = parseInt(200 * size);
 sectorspertrack.style.top = parseInt(214 * size);
 sectorspertrack_data.style.top = parseInt(214 * size);
 trackspercylinder.style.top = parseInt(228 * size);
 trackspercylinder_data.style.top = parseInt(228 * size);
 capabilities.style.top = parseInt(244 * size);
 capabilities_data.style.top = parseInt(244 * size);
 capabilitiesdescription.style.top = parseInt(258 * size);
 closeflyout.style.top = parseInt(272 * size);

 //width
 gtitle.style.width = parseInt(130 * size);
 physdriveslist.style.width = parseInt(144 * size);
 model_name.style.width = parseInt(tech[lngID][0] * size);
 model_data.style.width = tech[lngID][1];
 serialnumber.style.width = parseInt(32 * size);
 capacity.style.width = parseInt(tech[lngID][3] * size);
 mediatype.style.width = parseInt(120 * size);
 interfacetype.style.width = parseInt(120 * size);
 partitions.style.width = parseInt(120 * size);
 signature.style.width = parseInt(120 * size);
 firmwarerevision.style.width = parseInt(120 * size);
 totalcylinders.style.width = parseInt(120 * size);
 totalsectors.style.width = parseInt(120 * size);
 totalheads.style.width = parseInt(120 * size);
 totaltracks.style.width = parseInt(120 * size);
 bytespersector.style.width = parseInt(120 * size);
 sectorspertrack.style.width = parseInt(120 * size);
 trackspercylinder.style.width = parseInt(120 * size);
 capabilities.style.width = parseInt(120 * size);

 //left
 physdrives.style.left = parseInt(134 * size);
 model_data.style.left = parseInt(tech[lngID][2] * size);
 serialnumber_data.style.left = parseInt(34 * size);
 capacity_data.style.left = parseInt(tech[lngID][4] * size);
 mediatype_data.style.left = parseInt(124 * size);
 interfacetype_data.style.left = parseInt(124 * size);
 partitions_data.style.left = parseInt(124 * size);
 signature_data.style.left = parseInt(124 * size);
 firmwarerevision_data.style.left = parseInt(124 * size);
 totalcylinders_data.style.left = parseInt(124 * size);
 totalsectors_data.style.left = parseInt(124 * size);
 totalheads_data.style.left = parseInt(124 * size);
 totaltracks_data.style.left = parseInt(124 * size);
 bytespersector_data.style.left = parseInt(124 * size);
 sectorspertrack_data.style.left = parseInt(124 * size);
 trackspercylinder_data.style.left = parseInt(124 * size);
 capabilities_data.style.left = parseInt(124 * size);

 //font-size
 gtitle.style.fontSize = parseInt(11 * size);
 model_name.style.fontSize = parseInt(11 * size);
 model_data.style.fontSize = parseInt(11 * size);
 serialnumber.style.fontSize = parseInt(11 * size);
 serialnumber_data.style.fontSize = parseInt(11 * size);
 capacity.style.fontSize = parseInt(11 * size);
 capacity_data.style.fontSize = parseInt(11 * size);
 mediatype.style.fontSize = parseInt(11 * size);
 mediatype_data.style.fontSize = parseInt(11 * size);
 interfacetype.style.fontSize = parseInt(11 * size);
 interfacetype_data.style.fontSize = parseInt(11 * size);
 partitions.style.fontSize = parseInt(11 * size);
 partitions_data.style.fontSize = parseInt(11 * size);
 signature.style.fontSize = parseInt(11 * size);
 signature_data.style.fontSize = parseInt(11 * size);
 firmwarerevision.style.fontSize = parseInt(11 * size);
 firmwarerevision_data.style.fontSize = parseInt(11 * size);
 totalcylinders.style.fontSize = parseInt(11 * size);
 totalcylinders_data.style.fontSize = parseInt(11 * size);
 totalsectors.style.fontSize = parseInt(11 * size);
 totalsectors_data.style.fontSize = parseInt(11 * size);
 totalheads.style.fontSize = parseInt(11 * size);
 totalheads_data.style.fontSize = parseInt(11 * size);
 totaltracks.style.fontSize = parseInt(11 * size);
 totaltracks_data.style.fontSize = parseInt(11 * size);
 bytespersector.style.fontSize = parseInt(11 * size);
 bytespersector_data.style.fontSize = parseInt(11 * size);
 sectorspertrack.style.fontSize = parseInt(11 * size);
 sectorspertrack_data.style.fontSize = parseInt(11 * size);
 trackspercylinder.style.fontSize = parseInt(11 * size);
 trackspercylinder_data.style.fontSize = parseInt(11 * size);
 capabilities.style.fontSize = parseInt(11 * size);
 capabilities_data.style.fontSize = parseInt(12 * size);
 capabilitiesdescription.style.fontSize = parseInt(11 * size);
 closeflyout.style.fontSize = parseInt(10 * size);

 index = 0;
 var resultSet = service.ExecQuery("SELECT Name FROM Win32_PerfFormattedData_PerfDisk_PhysicalDisk");
 for (var i = 0; i < resultSet.Count; i++){
  if (resultSet.ItemIndex(i).Name == "_Total") continue;
  newOption = new Option(resultSet.ItemIndex(i).Name, resultSet.ItemIndex(i).Name.split(" ")[0]);
  physdriveslist.options[physdriveslist.length] = newOption;
  if (resultSet.ItemIndex(i).Name.indexOf(System.Gadget.document.parentWindow.driveforflyout) != -1) index = resultSet.ItemIndex(i).Name.split(" ")[0];
 }
 physdriveslist.value = index;
 getphysdrives(index);
}

function getphysdrives(num){
 var resultDriveInfo = service.ExecQuery("SELECT * FROM Win32_DiskDrive WHERE Index='" + num + "'");
 try{model_data.innerText = resultDriveInfo.ItemIndex(0).Model}
 catch (e){model_data.innerText = flylng[lngID][19]}
 try{
  var number = resultDriveInfo.ItemIndex(0).SerialNumber;
  if (number.substr(0,2) == "20"){
   var tmpnum = "";
   for (var i = 0; i < number.length; i += 2){
    tmpnum += String.fromCharCode(parseInt(number.substr(i,2),16));
   }
   number = "";
   for (var i = 0; i < tmpnum.length; i+=2){
    number += tmpnum.charAt(i+1) + tmpnum.charAt(i);
   }
  }
  serialnumber_data.innerText = number.removeblankbegin();
 }
 catch (e){serialnumber_data.innerText = flylng[lngID][19]}
 try{capacity_data.innerText = resultDriveInfo.ItemIndex(0).Size != null ? resultDriveInfo.ItemIndex(0).Size + flylng[lngID][21] + " (" + Math.round(resultDriveInfo.ItemIndex(0).Size / 1073741824) + flylng[lngID][20] + ")" : flylng[lngID][19]}
 catch (e){capacity_data.innerText = flylng[lngID][19]}
 try{mediatype_data.innerText = resultDriveInfo.ItemIndex(0).MediaType != null ? resultDriveInfo.ItemIndex(0).MediaType : flylng[lngID][19]}
 catch (e){mediatype_data.innerText = flylng[lngID][19]}
 try{interfacetype_data.innerText = resultDriveInfo.ItemIndex(0).InterfaceType}
 catch (e){interfacetype_data.innerText = flylng[lngID][19]}
 try{partitions_data.innerText = resultDriveInfo.ItemIndex(0).Partitions}
 catch (e){partitions_data.innerText = flylng[lngID][19]}
 try{signature_data.innerText = resultDriveInfo.ItemIndex(0).Signature}
 catch (e){signature_data.innerText = flylng[lngID][19]}
 try{firmwarerevision_data.innerText = resultDriveInfo.ItemIndex(0).FirmwareRevision}
 catch (e){firmwarerevision_data.innerText = flylng[lngID][19]}
 try{totalcylinders_data.innerText = resultDriveInfo.ItemIndex(0).TotalCylinders != null ? resultDriveInfo.ItemIndex(0).TotalCylinders : flylng[lngID][19]}
 catch (e){totalcylinders_data.innerText = flylng[lngID][19]}
 try{totalsectors_data.innerText = resultDriveInfo.ItemIndex(0).TotalSectors != null ? resultDriveInfo.ItemIndex(0).TotalSectors : flylng[lngID][19]}
 catch (e){totalsectors_data.innerText= flylng[lngID][19]}
 try{totalheads_data.innerText = resultDriveInfo.ItemIndex(0).TotalHeads != null ? resultDriveInfo.ItemIndex(0).TotalHeads : flylng[lngID][19]}
 catch (e){totalheads_data.innerText = flylng[lngID][19]}
 try{totaltracks_data.innerText = resultDriveInfo.ItemIndex(0).TotalTracks != null ? resultDriveInfo.ItemIndex(0).TotalTracks : flylng[lngID][19]}
 catch (e){totaltracks_data.innerText = flylng[lngID][19]}
 try{bytespersector_data.innerText = resultDriveInfo.ItemIndex(0).BytesPerSector != null ? resultDriveInfo.ItemIndex(0).BytesPerSector : flylng[lngID][19]}
 catch (e){bytespersector_data.innerText = flylng[lngID][19]}
 try{sectorspertrack_data.innerText = resultDriveInfo.ItemIndex(0).SectorsPerTrack != null ? resultDriveInfo.ItemIndex(0).SectorsPerTrack : flylng[lngID][19]}
 catch (e){sectorspertrack_data.innerText = flylng[lngID][19]}
 try{trackspercylinder_data.innerText = resultDriveInfo.ItemIndex(0).TracksPerCylinder != null ? resultDriveInfo.ItemIndex(0).TracksPerCylinder : flylng[lngID][19]}
 catch (e){trackspercylinder_data.innerText = flylng[lngID][19]}
 try{
  var i = 0;
  var htmlstring = "<span style='cursor:pointer' onmouseover='capabilitiesdescription.innerText=" + '"' +
  resultDriveInfo.ItemIndex(0).CapabilityDescriptions.toArray()[i] + '"' + "' onmouseout='capabilitiesdescription.innerText=" + '""' + "'>" +
  resultDriveInfo.ItemIndex(0).Capabilities.toArray()[i] + "</span>";
  i++;
  while (i < resultDriveInfo.ItemIndex(0).Capabilities.toArray().length){
   htmlstring += ", <span style='cursor:pointer' onmouseover='capabilitiesdescription.innerText=" + '"' +
   resultDriveInfo.ItemIndex(0).CapabilityDescriptions.toArray()[i] + '"' + "' onmouseout='capabilitiesdescription.innerText=" + '""' + "'>" +
   resultDriveInfo.ItemIndex(0).Capabilities.toArray()[i] + "</span>";
   i++;
  }
  capabilities_data.innerHTML = htmlstring;
 }
 catch (e){capabilities_data.innerText = flylng[lngID][19]}
}

function copyserial(){
 window.clipboardData.setData('Text',serialnumber_data.innerText);
}