window.downloader = {
	downloadTimeout : 60,
	listenInterval : 1000,
	currInterval : 0,
	listener : null,
	pluginName : 'Station Installer',
	pageType : '',
	flashVersion : {},
	hasInstaller : false,
	hasflash : false,
	ccFlashVars : "",
	dlStatusMovie : null,
	useFlashDownload : true,
	serverSelectUrl : "/game/play!defaultServer.action",
	origPageContent : null,
	affid : '',
	newReg: false,
	statusDivId: 'pluginStatus',

	labels : {
		"waitingForPluginSetup" : "",
		"waitingForFlashSetup" :"",
		"flashRequired" : ""
	},

	init : function () {
		browser.init();
		this.hasInstaller = this.checkInstaller();
		this.hasFlash = this.checkFlash();
		this.flashVersion = swfobject.getFlashPlayerVersion();
//		if (navigator.appVersion.indexOf("Win") === -1) {
//			document.getElementById(downloader.statusDivId).className = "plugin_status_header_noflash";
//			document.getElementById(downloader.statusDivId).innerHTML = '<div class="exPointIcon" style="top: 6px; left: 0px;"></div><p style="font-size: 14px; line-height:16px;">Free Realms is currently only available on Windows.</p>';
//		} else {
			if (this.useFlashDownload) {
				//for flash install process
				if (!this.hasFlash || this.flashVersion.major < 9) {
					//alert if flash not found
					if (this.pageType == "characterCreate") {
						document.getElementById('pluginRequiredContent').style.display = "none";
						document.getElementById('flashRequiredContent').style.display = "block";
					} else {
						this.displayNoFlashError();
					}
				} else {
					if (document.getElementById(downloader.statusDivId) !== null) {
						if (document.getElementById(downloader.statusDivId).className != "plugin_status_header") {
							document.getElementById(downloader.statusDivId).className = "plugin_status_header"
						}
					}
					this.showDownloadButton();
					if (!this.hasInstaller) {
						if (navigator.appName == "Microsoft Internet Explorer") {
							plugin.test();
						}
						if (this.pageType == "characterCreate") {
							document.getElementById('pluginRequiredContent').style.display = "block";
							document.getElementById('flashRequiredContent').style.display = "none";
						}
					} else {
						//this.loadPlugin();
						if (this.pageType == "characterCreate") {
							downloader.showCharCreate();
						}
					//
					//the rest of setup happens after the flash movie calls downloader.statusLoaded();
					}
				}
			} else {
				//for non flash install process
				if (!this.hasInstaller) {
					this.showDownloadButton();
					//if (navigator.appName == "Microsoft Internet Explorer") {
						plugin.test();
					//}
				} else if (!this.hasFlash && this.hasInstaller) {
					if (this.pageType == "characterCreate") {
						document.getElementById('pluginRequiredContent').style.display = "none";
						document.getElementById('flashRequiredContent').style.display = "block";
					} else {
						document.getElementById(downloader.statusDivId).innerHTML = "";
						tempSpan = document.createElement("span");
						tempSpan.setAttribute('id', 'buttonHolder');
						document.getElementById(downloader.statusDivId).appendChild(tempSpan);
						this.loadPlugin();
						if (this.pageType != "download") {
							document.getElementById('buttonHolder').innerHTML = '<div class="exPointIcon" style="top: 6px; left: 0px;"></div><p style="font-size: 16px; line-height:16px;">' + downloader.labels.flashRequired + '</p>';
						}
					}
				} else if (this.hasFlash && this.hasInstaller) {
					this.loadPlugin();
					if (this.pageType == "characterCreate") {
					//this.showCharCreate();
					}
				}
			}
//		}
	},

	showDownloadProgMsg : function(msgType) {
		if (msgType == "installer") {
			if ( downloader.pageType === "download" ) {
				downloader.dlStatusMovie.showProgressMsg(downloader.labels.waitForPluginSetupNoLink);
			} else {
				downloader.dlStatusMovie.showProgressMsg(downloader.labels.waitingForPluginSetup);
			}
		} else if(msgType == "directX") {
			downloader.dlStatusMovie.showProgressMsg(plugin.labels.almostDone);

		}
	},

	//this function should be obsolete
	showCharCreate : function () {
		document.getElementById('pluginRequiredContent').style.display = "none";
		document.getElementById('flashRequiredContent').style.display = "none";

		/* output flash for char create */
		var flashVars = false

		var params = {
			allowscriptaccess: "sameDomain",
			flashvars: downloader.ccFlashVars,
			wmode: "opaque"
		};

		var attributes = {
			id: "playNowbutton",
			name: "charactercreation",
			align: "middle"
		};
		timestamp = this.createTimestamp();
		//swfobject.embedSWF(common.contextPath + "/flash/charCreate/swf/loader.swf?ts=" + timestamp, "flashRequiredContent", "970", "570", "9.0.0", common.cdnPath + "/flash/expressInstall.swf", '', params, attributes);
	},

	loadPlugin : function () {
		var pluginType = typeof plugin;
		if(pluginType === "object") {
			if (!this.hasFlash && this.hasInstaller && this.pageType == "characterCreate") {
				document.getElementById('pluginRequiredContent').style.display = "none";
				document.getElementById('flashRequiredContent').style.display = "block";
			} else {
				if (downloader.listner !== null) {
					clearTimeout(downloader.listner);
				}
				plugin.load();
			}
		}
	},

	statusLoaded : function () {
		this.dlStatusMovie = document.getElementById('downloadStatusMovie');
		if (!this.hasInstaller) {
			if(this.pageType !== "characterCreate") {
				this.dlStatusMovie.setState("downloadButton");
			}
		} else {
			//this.dlStatusMovie.setState("downloadProgress");
			this.loadPlugin();
			//if (this.pageType == "characterCreate") {
			//	this.showCharCreate();
			//}
		}
	},

	flashLoaded : function() {
		document.getElementById('pluginRequiredContent').style.display = "block";
		document.getElementById('flashRequiredContent').style.display = "none";
		if(this.pageType !== "characterCreate") {
			document.getElementById('inside_container').innerHTML = this.origPageContent;
		}
	},

	pluginLoaded : function() {
		document.getElementById('pluginRequiredContent').style.display = "none";
		document.getElementById('flashRequiredContent').style.display = "none";
		//this.showCharCreate();
	},

	listen : function () {
		if(downloader.currInterval == 0 && !this.useFlashDownload) {
			document.getElementById(downloader.statusDivId).innerHTML = (this.pageType == "download") ? "" : '<div class="exPointIcon" style="top: 0px; left: 0px;"></div>'
			document.getElementById(downloader.statusDivId).innerHTML += '<p style="font-size: 18px; line-height:16px; margin: 0;">' + this.labels.waitingForPluginSetup;
		} else if(downloader.currInterval == 0 && this.useFlashDownload) {
			//console.debug(downloader.pageType)

			//if ( downloader.pageType === "download" ) {
			//	downloader.dlStatusMovie.showProgressMsg(downloader.labels.waitForPluginSetupNoLink);
			//} else {
				downloader.dlStatusMovie.showProgressMsg(downloader.labels.waitingForPluginSetup);
			//}


		}
		this.hasInstaller = this.checkInstaller();
		if(this.hasInstaller) {
			window.focus();
			//downloader.dlStatusMovie.setState("downloadProgress");
			//downloader.dlStatusMovie.showProgressMsg("mid install state");
			if(this.pageType == "download") {
				window.location = common.contextPath = "/downloadClient.vm";
			} else {
				window.location.reload();
			}

			downloader.currInterval = 0;
		} else {
			downloader.listener = setTimeout("downloader.listen()", downloader.listenInterval);
			downloader.currInterval = downloader.currInterval + 1;
		}

		if(downloader.currInterval === downloader.downloadTimeout) {
			if (this.useFlashDownload) {
				if (this.pageType == "characterCreate") {
					downloader.dlStatusMovie.setState("emptyState");
				} else {
					downloader.dlStatusMovie.setState("downloadButton");
				}
			} else {
				document.getElementById(downloader.statusDivId).innerHTML = "";
				tempSpan = document.createElement("span");
				tempSpan.setAttribute('id', 'buttonHolder');
				document.getElementById(downloader.statusDivId).appendChild(tempSpan);
				downloader.showDownloadButton();
				if (navigator.appName == "Microsoft Internet Explorer") {
					clearTimeout(ctime1);
					clearTimeout(ctime2);
				}
			}
		}
	},

	listenForFlash : function () {
		if(downloader.currInterval == 0) {
			if (downloader.pageType !== "flashInstall") {
				document.getElementById(downloader.statusDivId).innerHTML = '<div class="exPointIcon" style="top: 6px; left: 0px;"></div><p style="padding-top: 10px; font-size: 14px; line-height:14px;">' + this.labels.waitingForFlashSetup + '<br>';
			}
		}
		this.hasFlash = this.checkFlash();
		if(this.hasFlash) {
			if(downloader.listner !== null) {
				if (downloader.pageType === "flashInstall") {
					window.location = common.contextPath = "/character/profile.action";
				} else {
					if (navigator.appName !== "Microsoft Internet Explorer") {
						window.location.reload();
					}
				}
			}
		} else {
			downloader.listener = setTimeout("downloader.listenForFlash()", downloader.listenInterval);
			downloader.currInterval = downloader.currInterval + 1;
		}

	},

	startDownload : function() {
		downloader.currInterval = 0;
		if(downloader.listener !== null) {
			clearTimeout(downloader.listener);
		}
		downloader.listener = setTimeout("downloader.listen()", 500);
		if((navigator.appName == "Microsoft Internet Explorer" || ((navigator.userAgent.indexOf("Firefox") > -1) && browser.version > 2)) && !this.affid) {
			window.location = common.contextPath = "/downloadClient.vm";
		}else{
			window.location = common.cdnPath + "/gamedata/plugins/" + plugin.longVersion + "/Install_FreeRealms.exe?v=" + common.appVersion + ".exe";
		}
			
	},

	startFlashDownload : function() {
		downloader.currInterval = 0;
		clearTimeout(downloader.listener);
		downloader.listener = setTimeout("downloader.listenForFlash()", 500);
		window.open('http://get.adobe.com/flashplayer/', '', '');
	},

	showDownloadButton : function () {
		if(document.getElementById("simpleServerSelect")){
			document.getElementById("simpleServerSelect").style.display = "none";
		}
		if(this.pageType == "characterCreate") {
			document.getElementById('pluginRequiredContent').style.display = "block";
			document.getElementById('flashRequiredContent').style.display = "none";
		}
		if (!this.useFlashDownload) {
			// try to write to plugin status div if its there else fail silently
			if (navigator.appName == "Microsoft Internet Explorer" && this.pageType !== "download" && !this.affid) {
				activeXinstr = document.createElement("img");
				activeXinstr.setAttribute("id", "chadtyHolder1");
				activeXinstr.setAttribute("src", common.cdnPath + "/images/default/download/" + common.locale + "/IEactiveXdownloadStepSmall.png");
				activeXinstr.style.height = "127px";
				activeXinstr.style.width = "262px";
				if (document.getElementById(downloader.statusDivId) !== null) {
					document.getElementById(downloader.statusDivId).appendChild(activeXinstr);
				}
			}else if((navigator.userAgent.indexOf("Firefox") > -1) && browser.version > 2 && this.pageType !== "download" && !this.affid) {
				activeXinstr = document.createElement("img");
				activeXinstr.setAttribute("id", "chadtyHolder1");
				activeXinstr.setAttribute("src", common.cdnPath + "/images/default/download/" + common.locale + "/FFXPIdownloadStepSmall.png");
				activeXinstr.style.height = "127px";
				activeXinstr.style.width = "262px";
				if (document.getElementById(downloader.statusDivId) !== null) {
					document.getElementById(downloader.statusDivId).appendChild(activeXinstr);
				}
			}else{
				try {
					if(this.pageType == "download"){
						document.getElementById(downloader.statusDivId).innerHTML = "<a href='javascript:downloader.startDownload()'><div id='continueButton'></div></a>";
					}else{
						document.getElementById(downloader.statusDivId).innerHTML = "<a href='javascript:downloader.startDownload()'><div id='downloadButton'></div></a>";
					}
					document.getElementById("simpleServerSelect").style.display = "none";
				}
				catch (e) {
				}
			}
		} else {
			var flashVars = {};
			//flashVars.setupPath = "${contextPath}/gamedata/Install_FreeRealms.exe";
			flashVars.downloadingHeaderText = plugin.labels.downloadingHeader;
			flashVars.completeText = plugin.labels.downloadStatusComplete;
			flashVars.verifyingDataText = plugin.labels.verifyingData;

			var params = {
				wmode: "transparent"
			};

			var attributes = {
				id: "downloadStatusMovie",
				name: "downloadStatusMovie",
				align: "middle"
			};

			swfobject.embedSWF(common.contextPath + "/flash/playNowButton/sfws/playNowButton.swf?v=" + common.appVersion, "buttonHolder", "750", "255", "9.0.0", common.contextPath + "/flash/expressinstall.swf?v=" + common.appVersion, flashVars, params, attributes);
		}
	},

	checkFlash : function () {
		pluginFound = this.detect('Flash');
		// if not found, try to detect with VisualBasic
		if (navigator.appName == "Microsoft Internet Explorer") {
			try  {
				var axobj = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.1');
				var valid = (axobj != null);
				delete axobj;
				pluginFound =  true;
			}  catch(e)  {
				pluginFound =  false;
			}
		}
		if(pluginFound && swfobject.getFlashPlayerVersion() < 9) {
			pluginFound = false;
		}

		return pluginFound;
	},

	checkInstaller : function () {
		pluginFound = this.detect('SOE Web Installer','Station Installer','Free Realms Installer');
		// if not found, try to detect with VisualBasic
		if (navigator.appName == "Microsoft Internet Explorer") {
			try  {
				var axobj = new ActiveXObject("soex.SonyOnlineInstallerXctl.1");
				var valid = (axobj != null);
				delete axobj;
				pluginFound =  true;
			}  catch(e)  {
				pluginFound =  false;
			}
		}
		return pluginFound;
	},

	installerFound : function (status) {
		pluginFound = true;
		this.hasFlash = this.checkFlash();
		if(!this.hasFlash)  {
			if(this.pageType == "characterCreate") {
				document.getElementById('pluginRequiredContent').style.display = "none";
				document.getElementById('flashRequiredContent').style.display = "block";
			} else {
				document.getElementById(downloader.statusDivId).innerHTML = "";
				tempSpan = document.createElement("span");
				tempSpan.setAttribute('id','buttonHolder');
				document.getElementById(downloader.statusDivId).appendChild(tempSpan);
				//this.loadPlugin();
				plugin.onInit(status);
				this.displayNoFlashError();
			}
		} else {
			//this.loadPlugin();
			plugin.onInit(status);
			if(this.pageType == "characterCreate") {
				this.showCharCreate();
			}
		}
	},

	updateContainerClass : function(boxState, boxWidth, boxHeight) {
		var statusBox = document.getElementById(downloader.statusDivId);
		if (this.pageType !== "download") {
			statusBox.style.width = boxWidth + 'px';
			statusBox.style.height = boxHeight + 'px';
			if (boxState == "large" || boxState == "chadty") {
				statusBox.style.zIndex = "21000";
			} else {
				statusBox.style.zIndex = "100";
			}
			if (boxState == "chadty") {
				if (this.affid === "") {
					statusBox.style.top = "70px";
				} else {
					statusBox.style.top = "70px";
				}
			} else {
				if (this.affid === "") {
					statusBox.style.top = "66px";
				} else {
					statusBox.style.top = "19px";
				}
			}
		}
	},

	playNow : function () {
		if (this.pageType == "characterProfile") {
			if (charProfile.profile.isOwner) {
				window.location = common.contextPath + downloader.serverSelectUrl + "?characterId=" + charProfile.profile.defaultCharId;
			} else {
				window.location = common.contextPath + downloader.serverSelectUrl + "?characterId=" + plugin.activeCharId;
			}
		} else {

			window.location = common.contextPath + downloader.serverSelectUrl + "?characterId=" + plugin.activeCharId;
		}
	},

	displayNoFlashError : function() {
		if(this.pageType == "characterCreate") {
			this.origPageContent = document.getElementById('interactive_content').innerHTML;
			document.getElementById('interactive_content').style.height = "570px";
			document.getElementById('interactive_content').innerHTML = '<div id="flashRequiredContent2">';
			document.getElementById('interactive_content').innerHTML += '<div class="flashContentStyle2"></div>';
			document.getElementById('interactive_content').innerHTML += '<a href="javascript:downloader.startFlashDownload();"><div class="flashContentStyleStartButton"></div></a>';
			//document.getElementById('interactive_content').innerHTML += '<a href="' + common.contextPath + '/javascript.vm"><div class="flashContentStyleInstLink"></div></a>';
			document.getElementById('interactive_content').innerHTML += '</div>';
			document.getElementById('mainBodyContainer').style.height = "50px";
			document.getElementById(downloader.statusDivId).className = "plugin_status_header_noflash";
			document.getElementById(downloader.statusDivId).innerHTML = "";//"<div class='exPointIcon' style='top: 6px; left: 0px;'></div><p style='font-size: 14px; line-height: 14px;'>Flash 9 or higher required<br><a href='javascript:downloader.startFlashDownload();'>Click here to install</a></p>";
		}
	},

	createTimestamp : function () {
		currentDate = new Date();
		return currentDate.getUTCDay() + '' + currentDate.getUTCHours() + '' + currentDate.getUTCMonth() + '' + currentDate.getUTCMinutes() + '' +currentDate.getUTCSeconds() + '' + currentDate.getUTCMilliseconds();
	},

	detect : function () {
		// consider pluginFound to be false until proven true
		pluginFound = false;
		//console.debug("PLUGIN detect()");
		// allow for multiple checks in a single pass
		var daPlugins = this.detect.arguments;

		if (window.navigator.userAgent.indexOf("Chrome") != -1) {
			// Refresh the plug-ins first in case it changed since last check (chrome only)
			navigator.plugins.refresh(true);
		} else {
			// Refresh the plug-ins first in case it changed since last check
			navigator.plugins.refresh();
		}
		// if plugins array is there and not fake
		if (navigator.plugins && navigator.plugins.length>0) {
			var pluginsArrayLength = navigator.plugins.length;
			// for each plugin...
			var numFound = 0;
			for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
				// loop through all desired names and check each against the current plugin name
				//console.debug(navigator.plugins[pluginsArrayCounter].name);
				for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
					// if desired plugin name is found in either plugin name or description
					if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) ||
						(navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
						// this name was found
						numFound++;
						pluginFound = true;
						break;
					}
				}
			}
		}
		//console.debug(pluginFound);
		return pluginFound;

	}


}
