var Regions=new Array('China', 'Indonesia', 'Europe', 'North/South America', 'Vietnam/Burma', 'Singapore', 'Rest of Asia', 'Others');
var Genres=new Array('Monochromatic', 'Contemporary', 'Cultures', 'Calligraphy', 'Traditional', 'Naive', 'Illustrative', 'Impressionist', 'Minimalist', 'Abstract', 'Traditional Chinese', 'Realism', 'Landscape', 'Figurative', 'Abstract', 'Floral', 'Religious', 'Symbols of Auspiciousness', 'Politics', 'Figurative ', 'Contemporary Pop');
//var Genres=new Array('Landscape', 'Figurative', 'Floral', 'Pop', 'Contemporary', 'Chinese Ink', 'Naive', 'Modern Abstract', 'Realism', 'Architectural');
var NameSelectors=new Array('A - E', 'F - K', 'L - Q', 'R - Z');

var CurrentArtistSort=null;
var TMArtistPopup=null; var TMSculptorPopup=null; var TMArtistBPopup=null; var TMSculptorBPopup=null;
/*function HidePopup(ThisElement, Type) {
	var PopupBottom=ElementIsOfClass(ThisElement.parentNode.parentNode, 'NavPopupBottom')?'B':'';
	eval('TM'+Type+PopupBottom+'Popup.StartZoom(0, 0.3, 1, 1); TM'+Type+'Popup.StartFade(0, 0.3, 10);');
	setTimeout('TM'+Type+PopupBottom+'Popup.OBJ.innerHTML=\'\'; TM'+Type+'Popup.OBJ.style.display=\'none\';', 350);
	CurrentPopup=false;
};*/
function HideCurrentPopup(Quick) {
	if (!CurrentPopup)
		return;
	if (Quick==undefined || !Quick) {
		CurrentPopup.StartZoom(0, 0.3, 1, 1);
		CurrentPopup.StartFade(0, 0.3, 10);
		setTimeout('CurrentPopup.OBJ.innerHTML=\'\'; CurrentPopup.OBJ.style.display=\'none\'; CurrentPopup=false;', 320);
	} else {
		CurrentPopup.StartZoom(0, 0.05, 1, 1);
		CurrentPopup.StartFade(0, 0.05, 10);
		CurrentPopup.OBJ.style.display='none';
		CurrentPopup.OBJ.innerHTML='';
		CurrentPopup=false;
	};
	CurrentPopupMenuH3.className='';
//	document.onclick=false;
	CurrentPopupMenuH3=false;
};

