/**
 * @author Matthew Foster
 * @date   August 3rd 2007
 * @purpose To have a class that will respond and build Google Gwebresult objects into HTML elements describing the search result
 */
var GWebSearchResult = Class.create();
var appendChild = function(ele){
								this.appendChild(ele);
							}
	
Object.extend(Object.extend(GWebSearchResult.prototype, EventDispatcher.prototype),
				{
					
					initialize : function(container, options){
						
						this.buildInterface(container);
						this.createListener();
						this.buildOptions(options);
						
					},
					buildOptions : function(options){
						
						this.options = Object.extend({ format : true}, options);
						
					},
					buildInterface : function(container){
						
						this.container = $(container);
					
					},
					createListener : function(){
						
						this.searchHandle = this.handleSearch.bind(this);
						this.buildHandle = this.buildResult.bind(this);
						
						
					},
					handleSearch : function(obj){
					
						this.container.innerHTML = "";
						var header = $C("h3", { innerHTML : "Query : " + obj.query });
						
						this.container.appendChild(header);
						
						if(obj.results.length < 1)
							this.container.innerHTML = "There are no results for \""+obj.query +"\" on this site/search engine";
						else
							obj.results.collect(this.buildHandle).each(appendChild.bind(this.container));
					
					},
					buildResult : function(result){
						
						var container = $C("div");
						var title = $C("a", { innerHTML : result.title, href : result.url });
						title.setAttribute("target", "_blank");
						var content = $C("p", { innerHTML : result.content });
						var url = $C("span", { innerHTML : this.formatURL(result.url) });
						
						[title, content, url].each(appendChild.bind(container)); 
						
						return container;
					
					},
					formatURL : function(url){
						
						if(this.options.format)
							return url.replace(/^http:\/\/[^/]+/i, "");
											
						
						return url;
						
					}
					
				
				}
			);