$(function() {
	var useragent = navigator.userAgent.toLowerCase();
	var os = {
		osx: /macintosh/i.test(useragent),
		windows: {
			xp: /windows nt 5/i.test(useragent),
			vista: /windows nt 6/i.test(useragent),
			win7: /windows nt 7/i.test(useragent)
		},
		linux: /linux/i.test(useragent)
	}
	
	if ($("#OperatingSystem").length>0) {
		var osSelect = $("#OperatingSystem");
		var identifiedOS = 0;
		if (os.osx) identifiedOS = 1;
		else if (os.linux) identifiedOS = 3;
		else if (os.windows.vista||os.windows.win7) identifiedOS = 0;
		else identifiedOS = 2;
		osSelect[0].selectedIndex = identifiedOS;
		setFilesize();
	}
	
	$("a[rel='screenshots']").click(function() {
		Screenshots(this);
	});
	
	$("#DownloadButton").click(function() {
		downloadApplication($("#OperatingSystem").val());
	});
	
	$("#OperatingSystem").change(setFilesize);
	
});

function setFilesize() {
	var type = $("#OperatingSystem").val();
	$("#Filesize").html("Filesize: " + downloads[type].size);
}

function Screenshots(el) {
	var type = el.href.split("/")[6];
	var title = "";
	switch (type) {
		case "windows":
			title = "Windows 7 / Vista Sidebar Gadget";
			break;
		case "osx":
			title = "Mac OS X Dashboard Widget";
			break;
		case "google":
			title = "Google Gadget";
			break;
		case "yahoo":
			title = "Yahoo! Widget";
			break;
	}
	
	var c = new au.com.tramtracker.Modal();
	c.init({
		title: title + " Screenshots",
		id: "screenshotPopup",
		width: '710px',
		height: '450px',
		animation: true
	});
	c.load({
		type: "GET",
		url: "/modal.php?p=downloads&s="+type,
		dataType: "html"
	});
}

var downloads = {
	goo: {
		href: "http://gadget.tramtracker.com/GoogleGadget/tramtracker.gg",
		size: "78.4kb"
	},
	yah: {
		href: "http://widget.tramtracker.com/YahooWidget/tramTRACKER.widget",
		size: "97.2kb"
	},
	osx: {
		href: "http://widget.tramtracker.com/DashboardWidget/tramTRACKER.zip",
		size: "321kb"
	},
	win: {
		href: "http://gadget.tramtracker.com/VistaGadget/tramTRACKER.gadget",
		size: "155kb"
	}
}

function downloadApplication(type) {
	var download = downloads[type];
	if ((type == "goo") || (type == "yah")) {
		var c = new au.com.tramtracker.Modal();
		c.init({
			title: "Requirements",
			id: "RequirementsPopup",
			width: "300px",
			height: "250px",
			animation: false
		});
		c.load({
			type: "GET",
			url: "/modal.php?p=downloads&s="+type,
			dataType: "html"
		});
		document.documentElement.scrollTop = 0;
	}
	else {
		if (download.href) window.location = download.href;
	}
}