var FilterCache={};
FilterCache.Artist={'region':'all', 'style':'all', 'year':'all', 'name':'all'};
FilterCache.Sculptor={'region':'all', 'style':'all', 'year':'all', 'name':'all'};
var Today=new Date();
var CurrentPopup=false; var CurrentPopupMenuH3=false;
function ShowPopup(ClickedElement) {
	if (CurrentPopupMenuH3==ClickedElement)
		return;
	else if (CurrentPopup!=false)
		HideCurrentPopup(true);
	CurrentPopupMenuH3=ClickedElement;
	ClickedElement.className='Current';
	document.onclick=HideCurrentPopup;
	var Popup=ClickedElement.previousSibling;
	var PopupBottom=ElementIsOfClass(Popup, 'NavPopupBottom')?'B':'';
	var Type=ClickedElement.innerHTML.substr(0, 1).toUpperCase()+ClickedElement.innerHTML.substr(1, ClickedElement.innerHTML.length-2);
	eval('CurrentPopup=TM'+Type+PopupBottom+'Popup; var ThisFilterCache=FilterCache.'+Type+';');
	var PopupHTML='';
//<div class="Inst">Please Select Filters<a href="javascript:void(0);" onclick="HideCurrentPopup();">Close</a></div>
	PopupHTML='<div class="PopupBody">';
	PopupHTML+='<div class="Filter Regions"><p><a href="javascript:void(0);" id="region_all"'+(ThisFilterCache.region=='all'?' class="Current"':'')+' onclick="ChangeFilter(\''+Type+'\', \'region\', this);">All Regions</a></p>';
	for (i=0; i<Regions.length; i++)
		PopupHTML+='<a href="javascript:void(0);" id="region_'+Regions[i].replace(/\s/g, '')+'"'+(ThisFilterCache.region==Regions[i]?' class="Current"':'')+' onclick="ChangeFilter(\''+Type+'\', \'region\', this);">'+Regions[i]+'</a>';
/*	PopupHTML+='</div><div class="Filter Genres"><p><a href="javascript:void(0);" id="style_all"'+(ThisFilterCache.style=='all'?' class="Current"':'')+' onclick="ChangeFilter(\''+Type+'\', \'style\', this);">All Styles</a></p>';
	for (i=0; i<Genres.length; i++)
		PopupHTML+='<a href="javascript:void(0);" id="style_'+Genres[i].replace(/\s/g, '')+'"'+(ThisFilterCache.style==Genres[i]?' class="Current"':'')+' onclick="ChangeFilter(\''+Type+'\', \'style\', this);">'+Genres[i]+'</a>';
/*	PopupHTML+='</div><div class="Filter Years"><p><a href="javascript:void(0);" id="year_all"'+(ThisFilterCache.year=='all'?' class="Current"':'')+' onclick="ChangeFilter(\''+Type+'\', \'year\', this);">All Years</a></p>';
	for (i=Today.getFullYear(); i>2005; i--)
		PopupHTML+='<a href="javascript:void(0);" id="year_'+i+'"'+(ThisFilterCache.year==i?' class="Current"':'')+' onclick="ChangeFilter(\''+Type+'\', \'year\', this);">'+i+'</a>';*/
	PopupHTML+='</div><div class="Filter Artists Filter2"><p><a href="javascript:void(0);" id="name_all"'+(ThisFilterCache.name=='all'?' class="Current"':'')+' onclick="ChangeFilter(\''+Type+'\', \'name\', this);">All Artists</a></p>';
/*<div class="Selector">';
	for (i=0; i<NameSelectors.length; i++)
		PopupHTML+='<a href="javascript:void(0);" id="name_'+NameSelectors[i].replace(/\s/g, '')+'"'+(ThisFilterCache.name==NameSelectors[i]?' class="Current"':'')+' onclick="ChangeFilter(\''+Type+'\', \'name\', this);">'+NameSelectors[i]+'</a>';
</div>*/
	PopupHTML+='<div id="ArtistShortlist"></div>';
	PopupHTML+='</div><div class="Filter Preview"><p><a onclick="javascript:void(0);">Preview</a></p><div id="NavArtistPreview"></div></div></div>';
/*	if (!PopupBottom) {
		PopupHTML='<h3 onclick="HideCurrentPopup();">'+ClickedElement.innerHTML+'</h3>'+PopupHTML;
	} else {
		PopupHTML+='<h3 onclick="HideCurrentPopup();">'+ClickedElement.innerHTML+'</h3>';
	};*/
	Popup.innerHTML=PopupHTML;
	if (CurrentPopup==null) {
		eval('TM'+Type+PopupBottom+'Popup=TM.AddOBJ(Popup);');
		eval('CurrentPopup=TM'+Type+PopupBottom+'Popup;');
		CurrentPopup.SetOpacity(0);
	};
	CurrentPopup.OBJ.style.display='block';
	CurrentPopup.StartFade(1, 0.2);
	CurrentPopup.StartZoom(1, 0.2, 600, 187);
	ChangeFilter(Type);
};
function ChangeFilter(Type, FilterType, ClickedElement) {
	eval('var ThisFilterCache=FilterCache.'+Type+';');
	if (FilterType!==undefined) {
		eval('var CurrentChoice=FilterCache.'+Type+'.'+FilterType+';');
		RemoveClassFromElement(document.getElementById(FilterType+'_'+CurrentChoice.replace(/\s/g, '')), 'Current');
		var Choice=ClickedElement.innerHTML;
		if (Choice.match(/^All/))
			Choice='all';
		eval('FilterCache.'+Type+'.'+FilterType+'=Choice;');
		AddClassToElement(ClickedElement, 'Current');
	};
	document.getElementById('ArtistShortlist').innerHTML='Retrieving matching '+(Type=='Artist'?'artists':'sculptors')+'...';
	LoadJSONResult('artistslisting&type='+Type+'&region='+ThisFilterCache.region+'&style='+ThisFilterCache.style+'&year='+ThisFilterCache.year+'&name='+ThisFilterCache.name, 'LoadArtists');
};
function LoadArtists(Result, JSONObject) {
	if (Result) {
		var Listing='';
		for (i=0; i<JSONObject.artists.length; i++) {
			Listing+='<a href="/artist.php/'+JSONObject.artists[i].ID+','+JSONObject.artists[i].Name+'" onmouseover="NavPreviewArtistWork(this);">'+JSONObject.artists[i].Name+'</a>';
		};
		document.getElementById('ArtistShortlist').innerHTML='<div>'+Listing+'</div>';
	} else
		document.getElementById('ArtistShortlist').innerHTML='There was an error connecting to the system, please try again shortly.';
};

