const
  StepCount=15;
var
  NewColor,OldColor:integer;
  AniIndex,ColorStep:integer;
  SearchEngineIndex, KindIndex:integer;
//  tmrAni:TTimer;
  mibgColor,miInterval,  miBlue,miGreen,miOrange,miRed,miPurple,miPink,miGray,miTranparent:TMenuItem;

	CodeRule:string; //是否需要编码，true则 加encodeURIComponent （editfind..text）
	CategoryElseHtml:string;  
  pmenu:TPopupMenu;
  Section:string;   
	imgsLogo,CategoryCaption,CategoryHtml;  
	CategoryCount:integer;  //当前选择的搜索引擎的分类数量
  ImgLogoCounts,PageIndex,PageCount,Selindex:integer; // 搜索引擎总数量，当前页，总页，当前选择的搜索引擎的Tag
  SearchEngineName:string; //当前选择的搜索引擎名称 例如：baidu、yahoo、google、youdao、aiwen等 
  imgCount,	RowCount,ColCount:integer;

//--------  为搜索引擎分类类别赋初值----------
procedure SetAllEngineCategory;
begin
	// 为引擎分类赋初值 
	CategoryCaption:=['网页','新闻','图片','音乐','博客','说吧','视频','网页','新闻','图片','音乐','博客','说吧','视频'];
	CategoryHtml:=['网页','新闻','图片','音乐','博客','说吧','视频','网页','新闻','图片','音乐','博客','说吧','视频'];		
    imgsLogo:=[img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,
	           img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,
						 img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,
						 img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,
						img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0,img0];
end;

procedure RefreshImgsLogo;
var
  ini:TIniFile;
  FirstIndex:integer;
