// copyright 2007 Dean Laforet
// do not use without permission.

var gadgetPath = System.Gadget.path;
var errorImage = gadgetPath+"\\images\\error_24x24.png";
var myComp = gadgetPath+"\\images\\myComp.ico";
var control = gadgetPath+"\\images\\control.ico";
var network = gadgetPath+"\\images\\network.ico";
var ie = gadgetPath+"\\images\\ie.ico";
var rbEmpty = gadgetPath+"\\images\\rbEmpty.ico";
var rbFull = gadgetPath+"\\images\\rbFull.ico";
var tmpArray = new Array();

var oShell = new ActiveXObject("WScript.Shell");
var oFSO = new ActiveXObject("Scripting.FileSystemObject");
var bDebug = oFSO.FileExists(gadgetPath+"\\debugset.txt");
try{
	if (bDebug)
		var debugLogFile = oFSO.OpenTextFile(gadgetPath+"\\debugset.txt", 2);
} catch(err) {bDebug = false; debugLog("Open debug.txt error"+err.name+" - "+err.message)}

function debugLog(outStr) {
	try{
		System.Debug.outputString(outStr);
		if (bDebug)
			debugLogFile.WriteLine(outStr);
	} catch(err) {}
}

onerror = handleErr;

function handleErr(msg,url,l) {
	debugLog("Javascript error: "+msg+" - on line: "+l);
}

document.onreadystatechange = function()
{    
    if(document.readyState=="complete")
    {
	try{
		getSettings();
	
		if (linksList == "none" || linksList == "")
		{
			DisplayNoLinks(true);
		}
		else
		{
			DisplayNoLinks(false);
			bldShortCuts();
		}
	} catch(err) {debugLog("LoadSettings: "+err.name+" - "+err.message)}
	}
}

System.Gadget.onSettingsClosing = function(event)
{
try{
	if (event.closeAction == event.Action.commit)
	{
		System.Gadget.Settings.write("Links", tmpArray.toString() );
		System.Gadget.Settings.write("TargetLink", linkSelect.options.selectedIndex);
	}
	event.cancel = false;
} catch(err) {debugLog("SettingsClosing: "+err.name+" - "+err.message)}
}

function getSettings()
{
try{
	var targetLink = System.Gadget.Settings.read("TargetLink");
	linkSelect.options.selectedIndex = targetLink;
	
	var linksString = System.Gadget.Settings.read("Links");
	if (linksString != "none" || linksString != "" )
	{   
		linksList = linksString.split(",", linksString.length);
		tmpArray = linksList;
		checkDefaults();
	} 
	else { linksList = new Array(); }
} catch(err) {debugLog("getSettings: "+err.name+" - "+err.message)}
}

function checkDefaults()
{
try{
	var isMyC; var isNet; var isNetV; var isCPcla; var isCPcat; var isCPv; var isIE; var isRB; var isFavIE; var isFavFire;
	for (var i = 1;i < 9 ; i++)
	{
		document.getElementById('sp'+i).checked = false;
	}
	document.getElementById('favIE').checked = false;
	document.getElementById('favFire').checked = false;
	
	
	for (var i = 0;i < tmpArray.length ; i++)
	{
		var testIt = tmpArray[i];
		if (testIt == "My Computer") { isMyC = true; }
		if (testIt == "Network") { isNet = true;}
		if (testIt == "Network VAIO") { isNetV = true; }
		if (testIt == "Control Panel Classic") { isCPcla = true; }
		if (testIt == "Control Panel Category") { isCPcat = true; }
		if (testIt == "Control Panel VAIO") { isCPv = true; }
		if (testIt == "Internet Explorer") { isIE = true; }
		if (testIt == "Recycle Bin") { isRB = true; }
		if (testIt == "Internet Explorer Favorites") { isFavIE = true; }
		if (testIt == "Firefox Favorites") { isFavFire = true; }
	}
	
	if (isMyC) { document.getElementById('sp1').style.display = "none";	}else{ document.getElementById('sp1').style.display = "";}
	if (isNet) { document.getElementById('sp2').style.display = "none";	}else{ document.getElementById('sp2').style.display = "";}
	if (isCPcat) { document.getElementById('sp3').style.display = "none";	}else{ document.getElementById('sp3').style.display = "";}
	if (isCPcla) { document.getElementById('sp4').style.display = "none";	}else{ document.getElementById('sp4').style.display = "";}
	if (isIE) { document.getElementById('sp5').style.display = "none";	}else{ document.getElementById('sp5').style.display = "";}
	if (isRB) { document.getElementById('sp6').style.display = "none";	}else{ document.getElementById('sp6').style.display = "";}
	if (isNetV) { document.getElementById('sp7').style.display = "none";	}else{ document.getElementById('sp7').style.display = "";}
	if (isCPv) { document.getElementById('sp8').style.display = "none";	}else{ document.getElementById('sp8').style.display = "";}
	if (isFavIE) { document.getElementById('favIE').style.display = "none";	}else{ document.getElementById('favIE').style.display = "";}
	if (isFavFire) { document.getElementById('favFire').style.display = "none";	}else{ document.getElementById('favFire').style.display = "";}
	
	
} catch(err) {debugLog("checkDefaults: "+err.name+" - "+err.message)}
}

