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

local 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,"<SPAN")
	speedykm=string.sub(speedykm,p+5)
	p=string.find(speedykm,"</DIV>")
	speedykm=string.sub(speedykm,1,p-1)
	for i=1,16,1 do
		p=string.find(speedykm,"<SPAN")
		km[i]=string.sub(speedykm,1,p-1)
		speedykm=string.sub(speedykm,p+5)
		
		p=string.find(km[i],"href=\"")
		local temp=string.sub(km[i],p+6)
		p=string.find(temp,"\"")
		kmUrl[i]=string.sub(temp,1,p-1)
		
		p=string.find(km[i],"alt=\"")
		local temp=string.sub(km[i],p+5)
		p=string.find(temp,"\"")
		kmTitle[i]=string.sub(temp,1,p-1)
	end
	return kmUrl,kmTitle
end

local function writeSpeedy(kmTitle,kmUrl,thumnail)
	local f = assert(io.open(fname,"r"))
	local speedykm = f:read("*all")
	f:close()	
	local km={}
	local kmImage={}
	local p=string.find(speedykm,"<SPAN")
	local temp1=string.sub(speedykm,1,p+4)
	local temp2=string.sub(speedykm,p+5)
	p=string.find(temp2,"</DIV>")
	local temp=string.sub(temp2,1,p-1)
	temp2=string.sub(temp2,p)
	for i=1,16,1 do
		p=string.find(temp,"<SPAN")
		km[i]=string.sub(temp,1,p+4)
		temp=string.sub(temp,p+5)
		
		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,"alt")
		local g5=string.sub(g4,1,r+4)
		local g6=string.sub(g4,r+5)
		r=string.find(g6,"\"")
		g6=string.sub(g6,r)
		--kmImage[i]="http://www.thumbalizr.com/api/?url="..kmUrl[i]
		kmImage[i]=thumnail..kmUrl[i]
		km[i]=g1..kmUrl[i]..g3..kmImage[i]..g5..kmTitle[i]..g6
	end
	temp=""
	for i=1,16,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 = 260
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 = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 5, y = 161, width = 50, height = 10, id = "IDSEARCHNAME13", caption = "SEARCHNAME13"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 60, y = 161, width = 250, height = 10, id = "IDSEARCH13", caption = "SEARCH13"}, 
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 5, y = 174, width = 50, height = 10, id = "IDSEARCHNAME14", caption = "SEARCHNAME14"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 60, y = 174, width = 250, height = 10, id = "IDSEARCH14", caption = "SEARCH14"}, 
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 5, y = 187, width = 50, height = 10, id = "IDSEARCHNAME15", caption = "SEARCHNAME15"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 60, y = 187, width = 250, height = 10, id = "IDSEARCH15", caption = "SEARCH15"}, 
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 5, y = 200, width = 50, height = 10, id = "IDSEARCHNAME16", caption = "SEARCHNAME16"},
	{type = "edit", style = w32dlg.WS_BORDER+w32dlg.ES_AUTOHSCROLL, x = 60, y = 200, width = 250, height = 10, id = "IDSEARCH16", caption = "SEARCH16"}, 
	{type="static",x=5,y=215,width=90,height=10,id="IDSTATIC",caption="Thumnail Provider"},
	{type="combobox",style=w32dlg.WS_BORDER+w32dlg.WS_VSCROLL+w32dlg.WS_HSCROLL+w32dlg.CBS_DROPDOWN,x=80,y=215,width=180,height=150,id="IDTHUMNAIL"},
	{type="button",style=w32dlg.BS_DEFPUSHBUTTON,x=5,y=230,width=40,height=12,id="IDOK",caption="Done"}, 
})

function property_speedkm()
	local kmUrl,kmTitle=readSpeedy()
	local thumnail
	local m={"http://images.websnapr.com/?url=","http://open.thumbshots.org/image.pxf?url=","http://www.webshotspro.com/thumb.php?url=","http://www.thumbalizr.com/api/?url="}
	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],
		IDSEARCHNAME13=kmTitle[13],
		IDSEARCH13=kmUrl[13],
		IDSEARCHNAME14=kmTitle[14],
		IDSEARCH14=kmUrl[14],
		IDSEARCHNAME15=kmTitle[15],
		IDSEARCH15=kmUrl[15],
		IDSEARCHNAME16=kmTitle[16],
		IDSEARCH16=kmUrl[16],
		IDTHUMNAIL={"http://images.websnapr.com/?url=",m},
	})
	
	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
	kmTitle[13]=rr.IDSEARCHNAME13
	kmUrl[13]=rr.IDSEARCH13
	kmTitle[14]=rr.IDSEARCHNAME14
	kmUrl[14]=rr.IDSEARCH14
	kmTitle[15]=rr.IDSEARCHNAME15
	kmUrl[15]=rr.IDSEARCH15
	kmTitle[16]=rr.IDSEARCHNAME16
	kmUrl[16]=rr.IDSEARCH16
	thumnail=rr.IDTHUMNAIL
	
	writeSpeedy(kmTitle,kmUrl,thumnail)
end