begin
  ini:=TIniFile.create(Widgetpath + 'Engine.ini');
  try
    FirstIndex:=(PageIndex-1)*imgCount;
		for i:=0 to imgCount-1 do
		begin
			if FirstIndex+i<ImgLogoCounts then
			begin
				imgsLogo[i].hint:=ini.ReadString(Section,inttostr(FirstIndex+i)+'caption','');
				imgsLogo[i].showhint:=true;
				imgsLogo[i].visible:=true;	
        imgsLogo[i].PopupMenu:=pmenu;				
				s:=ini.ReadString(Section, inttostr(FirstIndex+i)+'path','');
				try
					if FileExists(WidgetPath+'images\' + s) then
					begin
						imgsLogo[i].FileName:=WidgetPath+'images\' + s ;
					end	else begin
						imgsLogo[i].FileName:='';   
				    imgsLogo[i].visible:=false;
					end;
				except
						
				end
			end  else
			begin
			//	if firstindex > 15  then showmessage(inttostr(i));
				imgsLogo[i].FileName:='';   
				imgsLogo[i].hint:='';
				imgsLogo[i].visible:=false;
        imgsLogo[i].PopupMenu:='';		
			end;
			SetSettingInt('PageIndex',PageIndex);
		end;
		
  finally
    ini.free;
  end;
end;

procedure RefreshPageInfo;
begin
  if ImgLogoCounts mod imgCount=0 then
    PageCount:=ImgLogoCounts div imgCount
  else
    PageCount:=(ImgLogoCounts div imgCount)+1;
  if PageCount=0 then PageCount:=1;
  lblPage.caption:=format('%d / %d',[PageIndex,PageCount]);  
  imgNext.Enabled:=PageIndex<PageCount;
  imgPrev.Enabled:=PageIndex>1;
end;
  
  //--- PopMenu 相关函数 ----------------------------
//系统默认已经定义了__PopupMenu的组件,只需直接调用即可
function PopMenu_Add(aCaption:string; aFunctionName:string; aTag:integer):TMenuItem;
begin
  result:=nil;
  if trim(aCaption)='' then exit;
  result:=TMenuItem.Create(Widget);
  result.caption:=aCaption;
  result.radioitem:=true;
  result.OnClick:=aFunctionName;  
  result.Tag:=aTag;
  if aTag = 0 then  
	begin
	   result.Checked:=true; 			 
	end;
	
  __PopupMenu.items.Add(result);
end;

procedure PopMenu_AddChild(aParentMenu:TMenuItem; aCaption:stirng; aFunctionName:string; aTag:integer);
var
  mi:TMenuItem;
begin
  if aParentMenu=nil then exit;
  mi:=TMenuItem.Create(Widget);
  mi.caption:=aCaption;
  mi.OnClick:=aFunctionName;
  mi.Tag:=aTag;
  aParentMenu.add(mi);
end;

procedure PopMenu_Clear;
begin
  __PopupMenu.Items.Clear;
end;
    
procedure PopMenu_Popup;
begin
  __PopupMenu.Popup(Mouse.CursorPos.x,Mouse.CursorPos.y);
end;




 //--------  类别菜单单击----------
procedure KindClick(sender);
var
  i,count:integer;
  ini:TIniFile;
	strtmp:string;
begin
  BeginUpdate;
	KindIndex:=sender.tag;
  SearchEngineName:=lowercase(SearchEngineName);
	//showmessage(SearchEngineName);
  ini:=TIniFile.create(Widgetpath + 'Category.ini');
  try
		CodeRule:=ini.ReadString(SearchEngineName,inttostr(kindIndex) + 'coderule','false');  //是否需要编码encodeURIComponent
		//showmessage(coderule);
	finally 
	  ini.free;
	end;  
  sender.checked:=true;
  EndUpdate;
end;

//--------  添加所有类别到菜单----------
procedure imgKindAddMenu;
var
  i,count:integer;
  ini:TIniFile;
	strtmp:string;
begin
  SearchEngineName:=lowercase(SearchEngineName);
	//showmessage(SearchEngineName);
  ini:=TIniFile.create(Widgetpath + 'Category.ini');
  try
	  count:=ini.Readinteger(SearchEngineName,'count',0);
    if count <> 0 then
		begin
			CategoryCount:=count;	
		  for i:=0 to count-1 do 
			begin
			  strtmp:=ini.ReadString(SearchEngineName,inttostr(i) + 'caption','网页');
			  CategoryCaption[i]:=strtmp;
				strtmp:=ini.ReadString(SearchEngineName,inttostr(i) + 'html','http://www.baidu.com');
				CategoryHtml[i]:=strtmp;
			end;   
			CodeRule:=ini.ReadString(SearchEngineName, '0coderule','false');
	    PopMenu_Clear;  
	    for i:=0 to  CategoryCount-1 do
	    PopMenu_Add(CategoryCaption[i],'KindClick',i);
		end;
	finally 
	  ini.free;
	end;  
  
  KindIndex:=0; //设置分类初值为第一个 ，一般为网页 或全部
end;   




procedure ShowKindMenu;
begin
 PopMenu_Popup;  
end;

procedure HideLogoMenu;
var
  i:integer;
begin
  BeginUpdate;
  if Docking then
    Widget.height:=78;
  imgLogoMenu.visible:=false;

  imgPrev.visible:=false;
  imgNext.visible:=false;
  lblPage.visible:=false; 
	
  for i:=0 to imgcount -1 do
	begin
    imgsLogo[i].visible:=false;
	end;
  EndUpdate;
end;

procedure ShowLogoMenu;
begin
  BeginUpdate;	
  Widget.height:=33 + imgLogoMenu.height; 
  imgLogoMenu.visible:=true;
  imgPrev.visible:=true;
  imgNext.visible:=true;
  lblPage.visible:=true;
	for i:=0 to imgcount -1 do
	begin
    imgsLogo[i].visible:=true;
	end;
  
  EndUpdate;
end;

procedure SetSearchEngineName;
var
  ini:TIniFile;
  SenderTag,FirstIndex:integer;  
begin  
  BeginUpdate;
  FirstIndex:=(PageIndex-1)*imgCount;
  SenderTag:=SearchEngineIndex - firstIndex;  
  if imgsLogo[SenderTag].FileName<>'' then 
  begin 
	  //imgLogo.FileName:=imgslogo[aIndex].FileName;
	  ini:=TIniFile.create(Widgetpath + 'Engine.ini');
	  try
	    SearchEngineName:=ini.ReadString(Section,inttostr(SearchEngineIndex)+'name','baidu');
		  imgLogo.FileName:=	WidgetPath+'images\' + ini.ReadString(Section,inttostr(SearchEngineIndex)+'path','logo_baidu.png') ;
		  imgLogo.Hint:=ini.ReadString(Section,inttostr(SearchEngineIndex)+'caption','百度');
	  finally
	    ini.free;
	  end;  
  end;
  imgKindAddMenu;
  SetSettingInt('EngineSumIndex',SearchEngineIndex);
  EndUpdate;

end;

procedure SetimgsLogoInfo(row,col:integer);
var
  FirstIndex:integer;
	aindex:integer;
begin
	ColCount:=	col;
	RowCount:=	row;
  imgCount:=row*col; 		
	//showmessage(inttostr(SearchEngineIndex));
	
	aindex:=(SearchEngineIndex + 1 ) Div imgCount;	
	if  aindex  >0 then
  begin
	  pageindex:= aindex + 1;	
	end else
	begin
	  pageindex:=1;
	end;
	//showmessage(inttostr(aindex) + ' ' + inttostr(pageindex));
	//动态生成image组件 
	for i:=0 to imgcount-1 do
  begin		
		imagtmp:=Txspimage.create(widget);
		imagtmp.Parent:=widget;
	  imagtmp.tag:=i;	
		imagtmp.left:=17 + 50*(i mod ColCount);
	  imagtmp.Top:=70 + 40*(i div ColCount );
		imagtmp.Width:=40;
		imagtmp.Height:=35;
		imagtmp.ImageStyle.Count:=2;
		imagtmp.cursor:=crHandPoint;
    imagtmp.autosize:=0;
		imagtmp.ImageStyle.align:=4;
		imagtmp.OnMouseEnter:='imgMouseEnter';
    imagtmp.OnMouseLeave:='imgMouseLeave';
    imagtmp.PopupMenu:=pmenu;
    imagtmp.OnClick:='LogoClick';
		imgsLogo[i]:=imagtmp;
	end;	
	
	imgLogoMenu.left:=5;
	imgLogoMenu.top:=33;
	imglogomenu.width:=50*colcount + 20;
	imglogomenu.height:=40*rowcount  + 70 -33 +  10;
	widget.height:=imglogomenu.height + 33;
	lblPage.left := imgLogoMenu.left + imgLogoMenu.width - lblPage.Width - 15;
	
  RefreshImgsLogo;
  RefreshPageInfo;	
  SetSearchEngineName;	
	HideLogoMenu;
end;


procedure SwitchColor;
var
  s:string;
  i:integer;
begin
  Randomize;
  NewColor:=random*$7FFFFF0;
  BeginUpdate;
  if not shpbg.visible then
    shpbg.visible:=true;
  shpbg.ColorTo:=NewColor;
  shpbg.Color:=shpbg.ColorTo+$300000;
  shpbg.BorderColor:=shpbg.ColorTo;
  EndUpdate;
//  AniIndex:=1;
//  ColorStep:=(NewColor-OldColor) div StepCount;
//  tmrAni.Enabled:=true;
  if not shpbg.visible then
    shpbg.visible:=true;
end;

procedure SwitchView;
var
  s:string;
begin
  BeginUpdate;
	SearchEngineIndex:=GetSettingInt('EngineSumIndex',0);		
  if Docking then //tow line
  begin
	  hidelogomenu;
	  SetimgsLogoInfo(4,2);
    imgbg.FileName:=WidgetPath+'images\bg_dock.png';
    Widget.Width:=136;
    Widget.Height:=78;
    shpbg.left:=3;
    shpbg.top:=35;
    shpbg.width:=125;
    shpbg.height:=40;
    shpbg.Shape:=xstBottomRoundRect;
		imgLogo.left:=5;
		imgLogo.top:=0;
    imgColor.left:=110; 
    imgColor.top:=16;
    imgFind.left:=96;
    imgFind.top:=44;
    imgKind.left:=110;
    imgKind.top:=44;
    edtFind.left:=10;
    edtFind.top:=43;
    edtFind.width:=85;
    edtFind.height:=17;		
  end
  else //default
  begin
	  hidelogomenu;
		ColCount:=	GetSettingInt('ColCount',2);// 列数
	  RowCount:=	GetSettingInt('RowCount',4);//行数	
	  SetimgsLogoInfo(RowCount,ColCount);
    imgbg.FileName:=WidgetPath+'images\bg.png';
    Widget.Width:=425;
    Widget.Height:=250;
	  //imgbg.height:=20;
    shpbg.left:=67;
    shpbg.top:=3;
    shpbg.width:=340;
    shpbg.height:=31;
    shpbg.Shape:=xstRightRoundRect;
		imgLogo.left:=10;
		imgLogo.top:=0;
    imgColor.left:=389; 
    imgColor.top:=4;
    imgFind.left:=353;
    imgFind.top:=12;
    imgKind.left:=370;
    imgKind.top:=11;
    edtFind.left:=81;
    edtFind.top:=10;
    edtFind.width:=270;
    edtFind.height:=18;
		
  end;  
  EndUpdateAll;
end;

procedure SearchHtml;
const
  ac_baidu='dwso_5_pg';
  ac_google='aff-cs-gbto';
  //ac_baidu='8jie_dg';
  //ac_google='pub-7809292325516887';
var
  s:string;
begin
  if trim(edtFind.text)='' then exit;
	SearchEngineName:=lowercase(SearchEngineName);
//	showmessage(SearchEngineName);
	s:=CategoryHtml[kindindex];
	if CodeRule ='true' then
	begin
	   	s:=s + encodeURIComponent(trim(edtFind.text));		
	end else
	begin
    s:=s + trim(edtFind.text);		
	end;

 // showmessage(s);
	ShellExecute(0,'open',s,'','',1);
end;


procedure OnEditFindEnter(sender);
begin
  SearchHtml;
end;



procedure LogoClick(sender);
var
  FirstIndex:integer;
begin
  FirstIndex:=(PageIndex-1)*imgCount;
  SearchEngineIndex:=firstIndex + sender.tag;  
  SetSearchEngineName;
  HideLogoMenu;
end;



function AddMenu(aCaption:string; aTag:integer):TMenuItem;
begin
  result:=TMenuItem.Create(Widget);
  result.caption:=aCaption;
  result.tag:=aTag;
  result.onClick:='mibgColorClick';
  mibgColor.Insert(0,result);
end;



procedure InitMenu;
begin
  mibgColor:=TMenuItem.Create(Widget);
  mibgColor.Caption:='背景颜色';
  //mibgColor.onclick:='mibgColorClick';
  pm.Items.Insert(0,mibgColor);
  miGray:=AddMenu('灰色',7);
  miPink:=AddMenu('品红',6);
  miPurple:=AddMenu('紫色',5);
  miRed:=AddMenu('红色',4);
  miOrange:=AddMenu('橙色',3);
  miGreen:=AddMenu('绿色',2);
  miBlue:=AddMenu('蓝色',1);
  miTranparent:=AddMenu('透明',0);
	
	
  miInterval:=TMenuItem.Create(Widget);
  miInterval.Caption:='-';
  pm.Items.Insert(2,miInterval);
	
	
	pmenu:=TPopupMenu.Create(Widget);
  pmenu.AutoHotkeys:=maManual;

  miMoveNext:=TMenuItem.Create(Widget);
  miMoveNext.Caption:='向后移';
  miMoveNext.onclick:='miMoveNextOnClick';
  pmenu.Items.Insert(0,miMoveNext);
  
  miMovePrior:=TMenuItem.Create(Widget);
  miMovePrior.Caption:='向前移';
  miMovePrior.onclick:='miMovePriorOnClick';
  pmenu.Items.Insert(0,miMovePrior); 
  
 { miInterval:=TMenuItem.Create(Widget);
  miInterval.Caption:='-';
  pmenu.Items.Insert(0,miInterval);  
  
  
  miDel:=TMenuItem.Create(Widget);
  miDel.Caption:='删除';
  miDel.onclick:='miDelOnClick';
  pmenu.Items.Insert(0,miDel);}


end;



procedure imgMouseEnter(sender);
begin
  //img:=imgs[sender.tag];
  BeginUpdate;

//  lblhint.caption:= trim(img.hint);
  EndUpdate;
  SelIndex:=sender.tag;
end;

procedure imgMouseLeave(sender);
begin
  BeginUpdate;
  imgsLogo[sender.tag].NeedRefresh:=true;
  //lblhint.caption:='';
  EndUpdate;
  //SelIndex:=-1;
end;

procedure imgLogoMouseLeave(sender);
begin
  //HideLogoMenu;
end;

procedure imgbgMouseEnter;
begin
{
	if docking then 
	begin

	end else
	begin
	 hideLogomenu;
	end;
	}
end;




//读取当前Widget的INI设置
procedure InitIni;
var
  FirstIndex:integer;
  ini:TIniFile;
	i:integer;
	imagtmp:Txspimage;
	lbl:txspimage;
  aIndex:integer;
begin
  beginupdate;

	ColCount:=	GetSettingInt('ColCount',2);// 列数
	RowCount:=	GetSettingInt('RowCount',4);//行数	
	imgCount:=RowCount * ColCount;
	
	PageIndex:=GetSettingInt('PageIndex',1);		
	SearchEngineIndex:=GetSettingInt('EngineSumIndex',0);

	SetimgsLogoInfo(RowCount,ColCount);
	
  Endupdate;
end;





//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
var
  ini:TIniFile;
begin
  Widget.KeyPreview:=true;
  Widget.OnKeyDown:='FormKeyDown';
  
  OldColor:=0;
  Section:='default';
	Selindex:=0;
  try
	   ini:=TIniFile.create(Widgetpath + 'Engine.ini');
     imgLogoCounts:= strtoint(ini.ReadString(Section,'count','25')); //读出所有搜索引擎的数量
  finally
    ini.free;
  end; 	
	  SetAllEngineCategory;//为引擎分类名称赋初值  
	
  InitMenu;
	InitIni;  
  
  edtFind.OnEnter:='OnEditFindEnter';
  edtFind.OnMouseEnter:='OnEditFindMouseEnter';
	imgbg.OnMouseEnter:='imgbgMouseEnter';
	imgLogoMenu.OnMouseLeave:='imgLogoMouseLeave';
  NewColor:=GetSettingInt('bgColor',0);
	
  if Docking then   SwitchView;
	  OldColor:=0;
  if NewColor>0 then
    SwitchColor;
end;

{procedure OntmrAniTimer(sender)
begin
  if AniIndex>25 then
  begin
    tmrAni.Enabled:=false;
  	BeginUpdate;
  	shpbg.ColorTo:=NewColor;
  	OldColor:=NewColor;
  	EndUpdate;
  end
  else
    shpbg.ColorTo:=OldColor+ColorStep*AniIndex;
  shpbg.Color:=shpbg.ColorTo+$300000;
  shpbg.BorderColor:=shpbg.ColorTo;

  inc(AniIndex);
end;
}
//--- Widget移入边栏事件，当Widget被移到边栏上时会触发此事件 ---
procedure OnWidgetDock;
begin
  SwitchView;
end;

//--- Widget移出边栏事件，当Widget被移出边栏时会触发此事件 ---
procedure OnWidgetUnDock;
begin
  SwitchView;
end;

//--- Widget事件，当打开鼠标移出Widget区域时触发此事件 ---
procedure OnWidgetMouseLeave;
begin
  //showmessage('leave');
 //if imgLogoMenu.visible then
  //  HideLogoMenu;

end;

procedure OnEditFindMouseEnter(sender);
begin
  //OnWidgetMouseLeave;
	if not docking then
	begin
	  	if imgLogoMenu.visible then
      HideLogoMenu;
	end;


end;

procedure mibgColorClick(sender);
begin
{1蓝：#588dd9 #014ab9 
2绿：#5cde00 #288f00
3橙：e37c05  c14500
4红：e01f16  95171b
5紫：f470b9  833799 
6品红ee6cb2  e3258f
7灰：c6c6c8  a1a5a8}
  BeginUpdate;
  shpbg.visible:=(sender.tag>=1) and (sender.tag<=7);
  case sender.tag of
    1: begin shpbg.Color:=$d98d58; shpbg.ColorTo:=$b94a01; end;
    2: begin shpbg.Color:=$00de5c; shpbg.ColorTo:=$008f28; end;
    3: begin shpbg.Color:=$057ce3; shpbg.ColorTo:=$0045c1; end;
    4: begin shpbg.Color:=$181fca; shpbg.ColorTo:=$1315af; end;
    5: begin shpbg.Color:=$b970f4; shpbg.ColorTo:=$993783; end;
    6: begin shpbg.Color:=$b26cee; shpbg.ColorTo:=$8f25e3; end;
    7: begin shpbg.Color:=$c6c6c8; shpbg.ColorTo:=$a8a5a1; end;
  end;
    
  shpbg.BorderColor:=shpbg.ColorTo;
  EndUpdate;
end;
procedure imgLogoClick(Sender:TObject);
begin
  ShowLogoMenu
end;
procedure imgColorClick(Sender:TObject);
begin
  SwitchColor
end;
procedure imgFindClick(Sender:TObject);
begin
  SearchHtml
end;
procedure imgKindClick(Sender:TObject);
begin
  ShowKindMenu
end;

procedure PrevPage;
begin
  BeginUpdate;
  PageIndex:=PageIndex-1;
  RefreshImgsLogo;
  RefreshPageInfo;
  ShowLogoMenu
  EndUpdate;
end;

procedure NextPage;
begin
  BeginUpdate;
  PageIndex:=PageIndex+1;
  RefreshImgsLogo;
  RefreshPageInfo;
  ShowLogoMenu;
  EndUpdate;
end;


procedure MovePrior(index:integer);
var
  aIndex:integer;
  ini:TIniFile;
  s1,s2:string;
  varcaption,varname,varpath:string;
begin
  aIndex:=(PageIndex-1)*imgCount+index;
 // showmessage(inttostr(ShortcutNums) + inttostr(aindex));
  if (aIndex<=0) or (aIndex>=ImgLogoCounts) then exit;
  ini:=TIniFile.create(widgetpath + 'Engine.ini');
  try
    if aIndex+1<=ImgLogoCounts then
    begin
      s1:=inttostr(aIndex-1);
      s2:=inttostr(aIndex);
  
      varcaption:=ini.ReadString(Section,s1+'caption','');
      varname:=ini.ReadString(Section,s1+'name','');
      varpath:=ini.ReadString(Section,s1+'path','');
      
      ini.WriteString(Section,s1+'caption',ini.ReadString(Section,s2+'caption',''));
      ini.WriteString(Section,s1+'name',ini.ReadString(Section,s2+'name',''));
      ini.WriteString(Section,s1+'path',ini.ReadString(Section,s2+'path',''));
      
      ini.WriteString(Section,s2+'caption',varcaption);
      ini.WriteString(Section,s2+'name',varname);
      ini.WriteString(Section,s2+'path',varpath);       
    end;  
  finally
    ini.free;
  end;
  RefreshPageInfo;
  RefreshImgsLogo;
end;

procedure MoveNext(index:integer);
var
  aIndex:integer;
  ini:TIniFile;
  s1,s2:string;
  varcaption,varname,varpath:string;
begin
  aIndex:=(PageIndex-1)*imgCount+index;
  if (aIndex<0) or (aIndex>=ImgLogoCounts-1) then exit;
  ini:=TIniFile.create(widgetpath + 'Engine.ini');
  try
    if aIndex+1<ImgLogoCounts then
    begin
      s1:=inttostr(aIndex);
      s2:=inttostr(aIndex+1);
      
      varcaption:=ini.ReadString(Section,s1+'caption','');
      varaption:=ini.ReadString(Section,s1+'name','');
      varpath:=ini.ReadString(Section,s1+'path','');
      
      ini.WriteString(Section,s1+'caption',ini.ReadString(Section,s2+'caption',''));
      ini.WriteString(Section,s1+'name',ini.ReadString(Section,s2+'name',''));
      ini.WriteString(Section,s1+'path',ini.ReadString(Section,s2+'path',''));
      
      ini.WriteString(Section,s2+'caption',varcaption);
      ini.WriteString(Section,s2+'name',varaption);
      ini.WriteString(Section,s2+'path',varpath);        
    end;  
  finally
    ini.free;
  end;
  RefreshPageInfo;
  RefreshImgsLogo;
end;


procedure miMovePriorOnClick(sender);
begin 
   MovePrior(SelIndex);
	 
end;

procedure miMoveNextOnClick(sender);
begin
    MoveNext(SelIndex); 
end;



procedure OnWidgetDeactivate;
begin
  BeginUpdate;
    hidelogomenu;
  EndUpdate;
end;




procedure MoveLeft;
var
  TmpEngineIndex:integer;
begin
  exit;  
	{
  if imgLogoMenu.visible = false then exit ;  
  FirstIndex:=(PageIndex-1)*imgCount;
  TmpEngineIndex:=SearchEngineIndex-1; 

  if TmpEngineIndex < FirstIndex then
  begin
    if FirstIndex >0 then  //换上一页
	begin
	  PrevPage;
      SearchEngineIndex:= TmpEngineIndex;  
	  SetSearchEngineName;	   
	end else 
    begin
      //showmessage('已经到第一个')
      exit;
    end;	 
  end else
  begin
    SearchEngineIndex:= TmpEngineIndex;  
	SetSearchEngineName;
  end;}
end;

procedure MoveRight;
var
  TmpEngineIndex:integer;  
begin
  exit;
	{
  if imgLogoMenu.visible = false then  exit ;  
  FirstIndex:=(PageIndex-1)*imgCount;
  TmpEngineIndex:=SearchEngineIndex +1; 
  if TmpEngineIndex < ImgLogoCounts  then //还未到最后一个，继续后移
  begin
    if (FirstIndex + imgcount ) <= TmpEngineIndex then // 换下一页
	  begin
	    NextPage;	  
      SearchEngineIndex:= TmpEngineIndex;  
	    SetSearchEngineName;	   
	  end else 
    begin
      SearchEngineIndex:= TmpEngineIndex;  
	    SetSearchEngineName;
    end;	 
  end else
  begin
      //showmessage('已经到第一个')
      exit;
  end;
	}
end;

procedure MoveUp;
var
  TmpEngineIndex:integer;
begin
  if imgLogoMenu.visible = false then 
  begin
	  FirstIndex:=(PageIndex-1)*imgCount;
	  TmpEngineIndex:=SearchEngineIndex-1; 
	  if TmpEngineIndex < FirstIndex then 
	  begin
			if FirstIndex >0 then  //换上一页
			begin
				PageIndex:=PageIndex-1;
				RefreshImgsLogo;
				RefreshPageInfo;
				hidelogomenu;
				SearchEngineIndex:= TmpEngineIndex;  
				SetSearchEngineName;	   
			end else // 到最前一个，循环到最后一个
			begin					
				pageindex:=PageCount;
				RefreshImgsLogo;
				RefreshPageInfo;
				hidelogomenu;
				SearchEngineIndex:= imgLogocounts-1;
				SetSearchEngineName;		      
			end;		 
		end else
		begin
				SearchEngineIndex:= TmpEngineIndex;  
				SetSearchEngineName;
		end; 
  end else
  begin
	  FirstIndex:=(PageIndex-1)*imgCount;
	  TmpEngineIndex:=SearchEngineIndex -1; 	  
	  if TmpEngineIndex < FirstIndex then
	  begin
	    if FirstIndex >0 then  //换上一页
		  begin
			  PrevPage;
			  SearchEngineIndex:= TmpEngineIndex;  
			  SetSearchEngineName;	   
		  end else 
	    begin       
				pageindex:=PageCount;
				RefreshImgsLogo;
				RefreshPageInfo;
				SearchEngineIndex:= imgLogocounts-1;
				SetSearchEngineName;	
	    end;		 
	  end else
	  begin
	    SearchEngineIndex:= TmpEngineIndex;  
		  SetSearchEngineName;
	  end;
  end;	
end;


procedure MoveDown;
var
  TmpEngineIndex:integer;
begin
  if imgLogoMenu.visible = false then  //下拉菜单不显示时，顺序向下移动
  begin
		FirstIndex:=(PageIndex-1)*imgCount;
		TmpEngineIndex:=SearchEngineIndex +1; 
		if TmpEngineIndex < ImgLogoCounts  then //还未到最后一个，继续后移
		begin
			if (FirstIndex + imgcount ) <= TmpEngineIndex then // 换下一页
			begin
			  PageIndex:=PageIndex+1;
	      RefreshImgsLogo;
	      RefreshPageInfo;
				hidelogomenu;
		    SearchEngineIndex:= TmpEngineIndex;  
			  SetSearchEngineName;	   
			end  else   //不必换页
			begin
			  SearchEngineIndex:= TmpEngineIndex;  
	  	  SetSearchEngineName;
			end;	 
		end else // 到最后一页，换第一页
		begin
			PageIndex:=1;
			RefreshImgsLogo;
			RefreshPageInfo;
			hidelogomenu;				
			SearchEngineIndex:=0;  
			SetSearchEngineName;
		end;
  end else   //下拉菜单显示时，按列向下移动
	begin
	  FirstIndex:=(PageIndex-1)*imgCount;
	  TmpEngineIndex:=SearchEngineIndex + 1; 
	  if TmpEngineIndex < ImgLogoCounts  then //还未到最后一个，继续后移
	  begin
	    if (FirstIndex + imgcount ) <= TmpEngineIndex then // 换下一页
		  begin
		   NextPage;
		 // showmessage(inttostr(TmpEngineIndex));
	      SearchEngineIndex:= TmpEngineIndex;  
		    SetSearchEngineName;	   
		  end else 
	    begin
	      SearchEngineIndex:= TmpEngineIndex;  
		    SetSearchEngineName;
	    end;	 
	  end else
	  begin
			PageIndex:=1;
			RefreshImgsLogo;
			RefreshPageInfo;		
			SearchEngineIndex:=0;  
			SetSearchEngineName;
	  end;
  end;		
end;

procedure OnReturn;
begin
   if imgLogoMenu.visible then  
   begin
     HideLogoMenu;
	 exit;
   end else
   begin   
     ShowLogoMenu
   end;   
  
end;

procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
const
  VK_RETURN=13;
  VK_LEFT = 37;
  VK_UP = 38;
  VK_RIGHT = 39;
  VK_DOWN = 40;
begin
  case Key of
    VK_LEFT:  MoveLeft;
    VK_RIGHT: MoveRight;
    VK_UP:    MoveUp;
    VK_DOWN:  MoveDown;
	VK_RETURN: OnReturn;
  end;
  
end;

