var ff = false;
var IESIX = false;
var ua = navigator.userAgent;
var xmlHttp = null;
var affCode = "t_08ti";

if(ua.indexOf("MSIE") == -1)
{
	ff = true;
}
else
{
	if(ua.indexOf("6.0") != -1)
	{
		IESIX = true;
	}
}
if(ff)
{
	xmlHttp = new XMLHttpRequest();
}
else
{
	try
	{
		xmlHttp = new ActiveXObject("'Msxml2.DOMDocument.3.0");
	}
	catch (e)
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
}

//-------------------------------------------------------------------
function getArgs()
//-------------------------------------------------------------------
{	
	var args = new Object();	
	var query = unescape(location.search.substring(1));
	var pairs = query.split("&");
	for(var i = 0; i < pairs.length; i++) {
			var pos = pairs[i].indexOf('=');
			if (pos == -1) continue;
			var argname = pairs[i].substring(0,pos);
			argname = argname.toLowerCase();
			var value = pairs[i].substring(pos+1);
			args[argname] = unescape(value);
	}
	return args;
}

var queryString = getArgs();

xmlHttp.onreadystatechange = state_Change;
//xmlHttp.open("GET", queryString["g"] + ".xml", true);
xmlHttp.open("GET", "gameData.xml", true);
xmlHttp.send(null);


//-------------------------------------------------------------------
function newDiv(name, content, thumb)
//-------------------------------------------------------------------
{
	var d = document.createElement("DIV");
	d.id = name;
	d.className=name;
	d.innerHTML = content;
	if(thumb != null)
	{
		d.style.backgroundImage = "url(" + thumb + ")";
	}
	return d;
}

