Components.utils.import("resource://gre/modules/ctypes.jsm");

const FLASHCCLICKCALLBACK = ctypes.FunctionType((ctypes.size_t.size == 8 ? ctypes.default_abi : ctypes.stdcall_abi), ctypes.voidptr_t, [ctypes.long, ctypes.long]).ptr;
var FlashOnClick = FLASHCCLICKCALLBACK(function(top, left)
{
 	var oBwsr = gBrowser.getBrowserForTab(gBrowser.selectedTab);
 	oBwsr.setAttribute("dmmmVideoPlayed", "True");
 	oBwsr.setAttribute("dmmmFlashClickedTop", top);
 	oBwsr.setAttribute("dmmmFlashClickedLeft", left);
 	
});

var DmmmDllWrapper =
{
	Init: function(extPath)
	{		
		this.BOOL = ctypes.bool;
		this.LONG = ctypes.long;
		this.WORD = ctypes.unsigned_short;
		this.DWORD = ctypes.unsigned_long;
		this.INT = ctypes.int;
		this.UINT = ctypes.unsigned_int;
		this.ULONG = ctypes.unsigned_long;
		this.USHORT = ctypes.unsigned_short;
		this.HANDLE = ctypes.size_t;
		this.LPSTR = ctypes.char.ptr;
		this.LPCSTR = ctypes.char.ptr;
		this.LPCTSTR = ctypes.char.ptr;
		this.LPCWSTR = ctypes.jschar.ptr
		this.PVOID = ctypes.voidptr_t;
		
		if (ctypes.size_t.size == 8) {
			this.CallBackABI = ctypes.default_abi;
			this.WinABI = ctypes.default_abi;
		} else {
			this.CallBackABI = ctypes.stdcall_abi;
			this.WinABI = ctypes.winapi_abi;
		}

		DmmmDllWrapper.ExtPath = extPath;
	},
	
	InitWin: function(hWnd)
	{
		DmmmDllWrapper.DmmmwhDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmwh.dll");
		var InitWin = DmmmDllWrapper.DmmmwhDll.declare("InitWin", 
						this.WinABI, 
						this.PVOID,
						this.HANDLE,
						FLASHCCLICKCALLBACK);
			
		InitWin(hWnd, FlashOnClick);
	},
	
	Shutdown: function()
	{
		var ReleaseWin = DmmmDllWrapper.DmmmwhDll.declare("ReleaseWin", 
						this.WinABI, 
						this.PVOID);
			
		ReleaseWin();
		
		DmmmDllWrapper.DmmmwhDll.close();
	},
	
	ShowMessageBox: function(text, caption, type)
	{
		var dmmmcDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmc.dll");
		
		var ShowMessageBox = dmmmcDll.declare("ShowMessageBox", 
						this.WinABI, 
						this.INT,
						this.LPCWSTR,
						this.LPCWSTR,
						this.UINT);
			
		var iRet = ShowMessageBox(text, caption, type);
		dmmmcDll.close();
		return iRet;
	},
	
	RunDM: function(url, httpMethod, docLoc, title, cookies, userAgent, mimeType)
	{
		var dmmmcDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmc.dll");
		
		var RunDM = dmmmcDll.declare("RunDM", 
						this.WinABI, 
						this.PVOID,
						this.LPSTR);

		var args = "-addurl \"<url>" + url + "</url>" + (httpMethod == "POST" ? "<post>1</post>" : "") + 
			"<referer>" + docLoc + "</referer><title>" + title.replace(/\"/g, "'") + "</title><cookies>" + cookies + "</cookies>" + 
			"<useragent>" + userAgent + "</useragent><mimetype>" + mimeType + "</mimetype>\"";	
		
		RunDM(args);
		
		dmmmcDll.close();
		
	},
		
	CheckInstalled: function(bInitWin, hWnd)
	{
		var dmmmcDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmc.dll");
		
		var CheckInstalled = dmmmcDll.declare("CheckInstalled", 
						this.WinABI, 
						this.BOOL);
		
		var bRet = CheckInstalled();
		dmmmcDll.close();
		return bRet;
	},
		
	GetWinShow: function()
	{
		var dmmmcDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmc.dll");
		
		var GetWinShow = dmmmcDll.declare("GetWinShow", 
						this.WinABI, 
						this.BOOL);
		
		var bRet = GetWinShow();
		dmmmcDll.close();
		return bRet;
	},	
	
	GetVideoMinSize: function()
	{
		var dmmmcDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmc.dll");
		
		var GetVideoMinSize = dmmmcDll.declare("GetVideoMinSize", 
						this.WinABI, 
						this.DWORD);
		
		var dwRet = GetVideoMinSize();
		if (dwRet == 0) dwRet = 500;
		dmmmcDll.close();
		return dwRet;
	},	
	
	GetAudioMinSize: function()
	{
		var dmmmcDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmc.dll");
		
		var GetAudioMinSize = dmmmcDll.declare("GetAudioMinSize", 
						this.WinABI, 
						this.DWORD);
		
		var dwRet = GetAudioMinSize();
		if (dwRet == 0) dwRet = 100;
		dmmmcDll.close();
		return dwRet;
	},
	
	GetVideoMimeTypes: function()
	{
		var dmmmcDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmc.dll");
		
		var GetVideoMimeTypes = dmmmcDll.declare("GetVideoMimeTypes", 
						this.WinABI, 
						this.LPSTR);
		
		var FreeMemory = dmmmcDll.declare("FreeMemory", 
						this.WinABI,
						this.PVOID,
						this.PVOID);
		
		var videoMimeTypesPtr = GetVideoMimeTypes();
		var videoMimeTypes = videoMimeTypesPtr.readString();
		
		FreeMemory(videoMimeTypesPtr);
		dmmmcDll.close();
		
		return videoMimeTypes;
	},
	
	GetVideoExtensions: function()
	{
		
		var dmmmcDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmc.dll");
		
		var GetVideoExtensions = dmmmcDll.declare("GetVideoExtensions", 
						this.WinABI, 
						this.LPSTR);
		
		var FreeMemory = dmmmcDll.declare("FreeMemory", 
						this.WinABI,
						this.PVOID,
						this.PVOID);
		
		var videoExtensionsPtr = GetVideoExtensions();
		var videoExtensions = videoExtensionsPtr.readString();
		
		FreeMemory(videoExtensionsPtr);
		dmmmcDll.close();
		
		return videoExtensions;
	},
	
	GetAudioMimeTypes: function()
	{
		var dmmmcDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmc.dll");
		
		var GetAudioMimeTypes = dmmmcDll.declare("GetAudioMimeTypes", 
						this.WinABI, 
						this.LPSTR);
		
		var FreeMemory = dmmmcDll.declare("FreeMemory", 
						this.WinABI,
						this.PVOID,
						this.PVOID);
		
		var audioMimeTypesPtr = GetAudioMimeTypes();
		var audioMimeTypes = audioMimeTypesPtr.readString();
		
		FreeMemory(audioMimeTypesPtr);
		dmmmcDll.close();
		
		return audioMimeTypes;
	},
	
	GetAudioExtensions: function()
	{
		var dmmmcDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmc.dll");
		
		var GetAudioExtensions = dmmmcDll.declare("GetAudioExtensions", 
						this.WinABI, 
						this.LPSTR);
		
		var FreeMemory = dmmmcDll.declare("FreeMemory", 
						this.WinABI,
						this.PVOID,
						this.PVOID);
		
		var audioExtensionsPtr = GetAudioExtensions();
		var audioExtensions = audioExtensionsPtr.readString();
		
		FreeMemory(audioExtensionsPtr);
		dmmmcDll.close();
		
		return audioExtensions;
	},
	
	SetWinShow: function(bWinShow)
	{
		var dmmmcDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmc.dll");
		
		var SetWinShow = dmmmcDll.declare("SetWinShow", 
						this.WinABI,
						this.PVOID,
						this.BOOL);
		
		SetWinShow(bWinShow);
		dmmmcDll.close();
	},	
	
	SetVideoMinSize: function(videoMinSize)
	{
		var dmmmcDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmc.dll");
		
		var SetVideoMinSize = dmmmcDll.declare("SetVideoMinSize", 
						this.WinABI,
						this.PVOID,
						this.DWORD);
		
		SetVideoMinSize(videoMinSize);
		dmmmcDll.close();
	},	
	
	SetAudioMinSize: function(audioMinSize)
	{
		var dmmmcDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmc.dll");
		
		var SetAudioMinSize = dmmmcDll.declare("SetAudioMinSize", 
						this.WinABI,
						this.PVOID,
						this.DWORD);
		
		SetAudioMinSize(audioMinSize);
		dmmmcDll.close();
	},	
	
	SetVideoMimeTypes: function(videoMimeTypes)
	{
		var dmmmcDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmc.dll");
		
		var SetVideoMimeTypes = dmmmcDll.declare("SetVideoMimeTypes", 
						this.WinABI, 
						this.PVOID,
						this.LPSTR);
		
		SetVideoMimeTypes(videoMimeTypes);
		dmmmcDll.close();
	},
		
	SetVideoExtensions: function(videoExtensions)
	{
		var dmmmcDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmc.dll");
		
		var SetVideoExtensions = dmmmcDll.declare("SetVideoExtensions", 
						this.WinABI, 
						this.PVOID,
						this.LPSTR);
		
		SetVideoExtensions(videoExtensions);
		dmmmcDll.close();
	},
		
	SetAudioMimeTypes: function(audioMimeTypes)
	{
		var dmmmcDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmc.dll");
		
		var SetAudioMimeTypes = dmmmcDll.declare("SetAudioMimeTypes", 
						this.WinABI, 
						this.PVOID,
						this.LPSTR);
		
		SetAudioMimeTypes(audioMimeTypes);
		dmmmcDll.close();
	},
		
	SetAudioExtensions: function(audioExtensions)
	{
		var dmmmcDll = ctypes.open(DmmmDllWrapper.ExtPath + "components\\dmmmc.dll");
		
		var SetAudioExtensions = dmmmcDll.declare("SetAudioExtensions", 
						this.WinABI, 
						this.PVOID,
						this.LPSTR);
		
		SetAudioExtensions(audioExtensions);
		dmmmcDll.close();
	},
	
	GetActiveWindow: function()
	{
		var user32dll = ctypes.open("user32.dll");
		
		var GetActiveWindow = user32dll.declare("GetActiveWindow", 
				this.WinABI, 
				this.HANDLE);
		
		var hwnd = GetActiveWindow();
		
		user32dll.close();
		return hwnd;
	},
	
}