// JavaScript Document
var ajaxAgent={ //detact if AJAX & javascript is supported by Browser

	ajaxloadingmsg_post:'<table align="center" width="95%" bgcolor="#000000" height="100%"><tr><td align="center" height="300"><font class="loading">Your information has been sent <img src="images/loading.gif" align="center"><br />Please wait for a response...</font></td></tr></table>', //HTML to show while ajax page is being feched, if applicable

	ajaxloadingmsg: '<table align="center" width="95%" bgcolor="#000000" height="100%" style="border: solid 2px #999900" cellspacing="2"><tr><td align="center" height="300" style="border: solid 2px #006600"><font class="loading"><img src="images/loading_GS_bg.jpg" align="center"><br /><img src="images/loading.gif" align="center"><br />Loading content. Please wait...</font><br /><br /></td></tr></table>', //HTML to show while ajax page is being feched, if applicable

	test:function(){
		var page_request = false;

		if (window.XMLHttpRequest){ // if Mozilla, IE7, Safari etc
			page_request = new XMLHttpRequest();
		}else if (window.ActiveXObject){ // if IE6 or below
			try {
				page_request = new ActiveXObject("Msxml2.XMLHTTP")
			} 
			catch (e){
				try{
					page_request = new ActiveXObject("Microsoft.XMLHTTP")
				}
				catch (e){}
			}
		}else{
			return false
		}
	
		if(page_request != false){//test if script above has worked
			//javascript and AJAX are supported
			return true	
		}else{
			//javascript and AJAX are NOT supported
			return false
		}
	},//end of test function

	create:function(){ //create object for request
	
	}, //end of function, ajax object created
	
	retrieve:function(script){
		//alert('ajaxRetrieve; will get page: ' + script +';');
		if (window.XMLHttpRequest){ // if Mozilla, IE7, Safari etc
			page_request = new XMLHttpRequest();
		}else if (window.ActiveXObject){ // if IE6 or below
			try {
				page_request = new ActiveXObject("Msxml2.XMLHTTP")
			} 
			catch (e){
				try{
					page_request = new ActiveXObject("Microsoft.XMLHTTP")
				}
				catch (e){}
			}
		}
		//alert('will set new inner text');
		document.getElementById('Site_Content').innerHTML = this.ajaxloadingmsg
		//alert('updated inner text');
		page_request.onreadystatechange=function(){
			//alert('ready state: ' + page_request.readyState + ' |&| status: ' + page_request.status + '||');
			if (page_request.readyState == 4 && (page_request.status==200)){
				document.getElementById('Site_Content').innerHTML = page_request.responseText;
				PageAgent.sanitizepage();
			}
		}
		page_request.open('GET', script, true)
		page_request.send(null)	
	}
}

var PageAgent={
	ajaxexist: false,

	page : "-",
	
	init:function(){
		if(ajaxAgent.test() == true){
			//alert('AJAX is supported by your browser. Enhance version of this site is enabled');
			PageAgent.sanitizeMenu();
			PageAgent.sanitizepage();
		}else{
			//alert('AJAX is NOT supported by your browser. Enhance version of this site is disenabled');
		}
	},
	
	sanitizepage:function(){ //sanitise page content links
		var mainInstance = this;
		var extra = "-";
		thispage = document.getElementById('Site_Content');
		for (var j = 0; j < thispage.getElementsByTagName('a').length; j++){
			if(thispage.getElementsByTagName('a')[j].getAttribute('rel') == 'j_popup'){
				// do nothing
			}else{
				thispage.getElementsByTagName('a')[j].onclick = function() {
					mainInstance.followlink(this)
					return false
				}
			}
		}
		extra = document.getElementById('extra');
		if(extra != null){
			if (extra.getAttribute('value') == 'lightbox'){
				initLightbox();
			}
			if (extra.getAttribute('value') == 'wysiwyg'){
				if(document.getElementById('defaultpageload').value == 0){
					var mysettings = new WYSIWYG.Settings(); 
					mysettings.ImagesDir = "/images/wysiwyg/";
					mysettings.PopupsDir = "/include/wysiwyg/";
					mysettings.CSSFile = "/css/wysiwyg.css"; 
					mysettings.PreviewWidth = '450'; 
					mysettings.Width = '450px'; 
					mysettings.DefaultStyle = "font-family: Arial; font-size: 12px; background-color: #FFFFFF; color:#333333;";
					WYSIWYG.attach('postmsg', mysettings, 1);
				}
			}
		}
		for (var j= 0; j < thispage.getElementsByTagName('input').length; j++){
			if(thispage.getElementsByTagName('input')[j].getAttribute('type') == 'submit'){
				thispage.getElementsByTagName('input')[j].onclick = function() {
					mainInstance.postFormAjaxed(this)
					return false
				}
			}
		}
	},

	sanitizeMenu:function(){ //sanitise page content links
		//alert('will now sinitize');
		var mainInstance = this;
		var extra = "-";
		thispage = document.getElementById('Site_Menu');
		for (var j = 0; j < thispage.getElementsByTagName('a').length; j++){
			thispage.getElementsByTagName('a')[j].onclick = function() {
				mainInstance.followlink(this)
				return false
			}
		}
	},

	followlink:function(submenulink){
		var domainURL = 'http://www.absolutegamestory.com/'
		var oldDomainURL = 'http://www.gamestory.net/'
		var LinkTarget = new String(submenulink)
		var TargetDom = LinkTarget.substring(0,domainURL.length)
		var TargetDom2 = LinkTarget.substring(0,oldDomainURL.length)
		var scriptLink = ''
		if (TargetDom == domainURL){//link is on this site
			scriptLink = LinkTarget.replace(domainURL, '')
			if(scriptLink.substring(0,8) == 'include_'){//ajax-ed page link
				ajaxAgent.retrieve(scriptLink )
				return false;
			}else{ //old link, reformat it to a new one.
				ajaxAgent.retrieve('include/include_' + scriptLink )
				return false;
			}
		}else if(TargetDom2 == oldDomainURL){ //gamestory.net link
			scriptLink = LinkTarget.replace(oldDomainURL, '')
			if(scriptLink.substring(0,8) == 'include_'){//ajax-ed page link
				ajaxAgent.retrieve(scriptLink )
				return false;
			}else{ //old link, reformat it to a new one.
				ajaxAgent.retrieve('include/include_' + scriptLink )
				return false;
			}
		}else{//it's an external link
			window.open(submenulink,'');
			return false;
		}
	}
}



function fback(book){
	window.open ("feedback.php?b=" + book, "AbsoluteGameStory_BookCommnents", "toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,copyhistory=no,width=500,height=520");
}
function fvotes(book){
	window.open ("feedback.php?page=votes&b=" + book, "AbsoluteGameStory_Votes", "toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,copyhistory=no,width=500,height=520");
}