var shown = false;

var milestones = {
	rocketarena : {
		title : "Rocket Arena Performance Demo",
		date : "September 2011",
		img : "images/vanguard.png",
		yt : "jsD7j9alyqY",
		demo : "DeltaStrike.swf",
		icon : "images/artifact_small.png",
		height : "auto",
		maxHeight : 768,
		width : "auto",
		maxWidth : 1024,
		ratio : (4 / 3),
		failure : "Sorry. The demo seems to be too awesome for your browser.<br />",
		info : "Hold SHIFT key + drag mouse to rotate camera. Use mouse wheel to zoom.",
		tooltip : "show 3D demo",
		style : "demo",
		active : true,
		flashvars : {
			config : "resources/xml/VanguardMAXGameConfiguration.xml",
			gateway : "local"
		}
	},
	vanguard : {
		title : "Vanguard Demo",
		date : "June 2011",
		img : "images/vanguard.png",
		yt : "jsD7j9alyqY",
		demo : "DeltaStrike.swf",
		icon : "images/artifact_small.png",
		height : "auto",
		maxHeight : 768,
		width : "auto",
		maxWidth : 1024,
		ratio : (4 / 3),
		failure : "Sorry. The demo seems to be too awesome for your browser.<br />",
		info : "Hold SHIFT key + drag mouse to rotate camera. Use mouse wheel to zoom. 1-5 to fire special weapons.",
		tooltip : "show 3D demo",
		style : "demo",
		active : true,
		flashvars : {
			config : "resources/xml/VanguardGameConfiguration.xml"
		}
	},
	ds3mark : {
		title : "DS3Mark",
		date : "April 2011",
		img : "images/ds3mark.png",
		yt : "",
		demo : "DeltaStrike.swf",
		icon : "images/ds3mark80square.png",
		height : 600,
		width : 800,
		failure : "Sorry. The ds3mark seems to be too awesome for your browser.<br />",
		info : "Please don't forget to submit your results afterwards!",
		tooltip : "show 3D ds3mark",
		style : "ds3mark",
		active : true,
		flashvars : {
			config: "resources/xml/BenchmarkConfiguration.xml"
		}
	},
	blaster : {
		title : "Blaster Demo",
		date : "April 2011",
		img : "images/blasterDemo.png",
		yt : "z-Qc_e7HbBw",
		demo : "DeltaStrike.swf",
		icon : "images/mob_small.png",
		height : "auto",
		maxHeight : 720,
		width : "auto",
		maxWidth : 1280,
		ratio : (16 / 9),
		failure : "Sorry. The blaster demo seems to be too awesome for your browser.<br />",
		info : "Hold SHIFT key + drag mouse to rotate camera. Use mouse wheel to zoom.",
		tooltip : "show 3D demo",
		style : "demo",
		active : true,
		flashvars : {}
	},
	buffalo : {
		title : "Buffalo Demo",
		date : "March 2011",
		img : "images/buffaloDemo.png",
		yt : "VbFYJ_rb-GA",
		demo : "BuffaloScene.swf",
		icon : "images/buffalo_small.png",
		height : 720,
		width : 1280,
		failure : "Sorry. The awesome 3D demo seems to be too awesome for your browser.<br />",
		info : "Click and drag mouse to rotate camera. Use mouse wheel to zoom.",
		tooltip : "show 3D demo",
		style : "demo",
		active : true,
		flashvars : {}
	}
};

function showDemo(id) {
	
	var that = {visible: false};

	suspendBackground();
	$("body").css("overflow", "hidden");

	with(milestones[id]) {
		var lWidth = width;
		var lHeight = height;
		if(lWidth == "auto" || lHeight == "auto") {
			var bW = parseInt($(window).width());
			var bH = parseInt($(window).height());
			var asp = bW / bH;
			if(asp >= ratio) {
				lHeight = (bH - 140 > maxHeight) ? maxHeight : (bH - 140);
				lWidth = lHeight * ratio;
			}
			else {
				lWidth = (bW - 140 > maxWidth) ? maxWidth : (bW - 140);
				lHeight = lWidth * (1/ratio);
			}
		}
		var container = $("#demoContainer");
		container.dialog({
			height: lHeight + 60,
			width: lWidth,
			modal: true,
			open: function() {
				that.visible = true;
	        	$(".ui-dialog, .ui-widget-overlay").hide().fadeIn();
			},
			beforeClose: function(event, ui) {
		    	if (!that.visible) {
		    		return true;
		    	}
		        $(".ui-dialog, .ui-widget-overlay").fadeOut(function(){
		        	that.visible = false;
		        	container.dialog("close");
		        });
			},
			close: function(event, ui) {
				$("body").css("overflow", "auto");
				resumeBackground();
				container.empty().hide();
				container.dialog("destroy");
				
				var leaderBoard = $("#leaderBoard");
				if (leaderBoard.size() > 0) {
					$(window).scrollTop(leaderBoard.parents(".leaderBoard").position().top);
					if (!document.user.verified) {
						document.user.updateState();
					} else {
						initUserBoard();
					}
					initLeaderBoard();
				}
			},
			title: title
		});
		$("#demoContainer").html("<iframe src='"+id+"/"+id+".html'></iframe>");
		$("#demoContainer iframe").css({width: lWidth, height: lHeight});
		$("#demoContainer").append("<div class='info'>" + info + "</div>");
	}
}

function loadFlash(id, ignoreVersion) {
	with(milestones[id]) {
		var minVersion = [11, 1];
		if(ignoreVersion !== undefined) {
			minVersion = [0, 0];
		}
		flashembed("demo", {
			src: demo,
			version: minVersion,
			bgcolor: "#000000",
			width: "100%",
			height: "100%",
			wmode: "direct",
			allowfullscreen: false,
			allowscriptaccess: true,
			quality: "high",
			onFail: function() {
				var code = "<div class='failure'><p>" + failure;
				code += "Please make sure that you have installed the latest Flash 11 Player from the official Adobe page.<br />" +
						"<div class='show flash'><a class='getFlash' id='a" + id + "GetFlash' title='get flash 11' href='http://get.adobe.com/flashplayer/' target='_blank'><span>Get Flash Player 11</span></a></div>" +
						"If you are absolutly sure that you have the correct version installed, skip version check and <a href='javascript:loadFlash(\""+id+"\", true);'>force start</a></p></div>";
				$("#demo").html(code);
				$("#a" + id + "GetFlash").click(function() {
					clickStats($(this).attr("id"));
				});
			}
		}, flashvars);
		$("#demo .failure").css({width: "100%", height: "100%"});
		var bH = parseInt($(window).height());
		$("#demo .failure p").css("padding-top", bH/2 - 60);
	}
}

function hideDemo() {
	$("#demoContainer").dialog("destroy");
	$("#demo").html("");
}

function reloadDemo() {
	location.reload();
}

function tweetResult(text) {
	window.open("http://twitter.com/share?url="+encodeURIComponent("http://www.deltastrike.org/")+"&text="+encodeURIComponent(text));
}