function FileExists(fullFileName)
{
	var bRetVal = true;
	try
	{
		var obj = System.Shell.itemFromPath(fullFileName);
	}
	catch(err)
	{
		bRetVal = false;
		debugLog("FileExists: "+err.name+" - "+err.message)
	}
	return bRetVal;
}

function getFocus() { self.focus(); }


function DisplayNoLinks(tf)
{
try{
	var t1 = null;
	if (tf)
	{
		idShortcutList.style.display = "none";
		idNoShortcutList.style.display = "";
		document.getElementById("L_REMOVE").disabled=true;
		document.getElementById("L_ADD").disabled=false;
		setTimeout("getFocus()", 300);
	}
	else
	{
		idShortcutList.style.display  = "";
		idNoShortcutList.style.display = "none";
		document.getElementById("L_REMOVE").disabled=false;       
	}
} catch(err) {debugLog("DisplayNoLinks: "+err.name+" - "+err.message)}
}

function bldShortCuts()
{
try{
	var img = null;
	var thisPath = null;
	var arFileName = null;
	while (iItemTbl.rows.length > 0) { iItemTbl.deleteRow(0); }
	for (var i = 0;i < tmpArray.length ; i++)
	{
		if (i == (tmpArray.length-1))
		{
			showDown = '<img src="images/blank.png">';
		} else
		{
			showDown = '<img src="images/down.png" onclick="moveDown('+i+');" onmouseover="overIt();" onmouseout="notOverIt();">';
		}
		if (i == 0)
		{
			showUp = '<img src="images/blank.png">';
		} else
		{
			showUp = '<img src="images/up.png" onclick="moveUp('+i+');" onmouseover="overIt();" onmouseout="notOverIt();">';
		}
	
		if (tmpArray[i] != null)
		{
			thisPath = tmpArray[i];
//			label = oFSO.GetBaseName(thisPath);
			label = oFSO.GetFileName(thisPath);
			
			if (label == "")
			{
				label = "Drive - "+thisPath;
			}

			if (thisPath == "My Computer")
			{
				filePath = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}";
				label = thisPath;
				img = "gimage:///" + myComp + "?width=20&height=20";
			}
			else if (thisPath == "Network")
			{
				filePath = "::{F02C1A0D-BE21-4350-88B0-7367FC96EF3C}";
				label = thisPath;
				img = "gimage:///" + network + "?width=20&height=20";
			}
			else if (thisPath == "Network VAIO")
			{
				filePath = "::{7007ACC7-3202-11D1-AAD2-00805FC1270E}";
				label = thisPath;
				img = "gimage:///" + network + "?width=20&height=20";
			}
			else if (thisPath == "Control Panel Category")
			{
				filePath = "::{26EE0668-A00A-44D7-9371-BEB064C98683}";
				label = thisPath;
				img = "gimage:///" + control + "?width=20&height=20";
			}
			else if (thisPath == "Control Panel Classic")
			{
				filePath = "::{21EC2020-3AEA-1069-A2DD-08002B30309D}";
				label = thisPath;
				img = "gimage:///" + control + "?width=20&height=20";
			}
			else if (thisPath == "Control Panel VAIO")
			{
				filePath = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\::{21EC2020-3AEA-1069-A2DD-08002B30309D}";
				label = thisPath;
				img = "gimage:///" + control + "?width=20&height=20";
			}
			else if (thisPath == "Internet Explorer")
			{
				filePath = "::{871C5380-42A0-1069-A2EA-08002B30309D}";
				label = thisPath;
				img = "gimage:///" + ie + "?width=20&height=20";
			}
			else if (thisPath == "Recycle Bin")
			{
				filePath = "::{645FF040-5081-101B-9F08-00AA002F954E}";
				label = thisPath;
				img = "gimage:///" + rbEmpty + "?width=20&height=20";
			}
			else if (thisPath == "Internet Explorer Favorites")
			{
				filePath = thisPath;
				label = thisPath;
				img = "images/ieFav.png";
			}
			else if (thisPath == "Firefox Favorites")
			{
				filePath = thisPath;
				label = thisPath;
				img = "images/ffFav.png";
			}
			else
			{
				filePath = thisPath;
				img = "gimage:///" + ( FileExists(filePath) ? filePath : errorImage )   +  "?width=20&height=20";
			}
			
			row = iItemTbl.insertRow(i);  
             
			cell = row.insertCell();
			cell.setAttribute("vAlign", "top");
			id = "r"+ i + "c0";
			cell.innerHTML = showUp+'&nbsp;'+showDown+'&nbsp;<input type="checkbox" id="'+ id + '" onfocus="blur();">';
		
			cell = row.insertCell();
			id = "r"+i + "c1";
			cell.innerHTML = "<img src=\"" + img + "\" width=\"20px\" height=\"20px\">";
			cell.setAttribute("vAlign","top");

			cell = row.insertCell();
			id = "r"+ i + "c2";
			cell.innerHTML = "<span class=\"inputFld\">"+ label +"</span>";
			cell.setAttribute("vAlign","top");	

			cell = row.insertCell();
			id = "r"+ i + "c3";
			cell.innerHTML ="<span id=\"" + id + "\" class=\"spanFld\">"+ thisPath + "</span>";
			cell.setAttribute("vAlign","top");	
			cell.setAttribute("nowrap");
		}
	}
	document.getElementById("L_ADD").disabled = false;
	setTimeout("getFocus()", 300);
} catch(err) {debugLog("bldShortCuts: "+err.name+" - "+err.message)}
}