//-------------------------------------------------------------------
function state_Change()
//-------------------------------------------------------------------
{
    if (xmlHttp.readyState == 4)
    {
		var premiumShell = document.getElementById("premiumShell");
        var products = xmlHttp.responseXML.getElementsByTagName("product");
		var requirements = xmlHttp.responseXML.getElementsByTagName("marketing_info")[0].getElementsByTagName("en-us")[0].getElementsByTagName("minimum_system_requirements")[0];
		var product = new Object();
		
		try{product.id = products[0].getElementsByTagName("productid")[0].childNodes[0].data.toString()}catch(e){};
		try{product.title = products[0].getElementsByTagName("title")[0].childNodes[0].data.toString()}catch(e){};
		try{product.genre = products[0].getElementsByTagName("genre")[0].childNodes[0].data.toString()}catch(e){};
		try{product.longDescription = products[0].getElementsByTagName("marketing_info")[0].getElementsByTagName("en-us")[0].getElementsByTagName("long_description")[0].childNodes[0].data.toString()}catch(e){};	
		try{product.feature1 = products[0].getElementsByTagName("marketing_info")[0].getElementsByTagName("en-us")[0].getElementsByTagName("features_bullets")[0].getElementsByTagName("feature")[0].childNodes[0].data.toString()}catch(e){};	
		try{product.feature2 = products[0].getElementsByTagName("marketing_info")[0].getElementsByTagName("en-us")[0].getElementsByTagName("features_bullets")[0].getElementsByTagName("feature")[1].childNodes[0].data.toString()}catch(e){};	
		try{product.feature3 = products[0].getElementsByTagName("marketing_info")[0].getElementsByTagName("en-us")[0].getElementsByTagName("features_bullets")[0].getElementsByTagName("feature")[2].childNodes[0].data.toString()}catch(e){};
		try{product.feature4 = products[0].getElementsByTagName("marketing_info")[0].getElementsByTagName("en-us")[0].getElementsByTagName("features_bullets")[0].getElementsByTagName("feature")[3].childNodes[0].data.toString()}catch(e){};
		try{product.feature5 = products[0].getElementsByTagName("marketing_info")[0].getElementsByTagName("en-us")[0].getElementsByTagName("features_bullets")[0].getElementsByTagName("feature")[4].childNodes[0].data.toString()}catch(e){};
		try{product.feature6 = products[0].getElementsByTagName("marketing_info")[0].getElementsByTagName("en-us")[0].getElementsByTagName("features_bullets")[0].getElementsByTagName("feature")[5].childNodes[0].data.toString()}catch(e){};
		try{product.feature7 = products[0].getElementsByTagName("marketing_info")[0].getElementsByTagName("en-us")[0].getElementsByTagName("features_bullets")[0].getElementsByTagName("feature")[6].childNodes[0].data.toString()}catch(e){};
		try{product.feature8 = products[0].getElementsByTagName("marketing_info")[0].getElementsByTagName("en-us")[0].getElementsByTagName("features_bullets")[0].getElementsByTagName("feature")[8].childNodes[0].data.toString()}catch(e){};
		try{product.feature9 = products[0].getElementsByTagName("marketing_info")[0].getElementsByTagName("en-us")[0].getElementsByTagName("features_bullets")[0].getElementsByTagName("feature")[9].childNodes[0].data.toString()}catch(e){};
		try{product.boxshot = products[0].getElementsByTagName("boxshot")[0].childNodes[0].data.toString()}catch(e){};
		try{product.mainImage = products[0].getElementsByTagName("screenshots")[0].getElementsByTagName("screenshot")[0].childNodes[0].data.toString()}catch(e){};
		try{product.screenShot1 = products[0].getElementsByTagName("screenshots")[0].getElementsByTagName("screenshot")[1].childNodes[0].data.toString()}catch(e){};
		try{product.screenShot2 = products[0].getElementsByTagName("screenshots")[0].getElementsByTagName("screenshot")[2].childNodes[0].data.toString()}catch(e){};
		try{product.screenShot3 = products[0].getElementsByTagName("screenshots")[0].getElementsByTagName("screenshot")[3].childNodes[0].data.toString()}catch(e){};
		try{product.price = products[0].getElementsByTagName("prices")[0].getElementsByTagName("usd")[0].childNodes[0].data.toString()}catch(e){};
		try{product.trialPeriod = products[0].getElementsByTagName("trial_criteria")[0].getElementsByTagName("en-us")[0].getElementsByTagName("maximum_usage_time")[0].childNodes[0].data.toString()}catch(e){};
		try{product.tryLink = products[0].getElementsByTagName("triallink")[0].childNodes[0].data.toString()}catch(e){};// + "&aff=" + affCode;
		try{product.buyLink = products[0].getElementsByTagName("buylink")[0].childNodes[0].data.toString()}catch(e){};// + "&aff=" + affCode;
		
		
		var mainImage = document.createElement("IMG");
		mainImage.src = product.mainImage;
		mainImage.alt = product.title;
		mainImage.id = "mainImage";
		
		var downloadWrapper = document.createElement("A");
		var downloadWrapperLeft = document.createElement("SPAN");
		var downloadWrapperRight = document.createElement("SPAN");
		var downloadButton = document.createElement("SPAN");
		downloadWrapper.href= product.tryLink;
		downloadWrapper.title = "Download Free Trial Now";
		downloadButton.innerHTML = "Download Free Trial";
		downloadWrapper.id = "downloadButton";
		downloadButton.className = "downloadButtonInner";
		downloadWrapperLeft.className = "leftRedButton";
		downloadWrapperRight.className = "rightRedButton";
		downloadWrapper.appendChild(downloadWrapperLeft);
		downloadWrapper.appendChild(downloadButton);
		downloadWrapper.appendChild(downloadWrapperRight);
		
		var buyWrapper = document.createElement("A");
		var buyWrapperLeft = document.createElement("SPAN");
		var buyWrapperRight = document.createElement("SPAN");
		var buyButton = document.createElement("SPAN");
		buyWrapper.href = product.buyLink;
		buyWrapper.title = "Buy Full Version";
		buyButton.innerHTML = "Buy Full Version";
		buyWrapper.id = "buyButton";
		buyButton.className = "buyButtonInner";
		buyWrapperLeft.className = "leftRedButton";
		buyWrapperRight.className = "rightRedButton";
		buyWrapper.appendChild(buyWrapperLeft);
		buyWrapper.appendChild(buyButton);
		buyWrapper.appendChild(buyWrapperRight);
		
		var descriptionWrapper = document.createElement("DIV");
		descriptionWrapper.id = "description";
		
		var description = document.createElement("P");
		var features = document.createElement("P");
		
		if (product.longDescription.length > 600)
		{
			descriptionWrapper.style.overflow = "scroll";
		}
		else
		{
			descriptionWrapper.style.overflow = "hidden";
		}
		
		description.innerHTML = product.longDescription;
		descriptionWrapper.appendChild(description);
		
		var screenshot1 = document.createElement("A");
		screenshot1.style.backgroundImage = "url(" + product.screenShot1 + ")";
		screenshot1.title = "Click to enlarge";
		screenshot1.id = "screenshot1";
		screenshot1.className = "ss";
		screenshot1.href = "#";
		screenshot1.onclick = function(){
			if (IESIX)
			{
				document.getElementById("screenshot1Large").style.display = "block";
			}
			else
			{
				$("#screenshot1Large").fadeIn(1000);
			}
			return false;
		};
		
		var screenshot2 = document.createElement("A");
		screenshot2.style.backgroundImage = "url(" + product.screenShot2 + ")";
		screenshot2.title = "Click to enlarge";
		screenshot2.id = "screenshot2";
		screenshot2.className = "ss";
		screenshot2.href = "#";
		screenshot2.onclick = function(){
			if (IESIX)
			{
				document.getElementById("screenshot2Large").style.display = "block";
			}
			else
			{
				$("#screenshot2Large").fadeIn(1000);
			}
			return false;
		};
		
		var screenshot3 = document.createElement("A");
		screenshot3.style.backgroundImage = "url(" + product.screenShot3 + ")";
		screenshot3.title = "Click to enlarge";
		screenshot3.id = "screenshot3";
		screenshot3.className = "ss";
		screenshot3.href = "#";
		screenshot3.onclick = function(){
			if (IESIX)
			{
				document.getElementById("screenshot3Large").style.display = "block";
			}
			else
			{
				$("#screenshot3Large").fadeIn(1000);
			}
			return false;
		};
		
		var screenshot1Large = document.createElement("A");
		screenshot1Large.style.backgroundImage = "url(" + product.screenShot1 + ")";
		screenshot1Large.id = "screenshot1Large";
		screenshot1Large.className = "ssLarge";
		screenshot1Large.href = "#";
		screenshot1Large.onclick = function(){
			if (IESIX)
			{
				document.getElementById("screenshot1Large").style.display = "none";
			}
			else
			{
				$("#screenshot1Large").fadeOut(1000);
			}
			return false;
		};
		
		var screenshot2Large = document.createElement("A");
		screenshot2Large.style.backgroundImage = "url(" + product.screenShot2 + ")";
		screenshot2Large.id = "screenshot2Large";
		screenshot2Large.className = "ssLarge";
		screenshot2Large.href = "#";
		screenshot2Large.onclick = function(){
			if (IESIX)
			{
				document.getElementById("screenshot2Large").style.display = "none";
			}
			else
			{
				$("#screenshot2Large").fadeOut(1000);
			}
			return false;
		};

		var screenshot3Large = document.createElement("A");
		screenshot3Large.style.backgroundImage = "url(" + product.screenShot3 + ")";
		screenshot3Large.id = "screenshot3Large";
		screenshot3Large.className = "ssLarge";
		screenshot3Large.href = "#";
		screenshot3Large.onclick = function(){
			if (IESIX)
			{
				document.getElementById("screenshot3Large").style.display = "none";
			}
			else
			{
				$("#screenshot3Large").fadeOut(1000);
			}
			return false;
		};
		
		var systemUL = document.createElement("UL");
		systemUL.id = "requirementsList";
		
		for (var i = 0; i < requirements.childNodes.length; i++)
		{
			try
			{
				var systemLI = document.createElement("LI");
				systemLI.innerHTML = requirements.childNodes[i].firstChild.data.toString();
				systemUL.appendChild(systemLI);
			}
			catch (e)
			{
			}
		}
		
		var featureList = document.createElement("UL");
		var feature1 = document.createElement("LI");
		var feature2 = document.createElement("LI");
		var feature3 = document.createElement("LI");
		var feature4 = document.createElement("LI");
		var feature5 = document.createElement("LI");
		var feature6 = document.createElement("LI");
		var feature7 = document.createElement("LI");
		var feature8 = document.createElement("LI");
		var feature9 = document.createElement("LI");
		
		featureList.id = "features";
		
		feature1.innerHTML = product.feature1;
		feature1.className = "feature";
		featureList.appendChild(feature1);
		
		feature2.innerHTML = product.feature2;
		feature2.className = "feature";
		featureList.appendChild(feature2);
		
		feature3.innerHTML = product.feature3;
		feature3.className = "feature";
		featureList.appendChild(feature3);
		
		if(product.feature4 != undefined)
		{
			feature4.innerHTML = product.feature4;
			feature4.className = "feature";
			featureList.appendChild(feature4);
		}
		
		if(product.feature5 != undefined)
		{
			feature5.innerHTML = product.feature5;
			feature5.className = "feature";
			featureList.appendChild(feature5);
		}
		
		if(product.feature6 != undefined)
		{
			feature6.innerHTML = product.feature6;
			feature6.className = "feature";
			featureList.appendChild(feature6);
		}
		
		if(product.feature7 != undefined)
		{
			feature7.innerHTML = product.feature7;
			feature7.className = "feature";
			featureList.appendChild(feature7);
		}
		
		if(product.feature8 != undefined)
		{
			feature8.innerHTML = product.feature8;
			feature8.className = "feature";
			featureList.appendChild(feature8);
		}
		
		if(product.feature9 != undefined)
		{
			feature9.innerHTML = product.feature9;
			feature9.className = "feature";
			featureList.appendChild(feature9);
		}
		
		
		
		descriptionWrapper.appendChild(featureList);
		
		var trialDetails = document.createElement("DIV");
		trialDetails.id = "trialDetails";
		
		var trialDetailsInner = document.createElement("DIV");
		trialDetailsInner.id = "trialDetailsInner";
		
		var trialDetailsTagLine = document.createElement("DIV");
		trialDetailsTagLine.id = "trialDetailsTagLine";
		trialDetailsTagLine.innerHTML = "<span><b>and play now!</b></span><p><b>Trial Period:</b><br/>" + product.trialPeriod + "</p>";
				
		var trialDetailsList = document.createElement("UL");
		trialDetailsList.id = "trialDetailsList";
		trialDetailsList.innerHTML = "<li>Better than a demo; it's the full game</li><li>No internet connection needed to play</li><li>No CD-ROM necessary</li><li>Convert to full version for only US$" + product.price + " with no additional downloads</li>";
		
		var buyDetails = document.createElement("DIV");
		buyDetails.id = "buyDetails";
		
		var buyDetailsInner = document.createElement("DIV");
		buyDetailsInner.id = "buyDetailsInner";
		
		var buyDetailsTagLine = document.createElement("DIV");
		buyDetailsTagLine.id = "buyDetailsTagLine";
		buyDetailsTagLine.innerHTML = "<span><b>for only US$" + product.price + "</b></span>";
				
		var buyDetailsList = document.createElement("UL");
		buyDetailsList.id = "buyDetailsList";
		buyDetailsList.innerHTML = "<li>Unlimited play; you own the game</li><li>Instant activation</li><li>No additional downloads</li><li>No shipping, waiting or CD-ROM necessary</li><li>48-hour money back guarantee</li>";
		
		buyDetailsInner.appendChild(buyDetailsTagLine);
		buyDetailsInner.appendChild(buyDetailsList);
		buyDetails.appendChild(buyDetailsInner);

		premiumShell.appendChild(mainImage);
		premiumShell.appendChild(descriptionWrapper);
		premiumShell.appendChild(buyWrapper);
		premiumShell.appendChild(systemUL);
		premiumShell.appendChild(newDiv("title", "<span class=\"leftButton\"></span><span class=\"titleInner\">" + product.title + "</span><span class=\"rightButton\"></span>", null));
		premiumShell.appendChild(newDiv("requirements", "<span class=\"leftButton\"></span><span class=\"requirementsInner\">System Requirements</span><span class=\"rightButton\"></span>", null));
		premiumShell.appendChild(newDiv("screenshots", "<span class=\"leftButton\"></span><span class=\"screenshotsInner\">Screenshots</span><span class=\"rightButton\"></span>", null));
		
		premiumShell.appendChild(screenshot1);
		premiumShell.appendChild(screenshot2);
		premiumShell.appendChild(screenshot3);
		
		premiumShell.appendChild(screenshot1Large);
		premiumShell.appendChild(screenshot2Large);
		premiumShell.appendChild(screenshot3Large);
		
		if(product.tryLink != undefined)
		{
			if (product.trialPeriod != undefined)
			{
				trialDetailsInner.appendChild(trialDetailsTagLine);
			}
			premiumShell.appendChild(downloadWrapper);
			premiumShell.appendChild(trialDetails);
		}
		else
		{
			buyDetails.className = "buyNoTrial";
			buyWrapper.className = "buyNoTrial";
		}
		
		trialDetailsInner.appendChild(trialDetailsList);
		trialDetails.appendChild(trialDetailsInner);
		premiumShell.appendChild(buyDetails);
    }
}