//AJAX functions
var path, prevPath, url;
function basename(pPath) {
	a=pPath.split('/');
	a=a[a.length-1].split('?');
	return a[0];
}
function getSection(pPath){
	a=pPath.split('_');
	a=a[0].split('.php');
	return a[0];
}
function getSSection(pPath) {
	a=pPath.split('_');
	if(a.length>=2) {
		a=a[1].split('.php');
		return a[0];
	} else return null;
}
function getSSSection(pPath) {
	a=pPath.split('_');
	if(a.length>=3) {
		a=a[2].split('.php');
		return a[0];
	} else return null;
}
function displayNav() {
	bName=basename(path);
	section=getSection(bName);
	a=document.getElementById('nav_left').getElementsByTagName('a');
	for(i=0;i<a.length;i++) {
		if(a[i].href.indexOf(section+'_')>-1 || a[i].href.indexOf(section+'.php')>-1) a[i].className='selected';
		else a[i].className='';
	}
	ssection=getSSection(bName);
	e=document.getElementById('snav_left');
	if(section=='artist' || section=='ex' || section=='contact') {
		//Set a navigation
		e.innerHTML='<li><div class="navSeparator"><!-- --></div></li>';
		//Recreate ssection + select!
		switch(section) {
			case 'artist':
				t=new Array();
				t[0]=new Array("Hurvin Anderson","artist_ha_bio.php");
				t[1]=new Array("Jos&eacute; Damasceno","artist_jd_bio.php");
				t[2]=new Array("Michel Fran&ccedil;ois","artist_mf_bio.php");
				t[3]=new Array("Anya Gallaccio","artist_ag_bio.php");
				t[4]=new Array("Stefan K&uuml;rten","artist_sk_bio.php");
				t[5]=new Array("Michael Landy","artist_ml_bio.php");
				t[6]=new Array("Glenn Ligon","artist_gl_bio.php");
				t[7]=new Array("Steve McQueen","artist_smq_bio.php");
				t[8]=new Array("Jean-Luc Moul&egrave;ne","artist_jlm_bio.php");
				t[9]=new Array("Albert Oehlen","artist_ao_bio.php");
				t[10]=new Array("Paul Pfeiffer","artist_pp_bio.php");
				t[11]=new Array("Jorge Queiroz","artist_jq_bio.php");
				for(i=0;i<t.length;i++) {
					s='<li><a ';
					if(ssection==getSSection(t[i][1])) s+='class="selected" ';
					s+='href="javascript:getContent(\''+t[i][1]+'\');">'+t[i][0]+'</a></li>';
					e.innerHTML+=s;
				}
				break;
			case 'ex':
				t=new Array();
				t[0]=new Array("Current","ex_curr_press.php");
				t[1]=new Array("Forthcoming","ex_forth.php");
				t[2]=new Array("Previous","ex_prev_ad_inst.php");
				for(i=0;i<t.length;i++) {
					s='<li><a ';
					if(ssection==getSSection(t[i][1])) s+='class="selected" ';
					s+='href="javascript:getContent(\''+t[i][1]+'\');">'+t[i][0]+'</a></li>';
					e.innerHTML+=s;
				}
				break;
			case 'contact':
				t=new Array();
				t[0]=new Array("Gallery Address","#contact_address");
				t[1]=new Array("Opening Hours","#contact_hours");
				t[2]=new Array("Email Contacts","#contact_emails");
				t[3]=new Array("Ltd Registration","#contact_info");
				for(i=0;i<t.length;i++) {
					s='<li><a href="'+t[i][1]+'">'+t[i][0]+'</a></li>';
					e.innerHTML+=s;
				}
				break;
		}
	} else {
		e.innerHTML='';
	}

	sssection=getSSSection(bName);
	f=document.getElementById('ssnav_left');
	if(section=='artist' || section=='ex') {
		//Recreate ssection + select!
		switch(section) {
			case 'artist':
				if(ssection!=null) {
					f.innerHTML='<li><div class="navSeparator"><!-- --></div></li>';
					if(t) delete t;
					t=null;
					t=new Array();
					t[0]=new Array("Artist's biography","artist_"+ssection+"_bio.php");
					t[1]=new Array("Selected Works","artist_"+ssection+"_selected.php");
					switch(ssection) {
						case "ao":
						case "gl":
						case "gf":
						case "ha":
						case "jlm":
						case "jq":
						case "jd":
						case "ml":
						case "pp":
						case "sk":
						case "smq":
							t[2]=new Array("Past TDG Exhibitions","artist_"+ssection+"_ex.php");
							break;
					}
					for(i=0;i<t.length;i++) {
						s='<li><a ';
						if(sssection==getSSSection(t[i][1]) || (sssection=='press' && getSSSection(t[i][1])=='ex')) s+='class="selected" ';
						s+='href="javascript:getContent(\''+t[i][1]+'\');">'+t[i][0]+'</a></li>';
						f.innerHTML+=s;
					}
				} else f.innerHTML='';
				break;
			case 'ex':
				switch(ssection) {
					case 'curr':
						f.innerHTML='<li><div class="navSeparator"><!-- --></div></li>';
						if(t) delete t;
						t=null;
						t=new Array();
						t[0]=new Array("Press Release","ex_curr_press.php");
						//t[1]=new Array("Reviews","ex_curr_review.php");
						for(i=0;i<t.length;i++) {
							s='<li><a ';
							if(sssection==getSSSection(t[i][1])) s+='class="selected" ';
							s+='href="javascript:getContent(\''+t[i][1]+'\');">'+t[i][0]+'</a></li>';
							f.innerHTML+=s;
						}
						break;
					case 'prev':
						f.innerHTML='<li><div class="navSeparator"><!-- --></div></li>';
						if(t) delete t;
						t=null;
						t=new Array();
						t[0]=new Array("Alan Davie","ex_prev_ad_inst.php");
						t[1]=new Array("G&uuml;nther F&ouml;rg","ex_prev_gf_inst.php");
						t[2]=new Array("Glenn Ligon","ex_prev_gl_inst.php");
						t[3]=new Array("Civil Restitutions","ex_prev_cr_inst.php");
						t[4]=new Array("Jean-Luc Moul&egrave;ne","ex_prev_jlm_inst.php");
						t[5]=new Array("Jorge Queiroz / Etienne Zack","ex_prev_jq_inst.php");
						t[6]=new Array("Jos&eacute; Damasceno","ex_prev_jd_inst.php");
						t[7]=new Array("Open Ended","ex_prev_oe_inst.php");
						t[8]=new Array("Albert Oehlen","ex_prev_ao_inst.php");
						t[9]=new Array("A Certain Tendency","ex_prev_act_inst.php");
						t[10]=new Array("Translations","ex_prev_t_inst.php");
						t[11]=new Array("Hurvin Anderson","ex_prev_ha_inst.php");
						t[12]=new Array("Calder, The Forties","ex_prev_ac_inst.php");
						t[13]=new Array("Stefan K&uuml;rten","ex_prev_sk_inst.php");
						t[14]=new Array("Michael Landy","ex_prev_ml_inst.php");
						t[15]=new Array("Paul Pfeiffer","ex_prev_pp_inst.php");
						t[16]=new Array("Steve McQueen","ex_prev_smq_inst.php");
						for(i=0;i<t.length;i++) {
							s='<li><a ';
							if(sssection==getSSSection(t[i][1])) s+='class="selected" ';
							s+='href="javascript:getContent(\''+t[i][1]+'\');">'+t[i][0]+'</a></li>';
							f.innerHTML+=s;
						}
						break;
					default:
						f.innerHTML='';
				}
				break;
		}
	} else {
		f.innerHTML='';
	}
}
function displayPage(response) {
	e=document.getElementById('target');
	e.innerHTML=response;
	if(http!=null) {
		//Select every link in "navList"
		aArray=e.getElementsByTagName('a');
		for(i=0;i<aArray.length;i++) {
			if(aArray[i].href.indexOf(url)>-1 && aArray[i].href.indexOf('javascript')==-1) {
				if(aArray[i].href.indexOf('mailto:')!=0) {
					if(basename(aArray[i].href)=='newsletter.php') aArray[i].href="javascript:newsletter_open();";
					else if(aArray[i].href.indexOf('#')==-1) aArray[i].href="javascript:getContent('"+aArray[i].href+"')";
				}
			}
		}
	}
	window.scrollTo(0,0);
}
function handleHttpResponse() {
	if(http.readyState==4) {
		document.getElementById('pageLoading').style.display='none';
		if(path.indexOf('subscribe.php')>-1) {
			newsletter_message(http.responseText);
		} else if(path.indexOf('common/gallery.php')==-1) {
			displayPage(http.responseText);
			//displayNav();
			if(path.indexOf('exhibition.php')>-1) {
				getContent('common/gallery.php?xml=1');
			}
		} else {
			pImg_array=new Array();
			if(http.responseXML.childNodes[1]) {
				pIndex=http.responseXML.childNodes[1].childNodes[0].firstChild.nodeValue;
				n=http.responseXML.childNodes[1].childNodes[1].childNodes;
				for(i=0;i<n.length;i++) {
					pImg_array[i]=new Array(n[i].firstChild.nodeValue,n[i].attributes[0].value,n[i].attributes[1].value,n[i].attributes[2].value);
				}
			} else {
				pIndex=http.responseXML.firstChild.childNodes[0].firstChild.nodeValue;
				n=http.responseXML.firstChild.childNodes[1].childNodes;
				for(i=0;i<n.length;i++) {
					s=n[i].attributes.source.value+', '+n[i].attributes.width.value;
					pImg_array[i]=new Array(n[i].firstChild.nodeValue,n[i].attributes.source.value,n[i].attributes.width.value,n[i].attributes.height.value);
				}
			}
			img_init(pIndex,pImg_array);
		}
	}
}
function getHTTPObject() {
	var x;
	/*@cc_on
	@if (@_jscript_version>=5)
		try {
			x=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				x=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				x=false;
			}
		}
	@else
	x=null;
	@end @*/
	if (!x && typeof XMLHttpRequest!='undefined') {
		try {
			x=new XMLHttpRequest();
		} catch (e) {
			x=false;
		}
	} else if(!x) return null;
	return x;
}
function getContent(pPath) {
	document.getElementById('pageLoading').style.display='block';
	prevPath=path;
	path=pPath;
	if(http.overrideMimeType) {
		if(path.indexOf('common/gallery.php')==-1) http.overrideMimeType('text/html');
		else if(http.overrideMimeType) http.overrideMimeType('text/xml');
	}
	if(path.indexOf('?')>0) http.open("GET", path+'&only=1', true);
	else http.open("GET", path+'?only=1', true);
	http.onreadystatechange=handleHttpResponse;
	http.send(null);
}
var http=getHTTPObject();