/**
*@  ClassName : AshGoogleSearch (package)
 Author : 			An.sehan (www.happyfri.com / plandas@naver.com)
 Creation Date : 	2009.03.22
 Last Modified : 	2009.03.22
 Version : 		0.5

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
*/

var AshGoogleSearch=new AshEventDispatcher();
AshGoogleSearch.SHOW='show';			
AshGoogleSearch.HIDE='hide';			
AshGoogleSearch.START='start';			
AshGoogleSearch.SEARCH='search';		
AshGoogleSearch.IMAGE_LOADED='imageLoaded';	
AshGoogleSearch.SELECTED='selected'		
AshGoogleSearch.google=null;
AshGoogleSearch.start=function(gs_api_url){if(arguments.callee.started)return;
	var ifr=document.body.appendChild(document.createElement('iframe'));
	var se=new AshEventDispatcher(ifr);
	se.addEventListener('load',function(e){AshGoogleSearch.google=ifr.contentWindow.google;
		AshGoogleSearch.dispatchEvent(new AshEvent(AshGoogleSearch.START));
		AshGoogleSearch.start.started=true;
	});
	ifr.setAttribute('src',gs_api_url);
	ifr.setAttribute('frameborder','0');
	ifr.style.width=ifr.style.height='0px';
}
AshGoogleSearch.Image=Ash.createClass('AshGoogleSearch.Image',AshAgent);
Ash.addPrototype(AshGoogleSearch.Image,{gsi:null,body:null,prev:null,next:null,pageInfo:null,searchForm:null,imgContainer:null,selectedImgSrc:'',totalPage:0,pageIndex:0,onShow:null,onHide:null,onSearch:null,onImageLoaded:null,initialize:function(){var own=this;
		this.owner=(function(){var ele=document.createElement('div');
			ele.innerHTML="<div style='position:absolute; display:none; min-width:300px; height:80px; background-color:#111111; border:3px solid #000000; color:#AAAAAA; font-family:Arial; ' ashdrag='true' onselectstart='return false;' ondragstart='return false;'>"+"<div name='pageInfo' style=' position:absolute; width:100%; height:20px; text-align:center; color:#888888; font-size:9px; cursor:default; ' ashdrag='true' ashdragtarget='parent' ></div>"+"<div title='close' style='position:absolute; z-index:1; right:0px; top:0px; width:20px; height:20px; text-align:center; cursor:pointer; font-size:12px;' onmouseover=\"style.color='#ffffff';\" onmouseout=\"style.color='';\" onclick='parentNode.sgis.hide();'>×</div>"+"<div name='prev' title='previous page' style='position:absolute; z-index:2; left:0px; top:15px; width:20px; height:50px; text-align:center; line-height:40px; cursor:pointer; ' onmouseover=\"style.color='#ffffff';\" onmouseout=\"style.color='';\" onclick='parentNode.sgis.prevPage();'>«</div>"+"<div name='next' title='next page' style='position:absolute; z-index:3; right:0px; top:15px; width:20px; height:50px; text-align:center; line-height:40px; cursor:pointer; ' onmouseover=\"style.color='#ffffff';\" onmouseout=\"style.color='';\" onclick='parentNode.sgis.nextPage();'>»</div>"+"<div name='body' style=' position:relative; top:15px; height:50px; padding:0px 20px 0px 20px; cursor:default; ' ashdrag='true' ashdragtarget='parent' ></div>"+"<div title='new searching query' style='position:absolute; left:20px; bottom:2px; cursor:pointer; font-size:7px; color:#FF8000; font-weight:bold;' onmouseover=\"style.color='#ffffff';\" onmouseout=\"style.color='#FF8000';\" onclick='parentNode.sgis.showSearchForm();'>SEARCH</div>"+"<div onclick='Ash.support();' style='position:absolute; right:20px; bottom:0px; color:#AAAAAA; font-size:9px; cursor:pointer;'>"+"AshGoogleSearch<span style='color:#A25100; font-size:9px;'> (ver 0.5)</span><span style='color:#A25100; font-size:8px;'> By AshAPI</span></div>"+"</div>";
			ele.firstChild.sgis=own;
			ele.firstChild.onmousedown=function(){AshDepthManager.manager.front(own.owner);
			}
			return document.body.appendChild(ele.firstChild);
		})();
		this.body=this.getChildByName('body');
		this.prev=this.getChildByName('prev');
		this.next=this.getChildByName('next');
		this.pageInfo=this.getChildByName('pageInfo');
		this.searchForm=(function(){var ele=document.createElement('div');
			ele.innerHTML="<div style=' width:155px; padding:5px; background-color:#111111; border:3px solid #000000; color:#AAAAAA; font-family:Arial; '>"+"<form style='display:inline;' onsubmit='parentNode.sgis.search(this.kwd.value); AshPopup.manager.clear(); return false;'>"+"<input name='kwd' type='text' style=' color:#666666; font-size:12px; background-color:#000000; border:1px solid #222222;'></form>"+"</div>";
			return ele.firstChild;
		})();
		this.imgContainer=(function(){var ele=document.createElement('span');
			ele.style.display='inline-block';
			ele.style.backgroundColor='#000000';
			ele.style.width='80px';
			ele.style.height='50px';
			ele.style.margin='0px 2px 0px 2px';
			ele.style.cursor='pointer';
			ele.style.filter="progid:DXImageTransform.Microsoft.Wave(Add='false', LightStrength=100, Phase=75, Strength=0,  Freq=1)";
			AshUtil.filter.opacity(ele,0);
			return ele;
		})();
		this.gsi=new AshGoogleSearch.google.search.ImageSearch();
		
		this.gsi.setSearchCompleteCallback(this,this.onSearchComplete,null);
		 
		if(arguments[0]&&typeof arguments[0]=='function')arguments[0].call(this);
		this.addEventListener(AshGoogleSearch.SHOW,function(e){if(this.onShow!=null)this.onShow(e);
		});
		this.addEventListener(AshGoogleSearch.HIDE,function(e){if(this.onHide!=null)this.onHide(e);
		});
		this.addEventListener(AshGoogleSearch.SEARCH,function(e){if(this.onSearch!=null)this.onSearch(e);
		});
		this.addEventListener(AshGoogleSearch.IMAGE_LOADED,function(e){if(this.onImageLoaded!=null)this.onImageLoaded(e);
		});
	},show:function(src){if(this.owner.style.display=='none'){this.owner.style.display='block';
			this.dispatchEvent(new AshEvent(AshGoogleSearch.SHOW));
		}
		var d=document.documentElement,dw=d.clientWidth,dh=d.clientHeight,sinfo=AshUtil.document.scrollInfo();
		var w=this.getWidth(),h=this.getHeight(),x=sinfo.left+(dw/2-w/2),y=sinfo.top+(dh/2-h/2);
		AshDepthManager.manager.front(this.owner);
		this.tween( {x:x,y:y},800);
	},hide:function(){this.owner.style.display='none';
		this.dispatchEvent(new AshEvent(AshGoogleSearch.HIDE));
	},search:function(keyword){if(keyword){this.show();
			this.gsi.execute(keyword);
		}
	},nextPage:function(){this.gsi.gotoPage(this.pageIndex+1);
	},prevPage:function(){this.gsi.gotoPage(this.pageIndex-1);
	},checkPage:function(){var cursor=this.gsi.cursor;
		this.totalPage=cursor.pages.length;
		this.pageIndex=cursor.currentPageIndex;
		this.prev.style.display=this.pageIndex==0?'none':'block';
		this.next.style.display=this.pageIndex==this.totalPage-1?'none':'block';
		this.pageInfo.innerHTML=(this.pageIndex+1)+' / '+this.totalPage;
	},showSearchForm:function(){var cloner=AshPopup.manager.create( this.searchForm,null,false,null,false);
		cloner.sgis=this;
		AshUtil.getChildByName(cloner,'kwd').focus();
	},onSearchComplete:function(){var own=this;
		if( this.gsi.results&&this.gsi.results.length>0){this.body.innerHTML='';
			var results=this.gsi.results;
			var imageScaler={width:70,height:50};
			for(var l=results.length,r,imgContainer,img,scaled,i=0; i<l; i++){r=results[i];
				imgContainer=this.imgContainer.cloneNode(true);
				imgContainer.title=r.contentNoFormatting;
				imgContainer.selectedImgSrc=r.unescapedUrl;
				imgContainer.onclick=function(){own.selectedImgSrc=this.selectedImgSrc;
					own.dispatchEvent(new AshEvent(AshGoogleSearch.SELECTED),{selectedImgSrc:own.selectedImgSrc} );
				}
				imgContainer.onmouseover=function(){this.firstChild.style.filter='';
				}
				imgContainer.onmouseout=function(){this.firstChild.style.filter="progid:DXImageTransform.Microsoft.Wave(Add='false', LightStrength=100, Phase=75, Strength=0,  Freq=1)";
				}		
				imgContainer.motioner=new AshMotionAgent(imgContainer);
				imgContainer.innerHTML="<img src='"+r.tbUrl+"' onload='parentNode.onLoadedImage();' style=\"filter:progid:DXImageTransform.Microsoft.Wave(Add='false', LightStrength=100, Phase=75, Strength=0,  Freq=1)'\">";
				imgContainer.onLoadedImage=function(o){this.motioner.alpha(1,500);
				}
				img=imgContainer.firstChild;
				scaled=AshGoogleSearch.google.search.Search.scaleImage(r.tbWidth,r.tbHeight,imageScaler);
				img.width=scaled.width;
				img.height=scaled.height;
				img.hspace=(80-scaled.width)/2;
				img.vspace=(50-scaled.height)/2;
				this.body.appendChild(imgContainer);
			}
			this.checkPage();
			this.dispatchEvent(new AshEvent(AshGoogleSearch.SEARCH),{results:this.gsi.results} );
			if(AshUtil.browser.ie6){this.body.style.width=(l*80+20)+'px';
				this.pageInfo.style.width=(this.getWidth()-6)+'px';
			}
		}
	}
});