fileEditors = {}
mediaPlayers = {}
DownGuis = {}
downlevel = 50000
-----------------------------------------------------------
function build_fedit(file,content,readonly)
	if fileEditors[file] then
		if isElement(fileEditors[file]["window"]) then
			return false -- This will avoid the opening of a file more than one time.
		end
	end
	
	fileEditors[file] = {}
	local screenWidth, screenHeight = guiGetScreenSize()
	local windowWidth, windowHeight = 452, 434
	local left = screenWidth/2 - windowWidth/2
	local top = screenHeight/2 - windowHeight/2
	fileEditors[file]["window"] = guiCreateWindow(left, top, windowWidth, windowHeight, ""..file.." - Notepad", false)
	guiWindowSetSizable(fileEditors[file]["window"], true)
	
	fileEditors[file]["save"] = guiCreateButton(10, 20, 71, 21, "Save", false, fileEditors[file]["window"])
	
	fileEditors[file]["close"] = guiCreateButton(420, 20, 21, 21, "X", false, fileEditors[file]["window"])
	
	fileEditors[file]["content"] = guiCreateMemo(10, 45, 431, 361, content or "", false, fileEditors[file]["window"])
	
	fileEditors[file]["sizes"] = guiCreateLabel(10, 410, 270, 16, "Loading file Size", false, fileEditors[file]["window"])
	guiLabelSetHorizontalAlign(fileEditors[file]["label"], "left", false)
	guiLabelSetVerticalAlign(fileEditors[file]["label"], "center")
	
	fileEditors[file]["status"] = guiCreateLabel(235, 410, 201, 16, "File Loaded", false, fileEditors[file]["window"])
	guiLabelSetHorizontalAlign(fileEditors[file]["label_2"], "left", false)
	guiLabelSetVerticalAlign(fileEditors[file]["label_2"], "center")
	guiSetVisible(fileEditors[file]["window"],true)
	guiBringToFront(fileEditors[file]["window"])
	
	local function UpdateSizes()
		local memotext = guiGetText(fileEditors[file]["content"])
		local lenght = string.len(memotext)
		local fixSize = getFixedSize(lenght,SizeBinary)
		guiSetText(fileEditors[file]["sizes"],""..lenght.." bytes | File Size = "..fixSize.."")
	end
	addEventHandler ( "onClientGUIChanged", fileEditors[file]["content"],  UpdateSizes, false )

	local function ResizeEditBox()
		local vertz, horz = guiGetSize(fileEditors[file]["window"],false)
		guiSetSize(fileEditors[file]["content"],vertz-20,horz-70,false)
		guiSetPosition(fileEditors[file]["close"],vertz-30,20,false)
		guiSetPosition(fileEditors[file]["sizes"],10,horz-23,false)
		guiSetPosition(fileEditors[file]["status"],245,horz-23,false)
	end
	addEventHandler ( "onClientGUISize", fileEditors[file]["window"],  ResizeEditBox, false )
	
	local function SaveThisFile()
		local memotext = guiGetText(fileEditors[file]["content"])
		local lenght = string.len(memotext)
		guiSetText(fileEditors[file]["status"],"Sending File.")
		triggerServerEvent("onPlayerSaveFile",getRootElement(),localPlayer,file,memotext,lenght)
	end
	addEventHandler ( "onClientGUIClick", fileEditors[file]["save"], SaveThisFile, false )
	
	local function ServerSendStatus(file,status)
		guiSetText(fileEditors[file]["status"],tostring(status))
	end
	addEvent("onServerSendFileStatus",true)
	addEventHandler ( "onServerSendFileStatus", getRootElement(), ServerSendStatus )
	local function ToggleVisible(visible)
		guiSetVisible(fileEditors[file]["window"],visible)
		if visible == true then
			guiBringToFront(fileEditors[file]["window"])
		end
	end
	addEventHandler ("OnSXWindowToggled",getRootElement(),ToggleVisible)	
	local function CloseThisWindow()
		removeEventHandler ( "onClientGUIClick", fileEditors[file]["close"], CloseThisWindow)
		removeEventHandler ( "onClientGUIClick", fileEditors[file]["save"], SaveThisFile )
		removeEventHandler ( "onClientGUISize", fileEditors[file]["window"],  ResizeEditBox )
		removeEventHandler ("OnSXWindowToggled",getRootElement(),ToggleVisible)
		removeEventHandler ( "onServerSendFileStatus", getRootElement(), ServerSendStatus )
		removeEventHandler ( "onClientGUIChanged", fileEditors[file]["content"],  UpdateSizes )
		destroyElement(fileEditors[file]["window"])
		fileEditors[file]["window"] = nil
	end
	addEventHandler ( "onClientGUIClick", fileEditors[file]["close"], CloseThisWindow, false )
	
	if readonly then
		guiSetVisible(fileEditors[file]["save"],false)
		guiMemoSetReadOnly ( fileEditors[file]["content"], true )
	end
	
	ResizeEditBox()
	UpdateSizes()
