

var DockedWidth = 125;
var UndockedWidth = 180;

var version = 220;
var Net = null;
var guid = "{460BD50A-CC02-45FE-9A4A-981B48D26B98}";
var progId = "Clipboarder.ClipboarderNet220";
var dllPath = System.Gadget.path + "\\Release\\Clipboarder.220.dll";
var altDllPath = System.Gadget.path + "\\..\\ClipboarderDLLs\\Clipboarder.220.dll";
var dllPath2 = System.Gadget.path + "\\Release\\Clipboarder2.220.dll";
var altDllPath2 = System.Gadget.path + "\\..\\ClipboarderDLLs\\Clipboarder2.220.dll";
var settingsPath = "Settings.220.html";
var h = 0;
var prevVersion = 0;
var CurrentWidth = 0;

function Load()
{
    SetDefaultSettings();
    CurrentWidth = System.Gadget.docked ? DockedWidth : UndockedWidth;
    h = System.Gadget.Settings.read("Height");
    with(document.body.style)
        width = CurrentWidth,
        height = h;

    LoadNet();
    Net.CheckVersion(version);
    Net.Init(NewSize, LocalizedFunction, WriteSetting, System.Gadget.path+"\\");
    SettingsClosed(null);

    System.Gadget.settingsUI = settingsPath;
    System.Gadget.onSettingsClosed = SettingsClosed;
    System.Gadget.onDock = Docked;
    System.Gadget.onUndock = Undocked;
}

function Unload()
{
    if(Net != null)
        Net.Dispose();
    Net = null;
}

function Docked()
{
    CurrentWidth = DockedWidth;
    with(document.body.style)
        width = CurrentWidth,
        height = h;
}

function Undocked()
{
    CurrentWidth = UndockedWidth;
    with(document.body.style)
        width = CurrentWidth,
        height = h;
}

function NewSize(size)
{
    h = size;
    System.Gadget.Settings.write("Height", size);
    with(document.body.style)
        width = CurrentWidth,
        height = h;
}

function LocalizedFunction(name)
{
    return Localized[name];
}

function WriteSetting(Var, Val)
{
    System.Gadget.Settings.write(Var, Val);
}

function SettingsClosed(event)
{
    Net.SetSettings(prevVersion,
                    parseInt(System.Gadget.Settings.read("UseHotKeys")),
                    parseInt(System.Gadget.Settings.read("HotKeyModifer1")),
                    System.Gadget.Settings.read("HotKey1"),
                    parseInt(System.Gadget.Settings.read("HotKeyModifer2")),
                    System.Gadget.Settings.read("HotKey2"),
                    parseInt(System.Gadget.Settings.read("MaxContents")),
                    parseInt(System.Gadget.Settings.read("FixedVisibleContents")),
                    parseInt(System.Gadget.Settings.read("UseGlass")),
                    parseInt(System.Gadget.Settings.read("UseAnimation")),
                    parseInt(System.Gadget.Settings.read("UseNumpadHotkey")),
                    parseInt(System.Gadget.Settings.read("UseForeground")),
                    System.Gadget.Settings.read("UseSave"),
                    parseInt(System.Gadget.Settings.read("UseSound")),
                    System.Gadget.Settings.read("SoundFile"),
                    System.Gadget.Settings.read("Browser"),
                    parseInt(System.Gadget.Settings.read("MonitorClipboard")),
                    parseInt(System.Gadget.Settings.read("UseHotkeyUnformatted")));
    prevVersion = System.Gadget.Settings.read("Version");
}


function SetDefaultSettings()
{
	if(System.Gadget.Settings.read("SettingsWritten") == "1")
    	prevVersion = parseInt(System.Gadget.Settings.read("Version"));

	if(System.Gadget.Settings.read("SettingsWritten") != "1")
	{
		System.Gadget.Settings.write("Height", 200);
		System.Gadget.Settings.write("HotKeyModifer1", 8);
		System.Gadget.Settings.write("HotKeyModifer2", 8);
		System.Gadget.Settings.write("HotKey1", "V");
		System.Gadget.Settings.write("HotKey2", "C");
		System.Gadget.Settings.write("MaxContents", 20);
		System.Gadget.Settings.write("FixedVisibleContents", 1);
		System.Gadget.Settings.write("UseGlass", "1");
		System.Gadget.Settings.write("SettingsWritten", "1");
    }
	if(prevVersion <= 100)
	{
		System.Gadget.Settings.write("UseAnimation", "1");
		System.Gadget.Settings.write("UseNumpadHotkey", "0");
		System.Gadget.Settings.write("UseForeground", "0");
		System.Gadget.Settings.write("UseSave", "@");
	}
	if(prevVersion <= 130)
	{
		System.Gadget.Settings.write("UseSound", "0");
		System.Gadget.Settings.write("SoundFile", "C:\\Windows\\Media\\chimes.wav");
	}
	if(prevVersion <= 180)
	{
	    System.Gadget.Settings.write("UseHotKeys", "1");
	    System.Gadget.Settings.write("MonitorClipboard", "1");
	}
	if(prevVersion <= 210)
    {
        System.Gadget.Settings.write("UseHotkeyUnformatted", "0");
	}
	System.Gadget.Settings.write("Version", version);		
}

