var swfName = "mySWF";
var cid = 1;
var installed = false;
var pollCount = 0;
var url = false;

window.onload = checkInstalled;

function checkInstalled(){
	var s = document.createElement("script");
	s.src = "http://127.0.0.1:9421/api?wrapper=setInstalled(*)&function=ping&cid=" + cid;
	document.getElementsByTagName("HEAD")["0"].appendChild(s);
}

function download()
{
	url = true;
	if(installed)
	{	
		setTimeout(function(){document.getElementById(swfName).download()}, 100);
	}
	else
	{
		install();
		return true;
	}
}

function install(){
	//showInstaller();
	document.getElementById(swfName).install();
	pollCount = 0;
	pollInstalled();
}

function pollInstalled(){
	pollCount++;
	if(installed || pollCount > 600)
	{
		return;
	}
	else
	{
		checkInstalled();
		setTimeout(pollInstalled, 1000);
	}
}

function setInstalled(){
	installed = true;
	if(document.getElementById("rsinstaller"))
		document.getElementById("rsinstaller").parentNode.removeChild(document.getElementById("rsinstaller"));
	if(url){
		setTimeout(function(){document.getElementById(swfName).download()}, 100);
		url = false;
	}
}

function showInstaller(){
	if(document.getElementById("rsinstaller"))
		document.getElementById("rsinstaller").parentNode.removeChild(document.getElementById("rsinstaller"));
	var d = document.createElement("div");
	d.className = "rsinstaller";
	d.id = "rsinstaller";
	//
	var OSName="Unknown OS";
	if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
	if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
	if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
	if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
	//
	var downloadLink ="#";
	if(OSName == "Windows")
	{
		downloadLink ="http://www.redswoosh.net/install/RSInstaller.exe";
	}
	else if(OSName == "MacOS")
	{
		downloadLink ="http://www.redswoosh.net/install/RedSwoosh%20Installer.dmg";
	}	
	//
	d.innerHTML = "<a href='"+downloadLink+"' target='_blank'><font color='#FFFFFF' size='+1'>Click here to install Red Swoosh.</font></a>";
	document.getElementsByTagName("BODY")[0].insertBefore(d, document.getElementsByTagName("BODY")[0].firstChild);
}
