	function search() {

	//variables definition start
		
		this.string;
	
		this.strings = new Object;
		this.stringSearch;
		this.loaded;
		this.sites = new Object;
		this.cache = new Object;
		this.results = new Array;
		
		this.filterTime;
	
		this.filters=new Array;
		
		this.category = "";
		this.categories = new Object;
		this.categories[''] = new Object;
		this.categories['']['results'] = new Array;
		this.categoriesIndex = new Array;
		this.sortColumn = "seeds";
		this.sortOrder = 1;
		this.done = 0;
		this.resultPage;
		this.resultMax = 30;
		this.resultCurrent = 0;
		this.pagination;
		this.c = 0;
		
		this.l_filter = "FILTER RESULTS";
		this.l_searching = "Searching, please wait...";
		this.l_no_matching = "No matching results were found, please redefine your query!";
		
		this.SERVER_PATH = "http://www.p2pfinder.com/";

		this.cells = { 
			date:'date',
			name:'string',
			size:'size',
			seeds:'color',
			peers:'color',
			engine:'engine',
			id:'id',
			link:'link' };
			
		this.rows = new Object;
		this.go = search_go;
		this.stop=search_stop;
		this.sort = search_sort;
		this.display = search_display;
		this.cat = search_category;
		this.filter=search_filter;
		this._get = search_get;
		this._cache = search_cache;
		this._done = search_done;
		this._filter=search_pfilter;

	  //variables definition end

		function search_go() {
			 
			this.strings["include"] = "";
			this.strings["exclude"] = "";
			this.strings["words"] = this.string.split(" ");
			
			//alert(this.strings["words"]);
			
			//if (this.strings["words"] != null)
			
			for(var x in this.strings["words"]) if (x != "indexOf"){
				var inex = this.strings["words"][x].substr(0,1);
				
				if(inex == "+") {
					this.strings["include"] += " "+this.strings["words"][x].substr(1)+".*";
				}
				else {
					if(inex == "-")
						this.strings["exclude"] += "|"+this.strings["words"][x].substr(1);
				}
			}
			 
			this.strings["include"] = this.strings["include"].substr(1);
			this.strings["exclude"] = this.strings["exclude"].substr(1);
			this.strings["include"] = this.strings["include"] ? new RegExp(this.strings["include"], "gi") : "";
			this.strings["exclude"] = this.strings["exclude"] ? new RegExp(this.strings["exclude"], "gim") : "";
			this.stringSearch = this.string.replace(new RegExp("\\+|\\-", "g"),"").replace(this.strings["exclude"],"");
			this.strings["highlight"] = new RegExp("(" + this.stringSearch.replace(new RegExp("\\s", "g"),"|") + ")", "gi");

			document.getElementById("filter").value=this.l_filter;
			
			for(var x in this.sites) if ( !this.sites[x]['state'] )
				{ this._get(x); }
				
			for (var x in this.cache)
				{this._cache(x);}
		}

	function search_stop(){
		for(var x in this.sites){
			if(this.sites[x]["state"]==1){
				this.sites[x]["request"].abort();
			}
		}
	};

		function search_sort(column, onlyUpdate) {

			if(!onlyUpdate)
				this.sortOrder = (this.sortColumn == column ? (this.sortOrder == 1 ? -1 : 1) : 1);
				
			document.getElementById(this.sortColumn).className = "";
			document.getElementById(column).className = "sort_" + (this.sortOrder == 1 ? 1 : 0);
			this.sortColumn = column;
			
			if(typeof(this.categories[this.category]) != 'undefined')
				this.categories[this.category]['results'].sort(_sort);     
			
			this.display(1); 
		}

	function search_filter(){
		if(this.filterTime){
			window.clearTimeout(this.filterTime);
		}
		this.filterTime=window.setTimeout("search._filter()",500);
	};

	function search_pfilter(){
		this.filters=new Array();
	if(document.getElementById("filter").value){
		var _6=document.getElementById("filter").value;
	var _7=this.stringSearch+" "+_6;
	this.strings["highlight"]=new RegExp("("+_7.replace(new RegExp("\\s","g"),"|")+")","gi");
	var _8=new RegExp(_6.replace(new RegExp("\\s","g"),".*"),"gi");
	var _9=this.categories[this.category]["results"];
	for(var x in _9){
		var _b=_9[x]["name"].toString();
	if(_8&&_b.match(_8)){
		this.filters[this.filters.length]=_9[x];
	}
		}
		}
		if(this.filters.length||!document.getElementById("filter").value){
		document.getElementById("filter").className="filter";
	this.display(1);
	}
		else{
		if(!this.filters.length){
		document.getElementById("filter").className="filterNo";
	}
		}
		};
	
		function search_display(page) {
			
			if(page != this.resultPage && typeof(window.scroll) == 'function') 
				window.scroll(0, 0); 
			else if(page != this.resultPage) 
				window.scrollTo(0, 0);

// Chequear esto aqui!!!!

			this.results=this.filters.length?this.filters:typeof (this.categories[this.category])!="undefined"?this.categories[this.category]["results"]:new Array;

			//this.results = typeof(this.categories[this.category]) != 'undefined' ? this.categories[this.category]['results'] : new Array;
			this.resultPage = page;

			while(document.getElementById('result').rows.length)
				document.getElementById('result').deleteRow(document.getElementById('result').rows.length-1);

			resultStart = this.resultMax * (this.resultPage - 1);
			resultStop = (this.results.length > this.resultMax ? this.resultMax * this.resultPage : this.results.length);
			document.getElementById("counter").innerHTML = "Results "+(resultStart+1)+"-"+(resultStop)+" of "+this.results.length+" for <b>"+this.string+"</b>";

			if(this.done == count(this.sites)) {
				 
				document.getElementById('status').innerHTML = "Search Complete";
			//     document.getElementById('sites').innerHTML = "Search Complete";
				document.getElementById('status').className = "status_state_2";
				
				if(!this.results.length) {
					
					var row = document.getElementById('result').insertRow(0);
					cell = row.insertCell(0);
					cell.innerHTML = this.l_no_matching;
					cell.className = "search_noResult";
					cell.colSpan = 6;
				 }
			} else {
				 
				if(!this.results.length) {
					var row = document.getElementById('result').insertRow(0); 
					cell = row.insertCell(0);
					//cell.innerHTML = "Searching, please wait...";
					cell.innerHTML = "<img src='"+ this.SERVER_PATH +"images/load_white.gif' /><br/><b>"+ this.l_searching +"</b>";
					cell.className = "search_wait";
					cell.colSpan = 6;
				}
				
				document.getElementById('status').innerHTML = "Searching engine "+ (this.done + 1) +" of "+ count(this.sites); 
			}

			var html = "";
			this.categoriesIndex.sort(_sort2); 
			
			//list cats
			
			var name = null;
			
			if (this.categoriesIndex != null) 
			
			for(var x in this.categoriesIndex) {
				var name = this.categoriesIndex[x];
				
				//alert(name);
				
				if (this.categories[name] != null) {
				
				html += "<a href=\"javascript:search.cat('"+name+"')\""+( name==this.category ? "class='active'":"")+">"+name+" ("+Math.round(this.categories[name]['results'].length)+")</a>";
				} else name = null;
			
			}
			
			if(html && html != "")
				html = "<a href=\"javascript:search.cat('')\""+(!this.category?" class='active'":"")+">All ("+this.categories['']['results'].length+")</a>" + html;
			
			document.getElementById("categories").innerHTML = html; 
			//end list cats

			var results = this.results.slice(resultStart, resultStop); 

			// for results
			
			var zcss;
			var cssrow;
			
			for(var x in results) if (x != "indexOf") {
				
				// zcss
				var rows = document.getElementById('result').rows.length;
				var row = document.getElementById('result').insertRow(rows);
				//document.getElementById('sites').innerHTML = "Search Complete";
				
				row.id = "results_" + x;
				if(typeof(this.rows[row.id]) != 'object') 
					this.rows[row.id] = new Object;
				
				this.rows[row.id]['even'] = isEven(x) ? 1 : 2;
				//  document.write(this.rows[row.id]['even']);
				
				//var cssrow;
				//cssrow = isEven(x) ? 2 : 1;
				//row.className = 'row_state_' + cssrow;

				row.className = 'row_state_' + this.rows[row.id]['even'];

				row.onmouseover = function() { 
					this.className = 'row_state_3';
				}

				row.onmouseout = function() { 
					this.className = 'row_state_' + search.rows[this.id]['even']; 
				}

				row.onclick = function() { 
					window.location = (search.rows[this.id]['url']+""+search.rows[this.id]['id']+"&f="+search.rows[this.id]['link']+"&s="+search.rows[this.id]['seeds']+"&l="+search.rows[this.id]['peers']+"&n="+search.rows[this.id]['name']+"&sz="+search.rows[this.id]['size']+"&d="+search.rows[this.id]['date']);
				}

				var y = 0; 
				
				//       document.write(results[x][name]);
				
				
				if (name != null)
				
				for(var name in this.cells) { 
					
					if(this.cells[name] != 'id' && this.cells[name] != 'link') 
						cell = row.insertCell(y);

				
					//       document.write(results[x][name]);
					
						switch(this.cells[name]) { 
						//        document.write(results[x][name]);
						
						case "id": this.rows[row.id]['id'] = results[x][name]; break;
						case "link": this.rows[row.id]['link'] = results[x][name]; break;
						case "date": this.rows[row.id]['date'] = convert_date(results[x][name]); cell.innerHTML = convert_date(results[x][name]);  break; 
						case "string": this.rows[row.id]['name'] = results[x][name]; cell.innerHTML = "<div title = '"+results[x][name]+"' class='name'>" + results[x][name].replace(this.strings["highlight"],"<b>$1</b>") + "</div>"; break; 
						case "size": this.rows[row.id]['size'] = results[x][name]; cell.innerHTML = convert_size(results[x][name], results[x]['engine']); break; 
						case "color": this.rows[row.id][name] = results[x][name]; cell.innerHTML = results[x][name] > -1 ? "<div class='green'>" + results[x][name] + "" : "" + results[x][name] + "</div>"; break;
						case "engine": this.rows[row.id]['url'] = this.sites[results[x][name]]['url']; 
										cell.innerHTML = "<div class='orange'>" +this.sites[results[x][name]]['name']+"</div>"; break;						
										
						} 
					
					y++; 
				}
			}
			 
			var html = ""; 
			var y = 0; 
			
			for(var x in this.sites) {

				html += "<div class='state_color_"+(isEven(y)?1:2)+"'><span class='state_"+this.sites[x]["state"]+"'></span>"+this.sites[x]["name"]+"</div>";
				//html += "&nbsp;<img src=\"images/tick.gif\" border=\"0\">&nbsp;"+this.sites[x]['name'] + "<br>"; y++;
				//html += "&nbsp;<img src=\"images/load.gif\" border=\"0\">&nbsp;"+this.sites[x]['name'] + "<br>"; y++;
				y++;

			}

			// if(this.done == count(this.sites)) {
			//               html = html.replace("load.gif","tick.gif");
					   
			//      document.getElementById('sites').innerHTML = html;
			  
			//      } else {
			//              html = html.replace("load.gif","tick.gif");
			
			document.getElementById("sites").innerHTML = html; 
			
			//         }

			//pagination
			if(this.pagination) { 
				var page = Math.ceil(this.results.length / this.resultMax); 
				
				if(page > 1) { 
					var html = "<ul>";
					
				for(var x = 1; x <= page; x++) { 
					html += "<li><a"+(x==this.resultPage?" class='active'":"")+" href='javascript:search.display("+x+")'>"+x+"</a></li> "; 
				} 

				for(var id in this.pagination) { 
					document.getElementById(this.pagination[id]).innerHTML = html; 
				}
				
				html += "</ul>";
			
				} else { 
				
					for(var id in this.pagination) { 
						document.getElementById(this.pagination[id]).innerHTML = ""; 
					}
				}
			}
		}
		//end pagination

		function search_category(cat) { 
			this.filters=new Array();
			//document.getElementById("filter").value=this.l_filter;
			this.strings["highlight"]=new RegExp("("+this.stringSearch.replace(new RegExp("\\s","g"),"|")+")","gi");
			this.category = cat; 
			this.sort(this.sortColumn, 1); 
		}
		
		function search_get(site) {
			
			var page_request = new Array();
			page_request["id"] = site; 
			page_request["xml"] = new XMLHttpRequest();
			
			page_request["xml"].onreadystatechange = function() {
			
					if(page_request["xml"].readyState == 4 && page_request["xml"].status == 200) {
						
						var results = new Array;  
						
						try {
							eval(page_request["xml"].responseText+"results=results");
							}
						
						catch(err) {
							
							search._done(page_request["id"], 2);
							return 0;
						
						} 
						
						if (results != null)

						for(var x in results) if (x != "indexOf") {
							
							var string = results[x]['name'].toString();
							
							if(search.strings["include"]) 
								if(!string.match(search.strings["include"])) continue;
								
							if(search.strings["exclude"]) 
								if(string.match(search.strings["exclude"])) continue;

							results[x]["engine"] = page_request["id"];
							results[x]["category"] = convert_category(results[x]["category"]);
							
							//    document.write(results[x]['name']);
							//    document.write('<br>');
							
							if(!search.categories[results[x]["category"]]) {
								
								search.categories[results[x]["category"]] = new Object();
								search.categories[results[x]["category"]]["results"] = new Array();
								
								var catId = search.categoriesIndex.length;
								
								search.categoriesIndex[catId] = new Object();
								search.categoriesIndex[catId] = results[x]["category"];
							}

							search.categories['']["results"][search.categories['']["results"].length] = results[x];
							search.categories[results[x]["category"]]["results"][search.categories[results[x]["category"]]["results"].length] = results[x]; 
						}


						//search.categories['']["results"] = search.categories['']["results"].concat(results);
						search._done(page_request["id"], 2);
						
					} else 
						if(page_request["xml"].readyState == 4 && page_request["xml"].status != 200) { 
							search._done(page_request["id"], 3); 
						} 
			}
			 
			this.sites[site]['state'] = 1; 
			this.sites[site]["request"]=page_request["xml"];
			page_request["xml"].open("POST", "" + this.SERVER_PATH + "get.php?site=" + site + "&string="+escape(this.stringSearch) + "&c="+escape(this.c), true);
			page_request["xml"].send("");
		}
		
		function search_cache(site) {
			
			var page_request = new Array();
			page_request["id"] = site; 
					
						var results = new Array;  
						
						try {
							
							eval(this.cache[page_request["id"]]+"results=results");
							}
						
						catch(err) {
							
							search._done(page_request["id"], 2);
							return 0;
						
						} 
						
						for(var x in results) if (x != "indexOf") {
							
							var string = results[x]['name'].toString();

							if(search.strings["include"]) 
								if(!string.match(search.strings["include"])) continue;
								
							if(search.strings["exclude"]) 
								if(string.match(search.strings["exclude"])) continue;

							results[x]["engine"] = page_request["id"];
							results[x]["category"] = convert_category(results[x]["category"]);
							
							if(!search.categories[results[x]["category"]]) {
								
								search.categories[results[x]["category"]] = new Object();
								search.categories[results[x]["category"]]["results"] = new Array();
								
								var catId = search.categoriesIndex.length;
								
								search.categoriesIndex[catId] = new Object();
								search.categoriesIndex[catId] = results[x]["category"];
							}

							search.categories['']["results"][search.categories['']["results"].length] = results[x];
							search.categories[results[x]["category"]]["results"][search.categories[results[x]["category"]]["results"].length] = results[x]; 
						}


						search._done(page_request["id"], 2);
		}

		function search_done(site, state) {
			
			this.sites[site]['state'] = state;
			this.done++;
			this.sort(this.sortColumn, 1); 
		}
	}
	
	var search = new search;
	
	window.onload = function() {
		search.display(1);
		search.go();
	}

	function count(object) {
		var y = 0;
		
		if(object.__count) 
			return object.__count;
		else 
			if(object.length)
				return object.length;
				
			else {
				
				for(var x in object)
					y++;
					
				return y;				
				}
	}

	function isEven(num) {
	
		return !(num % 2); 
	}

	function convert_date(input) {
		
		if(input == 0)
			return "-";
		
		var date = new Date();
		dif = Math.round(date / 1000) - input;
		years = Math.floor(dif/31535999);
		months = Math.floor(dif/2591999);
		days = Math.floor(dif/86399); 
		hours = Math.floor(dif/3599);
		
		if(years < 1)
			if(months < 1)
				if(days < 1)
					if(hours < 1)
						stime = 'today';
					else stime = hours + ' ' + (hours == 1 ? 'hour' : 'hours') + ' ago';
				else stime = days + ' ' + (days == 1 ? 'day' : 'days') + ' ago';
			else stime = months + ' ' + (months == 1 ? 'month' : 'months') + ' ago';
		else stime = years + ' ' + (years == 1 ? 'year' : 'years') + ' ago';
		
		return stime;
	}

	function convert_size(input, engine) { 
		
		var bytes = new Array('B', 'KB', 'MB','GB','TB'); 
		
		/*
		if (engine == "15")
			var bytes = new Array('B', 'KB', 'MB','GB','TB'); 
		else
			var bytes = new Array('MB','GB','TB');
		*/
		
		for(val in bytes) { 
			if(input > 1024) input = input / 1024; else break; 
		} 
		
		return Math.round(input*100)/100+" "+bytes[val]; 
	}
   
	function _sort(a, b) { 
		var x = a[search.sortColumn].toString().toLowerCase();
		var y = b[search.sortColumn].toString().toLowerCase();
		
		if (search.cells[search.sortColumn] != 'string') 
			return ((y - x) * search.sortOrder); 
		else 
			return (((x < y) ? -1 : ((x > y) ? 1 : 0)) * search.sortOrder); 
	}
 
	function _sort2(a, b) { 
		var x = a.toString().toLowerCase();
		var y = b.toString().toLowerCase();
		return ((x < y) ? -1 : ((x > y) ? 1 : 0)); 
	}