/*

Documebntation
Search engine
Structure

*/

if(typeof(Artres)=="undefined"){Artres={};}

Artres.SearchEngine={
	urlRequest:'',
        domain:'',
        init: function(urlR, inputSearch,formSearch,domainS){
		this.urlRequest=urlR;
		this.domain=domainS;
		//Load Css File for Search
		var head;
		var css;
		head = $$('head')[0];
		if (head)
		{
		    css = new Element('link', { type: 'text/css',rel:'stylesheet', href: 'styles/SearchEngine.css' });
		    head.appendChild(css);
		}
		//Create the div for results
		var body;
		var searchResults;
		body=$$('body')[0];
		if(body){
			searchResults= new Element('div', {id:'searchResultsContainer'});
			searchResults.hide();
			body.appendChild(searchResults);
		}
		
		//Init events for Search
		//On enter key
		Event.observe(inputSearch, 'keypress',function(event){
                    var code = event.keyCode;
                        if(code == Event.KEY_RETURN) {
                        //Search the searchForm input by user.
                        Artres.SearchEngine.searchInit({query:Form.Element.getValue(inputSearch)});
                        event.stop();
                        return false;
                    }
 		});
 		//On submit form
 		Event.observe(formSearch, 'submit',function(event){
 			//Search the searchForm input by user.
                    Artres.SearchEngine.searchInit({query:Form.Element.getValue(inputSearch)});
                    event.stop();
                    return false;
 		});

	},
	searchInfo: function(txt){
		$("searchResultsContainer").hide();
		$("searchResultsContainer").update("");
		$("searchResultsContainer").update(txt);
		//new Effect.SlideDown($("searchResultsContainer"), {duration:1});
		$("searchResultsContainer").show();
	},
	searchInit: function(datosForm){
		this.searchInfo("<div class='searching'>Buscando</div>");
		var datos={jsoncallback:"Artres.SearchEngine.showResults", search:"1", domain:this.domain};
		Object.extend(datos, datosForm);
		Artres.JsonRequest.init(this.urlRequest,datos);
		Artres.JsonRequest.send();
	},
	searchPages: function(datosForm){
		this.searchInfo("<div class='searching'>Buscando</div>");
		var datos={jsoncallback:"Artres.SearchEngine.showResults", search:"1", domain:this.domain};
		var datosFormParsed=datosForm.toQueryParams();
		Object.extend(datos, datosFormParsed);
		Artres.JsonRequest.init(this.urlRequest,datos);
		Artres.JsonRequest.send();
	},
	hideResults:function(){
		$("searchResultsContainer").hide();
	},
	showResults: function(json){
		txt="";
		//alert(datos.responseText.evalJSON())
		if(json.result_report!=undefined)
			txt+="<div class='report'>"+json.result_report+"</div>";
		//alert(datos.result_report);
		json.results.each( function(item,i){
			if(item.num!="-1"){
				txt+="<a href='"+item.link+"' class='row searchResult'>";
				txt+="	<div class='num'>"+item.num+"</div>";
				txt+="	<div class='searchContent'>";
				txt+="		<div class='searchTitle'>"+item.title+"</div>";
				txt+="		<div class='searchDescription'>"+item.description+"</div>";
				txt+="		<div class='searchMetadata'>"+item.link2+" - "+item.size+" - "+item.weight+"</div>";
				txt+="	</div>";
				txt+="</a>";
			}
		})
		txt+="<ul class='searchPages row'>";

		if(json.other_pages!=undefined){
			json.other_pages.each( function(item,i){
				txt+="<li><a href=\"javascript:Artres.SearchEngine.searchPages('"+item.link+"');\" class='"+item.active+"'>"+item.title+"</a></li>";
			})

		}
		txt+="<li><a href=\"javascript:Artres.SearchEngine.hideResults();\">Close</a>";
		txt+="</ul>";
		
		this.searchInfo(txt);
	}
}
