var menu_items=new Array("home","games","shop","blog","skins","links");
var menu_enabled={"home":1,
				 "games":1,
			  	 "shop":0,
			  	 "blog":1,
			  	 "skins":1,
			 	 "links":1};
var corporate_items=new Array("home","faqs","services","contact","jobs","termsofuse","privacypolicy");
var nitrome_ads=new Array("","artist_job.png","developer_job.png","bowcontest_1.png","bowcontest_2.png","bowcontest_3.png","bowcontest_4.png");
var nitrome_alts=new Array("","Pixel Artist Vacancy","Games Developer Vacancy","B.C. Bow Contest","B.C. Bow Contest","B.C. Bow Contest","B.C. Bow Contest");
var nitrome_links=new Array("","http://www.nitrome.com/jobs","http://www.nitrome.com/jobs","http://www.nitrome.com/games/bcbowcontest/","http://www.nitrome.com/games/bcbowcontest/","http://www.nitrome.com/games/bcbowcontest/","http://www.nitrome.com/games/bcbowcontest/");
var ad_chance=new Array(0,10,20,40,60,80,100);
//global
var top_req;
function fillWithImage(tag,image,w,h,border,alt,n,u){
	var s="";
	if(u!=""){
		s+="<a href='";
		s+=u;
		s+="' />";
	}
	s+="<img src='";
	s+=image;
	s+="' width='";
	s+=w;
	s+="' height='";
	s+=h;
	if(n!=""){
		s+="' name='";
		s+=n;
	}
	if(border!=""){
		s+="' border='";
		s+=border;
	}
	if(alt!=""){
		s+="' alt='";
		s+=alt;
	}
	s+="' />";
	if(u!=""){
		s+="</a>";
	}
	document.getElementById(tag).innerHTML=s;
}			 
function removeWhitespace(xml){
	var loopIndex;
	for (loopIndex = 0; loopIndex < xml.childNodes.length; loopIndex++){
		var currentNode = xml.childNodes[loopIndex];
		if (currentNode.nodeType == 1){
			removeWhitespace(currentNode);
		}
		if (((/^\s+$/.test(currentNode.nodeValue))) && (currentNode.nodeType == 3)){
			xml.removeChild(xml.childNodes[loopIndex--]);
		}
	}
	return(xml);
}
function writeSideAd(){
	//nitrome ad - have this randomly pull in?
	var r=Math.floor(Math.random()*100)+1;

	for(var i=0; i<ad_chance.length; i++){
		if(r<=ad_chance[i]){
			break;	
		}	
	}
	if(i==ad_chance.length){
		i--;
	}
	fillWithImage("left_bottom_bar",images_folder+"side_ad_top_bar.gif","122","19","0","","","");
	fillWithImage("left_bottom_box",cdn_path+"images/sidead/"+nitrome_ads[i],"122","205","0",nitrome_alts[i],"",nitrome_links[i]);
}
function writeMenu(){
	document.getElementById("menu_box").style.backgroundImage='url('+images_folder+"menu_sprite.png"+')';
	for(var i=0; i<=5; i++){
		if(menu_enabled[menu_items[i]]){
			var m="<a href='";
			if(menu_items[i]=="home"||menu_items[i]=="games"){
				m+="http://www.nitrome.com/";	
			}else{
				m+=root_dir;
				m+=menu_items[i];
			}
			m+="' ><img src='";
			m+=images_folder;
			m+="trans.gif' width='100%' height='100%' border='0' onmouseover='menuRollover(&#39;";
			m+=menu_items[i];
			m+="&#39;);' onmouseout='menuRollout();' /></a>";
			document.getElementById("menu_item_"+menu_items[i]).innerHTML=m;
		}
	}
}
function menuRollover(item){
	if(item=="home"){
		document.getElementById("menu_box").style.backgroundPosition="0 0";
	}else if(item=="games"){
		document.getElementById("menu_box").style.backgroundPosition="-122px 0";
	}else if(item=="shop"){
		document.getElementById("menu_box").style.backgroundPosition="-244px 0";
	}else if(item=="blog"){
		document.getElementById("menu_box").style.backgroundPosition="-366px 0";
	}else if(item=="skins"){
		document.getElementById("menu_box").style.backgroundPosition="-488px 0";
	}else if(item=="links"){
		document.getElementById("menu_box").style.backgroundPosition="-610px 0";
	}
}
function menuRollout(){
	document.getElementById("menu_box").style.backgroundPosition="0 0";
}
function writeTopBar(){
	fillWithImage("top_bar_box",images_folder+pageid+"_top_bar.png","566","19","0","","","");
}
function writeAdDescriptions(){
	fillWithImage("banner_description",images_folder+"banner_description.gif","7","90","0","","","");
	fillWithImage("skyscraper_description",images_folder+"skyscraper_description.gif","160","7","0","","","");
}
function writeTopGames(){
	//main function which calls in xml
	fillWithImage("top_five_inner",images_folder+"top_games_panel.png","108","137","0","","","");
	fillWithImage("left_top_bar",images_folder+"top_games_top_bar.gif","122","19","0","","","");
	//load the top five from xml
	if (window.XMLHttpRequest){// code for all new browsers
  		top_req=new XMLHttpRequest();
  	}else if (window.ActiveXObject){// code for IE5 and IE6
  		top_req=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	if (top_req!=null){
  		top_req.onreadystatechange=parseTopGames;
  		top_req.open("GET",xml_folder+"topgames.xml",true);
  		top_req.send(null);
  	}else{
  		//alert("Your browser does not support XMLHTTP.");
  	}
}
function parseTopGames(){
	//bit of a hack but seems to work
	if(top_req.readyState==4){
		if(top_req.responseText!=""){
		 	try{
			 	//Internet Explorer
			 	xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async="false";
				xmlDoc.loadXML(top_req.responseText);
		 	}catch(e){
			 	// code for all new browsers
				parser=new DOMParser();
   		 		xmlDoc=parser.parseFromString(top_req.responseText,"text/xml");
		 	}
		 	xmlDoc=removeWhitespace(xmlDoc);
    		var games=xmlDoc.documentElement.childNodes;
    		finalizeTopGames(games);
		}
	}
}
function finalizeTopGames(games){
	writeTopFivePic(games[0].getAttribute("id"),games[0].getAttribute("name"));
    for(var i=0; i<=4; i++){
	    var tf="";
		tf+="<a class='h' href='";
		tf+=root_dir;
		tf+="games/";
		tf+=games[i].getAttribute("id");
		tf+="' onMouseOver='topFiveRollover(&#39;";
		tf+=games[i].getAttribute("id");
		tf+="&#39;);' onMouseOut='topFiveRollover(&#39;";
		tf+=games[0].getAttribute("id");
		tf+="&#39;);'>";
		tf+=(i+1);
		tf+=". ";
		tf+=games[i].getAttribute("name");
		tf+="</a>";
		document.getElementById("top_five_text_"+(i+1)).innerHTML=tf;
    }
}
function topFiveRollover(id){
	var p=thumbnails_folder;
	p+="tf_";
	p+=id;
	p+=".png";
	document.topfivepic.src=p;
}
function writeTopFivePic(id,name_str){
	fillWithImage("top_five_pic",thumbnails_folder+"tf_"+id+".png","108","49","0","","topfivepic",root_dir+"games/"+id+"/");
}
function writeCorporate(){
	document.getElementById("corporate").style.backgroundImage='url('+images_folder+"corporate_sprite.png"+')';
	for(var i=0; i<=6; i++){
		var m="<a href='";
		if(corporate_items[i]=="home"){
			m+=root_dir;
			m+="index.html";
		}else{
			m+=root_dir;
			m+=corporate_items[i];
		}
		m+="' ><img src='";
		m+=images_folder;
		m+="trans.gif' width='100%' height='100%' border='0' onmouseover='corporateRollover(&#39;";
		m+=corporate_items[i];
		m+="&#39;);' onmouseout='corporateRollout();' /></a>";
		document.getElementById("corporate_"+corporate_items[i]).innerHTML=m;
	}
}
function corporateRollover(item){
	if(item=="home"){
		document.getElementById("corporate").style.backgroundPosition="0 0";
	}else if(item=="faqs"){
		document.getElementById("corporate").style.backgroundPosition="0 -21px";
	}else if(item=="services"){
		document.getElementById("corporate").style.backgroundPosition="0 -42px";
	}else if(item=="contact"){
		document.getElementById("corporate").style.backgroundPosition="0 -63px";
	}else if(item=="jobs"){
		document.getElementById("corporate").style.backgroundPosition="0 -84px";
	}else if(item=="termsofuse"){
		document.getElementById("corporate").style.backgroundPosition="0 -105px";
	}else if(item=="privacypolicy"){
		document.getElementById("corporate").style.backgroundPosition="0 -126px";
	}
}
function corporateRollout(){
	document.getElementById("corporate").style.backgroundPosition="0 0";
}