var PreviewArtistCurrent=null;
function NavPreviewArtistWork(MOElement) {
	if (PreviewArtistCurrent) {
		if (MOElement==PreviewArtistCurrent)
			return;
		RemoveClassFromElement(PreviewArtistCurrent, 'Current');
	};
	PreviewArtistCurrent=MOElement;
	AddClassToElement(MOElement, 'Current');
	var posSlash=MOElement.href.indexOf('/', MOElement.href.indexOf('.php'));
	var ID=MOElement.href.substring(posSlash+1, MOElement.href.indexOf(',', posSlash));
	LoadJSONResult('artistworkpreview&artist='+ID, 'NavLoadPreview');
	document.getElementById('NavArtistPreview').innerHTML='<img src="/images/loading.gif" style="margin-top:50px;"><br />Loading a sample of<br />'+MOElement.innerHTML+'\'s works...';
//alert('artistworkpreview&artist='+ID);
};
var NavPreviewImage=null; var NavPreviewName='';
function NavLoadPreview(Result, JSONObject) {
	if (Result && JSONObject && JSONObject.DefaultCompressedImagePath && JSONObject.Name) {
		NavPreviewImage=new Image();
		NavPreviewImage.onload=NavDisplayPreview;
		NavPreviewImage.src='/images/artworks/thumbs/'+JSONObject.DefaultCompressedImagePath;
		NavPreviewName=JSONObject.Name;
	} else if (Result!=undefined && JSONObject && !JSOBObject.DefaultCompressedImagePath) {
		document.getElementById('NavArtistPreview').innerHTML='No sample/preview available';
	};
};
function NavDisplayPreview() {
	var Width=NavPreviewImage.width; var Height=NavPreviewImage.height;
	if (Height>160) {
		var Ratio=160/Height;
		Width=Math.round(Width*Ratio);
		Height=160;
	};
	if (Width>220) {
		var Ratio=220/Width;
		Height=Math.round(Height*Ratio);
		Width=220;
	};
	document.getElementById('NavArtistPreview').innerHTML='<img src="'+NavPreviewImage.src+'" width="'+Width+'" height="'+Height+'" /><br />'+NavPreviewName+'';
};