end
---------------------------------------------------------------------------
---------------------------------------------------------------------------

function build_musicplayer(path,mask)
	if mediaPlayers[path] then return false end
	mediaPlayers[path] = {}
	mediaPlayers[path].sound = playSound(path,true)
	setSoundPaused(mediaPlayers[path].sound,true)
	local movedirection = 0
	mediaPlayers[path].info = {
	lenght = nil,
	}
	local tsl = math.ceil(getSoundLength(mediaPlayers[path].sound))
	local movediv = getSoundLength(mediaPlayers[path].sound)/100
	local ratio = 100/getSoundLength(mediaPlayers[path].sound)
	local tsld = tsl/60
	local tss = tsl-(math.floor(tsld)*60)
	if tss < 10 then tss = "0"..tss end
	mediaPlayers[path].info.lenght = math.floor(tsld)..":"..tss
	local screenWidth, screenHeight = guiGetScreenSize()
	local windowWidth, windowHeight = 425, 323
	local left = screenWidth/2 - windowWidth/2
	local top = screenHeight/2 - windowHeight/2
	mediaPlayers[path]["_root"] = guiCreateWindow(left, top, windowWidth, windowHeight,tostring(mask or path).." - Media Player", false)
	guiWindowSetSizable(mediaPlayers[path]["_root"], false)
	
	mediaPlayers[path]["fileinfo"] = guiCreateGridList(10, 25, 251, 211, false, mediaPlayers[path]["_root"])
	guiGridListSetSortingEnabled(mediaPlayers[path]["fileinfo"], false)
	mediaPlayers[path]["fileinfo_col0"] = guiGridListAddColumn(mediaPlayers[path]["fileinfo"], "Name", 0.398406)
	mediaPlayers[path]["fileinfo_col1"] = guiGridListAddColumn(mediaPlayers[path]["fileinfo"], "Value", 0.398406)
	irows = {}
	local soundinfo = getSoundMetaTags(mediaPlayers[path].sound)
	for key,val in ipairs(soundinfo) do
		if val then
			local trow = guiGridListAddRow(mediaPlayers[path]["fileinfo"])
			guiGridListSetItemText(mediaPlayers[path]["fileinfo"],trow, 1,tostring(key), false, false)
			guiGridListSetItemText(mediaPlayers[path]["fileinfo"],trow, 2,tostring(val), false, false)
		end
	end
	mediaPlayers[path]["time_start"] = guiCreateLabel(20, 245, 31, 16, "0:00", false, mediaPlayers[path]["_root"])
	guiLabelSetHorizontalAlign(mediaPlayers[path]["time_start"], "left", false)
	guiLabelSetVerticalAlign(mediaPlayers[path]["time_start"], "center")
	
	mediaPlayers[path]["time_end"] = guiCreateLabel(390, 245, 31, 16,mediaPlayers[path].info.lenght, false, mediaPlayers[path]["_root"])
	guiLabelSetHorizontalAlign(mediaPlayers[path]["time_end"], "left", false)
	guiLabelSetVerticalAlign(mediaPlayers[path]["time_end"], "center")
	

	mediaPlayers[path]["close"] = guiCreateButton(384, 25, 31, 21, "X", false, mediaPlayers[path]["_root"])
	
	mediaPlayers[path]["speedlabel"] = guiCreateLabel(350, 45, 41, 30, "Speed:\n1x", false, mediaPlayers[path]["_root"])
	guiLabelSetHorizontalAlign(mediaPlayers[path]["speedlabel"], "left", false)
	guiLabelSetVerticalAlign(mediaPlayers[path]["speedlabel"], "center")
	mediaPlayers[path]["scrollvel"] = guiCreateScrollBar(395, 44, 21, 201,false, false, mediaPlayers[path]["_root"])
	guiScrollBarSetScrollPosition(mediaPlayers[path]["scrollvel"],50)
	
	mediaPlayers[path]["vollabel"] = guiCreateLabel(270, 45, 41,30, "Volume:\n100%", false, mediaPlayers[path]["_root"])
	guiLabelSetHorizontalAlign(mediaPlayers[path]["vollabel"], "left", false)
	guiLabelSetVerticalAlign(mediaPlayers[path]["vollabel"], "center")
	mediaPlayers[path]["scrollvol"] = guiCreateScrollBar(320, 44, 21, 201,false, false, mediaPlayers[path]["_root"])
		
	mediaPlayers[path]["scrollspot"] = guiCreateScrollBar(10, 265, 401, 16,true, false, mediaPlayers[path]["_root"])

	mediaPlayers[path]["mediaact_play"] = guiCreateButton(160, 295, 101, 23, "Play", false, mediaPlayers[path]["_root"])
	mediaPlayers[path]["mediaact_for"] = guiCreateButton(310, 295, 101, 23, ">>", false, mediaPlayers[path]["_root"])
	mediaPlayers[path]["mediaact_back"] = guiCreateButton(10, 295, 101, 23, "<<", false, mediaPlayers[path]["_root"])
	local function updateScrolls()
		if source == mediaPlayers[path]["scrollvol"] then
			local newvol = (100-guiScrollBarGetScrollPosition(source))
			setSoundVolume(mediaPlayers[path].sound,newvol/100)
			guiSetText(mediaPlayers[path]["vollabel"],"Volume:\n"..newvol.."%")
		end
		if source == mediaPlayers[path]["scrollvel"] then
			local newspeed = 1+(((guiScrollBarGetScrollPosition(source)-50)/100)*2)
			if newspeed < 0.1 then newspeed = 0.1 end
			setSoundSpeed(mediaPlayers[path].sound,newspeed)
			guiSetText(mediaPlayers[path]["speedlabel"],"Speed:\n"..tonumber(("%."..(2).."f"):format(newspeed)).."x")
		end
	end
	addEventHandler("onClientGUIScroll",mediaPlayers[path]["_root"],updateScrolls)
	
	local function ResizeEditBox()
		local wx,wy = guiGetSize(mediaPlayers[path]["_root"],false)
		guiSetPosition(mediaPlayers[path]["close"],wx-41,25,false)
	end
	addEventHandler ( "onClientGUISize", mediaPlayers[path]["_root"],  ResizeEditBox, false )
	local function updateDisplays()
		if movedirection ~= 0 then
			setSoundPosition(mediaPlayers[path].sound,getSoundPosition(mediaPlayers[path].sound)+(movedirection*(movediv)))
		end
		guiScrollBarSetScrollPosition(mediaPlayers[path]["scrollspot"],getSoundPosition(mediaPlayers[path].sound)*ratio)
		local esl = math.ceil(getSoundPosition(mediaPlayers[path].sound))
		local esld = esl/60
		local ess = esl-(math.floor(esld)*60)
		if ess < 10 then ess = "0"..ess end
		local elapsed = math.floor(esld)..":"..ess
		guiSetText(mediaPlayers[path]["time_start"],elapsed)
	end
	addEventHandler("onClientRender",getRootElement(),updateDisplays)
	local function actionbuttons(but,state)
		if source == mediaPlayers[path]["mediaact_play"] then
			if isSoundPaused(mediaPlayers[path].sound) then
				guiSetText(mediaPlayers[path]["mediaact_play"],"Pause")
				setSoundPaused(mediaPlayers[path].sound,false)
			else
				guiSetText(mediaPlayers[path]["mediaact_play"],"Play")
				setSoundPaused(mediaPlayers[path].sound,true)
			end
		end
	end
	addEventHandler ( "onClientGUIClick", mediaPlayers[path]["_root"], actionbuttons )
	function pushClick()
		if source == mediaPlayers[path]["mediaact_for"] then
				movedirection = 1
		end
		if source == mediaPlayers[path]["mediaact_back"] then
				movedirection = -1
		end
	end
	addEventHandler( "onClientGUIMouseDown",mediaPlayers[path]["_root"],pushClick)
	function releaseClick()
		movedirection = 0
	end
	addEventHandler( "onClientGUIMouseUp",mediaPlayers[path]["_root"],releaseClick)
	local function ToggleVisible(visible)
	guiSetVisible(mediaPlayers[path]["_root"],visible)
		if visible == true then
			guiBringToFront(mediaPlayers[path]["_root"])
		end
	end
	addEventHandler ("OnSXWindowToggled",getRootElement(),ToggleVisible)
	local function CloseThisWindow()
		removeEventHandler ( "onClientGUISize", mediaPlayers[path]["_root"],  ResizeEditBox, false )
		removeEventHandler ( "onClientGUIClick", mediaPlayers[path]["_root"], actionbuttons )	
		removeEventHandler ( "onClientGUIClick", mediaPlayers[path]["close"], CloseThisWindow, false )	
		removeEventHandler ("OnSXWindowToggled",getRootElement(),ToggleVisible)
		removeEventHandler("onClientRender",getRootElement(),updateDisplays)
		removeEventHandler ( "onClientResourceStop", getResourceRootElement(), CloseThisWindow )
		destroyElement(mediaPlayers[path]["_root"])
		destroyElement(mediaPlayers[path].sound)
		mediaPlayers[path]["_root"] = nil
		mediaPlayers[path]=nil
	end
	addEventHandler ( "onClientGUIClick", mediaPlayers[path]["close"], CloseThisWindow, false )	
	addEventHandler ( "onClientResourceStop", getResourceRootElement(), CloseThisWindow )
	ResizeEditBox()
	return true
