/* FUNCTION TO ADD SEARCH THROUGH OPENSEARCH */
function addPlugin()
{
	window.external.AddSearchProvider("http://www.youtorrent.com/youtorrent.xml");

} // addPlugin

/* FUNCTION TO ADD SEARCH TO IE */
function addPluginIE()
{
	var strName = "YouTorrent";
	var strURI = "http://www.youtorrent.com/tag/?q=TEST";
	
	strName = strName.replace (/^\s+|\s+$/g,'');
	strURI = strURI.replace (/^\s+|\s+$/g,'');
	var strEncoding = "UTF-8";
	
	if (strURI.length<1) { alert ("Please enter the URL of the search results page."); document.getElementById("sURI").focus(); return false;}
	if (strURI.indexOf("TEST")<0) { alert ("The search results URL must contain the word TEST.  Please check the URL.\nIt's possible that this tool cannot be used to build a query for that search engine."); document.getElementById("sURI").focus(); return false;}
	if (strName.length<1) { alert ("Please enter a NAME for the new search provider"); document.getElementById("sName").focus(); return false;}
	
	var strQuery = "Encoding="+ encodeURIComponent(strEncoding) + "&Name=" + encodeURIComponent(strName) + "&URI="  + encodeURIComponent(strURI);
	
	var strAddURI = "http://www.microsoft.com/windows/ie/searchguide/spbuilder.mspx?" + strQuery;
	//alert(strAddURI);
	try{
		window.external.AddSearchProvider(strAddURI);
	}
	
	catch(eX){
		if (70 == (eX.number & 0xFFFF)){	
			alert("For security reasons, you must use the mouse\n(or the Enter key) to click the Install button.");
		}
		else {
			alert("Unable to add search provider. The Create Your\nOwn tool requires Internet Explorer 7 or higher.   [" + (eX.number & 0xFFFF) + "]");
		}
	}
	
	return false;
} // addPluginIE

/* FUNCTION TO DETECT PLUGIN SCRIPT NEEDED */
function getPlugin() {
	if (BrowserDetect.browser == "Firefox"){
		var pluginXML = 'addPlugin()';
		var pluginImage = 'ffplugin.png';
	}
	else if (BrowserDetect.browser == "Explorer" && (BrowserDetect.version == 7 || BrowserDetect.version == 8)){
		var pluginXML = "addPluginIE()";
		var pluginImage = 'ieplugin.png';
	}
	if (pluginXML){
		document.writeln('<div id="plugin" onclick="'+pluginXML+'">');
		document.writeln('<div class="title replaceme">Love '+BrowserDetect.OS+'. Love '+BrowserDetect.browser+'. Love YouTorrent.</div>');
		document.writeln('<img src="/i/'+pluginImage+'" alt="Plugin" />');
		document.write('<div class="text">Take the power of YouTorrent with you anywhere with our new '+BrowserDetect.browser+' plugin!</div>');
		document.write('</div>');
	}
	else {
 		document.writeln('<div id="pluginalt"></div>');
 	}
}

/* FUNCTION TO GET BROWSER TYPE */
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();