/*
var ArtistCache=new Array();
function ShowArtist(ClickedElement) {
	if (ClickedElement.className=='Current')
		return;
	var i; var ListingPH;
	if (ClickedElement.parentNode.className=='NavSection') {
		var PopupHTML='';
		PopupHTML='<h3 onclick="HidePopup(this);">artists</h3><div class="CriteriaNav">';
		var SortOrders=new Array('region', 'artist A-Z', 'genre', 'recently added');
		for (i=0; i<SortOrders.length; i++)
			PopupHTML+='<a href="javascript:void(0);" onclick="ShowArtist(this);"'+(ClickedElement.innerHTML.toLowerCase()==SortOrders[i].toLowerCase()?' class="Current"':'')+'>'+SortOrders[i]+'</a>';
		PopupHTML+='</div><div class="Listing"></div>';
		ClickedElement.parentNode.firstChild.innerHTML=PopupHTML;
		ListingPH=ClickedElement.parentNode.firstChild.lastChild;
		for (i=0; i<ListingPH.previousSibling.childNodes.length; i++) {
			if (ListingPH.previousSibling.childNodes[i].className=='Current') {
				CurrentArtistSort=ListingPH.previousSibling.childNodes[i];
				break;
			};
		};
	} else {
		ListingPH=ClickedElement.parentNode.nextSibling;
		CurrentArtistSort.className='';
		ClickedElement.className='Current';
		CurrentArtistSort=ClickedElement;
	};
	if (TMArtistPopup==null) {
		TMArtistPopup=TM.AddOBJ(ListingPH.parentNode);
		TMArtistPopup.SetOpacity(0);
	};
	var ListingHTML=''; var PostExec='';
	switch (ClickedElement.innerHTML.toLowerCase()) {
	case 'region':
		if (!ArtistCache['RegionListing'])
			ArtistCache['RegionListing']=new Array();
		ListingHTML='<div class="Alpha RGPicker">';
		var Regions=new Array('Australia', 'China', 'North Asia', 'Europe', 'India', 'North America', 'Singapore', 'South East Asia', 'Vietnam');
		for (i=0; i<Regions.length; i++)
			ListingHTML+='<a href="javascript:void(0);" onclick="ChangeRegion(\'Artist\', \''+Regions[i]+'\');" id="Region_'+Regions[i].replace(/ /, '')+'">'+Regions[i]+'</a>';
		ListingHTML+='</div><div id="ArtistListing"></div>';
		PostExec="ChangeRegion('Artist', 'South East Asia');";
		break;
	case 'artist a-z':
		if (!ArtistCache['ArtistListing'])
			ArtistCache['ArtistListing']='';
		var AM='<div class="Alpha Picker">';
		var NZ='<div class="Alpha Picker">';
		var Char='A';
		for (i=0; i<13; i++) {
			AM+='<a href="">'+String.fromCharCode(Char.charCodeAt(0)+i)+'</a>';
			NZ+='<a href="">'+String.fromCharCode(Char.charCodeAt(0)+i+13)+'</a>';
		};
		ListingHTML=AM+'</div>'+NZ+'</div><div id="ArtistListing">';
		if (ArtistCache['ArtistListing']) {
			ListingHTML+=ArtistCache['ArtistListing'];
		} else {
			LoadJSONResult('artistslisting&type=az', 'LoadArtists', ArtistCache['ArtistListing']);
		};
		ListingHTML+='</div>';
		break;
	case 'genre':
		break;
	};
	ListingPH.innerHTML=ListingHTML;
	if (PostExec)
		eval(PostExec);
	var TotalHeight=0;
	for (i=0; i<TMArtistPopup.OBJ.childNodes.length; i++)
		TotalHeight+=TMArtistPopup.OBJ.childNodes[i].scrollHeight+TMArtistPopup.OBJ.childNodes[i].offsetTop;
//	TMArtistPopup.OBJ.style.display='block';
	TMArtistPopup.StartFade(1, 0.3);
	TMArtistPopup.StartZoom(1, 0.3, 740, TotalHeight);
};

function LoadArtists(Result, JSONObject, CacheEntry) {
//	ArtistCache['ArtistListing']='<p><a>A</></p><p><a>B</a></p><p><a>C</></p><p><a>D</a></p><p><a>E</></p><p><a>F</a></p><p><a>G</></p><p><a>H</a></p>';
	CacheEntry='';
	for (var i=0; i<JSONObject.artists.length; i++)
		CacheEntry+='<p><a href="" title="'+JSONObject.artists[i][0]+'">'+JSONObject.artists[i][1]+'</a></p>';
	document.getElementById('ArtistListing').innerHTML=CacheEntry;
};
var CurrentRegion=null;
function ChangeRegion(Type, Region) {
	if (Type=='Artist') {
		if (ArtistCache['RegionListing'] && ArtistCache['RegionListing'][Region]) {
			ListingHTML+=ArtistCache['RegionListing'][Region];
		} else {
			LoadJSONResult('artistslisting&type=region&region='+Region, 'LoadArtists', ArtistCache['RegionListing'][Region]);
		};
	};
	if (CurrentRegion!=null) {
		document.getElementById('Region_'+CurrentRegion.replace(/ /, '')).className='';
	};
	CurrentRegion=Region;
	document.getElementById('Region_'+CurrentRegion.replace(/ /, '')).className='Current';
};
*/

