if (!XMLHttpRequest) {
  window.XMLHttpRequest = function() {
    return new ActiveXObject('Microsoft.XMLHTTP');
  }
}

function switchto(dropdown){
    var myindex  = dropdown.selectedIndex;
    var SelValue = dropdown.options[myindex].value;
    var baseURL  = 'build.php?build='+SelValue;
    top.location.href = baseURL;
    
    return true;
}	
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}


function LinkList(m,config,action) {
	var link=new Array(),variable=new Array(),last=new Array();
	var temp,temp2,add,add2;
	if(config==0){
	for (i=0; i<document.links.length; i++) {
		temp=String(document.links[i]);
		link=temp.split('m=');
		if(link[1]){
			variable=link[1].split('&');
			temp2=document.links[i].id.indexOf('link');
			if(temp2==-1){
				if(variable[1]){
					add='&'+variable[1];
				}else{
					add='';
				}
				document.links[i].href=link[0]+'m='+m+add;
			}else if(temp2!=-1 && config==1){
				if(variable[1]){
					add='&'+variable[1];
				}else{
					add='';
				}
				document.links[i].href=link[0]+'m='+m+add;
			}
		}
   }
   }else{
	for (i=0; i<document.links.length; i++) {
		temp=String(document.links[i]);
		link=temp.split('m=');
		if(link[1]){
			variable=link[1].split('&');
			last=variable[0].split('a');
			if(last[1] && action==0){
				add2=variable[0];
			}else if(!last[1] && action==0){
				add2='a'+last[0];
			}else if(last[1] && action==1){
				add2=last[1];
			}else if(!last[1] && action==1){
				add2=last[0];
			}
			temp2=document.links[i].id.indexOf('link');
				if(variable[1]){
					add='&'+variable[1];
				}else{
					add='';
				}
				document.links[i].href=link[0]+'m='+add2+add;
		}
	}
   }
   

}

menu_status = new Array();

function showHide(num,action,sec)
{

	var i,action;
	var link=new Array(),variable=new Array(),fin=new Array();
	var menus=new Array();
		menus[0]='kingdom';
		menus[1]='military';
		menus[2]='research';
		menus[3]='diplomacy';
		menus[4]='other';
	var cook=readCookie('CO_M');
if(action==null){
	LinkList(num,0);
	if(num>=0 && num<6){
		var cook_status=document.getElementById(menus[num]).className;
	}
	if(num==cook && cook_status=='show'){
		createCookie('CO_M',9,'365');
	}else{
		createCookie('CO_M',num,'365');
	}
		for(i=0;i<5;i++){
		if(i==num){
		if(menu_status[menus[i]]!='show')

			{
				document.getElementById(menus[i]).className='show';
				menu_status[menus[i]]='show';
				document.getElementById('hide').value=num;
			}
		else
			{
				document.getElementById(menus[i]).className='hide';
				menu_status[menus[i]]='hide';
				document.getElementById('hide').value=9;
			}
		}
		else{
			document.getElementById(menus[i]).className='hide';
			menu_status[menus[i]]='hide';
		}
	}
}
else if(action=='plus'){
	LinkList(num,1,0);
	createCookie('CO_M','a0','365');
	for(i=0;i<5;i++){
		document.getElementById(menus[i]).className='show';
	}
}
else if(action=='minus'){
	LinkList(num,1,1);
	createCookie('CO_M','9','365');
	for(i=0;i<5;i++){
		document.getElementById(menus[i]).className='hide';
	}
}

}
function over(s)
{
var s=s,class_name=document.getElementById(s).className;
	if(class_name=='over'){
		document.getElementById(s).className='shown';
	}
	else{
		document.getElementById(s).className='over';
	}

}
var i=0,j=0;
while(document.links[i++]){
alert(document.links[j++]);
}
var tooltip = false;
	var tooltipShadow = false;
	var dhtmlgoodies_shadowSize = 0;
	var tooltipMaxWidth = 250;
	var tooltipMinWidth = 50;
	var dhtmlgoodies_iframe = false;
	var tooltip_is_msie = (navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('opera')==-1 && document.all)?true:false;

	function showtooltip(e,tooltipTxt)
	{
		var browser=navigator.userAgent;
		var res=browser.search(/MSIE/i);
		if((res>0 && window.parent.document.readyState == 'complete') || (res<0)){
		
			var bodyWidth = Math.max(document.body.clientWidth,document.documentElement.clientWidth) - 20;
		
			if(!tooltip){
				tooltip = document.createElement('DIV');
				tooltip.id = 'tooltip';
				tooltipShadow = document.createElement('DIV');
				tooltipShadow.id = 'tooltipShadow';
				document.body.appendChild(tooltip);
				document.body.appendChild(tooltipShadow);
				if(tooltip_is_msie){
					dhtmlgoodies_iframe = document.createElement('IFRAME');
					dhtmlgoodies_iframe.frameborder='5';
					dhtmlgoodies_iframe.style.backgroundColor='#FFFFFF';
					dhtmlgoodies_iframe.src = '#'; 	
					dhtmlgoodies_iframe.style.zIndex = 100;
					dhtmlgoodies_iframe.style.position = 'absolute';
					document.body.appendChild(dhtmlgoodies_iframe);
				}
				
			}
			
			tooltip.style.display='block';
			tooltipShadow.style.display='none';
			if(tooltip_is_msie)dhtmlgoodies_iframe.style.display='block';
			
			var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
			if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0; 
			var leftPos = e.clientX + 10;
			
			tooltip.style.width = null;	// Reset style width if it's set 
			tooltip.innerHTML = tooltipTxt;
			tooltip.style.left = leftPos + 'px';
			tooltip.style.top = e.clientY + 10 + st + 'px';

			
			tooltipShadow.style.left =  leftPos + dhtmlgoodies_shadowSize + 'px';
			tooltipShadow.style.top = e.clientY + 10 + st + dhtmlgoodies_shadowSize + 'px';
			
			if(tooltip.offsetWidth>tooltipMaxWidth){	/* Exceeding max width of tooltip ? */
				tooltip.style.width = tooltipMaxWidth + 'px';
			}
			
			var tooltipWidth = tooltip.offsetWidth;		
			if(tooltipWidth<tooltipMinWidth)tooltipWidth = tooltipMinWidth;
			
			
			tooltip.style.width = tooltipWidth + 'px';
			tooltipShadow.style.width = tooltip.offsetWidth + 'px';
			tooltipShadow.style.height = tooltip.offsetHeight + 'px';		
			
			if((leftPos + tooltipWidth)>bodyWidth){
				tooltip.style.left = (tooltipShadow.style.left.replace('px','') - ((leftPos + tooltipWidth)-bodyWidth)) + 'px';
				tooltipShadow.style.left = (tooltipShadow.style.left.replace('px','') - ((leftPos + tooltipWidth)-bodyWidth) + dhtmlgoodies_shadowSize) + 'px';
			}
			
			if(tooltip_is_msie){
				dhtmlgoodies_iframe.style.left = tooltip.style.left;
				dhtmlgoodies_iframe.style.top = tooltip.style.top;
				dhtmlgoodies_iframe.style.width = tooltip.offsetWidth + 'px';
				dhtmlgoodies_iframe.style.height = tooltip.offsetHeight + 'px';
			
			}
		}		
	};
	function hide()
	{
		tooltip.style.display='none';
		tooltipShadow.style.display='none';		
		if(tooltip_is_msie)dhtmlgoodies_iframe.style.display='none';		
	}
