var newWin,  nameSuffix = 0; 
var numGames=6;
function getRandNum(max){
	//returns a number from 0 up to max
    var ranNum= Math.floor(Math.random()*(max+1));
    return ranNum;
}
function popup(url, winWidth, winHeight){ 
	if(newWin && !newWin.closed){
		newWin.close(); 
		newWin = null; 
	} 
	newWin = window.open(url, "popup" + nameSuffix, "height=" + winHeight + ",width=" + winWidth + ",scrollbars=1,resizable=yes");
	newWin.focus();
} 
function popup(url, winWidth, winHeight, lock){ 
	if(newWin && !newWin.closed){
		newWin.close(); 
		newWin = null; 
	} 
	if(lock==true){
		newWin = window.open(url, "popup" + nameSuffix, "height=" + winHeight + ",width=" + winWidth + ",scrollbars=0,resizable=no");
	}else{
		newWin = window.open(url, "popup" + nameSuffix, "height=" + winHeight + ",width=" + winWidth + ",scrollbars=1,resizable=no");
	}
	newWin.focus();
} 
// shows/hides a tag by id
function hideID(val){
	var id 	= document.getElementById(val);
	if(id==null) return;
	id.style.display = 'none'; 
}  
// shows/hides a tag by id
function showID(val){
	var id 	= document.getElementById(val);
	if(id==null) return;
	id.style.display = 'inline'; 
}  
function isVisible(val){
	var id 	= document.getElementById(val);
	if(id==null) return false;
	if(id.style.display == 'none')
		return false; 
	else
		return true;
}  
function swap(imgName, path, filename) {
	if(document[imgName]) {
		document[imgName].src = path + filename;
	}
}
function viewScores(num){
	viewScoresFull(num, 330, 400);
}
function viewScoresFull(num, w, h){
	popup('http://www.dvidearts.com/images/games/'+ num +'/topten.php?action=all', w, h);
}
function viewStats(id, name, score, w, h, usePopup){
	var lurl = 'http://www.dvidearts.com/images/games/'+ id +'/stats.php?name=' +  escape(name) + '&score=' + score + '&id=' + id;
	if(usePopup){
		popup(lurl, w, h, true);
	}else{
		window.location.href = lurl;
	}
}
function DownloadDemo(id, filename){
	CloseDownload(id);
	var url = "http://www.dvidearts.com/images/games/"+id+"/demo/"+filename;
	window.location.href =url;
}
function ShowPlatformSelectByGID(pgid){
	if(isVisible('divPlatformGID'+pgid)){
		hideID('divPlatformGID'+pgid);
	}else{
		for(var i=1; i<=numGames; i++){
			hideID('divPlatformGID'+i);
			hideID('divPlatformDownloadGID'+i);
		}
		showID('divPlatformGID'+pgid);
	}
}
function ShowPlatformDownloadSelectByGID(pgid){
	if(isVisible('divPlatformDownloadGID'+pgid)){
		hideID('divPlatformDownloadGID'+pgid);
	}else{
		for(var i=1; i<=numGames; i++){
			hideID('divPlatformGID'+i);
			hideID('divPlatformDownloadGID'+i);
		}
		showID('divPlatformDownloadGID'+pgid);
	}
}
function downloadGame(id, buttonID){
	if(isVisible('divPlatformDownloadGID'+id)){
		CloseDownload(id);
	}else{
		document.getElementById('divPlatformDownloadGID'+id).style.left=getElementLeft(buttonID);	
		document.getElementById('divPlatformDownloadGID'+id).style.top=getElementTop(buttonID)+getElementHeight(buttonID);							 
		ShowPlatformDownloadSelectByGID(id);
	}
}
function buyGame(id, buttonID){
	if(isVisible('divPlatformGID'+id)){
		CloseBuyNow(id);
	}else{
		document.getElementById('divPlatformGID'+id).style.left=getElementLeft(buttonID);	
		document.getElementById('divPlatformGID'+id).style.top=getElementTop(buttonID)+getElementHeight(buttonID);							 
		ShowPlatformSelectByGID(id);
	}
}
function getAbsoluteTop(o){
	for (var absTop = 0; o != document.body; o = o.offsetParent){
		absTop += o.offsetTop;
	}
	return absTop;
}
function getElementLeft(Elem) {
		var elem;
		if(document.getElementById) {
			elem = document.getElementById(Elem);
		} else if (document.all){
			elem = document.all[Elem];
		}
		xPos = elem.offsetLeft;
		tempEl = elem.offsetParent;
  		while (tempEl != null) {
  			xPos += tempEl.offsetLeft;
	  		tempEl = tempEl.offsetParent;
  		}
		return xPos;
}
function getElementTop(Elem) {
		var elem;
		if(document.getElementById) {	
			elem = document.getElementById(Elem);
		} else if (document.all) {
			elem = document.all[Elem];
		}
		yPos = elem.offsetTop;
		tempEl = elem.offsetParent;
		while (tempEl != null) {
  			yPos += tempEl.offsetTop;
	  		tempEl = tempEl.offsetParent;
  		}
		return yPos;
}
function getElementHeight(Elem) {
	var elem = document.getElementById(Elem);
	if(document.getElementById(Elem)!=null){
		return elem.offsetHeight;
	}
	return 0;
}
function getElementWidth(Elem) {
	var elem = document.getElementById(Elem);
	if(document.getElementById(Elem)!=null){
		return elem.offsetWidth;
	}
	return 0;
}
function CloseBuyNow(pgid){
	hideID('divPlatformGID'+pgid);
}
function CloseDownload(pgid){
	hideID('divPlatformDownloadGID'+pgid);
}
function buyNowBMT(index, value){
	CloseBuyNow(index);
	var ret=true;
	if(index > 0){
		if(value=='28390013'){
			ret = confirm("Designed for 8xxx, 71xx, and Pearl series blackberries.\nOlder 72xx, 75xx, and 77xx models are not supported.");
		}
		if(ret==true){
			window.location.href = "https://secure.bmtmicro.com/servlets/Orders.ShoppingCart?CID=2839&PRODUCTID=" + value.toLowerCase();
		}
	}
}
function BuyNow(platform, appID){
	var newWindow = null;
	switch (platform) {
	case 'iphone':
		newWindow = window.open("http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=" + appID, '_blank');
		break;
	case 'blackberry':
		newWindow = window.open("http://appworld.blackberry.com/webstore/content/" + appID, '_blank');
		break;
	}
	if(newWindow) {
		newWindow.focus();
	}
}