function delLinks()
{
try{
	var index = 0;
	tmpArray.splice(0,tmpArray.length);
	for (var i = 0; i < iItemTbl.rows.length ; i++)
	{
		id = "r"+i+"c0";
		if (! document.getElementById(id).checked)
		{
			tmpArray[ index++ ] = document.getElementById("r"+i+"c3").innerHTML;
		}
	}
	if (tmpArray.length == 0)
	{
		DisplayNoLinks(true);
		tmpArray = "none";
	}
	else
	{
		bldShortCuts();
	}    
} catch(err) {debugLog("delLinks: "+err.name+" - "+err.message)}
}

function browseFile()
{
try{
	var oItem = System.Shell.chooseFile(true, "All Files:*.*::", "", "");
	if (oItem) 
	{
		if (tmpArray=="none" || tmpArray == "") { existLength = 0; tmpArray = new Array(); }
		else {existLength = tmpArray.length;}
		var addPath = oItem.path;
		tmpArray[existLength] = addPath;
		DisplayNoLinks(false);
		bldShortCuts(); 
	}
} catch(err) {debugLog("browseFile: "+err.name+" - "+err.message)}
}

function moveDown(thisOne) {
try{
	var index = 0;
	if (thisOne < tmpArray.length)
	{
		tmpArray.splice(0,tmpArray.length);
		for (var i = 0; i < iItemTbl.rows.length ; i++)
		{
			if (i == thisOne)
			{
				tmpArray[ index++ ] = document.getElementById("r"+(i+1)+"c3").innerHTML; 
				tmpArray[ index++ ] = document.getElementById("r"+i+"c3").innerHTML;      
				i=i+1;     
			}
			else
			{
				tmpArray[ index++ ] = document.getElementById("r"+i+"c3").innerHTML;
			}
		}
	}
	bldShortCuts();
} catch(err) {debugLog("moveDown: "+err.name+" - "+err.message)}
}