function pop(hand,m){
	var poprequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		poprequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			poprequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				poprequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	poprequest.onreadystatechange = function(){
		if(poprequest.readyState == 4){
			var popdisplay = document.getElementById('opt');
			popdisplay.innerHTML = poprequest.responseText;
		}
	}
	var act;
	if(document.getElementById(hand).checked==true){
		act=1;
	}
	if(document.getElementById(hand).checked==false){
		act=0;
	}
	var queryString='?action='+hand+'&act='+act+'&m='+m;
	poprequest.open("GET",'option_popups.php' + queryString, true);
	poprequest.send(null); 
}



function names(theid)
{
	if(menu_status[theid]!='on2')
    {
       document.getElementById(theid).className='on2';
       menu_status[theid]='on2';
    }
	else
	{
		document.getElementById(theid).className='off2';
		menu_status[theid]='off2';
	}
}

function popi(hand,m){
	var eve=new Array(),sec=new Array(),string,i;
	eve=hand.split('?');
	sec=eve[1].split('e=');

	for(i=1;i<6;i++){
		string='eve'+i;
		if(i!=sec[1]){
			document.getElementById(string).className='tab_off';
		}else{
			document.getElementById(string).className='tab';
		}
	}
	// Create a function that will receive data sent from the server
	document.getElementById('loading').style.display='block';
	document.getElementById('conts').src=hand;
}

function overview(hand,m){

	for(i=1;i<6;i++){
		string='eve'+i;
		if(i!=hand){
			document.getElementById(string).className='tab_off';
		}else{
			document.getElementById(string).className='tab';
		}
	}
	switch(hand){
		case 1: link='news_frame.php';
		break;
		case 2: link='event_frame.php?ch=1';
		break;
		case 3: link='event_frame.php?ch=2';
		break;
		case 4: link='event_frame.php?ch=3';
		break;
		case 5: link='event_frame.php?ch=4';
		break;
	}
	// Create a function that will receive data sent from the server
	document.getElementById('loading').style.display='block';
	document.getElementById('conts').src=link;
}
function tutorial(id){
	var getid=document.getElementById('tutorial');
	if(getid.style.display=='none'){
		getid.style.display='block';
	}else{
		getid.style.display='none';
	}
}
function tutorial2(){
	var poprequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		poprequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			poprequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				poprequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	poprequest.onreadystatechange = function(){
		if(poprequest.readyState == 4){
			var popdisplay = document.getElementById('opt');
			popdisplay.innerHTML = poprequest.responseText;
		}
	}
	poprequest.open("GET",'tutorial_action.php', true);
	poprequest.send(null); 
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

//
function change_city(){
	var sel=document.getElementById('ci');
	var spl1=new Array(),spl2=new Array(),spl3=new Array();
	var cityind  = sel.selectedIndex;
	var cityind_val = sel.options[cityind].value;
	var new_url=location.pathname;
	var i;
	var find_ci;
	
	spl1=location.href.split('.php?');
	if (spl1[1] !== undefined){
		spl2=spl1[1].split('#');
		spl3=spl2[0].split('&');
		spl2[0]='';
		for(i=0;i<spl3.length;i++){
			find_ci=spl3[i].indexOf('ci');
			if(find_ci==0){spl3[i]='';}
			if(i==0){var adds='';}else{var adds='&';}
			if(spl3[i]!=''){spl2[0]+=adds+spl3[i];}
		}
		if(spl2[1] !== undefined){
			new_url=new_url+'?'+spl2[0]+'&ci='+cityind_val+'#'+spl2[1];
		}else{
			if(spl2[0].length==0){
				new_url=new_url+'?'+spl2[0]+'ci='+cityind_val;
			}else{
				new_url=new_url+'?'+spl2[0]+'&ci='+cityind_val;
			}
		}
	}else{
		new_url=new_url+'?ci='+cityind_val;
	}
	window.location=new_url;
}