var lang_hu = new Array("Operációs rendszer", "Szervízcsomag","Verziószám", "Operációs rendszer információk", 
	"processzor aktuális órajel", "Processzor információk", "terheltség", "szabad", 
	"Merevlemez információk", "üres", "CD/DVD információk", "nem ismert", "nem ismert", "ólom sav", 
	"nikkel kadmium", "nikkel metál hidrid", "lithium-ion", "cink levegő", "lithium polimer", 
	"állapot", "nincs információ", "óra", "Akkumulátor információk", "várható használati idő", "töltöttség", 
	"Hálózati információk", "kikapcsolva", "Aktív tűzfal profil", "ismeretlen", "Elektromos hálózatra csatlakoztatva", 
	"töltődik", "feltöltve", "Nem található eszköz", "Nem található optikai lemez", "Nem található akkumulátor", 
	"Nem található hálózati kapcsolat", "Nincs telepített szervízcsomag", "Memória információk", "Hálózati meghajtó információk", 
	"Hordozható eszköz információk", "Tárolóeszköz információk", " információk", "Rendszer információ", "Kattints az ikonra", 
	"részletes információkért", "Uptime információk", "bekapcsolástól eltelt idő", "nap");

var lang_en = new Array("Operating system", "Service pack", "Version number", "Operating System Information",
	"Processor actual timing", "Processor Information", "utilization", "free",
	"Hard Disk Information", "empty", "CD/DVD Information", "other", "unknown", "lead-acid",
	"nickel cadmium", "nickel metal hydride", "lithium-ion", "zinc air", "lithium polimer",
	"status", "no information", "hrs", "Battery Information", "estimated run time", "chargement", 
	"Network Information", "off", "Active firewall profile", "unknown", "Connected to electrical network", 
	"charging", "charged", "No device found", "No optical disk found", "No battery found", "No network connection found", 
	"Service pack not installed", "Memory Information", "Network drive Information", "Removable storage Information", 
	"Storage Information", " Information", "System Information", "Click icon to view", "detailed information", "Uptime Information",
	"system's current uptime", "days");

var lang_idx = 0;
var wbemFlagReturnImmediately = 0x10;
var wbemFlagForwardOnly = 0x20;
var objWMIService = GetObject("winmgmts:\\\\.\\root\\CIMV2");
var window_refresh = 30000;
var cpurefresh = 2000;
var cputimer = null;
var windowtimer = null;
var uptimer = null;
var textcolor = "white";
var shadow = true;
var horizontal = false;
var continous_uptime = false;
var cpubars = false;
var size = "normal";
var fonttype = "Segoe UI";

var hspace = 320;
var vspace = 10;
var rowspace = 15;
var headerrowspace = 20;
var ox1 = 50;
var ox2 = 60;
var oy1 = 5;
var barwidth = 200;

var compact = 0;

var fontsize = 12;			//normal=12, small=10, tiny=8
var headerfontsize = fontsize + 2;	
var barleft = 52;			//normal=52, small=32, tiny=22
var image_s = "_xs";		//normal="", small="_s", tiny="_xs"
var ratio = "1"; 			//normal=1, small=0.8, tiny=0.7

var background, x, maxx, y, curry, maxy, lang, cputime, cputimeshadow, uptime, uptimeshadow, cputimer, cpubar, cx;
var drives = new Array();
var driveindex = 0;

function compactString(str)
{
	//var retarr = str.split(lang[41]);
	//var retval = retarr[0] + "............................................................";
	var retval = "";
	if(str.length > 20)
		retval = str.substr(0, 20) + "...";
	else
		retval = str;
	return retval;
}

function setSize(size)
{
	switch(size)
	{
		case "normal":
			fontsize = 12;
			headerfontsize = fontsize + 2;	
			barleft = 52;
			image_s = "";
			ratio = 1;
		break;
		case "small":
			fontsize = 10;
			headerfontsize = fontsize + 2;	
			barleft = 32;
			image_s = "_s";
			ratio = 0.8;
		break;
		case "tiny":
			fontsize = 8;
			headerfontsize = fontsize + 2;	
			barleft = 22;
			image_s = "_xs";
			ratio = 0.7;		
		break;
	}
	
	if(compact)
	{
		headerfontsize = 10;
		image_s = "_xs";
		ratio = 0.8;
		ox1 = 30;
		hspace = 160;
	}
	else
	{
		hspace = 320;
		ox1 = 50;
		
		if(fonttype == "Courier New" || fonttype == "Georgia" || fonttype == "Tahoma" || fonttype == "Verdana")
			hspace = 360;
	}
}

function convertBytes(b)
{
	var i = 0, u = Array(' MB', ' GB', ' TB');

	while (b >= 1024 && (b / 1024) >= 1)
	{
		b /= 1024;
		i++;
	}

	return (Math.round(b * 100) / 100) + u[i];
}

function nextSection()
{
	if(horizontal)
	{
		x = x + (hspace * ratio);

		if(x > maxx) maxx = x;
		if(y > maxy) maxy = y;
		
		if(x + (hspace * ratio) > screen.width)
		{
			curry = maxy + (vspace * ratio);
			x = 0;
		}
		
		y = curry;
	}
	else
	{
		y = y + (vspace * ratio);
		maxy = y;
	}		
}

function showFlyout()
{
	var s = window.event.srcElement.getAttribute('section');	
	System.Gadget.Settings.write("flyoutsection", s);
	System.Gadget.Flyout.show = true;
}