function moveUp(thisOne) {
try{
	var index = 0;
	if (thisOne > 0)
	{
		tmpArray.splice(0,tmpArray.length);
		for (var i = 0; i < iItemTbl.rows.length ; i++)
		{
			if (i == (thisOne-1))
			{
				tmpArray[ index++ ] = document.getElementById("r"+(i+1)+"c3").innerHTML;      
				tmpArray[ index++ ] = document.getElementById("r"+i+"c3").innerHTML; 
				i=i+1;     
			}
			else
			{
				tmpArray[ index++ ] = document.getElementById("r"+i+"c3").innerHTML;
			}
		}
	}
	bldShortCuts();
} catch(err) {debugLog("moveUp: "+err.name+" - "+err.message)}
}

function overIt()
{
	var img = event.srcElement;
	img.style.cursor = "hand";
}

function notOverIt()
{
	var img = event.srcElement;
	img.style.cursor = "default";
}

function menuAdd()
{
	reorder.style.display = "";
	defaults.style.display = "none";
	favorites.style.display = "none";
	save.style.display = "none";
	misc.style.display = "none";
	menu1.style.borderBottomColor = "#f0f0f0";
	menu2.style.borderBottomColor = "#505050";
	menu3.style.borderBottomColor = "#505050";
	menu4.style.borderBottomColor = "#505050";
	menu5.style.borderBottomColor = "#505050";
}

function menuDef()
{
	reorder.style.display = "none";
	defaults.style.display = "";
	favorites.style.display = "none";
	save.style.display = "none";
	misc.style.display = "none";
	menu1.style.borderBottomColor = "#505050";
	menu2.style.borderBottomColor = "#f0f0f0";
	menu3.style.borderBottomColor = "#505050";
	menu4.style.borderBottomColor = "#505050";
	menu5.style.borderBottomColor = "#505050";
	checkDefaults();
}

function menuFav()
{
	reorder.style.display = "none";
	defaults.style.display = "none";
	favorites.style.display = "";
	save.style.display = "none";
	misc.style.display = "none";
	menu1.style.borderBottomColor = "#505050";
	menu2.style.borderBottomColor = "#505050";
	menu3.style.borderBottomColor = "#f0f0f0";
	menu4.style.borderBottomColor = "#505050";
	menu5.style.borderBottomColor = "#505050";
	checkDefaults();
}

function menuSave()
{
	reorder.style.display = "none";
	defaults.style.display = "none";
	favorites.style.display = "none";
	save.style.display = "";
	misc.style.display = "none";
	menu1.style.borderBottomColor = "#505050";
	menu2.style.borderBottomColor = "#505050";
	menu3.style.borderBottomColor = "#505050";
	menu4.style.borderBottomColor = "#f0f0f0";
	menu5.style.borderBottomColor = "#505050";
}

function menuMisc()
{
	reorder.style.display = "none";
	defaults.style.display = "none";
	favorites.style.display = "none";
	save.style.display = "none";
	misc.style.display = "";
	menu1.style.borderBottomColor = "#505050";
	menu2.style.borderBottomColor = "#505050";
	menu3.style.borderBottomColor = "#505050";
	menu4.style.borderBottomColor = "#505050";
	menu5.style.borderBottomColor = "#f0f0f0";
}

function updateDef()
{
try{
	index = tmpArray.length;
	for (var i = 1; i < 9 ; i++)
	{
		id = "sp"+i;
		if (document.getElementById(id).checked)
		{
			tmpArray[index] = document.getElementById("sp"+i+"name").innerHTML;
			index++;
		}
	}
	checkDefaults();
	bldShortCuts();
} catch(err) {debugLog("updateDef: "+err.name+" - "+err.message)}
}

function updateFav()
{
try{
	index = tmpArray.length;
	if (document.getElementById('favIE').checked)
	{
		tmpArray[index] = "Internet Explorer Favorites";
		index++;
	}
	if (document.getElementById('favFire').checked)
	{
		tmpArray[index] = "Firefox Favorites";
	}

	checkDefaults();
	bldShortCuts();
} catch(err) {debugLog("updateFav: "+err.name+" - "+err.message)}
}

function saveShortcuts()
{
try{
	var shortList = "";
	var sFilename = gadgetPath + "\\defaults.txt";
	if (oFSO.FileExists(sFilename))
	{
		oFSO.DeleteFile(sFilename, true)
	}
	var oFile = oFSO.CreateTextFile(sFilename, true);
	
	for (var i = 0; i < tmpArray.length ; i++)
	{
		oFile.WriteLine(tmpArray[i]);
	}
	oFile.Close();
	
} catch(err) {debugLog("saveShortcuts: "+err.name+" - "+err.message)}
}