end


function start_download(path,size,action,mask)
	DownGuis[path] = {}
	DownGuis[path].parts = math.ceil(size/downlevel)
	DownGuis[path].cpart = 0
	DownGuis[path].bcount = 0
	DownGuis[path].size = size
	DownGuis[path].speed = 0
	DownGuis[path].mask = mask
	DownGuis[path].spath = nil
	DownGuis[path].percent = 0
	DownGuis[path].ltick = 0
	DownGuis[path].tabcontent = {}
	local screenWidth, screenHeight = guiGetScreenSize()
	local windowWidth, windowHeight = 438, 111
	local left = screenWidth/2 - windowWidth/2
	local top = screenHeight/2 - windowHeight/2
	DownGuis[path]["_root"] = guiCreateWindow(left, top, windowWidth, windowHeight, "Downloading "..path.."..", false)
	guiWindowSetSizable(DownGuis[path]["_root"], false)
	
	DownGuis[path]["dprogress"] =  guiCreateProgressBar(10, 25, 421, 31, false, DownGuis[path]["_root"])
	guiProgressBarSetProgress(DownGuis[path]["dprogress"], 24)
	
	DownGuis[path]["dspeed"] = guiCreateLabel(10, 65, 421, 16, "Downloading at 10 kb/s", false, DownGuis[path]["_root"])
	guiLabelSetHorizontalAlign(DownGuis[path]["dspeed"], "left", false)
	guiLabelSetVerticalAlign(DownGuis[path]["dspeed"], "center")
	
	DownGuis[path]["dcompleted"] = guiCreateLabel(10, 85, 321, 16, "Downloaded [100Kb of 10000kb]", false, DownGuis[path]["_root"])
	guiLabelSetHorizontalAlign(DownGuis[path]["dcompleted"], "left", false)
	guiLabelSetVerticalAlign(DownGuis[path]["dcompleted"], "center")
	
	DownGuis[path]["cancel"] = guiCreateButton(364, 65, 71, 41, "Cancel", false, DownGuis[path]["_root"])
	local function ToggleVisible(visible)
	guiSetVisible(DownGuis[path]["_root"],visible)
		if visible == true then
			guiBringToFront(DownGuis[path]["_root"])
		end
	end
	addEventHandler ("OnSXWindowToggled",getRootElement(),ToggleVisible)

	local function requestBit()
		local startbit = (downlevel*DownGuis[path].cpart)
		local nextbit = startbit+downlevel
		if nextbit > DownGuis[path].size then nextbit = DownGuis[path].size end
		DownGuis[path].ltick = getTickCount()
		triggerServerEvent("reqBit",getRootElement(),getLocalPlayer(),path,startbit,downlevel)
	end
	local function receiveBit(path,bit)
		if bit == "error" then close() end
		table.insert(DownGuis[path].tabcontent,bit)
		DownGuis[path].bcount = string.len(bit)+DownGuis[path].bcount
		DownGuis[path].cpart = DownGuis[path].cpart+1
		DownGuis[path].speed = getTickCount()-DownGuis[path].ltick
		guiSetText(DownGuis[path]["dspeed"],"Downloading at "..tonumber(("%."..(2).."f"):format(tonumber(downlevel/DownGuis[path].speed))).." Kb/s")
		guiSetText(DownGuis[path]["dcompleted"],"Downloaded ["..math.ceil(DownGuis[path].bcount/1000).."Kb of "..math.ceil(DownGuis[path].size/1000).."Kb]")
		DownGuis[path].percent = math.floor((DownGuis[path].bcount/DownGuis[path].size)*100)
		guiProgressBarSetProgress ( DownGuis[path]["dprogress"],DownGuis[path].percent )
		if DownGuis[path].bcount >= DownGuis[path].size then
			local tpath = string.gsub(path,":","")
			local newfile = fileCreate("downloaded/"..tpath)
			for i=1,#DownGuis[path].tabcontent do
				fileWrite(newfile,DownGuis[path].tabcontent[i])
			end
			fileClose(newfile)
			------
			if action == "audio" then
				local tpath = string.gsub(path,":","")
				build_musicplayer("downloaded/"..tpath,DownGuis[path].mask)
			end
		removeEventHandler('sendBit', getRootElement(),receiveBit)
		removeEventHandler ( "onClientGUIClick", DownGuis[path]["cancel"], close, false )
		destroyElement(DownGuis[path]["_root"])	
			-------
		else
			requestBit()
		end
	end
	addEvent('sendBit', true)
	addEventHandler('sendBit', getRootElement(),receiveBit)
	local function close()
		if action == "audio" then
			local tpath = string.gsub(path,":","")
			build_musicplayer("downloaded/"..tpath,DownGuis[path].mask)
		end
		removeEventHandler('sendBit', getRootElement(),receiveBit)
		removeEventHandler ( "onClientGUIClick", DownGuis[path]["cancel"], close, false )
		destroyElement(DownGuis[path]["_root"])	
	end
	addEventHandler ( "onClientGUIClick", DownGuis[path]["cancel"], close, false )	
	requestBit()
end