local fname=BROWSERPATH.."\\speedykm\\speedykm.html"

function readSpeedy(hwnd)
	local f = assert(io.open(fname,"r"))
	local speedykm = f:read("*all")
	local km={}
	local kmUrl={}
	local kmImage={}
	local kmTitle={}
	local p=string.find(speedykm,"\<DIV id\=screen")
	local q=string.find(speedykm,"style=\"Z-INDEX:")
	speedykm=string.sub(speedykm,p)
	p=string.find(speedykm,"</DIV>")
	speedykm=string.sub(speedykm,q,p-1)
	for i=1,16,1 do
		p=string.sub(speedykm,"<SPAN")
		km[i]=string.sub(speedykm,1,p-1)
		speedykm=string.sub(speedykm,p+5)
		alert(km[i])
	end
--[[	for i=1,16,1 do
		p[i]=string.find(speedykm,"Z-INDEX:"..i.."\"\>\<a")
		if i<10 then
			km[i]=string.sub(speedykm,p[i]+18)
			p[i]=string.find(km[i],"\"")
			kmUrl[i]=string.sub(km[i],1,p[i]-1)
			p[i]=string.find(km[i],"src")
			local temp=string.sub(km[i],p[i]+5)
			q[i]=string.find(temp,"\"")
			kmImage[i]=string.sub(temp,1,q[i]-1)
			p[i]=string.find(km[i],"TITLE")
			local temp=string.sub(km[i],p[i]+7)
			q[i]=string.find(temp,"\"")
			kmTitle[i]=string.sub(temp,1,q[i]-1)
		else 
			km[i]=string.sub(speedykm,p[i]+19)	
			p[i]=string.find(km[i],"\"")
			kmUrl[i]=string.sub(km[i],1,p[i]-1)
			p[i]=string.find(km[i],"src")
			local temp=string.sub(km[i],p[i]+5)
			q[i]=string.find(temp,"\"")
			kmImage[i]=string.sub(temp,1,q[i]-1)
			p[i]=string.find(km[i],"TITLE")
			local temp=string.sub(km[i],p[i]+7)
			q[i]=string.find(temp,"\"")
			kmTitle[i]=string.sub(temp,1,q[i]-1)
		end
		--alert(kmTitle[i])
	end --]]
	return kmUrl,kmTitle
end

local function writeSpeedy(kmTitle,kmUrl)
	local f = assert(io.open(fname,"r"))
	local speedykm = f:read("*all")
	f:close()	
	local km={}
	local kmImage={}
	local t1=string.find(speedykm,"id\=\"Layer14\"")
	local t2=string.find(speedykm,"style type\=\"text/css\"")
	local temp1=string.sub(speedykm,1,t1-6)
	local temp2=string.sub(speedykm,t2-1)
	local temp=string.sub(speedykm,t1-5,t2-2)
	for i=1,12,1 do
		local p=string.find(temp,"</div>")
		km[i]=string.sub(temp,1,p+6)
		temp=string.sub(temp,p+7)
		
		local r=string.find(km[i],"href")
		local g1=string.sub(km[i],1,r+5)
		local g2=string.sub(km[i],r+6)
		r=string.find(g2,"\"")
		g2=string.sub(g2,r)
		r=string.find(g2,"src")
		local g3=string.sub(g2,1,r+4)
		local g4=string.sub(g2,r+5)
		r=string.find(g4,"\"")
		g4=string.sub(g4,r)
		r=string.find(g4,"TITLE")
		local g5=string.sub(g4,1,r+6)
		local g6=string.sub(g4,r+7)
		r=string.find(g6,"\"")
		g6=string.sub(g6,r)
		kmImage[i]="http://images.websnapr.com/?url="..kmUrl[i]
		km[i]=g1..kmUrl[i]..g3..kmImage[i]..g5..kmTitle[i]..g6
	end
	temp=""
	for i=1,12,1 do
		temp=temp..km[i]
	end
	speedykm=temp1..temp..temp2
	f=assert(io.open(fname,"w"))
	if f then
		f:write(speedykm)
		f:close()
	end
end

local width = 360
local height = 200
local ffont="Arial"
local fpoint=10

