$(function() {
	makeAjaxLinks();
});

function makeAjaxLinks()
{
$('.page .main ul.otherversions li a').click(function()
{
	$(this).parent().addClass('loading');
	$.ajax({
		url: this.href + '/xml',
		dataType: 'xml',
		success: function(data, textStatus, xmlHttpObj)
		{
			if (textStatus == 'success')
			{
				//button
				$('.downloadarrow').removeClass('downloadarrow-beta').removeClass('downloadarrow-alpha');
				if ($(data).find('download beta').text() == 'true')
					$('.downloadarrow').addClass('downloadarrow-beta');
				if ($(data).find('download alpha').text() == 'true')
					$('.downloadarrow').addClass('downloadarrow-alpha');
				if ($(data).find('download version').text() != '')
					$('.info .version').text($(data).find('download version').text());
				if ($(data).find('download size').text() != '')
					$('.info .size').text($(data).find('download size').text());
				if ($(data).find('download url').text() != '')
					$('.downloadbutton a').attr("href",$(data).find('download url').text());
				if ($(data).find('download platform').text() != '') {
					$('.platformholder .platform').text($(data).find('download platform').text());
					$('.platformholder img').attr("alt",$(data).find('download platform').text());
				}
				if ($(data).find('download platformicon').text() != '')
					$('.downloadbutton span.platformholder img').attr("src",$(data).find('download platformicon').text());
				
				//prereq
				if ($(data).find('prerequisites').text() != '')
				{
					if ($('#prerequisites').length == 0)
					{
						$('ul.otherversions').after('<div id="prerequisites"><h3>' + $(data).find('prerequisites title').text() + '</h3><ul class="prerequisites"></ul></div>');
					}
					$('ul.prerequisites li').remove();
					$(data).find('prerequisites prerequisite').each(function()
					{
						var li = document.createElement('li');
						var a  = document.createElement('a');
						$(a).attr("href", $(this).find('url').text());
						$(a).attr("rel", "external");
						$(a).text($(this).find('name').text());
						$(li).append(a);
						$(li).addClass($(this).find('class').text());
						$('ul.prerequisites').append(li);
					});
				} else {
					$('#prerequisites').remove();
				}
				
				//sysreq
				if ($(data).find('systemrequirements os').text() != '')
					$('ul.systemrequirements .os').text($(data).find('systemrequirements os').text());
				if ($(data).find('systemrequirements processor').text() != '')
					$('ul.systemrequirements .processor').text($(data).find('systemrequirements processor').text());
				if ($(data).find('systemrequirements memory').text() != '')
					abbr(data, 'ul.systemrequirements .memory','systemrequirements memory');
				if ($(data).find('systemrequirements gfxcard').text() != '')
					abbr(data, 'ul.systemrequirements .gfxcard','systemrequirements gfxcard');
				if ($(data).find('systemrequirements hdd').text() != '')
					abbr(data, 'ul.systemrequirements .hdd','systemrequirements hdd');
					
				//alternatives
				{
					$('ul.otherversions li').remove();
					$(data).find('otherversions version').each(function()
					{
						var li = document.createElement('li');
						var a  = document.createElement('a');
						$(a).attr("href", $(this).find('url').text());
						$(a).text($(this).find('name').text());
						$(li).append(a);
						$('ul.otherversions').append(li);
					});
				}
				
				makeAjaxLinks();
			}
			return false;
		}
		});
		
		return false;
	});
}

function abbr(data, element, xml)
{
	var jq = $(element);
	jq.text('');
	var txt = $(data).find(xml).text();
	if (txt.indexOf("iB") >= 0)
	{
		var first = txt.substring(0,txt.indexOf("iB")-1);
		var last = txt.substring(txt.indexOf("iB")+2);
		var middle = txt.substring(txt.indexOf("iB")-1, txt.indexOf("iB")+2);
		jq.html(first + '<abbr title="' + $(data).find(xml + ' abbr').attr("title") + '">' + middle + "</abbr>" + last);
	} else jq.text(txt);
}
