var addData =
[
	// 0 - FireTorrent
	[ 
		'FireTorrent',
		
		[
		// Win
		'https://addons.mozilla.org/en-US/firefox/downloads/latest/10931/platform:5/addon-10931-latest.xpi',	
	
		// Mac
		'https://addons.mozilla.org/en-US/firefox/downloads/latest/10931/platform:3/addon-10931-latest.xpi',
	
		// Linux
		'https://addons.mozilla.org/en-US/firefox/downloads/latest/10931/platform:2/addon-10931-latest.xpi'
		]
	],
	
	// 1 - FireDownload
	[
		'FireDownload',
		
		[
		// Win
		'https://addons.mozilla.org/en-US/firefox/downloads/latest/10615/platform:5/addon-10615-latest.xpi',
		
		// Mac
		'https://addons.mozilla.org/en-US/firefox/downloads/latest/10615/platform:3/addon-10615-latest.xpi',
		
		// Linux
		'https://addons.mozilla.org/en-US/firefox/downloads/latest/10615/platform:2/addon-10615-latest.xpi',
		
		]
		
	]

];


function installAddon ( id, os)
{
	
	fa_windowheight = window.innerHeight;
	hn1 = document.createElement('div');
	hn1.id = 'fa_spacer';
	document.body.appendChild(hn1);
	
	var xpi= "({'"+addData[id][0]+"':'"+addData[id][1][os]+"'})";
	
	InstallTrigger.install(eval(xpi));
	
	show_allowbar(1);

}


function show_allowbar( iteration )
{
	var diff = fa_windowheight - window.innerHeight;
	if ( iteration > 10 )
		moz_allow();
	else if (diff > 0)
		moz_allow();
	else
		setTimeout("show_allowbar("+(iteration+1)+");", 250);
		
}

function hide_allowbar( wh )
{

	if ( parseInt(window.innerHeight) - parseInt(wh) > 0)
		moz_close();
	else
		setTimeout("hide_allowbar('"+wh+"');", 500);
		
}

function moz_allow()
{
		hn1.className = 'fa_olay1';	
		hn1.style.display = 'block';
		
		hn2.innerHTML = '';
		hn2.className = 'fa_moz_allow';
		hn2.style.display = 'block';	
		hn2.style.right = '10px';	
		hn2.style.left = 'auto';
				
		hide_allowbar(window.innerHeight);	
}

function moz_close()
{
	hn2.style.display = 'none';
	if (hn1)
	hn1.style.display = 'none';
	
	document.body.style.paddingTop = '0px';
}


function showUserPlatform ( pid )
{
	if (document.getElementById('win'+pid)) document.getElementById('win'+pid).style.display = "none";
	if (document.getElementById('mac'+pid)) document.getElementById('mac'+pid).style.display = "none";
	if (document.getElementById('lin'+pid)) document.getElementById('lin'+pid).style.display = "none";
	
	var userOS = "";
	switch (BrowserDetect.OS)
	{
		case "Mac":
		userOS = "mac";
		break;
		
		case "Windows":
		userOS = "win";
		break;
		
		case "Linux":
		userOS = "lin";
		break;
		
	}
	
	if (document.getElementById(userOS+pid))
	{
		document.getElementById(userOS+pid).style.display = "block";	
	}
	
	g('seeall'+pid).innerHTML = "See all supported platforms";
	g('seeall'+pid).href = "javascript:showAllPlatforms("+pid+")";		
	
}

function showAllPlatforms ( pid )
{
	if (document.getElementById('win'+pid)) document.getElementById('win'+pid).style.display = "block";
	if (document.getElementById('mac'+pid)) document.getElementById('mac'+pid).style.display = "block";
	if (document.getElementById('lin'+pid)) document.getElementById('lin'+pid).style.display = "block";
	
	g('seeall'+pid).innerHTML = "Only show version for my operating system";
	g('seeall'+pid).href = "javascript:showUserPlatform("+pid+")";	
}

function g (id)
{
 return document.getElementById(id);
}


window.onload = function(){

	hn0 = document.body;    
	hn2 = document.createElement('div');
	hn2.id = 'fa_tooltip';
	hn2.style.display = 'none';
	hn0.appendChild(hn2);	

};


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: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			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();
