String.prototype.getMyQuery = function(name) { 
		var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); 
		var r = this.substr(this.indexOf("\?")+1).match(reg); 
		if (r!=null) return unescape(r[2]); 
		return null; 
}

var appcontent = document.getElementById("appcontent");   
if(appcontent) 
{ 
	appcontent.addEventListener("DOMContentLoaded", OnPageLoad, true); 
} 



//window.addEventListener("pagehide", OnPageLoad, false);  
function OnPageLoad(aEvent) {  
    if (aEvent.originalTarget instanceof HTMLDocument) {  
        var doc = aEvent.originalTarget;  
		//var cdoc = aEven.currentTarget;
        //alert("page load1:" + doc.location.href); 
		//alert("page load2:" + cdoc.location.href); 
		//doc.location.href="http://www.baidu.com";
		var myreg = new RegExp(/2\.71828searchplugintest3\.1415926/);
		//var myreg2 = new RegExp(/3\.1415926searchplugintest2\.71828/);
		var index = doc.location.href.search(myreg);
		if(index>=0){
			var strHref=doc.location.href;
			var myQuery=doc.location.href.getMyQuery("q");
			var query="meta-search";
			if(myQuery!=null)
			{
				query=escape(myQuery);
			}
			var url="chrome://searchplugintest/content/metaresult.html?q="+encodeURI(query);
			var tp=strHref.getMyQuery("p");
			if(tp!=null)
			{
				var myPage=parseInt(tp);
				url+=("&p="+myPage);
			}
			var ts=strHref.getMyQuery("num");
			if(ts!=null)
			{
				var mySites=parseInt(ts);
				url+=("&num="+mySites);
				for(var k=0;k<mySites;k++)
				{
					var nk="n"+k;
					var tmpk=parseInt(strHref.getMyQuery(nk));
					if(tmpk!=null)
					{
						url+=("&"+nk+"="+tmpk);
					}
				}
			}
			doc.location.href=url;
			window.setTimeout("window.stop();",5000);
		}
    }  
} 