function initInfo()
{
	canvas.addImageObject('img/computer'+ image_s +'.png',x,y);
	if(shadow) canvas.addTextObject(lang[42], fonttype, headerfontsize, 'black', x + 26, y + 6);
	canvas.addTextObject(lang[42], fonttype, headerfontsize, textcolor, x + 25, y + 5);
	y = y + (headerrowspace * ratio);

	var o = document.createElement('DIV');
	o.className = 'target';
	o.style.posTop = y - (headerrowspace * ratio);              
	o.setAttribute('section', 'ALL');
	o.onclick = showFlyout;
	targets.appendChild(o);
	
	y = y + 5;
	if(shadow) canvas.addTextObject(lang[43], fonttype, headerfontsize, 'black', x + 21, y + 6);
	canvas.addTextObject(lang[43], fonttype, headerfontsize, textcolor, x + 20, y + 5);
	y = y + (headerrowspace * ratio)
	if(shadow) canvas.addTextObject(lang[44], fonttype, headerfontsize, 'black', x + 11, y + 6);
	canvas.addTextObject(lang[44], fonttype, headerfontsize, textcolor, x + 10, y + 5);
}

function initOS(minimal)
{	
	var osVersion = '';
	var csdVersion = '';
	var version = '';
	var buildtype = '';
	var osItems = objWMIService.ExecQuery("SELECT Caption,CSDVersion,Version,BuildType FROM Win32_OperatingSystem");

	var osEnum = new Enumerator(osItems);
	for (; !osEnum.atEnd(); osEnum.moveNext()) 
	{
		var osItem = osEnum.item();
		osVersion = osItem.Caption;
		csdVersion = osItem.CSDVersion;
		if(csdVersion == null)
			csdVersion = lang[36];
		version = osItem.Version;
		buildtype = osItem.BuildType;
	}

	canvas.addImageObject('img/os'+ image_s +'.png',x,y);
	caption = lang[3];
	if(minimal) caption = compactString(lang[3]);
	if(shadow) canvas.addTextObject(caption, fonttype, headerfontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
	canvas.addTextObject(caption, fonttype, headerfontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
	
	y = y + (headerrowspace * ratio);

	if(minimal > 0)
	{
		var o = document.createElement('DIV');
		o.className = 'target';
		o.style.posTop = y - (headerrowspace * ratio);              
		o.setAttribute('section', 'OS');
		o.onclick = showFlyout;
		targets.appendChild(o);
	}
	else
	{
		if(shadow) canvas.addTextObject(lang[0], fonttype, fontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
		canvas.addTextObject(lang[0], fonttype, fontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
		y = y + (rowspace * ratio);
		if(shadow) canvas.addTextObject(osVersion, fonttype, fontsize, 'black', x + (ox2 * ratio) + 1, y + (oy1 * ratio) + 1);
		canvas.addTextObject(osVersion, fonttype, fontsize, textcolor, x + (ox2 * ratio), y + (oy1 * ratio));
		y = y + (rowspace * ratio);
	
		if(shadow) canvas.addTextObject(lang[1], fonttype, fontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
		canvas.addTextObject(lang[1], fonttype, fontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
		y = y + (rowspace * ratio);
		if(shadow) canvas.addTextObject(csdVersion, fonttype, fontsize, 'black', x + (ox2 * ratio) + 1, y + (oy1 * ratio) + 1);
		canvas.addTextObject(csdVersion, fonttype, fontsize, textcolor, x + (ox2 * ratio), y + (oy1 * ratio));
		y = y + (rowspace * ratio);
	
		if(shadow) canvas.addTextObject(lang[2], fonttype, fontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
		canvas.addTextObject(lang[2], fonttype, fontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
		y = y + (rowspace * ratio);
		if(shadow) canvas.addTextObject(version + '  ' + buildtype, fonttype, fontsize, 'black', x + (ox2 * ratio) + 1, y + (oy1 * ratio) + 1);
		canvas.addTextObject(version + '  ' + buildtype, fonttype, fontsize, textcolor, x + (ox2 * ratio), y + (oy1 * ratio));
		y = y + (rowspace * ratio);
	}
	
	nextSection();
}

function initUptime(minimal)
{
	canvas.addImageObject('img/uptime'+ image_s +'.png',x,y);
	caption = lang[45];
	if(minimal) caption = compactString(lang[45]);
	if(shadow) canvas.addTextObject(caption, fonttype, headerfontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
	canvas.addTextObject(caption, fonttype, headerfontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
	y = y + (headerrowspace * ratio);
	window.clearTimeout(uptimer);

	if(minimal > 0)
	{
		var o = document.createElement('DIV');
		o.className = 'target';
		o.style.posTop = y - (headerrowspace * ratio);              
		o.setAttribute('section', 'UPTIME');
		o.onclick = showFlyout;
		targets.appendChild(o);
	}
	else
	{
		if(shadow) uptimeshadow = canvas.addTextObject(lang[46], fonttype, fontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
		uptime = canvas.addTextObject(lang[46], fonttype, fontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
		y = y + (rowspace * ratio);
							
		refreshUptime();	
	}
	
	nextSection();
}

function refreshUptime()
{
	var osItems = objWMIService.ExecQuery("SELECT LastBootUpTime FROM Win32_OperatingSystem");
	var osEnum = new Enumerator(osItems);
	var boottime = "";
	for (; !osEnum.atEnd(); osEnum.moveNext()) 
	{
		var osItem = osEnum.item();
		var boottime = osItem.LastBootUpTime;
	}

	var Y = boottime.substr(0,4);
	var M = boottime.substr(4,2);
	M = M - 1; //because months are 0-11
	var D = boottime.substr(6,2);
	var H = boottime.substr(8,2);
	var I = boottime.substr(10,2);
	var S = boottime.substr(12,2);
	var osBootTime = new Date(Y,M,D,H,I,S);
	var currentTime = new Date();
	var seconds = (currentTime - osBootTime) / 1000;
	var days = Math.floor(seconds / 60 / 60 / 24);
	seconds = seconds - (days*24*60*60);
	var hours = Math.floor(seconds / 60 / 60);
	seconds = seconds - (hours*60*60);
	var minutes = Math.floor(seconds / 60);
	seconds = seconds - (minutes*60);
	seconds = Math.floor(seconds);
	if(minutes < 10) minutes = "0" + minutes;
	if(seconds < 10) seconds = "0" + seconds;	
	
	var uptimestr = lang[46] + ": " + days + " " + lang[47] + ", " + hours + ":" + minutes + ":" + seconds;
	uptime.value = uptimestr;	
	uptime.width = 0;
	uptime.height = 0;
	if(shadow)
	{			
		uptimeshadow.value = uptimestr;
		uptimeshadow.width = 0;
		uptimeshadow.height = 0;
	}
	
	if(continous_uptime)
		uptimer = window.setTimeout(refreshUptime, 1000);
}

function initCPU(minimal)
{
	var oCPU = System.Machine.CPUs;
	cputime = new Array();
	cputimeshadow = new Array();
	cpubar = new Array();
	canvas.addImageObject('img/computer'+ image_s +'.png',x,y);
	caption = lang[5];
	if(minimal) caption = compactString(lang[5]);
	if(shadow) canvas.addTextObject(caption, fonttype, headerfontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
	canvas.addTextObject(caption, fonttype, headerfontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
	y = y + (headerrowspace * ratio);
	window.clearTimeout(cputimer);

	if(minimal > 0)
	{
		var o = document.createElement('DIV');
		o.className = 'target';
		o.style.posTop = y - (headerrowspace * ratio);              
		o.setAttribute('section', 'CPU');
		o.onclick = showFlyout;
		targets.appendChild(o);
	}
	else
	{
		var cpuItems = objWMIService.ExecQuery("SELECT Name,CurrentClockSpeed,NumberOfCores FROM Win32_Processor");
		var cpuEnum = new Enumerator(cpuItems);
		var index = 0;
		var cores = 0;
		for (; !cpuEnum.atEnd(); cpuEnum.moveNext()) 
		{
			var cpuItem = cpuEnum.item();
			if(shadow) canvas.addTextObject(cpuItem.Name, fonttype, fontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
			canvas.addTextObject(cpuItem.Name, fonttype, fontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
			y += (rowspace * ratio);
				
			if(shadow) canvas.addTextObject(lang[4] + ' ' + cpuItem.CurrentClockSpeed + ' Mhz', fonttype, fontsize, 'black', x + (ox2 * ratio) + 1, y + (oy1 * ratio) + 1);
			canvas.addTextObject(lang[4] + ' ' + cpuItem.CurrentClockSpeed + ' Mhz', fonttype, fontsize, textcolor, x + (ox2 * ratio), y + (oy1 * ratio));
			y += (rowspace * ratio);	
	
			for(i=1; i<=cpuItem.NumberOfCores; i++)
			{
				var cpu = oCPU.item(cores);
				if(shadow) canvas.addTextObject('CPU' + i + ' ' + lang[6] + ' ', fonttype, fontsize, 'black', x + (ox2 * ratio) + 1, y + (oy1 * ratio) + 1);
				canvas.addTextObject('CPU' + i + ' ' + lang[6] + ' ', fonttype, fontsize, textcolor, x + (ox2 * ratio), y + (oy1 * ratio));
				var usagep = Math.floor(cpu.usagePercentage);
				if(usagep > 100) usagep = 0;
				if(usagep < 0) usagep = 0;			
				if(shadow) cputimeshadow[cores] = canvas.addTextObject(usagep + '%', fonttype, fontsize, 'black', x + (160 * ratio) + 1, y + (oy1 * ratio) + 1);
				cputime[cores] = canvas.addTextObject(usagep + '%', fonttype, fontsize, textcolor, x + (160 * ratio), y + (oy1 * ratio));

				if(cpubars)
				{
					var s = canvas.addImageObject('img/grayline_xs.png', x + (160 * ratio) + (18 * ratio * ratio * ratio * ratio), y + (2 * oy1 * ratio));
					s.width = (70 * ratio);
					s.height = (6 * ratio);
					cpubar[cores] = canvas.addImageObject('img/' + (usagep < 70 ? 'green': (usagep < 90 ? 'yellow': 'red')) + 'line_xs.png', x + (160 * ratio) + (18 * ratio * ratio * ratio * ratio), y + (2 * oy1 * ratio));
					var width = Math.floor((usagep / 100) * (70 * ratio));
					if(width < 1) width = 1;
					cpubar[cores].width = width;
					cpubar[cores].height = (6 * ratio); 
					cpubar[cores].left = x + (160 * ratio) + (18 * ratio * ratio * ratio * ratio) - Math.floor((((70 * ratio) - width) / 2));
					cx = x;				
				}

				cores ++;
				y += (rowspace * ratio);
			}	
			index++;		
		}
					
		if(cpurefresh != window_refresh)
			refreshCPU();	
	}
	
	nextSection();	
}

function refreshCPU()
{
	var oCPU = System.Machine.CPUs;
	for(var i=0; i<cputime.length; i++)
	{
		var cpu = oCPU.item(i);
		var usagep = Math.floor(cpu.usagePercentage);
		if(usagep > 100) usagep = 0;
		if(usagep < 0) usagep = 0;			
		cputime[i].value = usagep + '%';	
		cputime[i].width = 0;
		cputime[i].height = 0;
		if(shadow)
		{			
			cputimeshadow[i].value = usagep + '%';		
			cputimeshadow[i].width = 0;
			cputimeshadow[i].height = 0;
		}
		if(cpubars)
		{
			cpubar[i].src = 'img/' + (usagep < 70 ? 'green': (usagep < 90 ? 'yellow': 'red')) + 'line_xs.png';			
			var width = Math.floor((usagep / 100) * (70 * ratio));
			if(width < 1) width = 1;
			cpubar[i].width = width; 
			cpubar[i].left = cx + (160 * ratio) + (18 * ratio * ratio * ratio * ratio) - Math.floor((((70 * ratio) - width) / 2));				
		}
	}	
	cputimer = window.setTimeout(refreshCPU, cpurefresh);
}

function initRAM(minimal)
{
	canvas.addImageObject('img/memory'+ image_s +'.png', x, y);
	caption = lang[37];
	if(minimal) caption = compactString(lang[37]);	
	if(shadow) canvas.addTextObject(caption, fonttype, headerfontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
	canvas.addTextObject(caption, fonttype, headerfontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
	y = y + (headerrowspace * ratio);	

	if(minimal > 0)
	{
		var o = document.createElement('DIV');
		o.className = 'target';
		o.style.posTop = y - (headerrowspace * ratio);              
		o.setAttribute('section', 'RAM');
		o.onclick = showFlyout;
		targets.appendChild(o);
	}
	else
	{
		total = System.Machine.totalMemory;
		free = System.Machine.availableMemory;
		u = Math.ceil((free / total) * 100);
		if(shadow) canvas.addTextObject(total + 'Mb/' + free + 'Mb  ' + lang[7] + ' ' + u + '%', fonttype, fontsize, 'black', x + (ox2 * ratio) + 1, y + (oy1 * ratio) + 1);
		canvas.addTextObject(total + 'Mb/' + free + 'Mb  ' + lang[7] + ' ' + u + '%', fonttype, fontsize, textcolor, x + (ox2 * ratio), y + (oy1 * ratio));
		y += (rowspace * ratio);
		
		var s = canvas.addImageObject('img/grayline'+ image_s +'.png', x + (ox2 * ratio) + barleft, y + (oy1 * 2 * ratio));
		s.width = (barwidth * ratio);
		var m = canvas.addImageObject('img/' + (u > 10 ? 'green': (u > 5 ? 'yellow': 'red')) + 'line'+ image_s +'.png', x + (ox2 * ratio), y + (oy1 * 2 * ratio));
		var width = Math.floor(((100 - u) * (barwidth * ratio) / 100));
		if(width < 1) width = 1;
		m.width = width;
		m.left = x + (ox2 * ratio) + barleft - Math.floor((((barwidth * ratio) - m.width) / 2));		
		
		y += (rowspace * ratio);
	}
	
	nextSection();
}

function openDrive()
{
	var d = window.event.srcElement.getAttribute('drive');    
	System.Shell.execute(d);
}

function highlightDrive()
{
	var index = window.event.srcElement.getAttribute('index');
	drives[index].addGlow(textcolor, 1, 50);	
}

function ahighlightDrive()
{
	var index = window.event.srcElement.getAttribute('index');
	drives[index].addGlow(textcolor, 0, 50);	
}

function initHDD(drivetype, minimal)
{
	caption = "";
	section = "STORAGE";
	if(drivetype == 0)
	{
		canvas.addImageObject('img/hdd'+ image_s +'.png',x,y);
		caption = lang[40];
		if(minimal) caption = compactString(lang[40]);
		section = "STORAGE";
	}
	else if(drivetype == 3)
	{
		canvas.addImageObject('img/hdd'+ image_s +'.png',x,y);
		caption = lang[8];		
		if(minimal) caption = compactString(lang[8]);
		section = "HDD";
	}
	else if(drivetype == 2)
	{
		canvas.addImageObject('img/removable'+ image_s +'.png',x,y);
		caption = lang[39];		
		if(minimal) caption = compactString(lang[39]);	
		section = "REMOVABLE";
	}
	else if(drivetype == 4)
	{
		canvas.addImageObject('img/netdrive'+ image_s +'.png',x,y);
		caption = lang[38];
		if(minimal) caption = compactString(lang[38]);			
		section = "NETDRIVE";
	}
		
	if(shadow) canvas.addTextObject(caption, fonttype, headerfontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
	canvas.addTextObject(caption, fonttype, headerfontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
	y = y + (headerrowspace * ratio);	

	if(minimal > 0)
	{
		var o = document.createElement('DIV');
		o.className = 'target';
		o.style.posTop = y - (headerrowspace * ratio);              
		o.setAttribute('section', section);
		o.onclick = showFlyout;
		targets.appendChild(o);
	}
	else
	{		
		var hddItems;		
		if(drivetype == 0) hddItems = objWMIService.ExecQuery("select VolumeName,FreeSpace,Size,FileSystem,DeviceID from Win32_LogicalDisk where DriveType < 5");
		else hddItems = objWMIService.ExecQuery("select VolumeName,FreeSpace,Size,FileSystem,DeviceID from Win32_LogicalDisk where DriveType=" + drivetype);
		var hddEnum = new Enumerator(hddItems);
		var index = 0;
		for (; !hddEnum.atEnd(); hddEnum.moveNext()) 
		{
			var hddItem = hddEnum.item();
			var volumename = '';
			if(hddItem.VolumeName != '')
				volumename = ' (' + hddItem.VolumeName + ')';
			var free = Math.ceil(hddItem.FreeSpace/1024/1024);
			var total = Math.ceil(hddItem.Size/1024/1024);
			var f = Math.round(hddItem.FreeSpace / hddItem.Size * 100);
	
			var  u = (100 - f);
			
			//"\u2776 "
			if(shadow) canvas.addTextObject(hddItem.DeviceID + volumename + '  [' + hddItem.FileSystem + ']', fonttype, fontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
			drives[driveindex] = canvas.addTextObject(hddItem.DeviceID + volumename + '  [' + hddItem.FileSystem + ']', fonttype, fontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
			y += (rowspace * ratio);
	
			if(hddItem.FreeSpace != null)
			{
				if(shadow) canvas.addTextObject(total + 'Mb/' + free + 'Mb  ' + lang[7] + ' ' + (100 - u) + '%', fonttype, fontsize, 'black', x + (ox2 * ratio) + 1, y + (oy1 * ratio) + 1);
				canvas.addTextObject(total + 'Mb/' + free + 'Mb  ' + lang[7] + ' ' + (100 - u) + '%', fonttype, fontsize, textcolor, x + (ox2 * ratio), y + (oy1 * ratio));
				y += (rowspace * ratio);
			
				var s = canvas.addImageObject('img/grayline'+ image_s +'.png', x + (ox2 * ratio) + barleft, y + (oy1 * 2 * ratio));
				s.width = (barwidth * ratio);
				var m = canvas.addImageObject('img/' + (u < 90 ? 'green': (u < 98 ? 'yellow': 'red')) + 'line'+ image_s +'.png', x + (ox2 * ratio), y + (oy1 * 2 * ratio));
				var width = Math.floor((u * (barwidth * ratio) / 100));
				if(width < 1) width = 1;
				m.width = width;
				m.left = x + (ox2 * ratio) + barleft - Math.floor((((barwidth * ratio) - m.width) / 2));		

				var o = document.createElement('DIV');
				o.className = 'drive';			
				o.style.posTop = y + (oy1 * ratio);  
				o.style.left = x + (ox2 * ratio); 
				o.style.height = 5;
				o.style.width = barwidth * ratio;          
				o.setAttribute('drive', hddItem.DeviceID);
				o.setAttribute('index', driveindex);
				o.onclick = openDrive;
				o.onmouseover = highlightDrive;
				o.onmouseout = ahighlightDrive;
				targets.appendChild(o);
												
				y += (rowspace * ratio);
			}
	
			driveindex++;
			index++;
		}
		
		if(index == 0)
		{
			if(shadow) canvas.addTextObject(lang[32], fonttype, fontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
			canvas.addTextObject(lang[32], fonttype, fontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
			y += (rowspace * ratio);
		}
	}	
	
	nextSection();
}

function initDVD(minimal)
{
	canvas.addImageObject('img/cd'+ image_s +'.png',x,y);
	caption = lang[10];
	if(minimal) caption = compactString(lang[10]);		
	if(shadow) canvas.addTextObject(caption, fonttype, headerfontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
	canvas.addTextObject(caption, fonttype, headerfontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
	y = y + (headerrowspace * ratio);	
	
	if(minimal > 0)
	{
		var o = document.createElement('DIV');
		o.className = 'target';
		o.style.posTop = y - (headerrowspace * ratio);              
		o.setAttribute('section', 'DVD');
		o.onclick = showFlyout;
		targets.appendChild(o);
	}
	else
	{
		var dvdItems = objWMIService.ExecQuery("select 	Capabilities,Caption,DeviceID,Drive,MediaType,Size,VolumeName from Win32_CDROMDrive");
		var dvdEnum = new Enumerator(dvdItems);
		var index = 0;
		for (; !dvdEnum.atEnd(); dvdEnum.moveNext()) 
		{
			var dvdItem = dvdEnum.item();
			var ddItems = objWMIService.ExecQuery("select Freespace from Win32_LogicalDisk WHERE DeviceID='" + dvdItem.Drive + "'");
			var ddEnum = new Enumerator(ddItems);
			var ddItem = ddEnum.item();						
			var free = Math.ceil(ddItem.FreeSpace/1024/1024);
			var total = Math.ceil(dvdItem.Size/1024/1024);
			var f = Math.round(ddItem.FreeSpace / dvdItem.Size * 100);
			var vname = dvdItem.VolumeName;
			if(vname == null)
				vname = lang[9];
	
			var  u = (100 - f);
	
			//if(shadow) canvas.addTextObject(dvdItem.Drive, fonttype, fontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
			//canvas.addTextObject(dvdItem.Drive, fonttype, fontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
			if(shadow) canvas.addTextObject(dvdItem.Drive + ' (' + dvdItem.Caption + ')', fonttype, fontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
			drives[driveindex] = canvas.addTextObject(dvdItem.Drive + ' (' + dvdItem.Caption + ')', fonttype, fontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
			y += (rowspace * ratio);	
			if(shadow) canvas.addTextObject(dvdItem.MediaType, fonttype, fontsize, 'black', x + (ox2 * ratio) + 1, y + (oy1 * ratio) + 1);
			canvas.addTextObject(dvdItem.MediaType, fonttype, fontsize, textcolor, x + (ox2 * ratio), y + (oy1 * ratio));		
			if(shadow) canvas.addTextObject(vname, fonttype, fontsize, 'black', x + (ox2 * ratio) + 1 + 80, y + (oy1 * ratio) + 1);
			canvas.addTextObject(vname, fonttype, fontsize, textcolor, x + (ox2 * ratio) + 80, y + (oy1 * ratio));
			y += (rowspace * ratio);	

			if(ddItem.FreeSpace != null)
			{
				if(shadow) canvas.addTextObject(total + 'Mb/' + free + 'Mb  ' + lang[7] + ' ' + (100 - u) + '%', fonttype, fontsize, 'black', x + (ox2 * ratio) + 1, y + (oy1 * ratio) + 1);
				canvas.addTextObject(total + 'Mb/' + free + 'Mb  ' + lang[7] + ' ' + (100 - u) + '%', fonttype, fontsize, textcolor, x + (ox2 * ratio), y + (oy1 * ratio));
				y += (rowspace * ratio);
			
				var s = canvas.addImageObject('img/grayline'+ image_s +'.png', x + (ox2 * ratio) + barleft, y + (oy1 * 2 * ratio));
				s.width = (barwidth * ratio);
				var m = canvas.addImageObject('img/' + (u < 90 ? 'green': (u < 98 ? 'yellow': 'red')) + 'line'+ image_s +'.png', x + (ox2 * ratio), y + (oy1 * 2 * ratio));
				var width = Math.floor((u * (barwidth * ratio) / 100));
				if(width < 1) width = 1;
				m.width = width;
				m.left = x + (ox2 * ratio) + barleft - Math.floor((((barwidth * ratio) - m.width) / 2));		

				var o = document.createElement('DIV');
				o.className = 'drive';			
				o.style.posTop = y + (oy1 * ratio);  
				o.style.left = x + (ox2 * ratio); 
				o.style.height = 5;
				o.style.width = barwidth * ratio;          
				o.setAttribute('drive', dvdItem.Drive);
				o.setAttribute('index', driveindex);
				o.onclick = openDrive;
				o.onmouseover = highlightDrive;
				o.onmouseout = ahighlightDrive;
				targets.appendChild(o);
												
				y += (rowspace * ratio);
			}

			driveindex++;
			index++;
		}
		
		if(index == 0)
		{
			if(shadow) canvas.addTextObject(lang[33], fonttype, fontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
			canvas.addTextObject(lang[33], fonttype, fontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
			y += (rowspace * ratio);
		}
	}	
	
	nextSection();
}

function initBattery(minimal)
{
	canvas.addImageObject('img/battery'+ image_s +'.png',x,y);
	caption = lang[22];
	if(minimal) caption = compactString(lang[22]);		
	if(shadow) canvas.addTextObject(caption, fonttype, headerfontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
	canvas.addTextObject(caption, fonttype, headerfontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
	y = y + (headerrowspace * ratio);	

	if(minimal > 0)
	{
		var o = document.createElement('DIV');
		o.className = 'target';
		o.style.posTop = y - (headerrowspace * ratio);              
		o.setAttribute('section', 'BATTERY');
		o.onclick = showFlyout;
		targets.appendChild(o);
	}
	else
	{
		var batItems = objWMIService.ExecQuery("select Caption,Chemistry,EstimatedRunTime,EstimatedChargeRemaining from Win32_Battery");
		var batEnum = new Enumerator(batItems);
		var index = 0;
		for (; !batEnum.atEnd(); batEnum.moveNext()) 
		{
			var batItem = batEnum.item();
			var chemistry = lang[12]
			switch(batItem.Chemistry)
			{
			case 1:
				chemistry = lang[11];
				break;
			case 2:
				chemistry = lang[12];
				break;
			case 3:
				chemistry = lang[13];
				break;
			case 4:
				chemistry = lang[14];
				break;
			case 5:
				chemistry = lang[15];
				break;
			case 6:
				chemistry = lang[16];
				break;
			case 7:
				chemistry = lang[17];
				break;
			case 8:
				chemistry = lang[18];
				break;
			}
	
	
			if(shadow) canvas.addTextObject(batItem.Caption + ' (' + chemistry + ')', fonttype, fontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
			canvas.addTextObject(batItem.Caption + ' (' + chemistry + ')', fonttype, fontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
			y += (rowspace * ratio);
			var runtime = batItem.EstimatedRunTime;
			if(runtime == null || runtime > 24 * 60)
				runtime = "N/A";
			else
			{
				var hours = Math.floor(runtime/60);
				var minutes = runtime - (Math.floor(runtime/60)*60);
				if(minutes < 10) minutes = "0" + minutes;
				runtime = hours + ":" + minutes;
			}
			if(!System.Machine.PowerStatus.isPowerLineConnected)
			{
				if(shadow) canvas.addTextObject(lang[24] + ' ' + batItem.EstimatedChargeRemaining + '% ' + lang[23] + ' ' + runtime + ' ' + lang[21], fonttype, fontsize, 'black', x + (ox2 * ratio) + 1, y + (oy1 * ratio) + 1);
				canvas.addTextObject(lang[24] + ' ' + batItem.EstimatedChargeRemaining + '% ' + lang[23] + ' ' + runtime + ' ' + lang[21], fonttype, fontsize, textcolor, x + (ox2 * ratio), y + (oy1 * ratio));
			}
			else
			{
				var charging = "";
				if(System.Machine.PowerStatus.isBatteryCharging)
					charging = "(" + lang[30] + ")";
				else
					charging = "(" + lang[31] + ")";
				if(shadow) canvas.addTextObject(lang[29] + " " + charging, fonttype, fontsize, 'black', x + (ox2 * ratio) + 1, y + (oy1 * ratio) + 1);
				canvas.addTextObject(lang[29] + " " + charging, fonttype, fontsize, textcolor, x + (ox2 * ratio), y + (oy1 * ratio));
			}
			y += (rowspace * ratio);
		
			var s = canvas.addImageObject('img/grayline'+ image_s +'.png', x + (ox2 * ratio) + barleft, y + (oy1 * 2 * ratio));
			s.width = (barwidth * ratio);
			var m = canvas.addImageObject('img/' + (batItem.EstimatedChargeRemaining > 50 ? 'green': (batItem.EstimatedChargeRemaining > 20 ? 'yellow': 'red')) + 'line'+ image_s +'.png', x + (ox2 * ratio), y + (oy1 * 2 * ratio));
			var width = Math.floor((batItem.EstimatedChargeRemaining * (barwidth * ratio) / 100));
			if(width < 1) width = 1;
			m.width = width;
			m.left = x + (ox2 * ratio) + barleft - Math.floor((((barwidth * ratio) - m.width) / 2));		
	
			y += (rowspace * ratio);
	
			index++;
		}
		
		if(index == 0)
		{
			if(shadow) canvas.addTextObject(lang[34], fonttype, fontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
			canvas.addTextObject(lang[34], fonttype, fontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
			y += (rowspace * ratio);
		}
	}
	
	nextSection();
}

function initNetwork(minimal)
{	
	canvas.addImageObject('img/network'+ image_s +'.png',x,y);
	caption = lang[25];
	if(minimal) caption = compactString(lang[25]);		
	if(shadow) canvas.addTextObject(caption, fonttype, headerfontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
	canvas.addTextObject(caption, fonttype, headerfontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
	y += (headerrowspace * ratio);
	
	if(minimal > 0)
	{
		var o = document.createElement('DIV');
		o.className = 'target';
		o.style.posTop = y - (headerrowspace * ratio);              
		o.setAttribute('section', 'NETWORK');
		o.onclick = showFlyout;
		targets.appendChild(o);
	}
	else
	{
		var fwpolicy = new ActiveXObject("HNetCfg.FwPolicy2");
		var fwpolicytxt = lang[28];
		switch(fwpolicy.CurrentProfileTypes)
		{
			case 1:
				fwpolicytxt = "domain";
				break;
			case 2:
				fwpolicytxt = "private";
				break;
			case 4:
				fwpolicytxt = "public";
				break;
			default:
				fwpolicytxt = lang[28];
				break;
		}
		if(shadow) canvas.addTextObject(lang[27] + ": " + fwpolicytxt, fonttype, fontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
		canvas.addTextObject(lang[27] + ": " + fwpolicytxt, fonttype, fontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
		y += (rowspace * ratio);	
	
		var netItems = objWMIService.ExecQuery("select Caption,MACAddress,IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=true");
		var netEnum = new Enumerator(netItems);
		var index = 0;
		for (; !netEnum.atEnd(); netEnum.moveNext()) 
		{
	
			var netItem = netEnum.item();
			if(shadow) canvas.addTextObject(netItem.Caption.substring(11, netItem.Caption.length), fonttype, fontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
			canvas.addTextObject(netItem.Caption.substring(11, netItem.Caption.length), fonttype, fontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
			y += (rowspace * ratio);
			if(shadow) canvas.addTextObject('MAC ' + netItem.MACAddress, fonttype, fontsize, 'black', x + (ox2 * ratio) + 1, y + (oy1 * ratio) + 1);
			canvas.addTextObject('MAC ' + netItem.MACAddress, fonttype, fontsize, textcolor, x + (ox2 * ratio), y + (oy1 * ratio));
			y += (rowspace * ratio);
	
			var ips = netItem.IPAddress.toArray();
			var ipstr = "";
			for(var g=0; g<ips.length; g++)
			{
				ipstr += ips[g];
				if(g < ips.length - 1)
					ipstr += ", ";
			}
			if(shadow) canvas.addTextObject('IP ' + ipstr, fonttype, fontsize, 'black', x + (ox2 * ratio) + 1, y + (oy1 * ratio) + 1);
			canvas.addTextObject('IP ' + ipstr, fonttype, fontsize, textcolor, x + (ox2 * ratio), y + (oy1 * ratio));
			y += (rowspace * ratio);
			index++;
		}
		
		if(index == 0)
		{
			if(shadow) canvas.addTextObject(lang[35], fonttype, fontsize, 'black', x + (ox1 * ratio) + 1, y + (oy1 * ratio) + 1);
			canvas.addTextObject(lang[35], fonttype, fontsize, textcolor, x + (ox1 * ratio), y + (oy1 * ratio));
			y += (rowspace * ratio);
		}	
	}	
	
	nextSection();
}

function onLoadFlyout()
{
	loadSettings();
	compact = 0;
	setSize(size);

	var s = System.Gadget.Settings.read("flyoutsection");
	
	switch(s)
	{
		case "OS":
			initOS(false);
		break;
		case "CPU":
			initCPU(false);
		break;
		case "UPTIME":
			initUptime(false);
		break;
		case "RAM":
			initRAM(false);
		break;
		case "STORAGE":
			initHDD(0, false);
		break;
		case "HDD":
			initHDD(3, false);
		break;
		case "REMOVABLE":
			initHDD(2, false);
		break;
		case "NETDRIVE":
			initHDD(4, false);
		break;
		case "DVD":
			initDVD(false);
		break;
		case "BATTERY":
			initBattery(false);
		break;
		case "NETWORK":
			initNetwork(false);
		break;
		case "ALL":
			showSections(0);
		break;		
		default:
			compact = 0;
			initGadget();
		break;		
	}
		
	canvas.style.height = maxy + (vspace * ratio);
	document.body.style.posHeight = maxy + (vspace * ratio);
	if (maxy + (vspace * ratio) < 57) canvas.style.height = 57;
	//if (x < 320) x = 320;
	canvas.style.width = maxx + (hspace * ratio);
	document.body.style.posWidth = maxx + (hspace * ratio);
}

function removeChildren(obj)
{
	var x = null;
	do
	{
		x = obj.firstChild;
		if(x != null)
			obj.removeChild(x);
	}while(x != null);
}

function loadSettings()
{
	y = 0;
	maxy = 0;
	maxx = 0;
	curry = 0;
	x = 0;
	driveindex = 0;	
	lang_idx = System.Gadget.Settings.read("lang_idx");
	window_refresh = System.Gadget.Settings.read("timer_val") * 1000;	
	cpurefresh = System.Gadget.Settings.read("cputimer_val") * 1000;
	textcolor = System.Gadget.Settings.read("color");
	shadow = System.Gadget.Settings.read("shadow");
	horizontal = System.Gadget.Settings.read("orientation");
	compact = System.Gadget.Settings.read("mode");
	if(compact > 0) horizontal = false;
	size = System.Gadget.Settings.read("size");
	removeChildren(targets);//targets.innerHtml = '';
	continous_uptime = System.Gadget.Settings.read("upcontinous");
	cpubars = System.Gadget.Settings.read("cpubars");
	fonttype = System.Gadget.Settings.read("fonttype");
	
	if(cpurefresh <= 0)
		cpurefresh = window_refresh;

	if(lang_idx == 0)
		lang = lang_en;
	else
		lang = lang_hu;		
}

function showSections(comp)
{
	var isone = false;
	if(System.Gadget.Settings.read("computer"))
	{
		isone = true;
		initOS(comp);		
	}
	if(System.Gadget.Settings.read("uptime"))
	{
		isone = true;
		initUptime(comp);
	}
	if(System.Gadget.Settings.read("processor"))
	{
		isone = true;
		initCPU(comp);		
	}
	if(System.Gadget.Settings.read("ram"))
	{
		isone = true;
		initRAM(comp);		
	}
	if(System.Gadget.Settings.read("storage"))
	{
		isone = true;
		initHDD(0, comp);
	}
	else
	{
		if(System.Gadget.Settings.read("hdd"))
		{
			isone = true;
			initHDD(3, comp);		
		}
		if(System.Gadget.Settings.read("netdrive"))
		{
			isone = true;
			initHDD(4, comp);		
		}
		if(System.Gadget.Settings.read("removable"))
		{
			isone = true;
			initHDD(2, comp);		
		}
	}
	if(System.Gadget.Settings.read("dvd"))
	{
		isone = true;
		initDVD(comp);		
	}
	if(System.Gadget.Settings.read("battery"))
	{
		isone = true;
		initBattery(comp);		
	}
	if(System.Gadget.Settings.read("network"))
	{
		isone = true;
		initNetwork(comp);
	}
	if(!isone)
	{
		System.Gadget.Settings.write("computer", true);
		initOS(comp);
	}
}

function initGadget()
{
	window.clearTimeout(windowtimer);
	canvas.removeObjects();

	loadSettings();
	setSize(size);
	
	if(compact > 1)
	{
		initInfo();
		canvas.style.height = 57;
		document.body.style.posHeight = 57;		
		canvas.style.width = maxx + (hspace * ratio);
		document.body.style.posWidth = maxx + (hspace * ratio);
	}
	else
	{
		showSections(compact);
		
		if (y < 57) y = 57;
		canvas.style.height = maxy + (vspace * ratio);
		document.body.style.posHeight = maxy + (vspace * ratio);

		canvas.style.width = maxx + (hspace * ratio);
		document.body.style.posWidth = maxx + (hspace * ratio);
		windowtimer = window.setTimeout(initGadget, window_refresh);	
	}
	
}

function defaultValues()
{
	System.Gadget.Settings.write("lang_idx", 0);	
	System.Gadget.Settings.write("timer_idx", 1);
	System.Gadget.Settings.write("timer_val", 30);
	System.Gadget.Settings.write("cputimer_idx", 2);
	System.Gadget.Settings.write("cputimer_val", 2);
	System.Gadget.Settings.write("computer", true);
	System.Gadget.Settings.write("uptime", true);
	System.Gadget.Settings.write("processor", true);
	System.Gadget.Settings.write("ram", true);
	System.Gadget.Settings.write("storage", false);
	System.Gadget.Settings.write("hdd", true);
	System.Gadget.Settings.write("netdrive", true);
	System.Gadget.Settings.write("removable", true);
	System.Gadget.Settings.write("dvd", true);
	System.Gadget.Settings.write("battery", true);
	System.Gadget.Settings.write("network", true);
	System.Gadget.Settings.write("initialized", true);
	System.Gadget.Settings.write("color", "white");
	System.Gadget.Settings.write("shadow", true);
	System.Gadget.Settings.write("size", "normal");
	System.Gadget.Settings.write("mode", 2);
	System.Gadget.Settings.write("upcontinous", false);
	System.Gadget.Settings.write("cpubars", false);
	System.Gadget.Settings.write("fonttype", "Segoe UI");
}

function onLoad()
{
	System.Gadget.settingsUI = "Settings.html";
	System.Gadget.Flyout.file = "Flyout.html";
	System.Gadget.onSettingsClosed = initGadget;
	
	if(!System.Gadget.Settings.read("initialized"))
	{
		defaultValues();
	}

	initGadget();	
}