local d = create_dialog({
	width = width,
	height = height,
	caption = "SpeedyKM",
	style = w32dlg.DS_CENTER,
	font = ffont, point = fpoint,
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 5, y = 5, width = 50, height = 10, id = "IDSEARCHNAME1", caption = "SEARCHNAME1"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 60, y = 5, width = 250, height = 10, id = "IDSEARCH1", caption = "SEARCH1"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 5, y = 18, width = 50, height = 10, id = "IDSEARCHNAME2", caption = "SEARCHNAME2"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 60, y = 18, width = 250, height = 10, id = "IDSEARCH2", caption = "SEARCH2"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 5, y = 31, width = 50, height = 10, id = "IDSEARCHNAME3", caption = "SEARCHNAME3"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 60, y = 31, width = 250, height = 10, id = "IDSEARCH3", caption = "SEARCH3"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 5, y = 44, width = 50, height = 10, id = "IDSEARCHNAME4", caption = "SEARCHNAME4"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 60, y = 44, width = 250, height = 10, id = "IDSEARCH4", caption = "SEARCH4"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 5, y = 57, width = 50, height = 10, id = "IDSEARCHNAME5", caption = "SEARCHNAME5"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 60, y = 57, width = 250, height = 10, id = "IDSEARCH5", caption = "SEARCH5"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 5, y = 70, width = 50, height = 10, id = "IDSEARCHNAME6", caption = "SEARCHNAME6"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 60, y = 70, width = 250, height = 10, id = "IDSEARCH6", caption = "SEARCH6"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 5, y = 83, width = 50, height = 10, id = "IDSEARCHNAME7", caption = "SEARCHN7AME"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 60, y = 83, width = 250, height = 10, id = "IDSEARCH7", caption = "SEARCH7"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 5, y = 96, width = 50, height = 10, id = "IDSEARCHNAME8", caption = "SEARCHNAME8"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 60, y = 96, width = 250, height = 10, id = "IDSEARCH8", caption = "SEARCH8"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 5, y = 109, width = 50, height = 10, id = "IDSEARCHNAME9", caption = "SEARCHNAME9"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 60, y = 109, width = 250, height = 10, id = "IDSEARCH9", caption = "SEARCH9"}, 
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 5, y = 122, width = 50, height = 10, id = "IDSEARCHNAME10", caption = "SEARCHNAME10"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 60, y = 122, width = 250, height = 10, id = "IDSEARCH10", caption = "SEARCH10"}, 
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 5, y = 135, width = 50, height = 10, id = "IDSEARCHNAME11", caption = "SEARCHNAME11"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 60, y = 135, width = 250, height = 10, id = "IDSEARCH11", caption = "SEARCH11"}, 
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 5, y = 148, width = 50, height = 10, id = "IDSEARCHNAME12", caption = "SEARCHNAME12"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 60, y = 148, width = 250, height = 10, id = "IDSEARCH12", caption = "SEARCH12"}, 
	{type="button",style=w32dlg.BS_DEFPUSHBUTTON,x=5,y=170,width=40,height=12,id="IDOK",caption="Done"}, 
})

function property_speedkm()
	local kmUrl,kmTitle=readSpeedy()
	r,rr=d("SpeedyKM",{
		IDSEARCHNAME1=kmTitle[1],
		IDSEARCH1=kmUrl[1],
		IDSEARCHNAME2=kmTitle[2],
		IDSEARCH2=kmUrl[2],
		IDSEARCHNAME3=kmTitle[3],
		IDSEARCH3=kmUrl[3],
		IDSEARCHNAME4=kmTitle[4],
		IDSEARCH4=kmUrl[4],
		IDSEARCHNAME5=kmTitle[5],
		IDSEARCH5=kmUrl[5],
		IDSEARCHNAME6=kmTitle[6],
		IDSEARCH6=kmUrl[6],
		IDSEARCHNAME7=kmTitle[7],
		IDSEARCH7=kmUrl[7],
		IDSEARCHNAME8=kmTitle[8],
		IDSEARCH8=kmUrl[8],
		IDSEARCHNAME9=kmTitle[9],
		IDSEARCH9=kmUrl[9],
		IDSEARCHNAME10=kmTitle[10],
		IDSEARCH10=kmUrl[10],
		IDSEARCHNAME11=kmTitle[11],
		IDSEARCH11=kmUrl[11],
		IDSEARCHNAME12=kmTitle[12],
		IDSEARCH12=kmUrl[12],
	})
	
	kmTitle[1]=rr.IDSEARCHNAME1
	kmUrl[1]=rr.IDSEARCH1
	kmTitle[2]=rr.IDSEARCHNAME2
	kmUrl[2]=rr.IDSEARCH2
	kmTitle[3]=rr.IDSEARCHNAME3
	kmUrl[3]=rr.IDSEARCH3
	kmTitle[4]=rr.IDSEARCHNAME4
	kmUrl[4]=rr.IDSEARCH4
	kmTitle[5]=rr.IDSEARCHNAME5
	kmUrl[5]=rr.IDSEARCH5
	kmTitle[6]=rr.IDSEARCHNAME6
	kmUrl[6]=rr.IDSEARCH6
	kmTitle[7]=rr.IDSEARCHNAME7
	kmUrl[7]=rr.IDSEARCH7
	kmTitle[8]=rr.IDSEARCHNAME8
	kmUrl[8]=rr.IDSEARCH8
	kmTitle[9]=rr.IDSEARCHNAME9
	kmUrl[9]=rr.IDSEARCH9
	kmTitle[10]=rr.IDSEARCHNAME10
	kmUrl[10]=rr.IDSEARCH10
	kmTitle[11]=rr.IDSEARCHNAME11
	kmUrl[11]=rr.IDSEARCH11
	kmTitle[12]=rr.IDSEARCHNAME12
	kmUrl[12]=rr.IDSEARCH12
	
	writeSpeedy(kmTitle,kmUrl)
end