function LoadNet()
{
    var wshShell = new ActiveXObject("WScript.Shell");

    RegisterNet(wshShell, "HKCU", dllPath, "v4.0.30319");
    RegisterNet(wshShell, "HKLM", dllPath, "v4.0.30319");
 
    // create the COM object and store it in global variable
    try
    {
        Net = new ActiveXObject(progId); 
    }
    catch(err)
    {
        UnregisterNet(wshShell, "HKCU");
        UnregisterNet(wshShell, "HKLM");

        RegisterNet(wshShell, "HKCU", altDllPath, "v4.0.30319");
        RegisterNet(wshShell, "HKLM", altDllPath, "v4.0.30319");
        try
        {
            Net = new ActiveXObject(progId);
        }
        catch (err)
        {
            UnregisterNet(wshShell, "HKCU");
            UnregisterNet(wshShell, "HKLM");

            RegisterNet(wshShell, "HKCU", dllPath2, "v2.0.50727");
            RegisterNet(wshShell, "HKLM", dllPath2, "v2.0.50727");
            try
            {
                Net = new ActiveXObject(progId);
            }
            catch (err)
            {
                UnregisterNet(wshShell, "HKCU");
                UnregisterNet(wshShell, "HKLM");

                RegisterNet(wshShell, "HKCU", altDllPath2, "v2.0.50727");
                RegisterNet(wshShell, "HKLM", altDllPath2, "v2.0.50727");
                Net = new ActiveXObject(progId);
            }
        }
    }
    if(Net == null)
    {
        alert("Gadgets\\ClipboarderDLLs folder not found. Install the gadget again.");
        debugger;
        return;
    }

    // delete all the entries again
    UnregisterNet(wshShell, "HKCU");
    UnregisterNet(wshShell, "HKLM");

    wshShell = null;
}

function RegisterNet(wshShell, root, path, netversion)
{
    try
    {
        wshShell.RegWrite(root + "\\Software\\Classes\\" + progId + "\\", progId);
        wshShell.RegWrite(root + "\\Software\\Classes\\" + progId + "\\CLSID\\", guid);       

        wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\", progId); 

        wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\InprocServer32\\", "mscoree.dll");
        wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\InprocServer32\\ThreadingModel", "Both");
        wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\InprocServer32\\Class", progId);
        wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\InprocServer32\\Assembly", "Clipboarder."+version+", Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
        wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\InprocServer32\\RuntimeVersion", netversion);
        wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\InprocServer32\\CodeBase", path); 

        wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\InprocServer32\\1.0.0.0\\Class", progId);
        wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\InprocServer32\\1.0.0.0\\Assembly", "Clipboarder."+version+", Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
        wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\InprocServer32\\1.0.0.0\\RuntimeVersion", netversion);
        wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\InprocServer32\\1.0.0.0\\CodeBase", path); 

        wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\ProgId\\", progId); 

        wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\Implemented Categories\\{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}\\", "");
    }
    catch(err)
    {
    }
}
function UnregisterNet(wshShell, root)
{ 
    try
    {
        wshShell.RegDelete(root + "\\Software\\Classes\\" + progId + "\\CLSID\\");
        wshShell.RegDelete(root + "\\Software\\Classes\\" + progId + "\\");
        wshShell.RegDelete(root + "\\Software\\Classes\\CLSID\\" + guid + "\\InprocServer32\\1.0.0.0\\");
        wshShell.RegDelete(root + "\\Software\\Classes\\CLSID\\" + guid + "\\InprocServer32\\");
        wshShell.RegDelete(root + "\\Software\\Classes\\CLSID\\" + guid + "\\ProgId\\");
        wshShell.RegDelete(root + "\\Software\\Classes\\CLSID\\" + guid + "\\Implemented Categories\\{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}\\");
        wshShell.RegDelete(root + "\\Software\\Classes\\CLSID\\" + guid + "\\Implemented Categories\\");
        wshShell.RegDelete(root + "\\Software\\Classes\\CLSID\\" + guid + "\\");
    }
    catch(err)
    {
    }
}

