if (window['loadFirebugConsole']) window.loadFirebugConsole(); 
else if (!window['console']) window.console = { info:function(s){}, log:function(s){}, warn:function(s){}, error:function(s){} }

var _NOSCROLL_POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,resizable=no,scrollbars=no,top=100,left=100,screenX=100,screenY=100';
var _SCROLL_POPUP_FEATURES = 'location=0,statusbar=1,menubar=0,resizable=yes,scrollbars=yes,top=100,left=100,screenX=100,screenY=100';
var current_img_id = 0;
var cYear = 0;
var cExhib = 0;
var cImg = 0;
var cArtist = 0;

String.prototype.parseJSON = function () {
    try { return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(this.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + this + ')'); } 
    catch (e) { return false; }
};
String.prototype.trim = function () { return this.replace(/^\s*|\s*$/g,""); };
function $() {
	if (arguments.length == 1) return get$(arguments[0]);
	var elements = [];
	$c(arguments).each(function(el){
		elements.push(get$(el));
	});
	return elements;

	function get$(el){
		if (typeof el == 'string') el = document.getElementById(el);
		return el;
	}
}
function trace( s ) { alert(s); }

function raw_popup(url, target, can_scroll, w_width, w_height) {
  // define base set depending on whether the window can scroll or not
  var features = _SCROLL_POPUP_FEATURES;
  if (can_scroll==false) features = _NOSCROLL_POPUP_FEATURES;
  // define target attr
  if (isUndefined(target)) target = '_blank';
  // add width and height to features
  if (isUndefined(w_width)) w_width = 400;
  if (isUndefined(w_height)) w_height = 300;
  features += ',width='+w_width+',height='+w_height;
  // call the window
  var theWindow = window.open(url, target, features);
  theWindow.focus();
  return theWindow;
}

function link_popup(src, can_scroll, w_width, w_height) {
  return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', can_scroll, w_width, w_height);
}
function img_popup(i, can_scroll, w_width, w_height) {
	if (current_img_id>0) i = current_img_id;
  	return raw_popup('image.php?i='+i, '_blank', true, w_width, w_height);
}

function launch(theURL,windowWidth,windowHeight)	{
	windowAttributes = "top=100,left=100,screenX=100,screenY=100,height="+windowHeight+",width="+windowWidth+",resizable=yes,scrollbars=yes";
	if (parseInt(navigator.appVersion) >= 4) { popWin = window.open(theURL, "_blank", windowAttributes); }
	setTimeout('popWin.focus();',350);
}
function launchNS(theURL,windowWidth,windowHeight)	{
	windowAttributes = "top=100,left=100,screenX=100,screenY=100,height="+windowHeight+",width="+windowWidth+",resizable=no,scrollbars=no";
	if (parseInt(navigator.appVersion) >= 4) { popWin = window.open(theURL, "_blank", windowAttributes); }
	setTimeout('popWin.focus();',350);
}
function writeFlashMenu()
{
	var el = document.getElementById('flash');
	if (!el) return;
	
	el.innerHTML = '<object type="application/x-shockwave-flash" data="flash/menu2.swf" width="685" height="103">'+
				   '<param name="movie" value="flash/menu2.swf"></object>';
}
function MM_findObj(n, d) { return $(n); }
function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function swapM52Image( imgName, newSrc, maxw )
{
	var j=0,i = $(imgName);
	var p = new Image();
	document.MM_sr=new Array;
	document.MM_sr[j++]=i;
	// set oSrc
	i.oSrc = i.src;
	
	i.src = newSrc;
}
function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function switchExhibYear( yr )
{
	if (cYear>0) {
		var head = $( cYear+'_head' );
		var titles = $( cYear+'_titles' );
		head.className='oyear';
		titles.style.display='none';
	}
	$( yr + '_head' ).className = 'tyear';
	$( yr + '_titles' ).style.display = 'block';
	cYear= yr;
}
function switchArtist( aid )
{
	// switch the CBP div.
	switchCBP( aid );
	
	// highlight the right one
	highlightItem( aid, cExhib, 'artist' );
	
	// drop down the right list of items
	createWorkList( aid );
	
	// reset the image source
	if (typeof(exhibs[aid]["image"])!='undefined') {
		$('exhibition_img').src = exhibs[aid]["image"];
	}
	else {
		$('exhibition_img').src = "images/singlepixel.gif";
	}
	$('exhibition_img').oSrc = $('exhibition_img').src;
	
	// reset actions
	$('img_link').href = "javascript:;";
	$('img_link').onclick = function() { return false; };
	
	// hide controls
	$('x_image_controls').style.display = 'none';
	// reset the metadata
	$( 'img_metadata' ).innerHTML = "";
	
	// reset cYear and cImg to avoid confusion
	cYear = 0;
	cImg = 0;
	
	// set the new artist
	cExhib = aid;
	
	// set the total images number
	if (typeof(exhibs[aid]["images"])!='undefined') $('num_imgs').innerHTML = exhibs[aid]["images"].length;
}

function createWorkList( aid )
{
	// init variables
	var work_cell = $('work_cell');
	var par = true; 		// parity for headers
	var tpar = true;		// parity for titles
	var str = "";
	
	if (typeof(exhibs[aid]["years"])=='undefined') { work_cell.innerHTML = ""; return; }
	if (exhibs[aid]["years"].length==0) { work_cell.innerHTML = ""; return; }
		
	for (var i in exhibs[aid]["years"]) {
		
		var thisyear = exhibs[aid]["years"][i];
			
		// choose parity for alternate line highlighting
		var style_clause = (par) ? " style=\"background-color:#f9f9f9;\" " : "";
		par = !par;
		
		// style modifications for selected and non-selected bits
		var year_class = "";
		var images_style_app = "";
		
		var year_click_event = "  ";

		// echo the header row for the year
		str += "<div class=\"menuitem\" "+style_clause+" > "+
			   "	<a id=\"" + thisyear + "_head\" class=\"oyear\" style=\"padding-top:2px;padding-bottom:2px;\" "+
			   "		href=\"?page=artists&a="+aid+"&yr="+thisyear+"\" onclick=\"switchExhibYear( "+thisyear+" );return false;\" > "+
			   				thisyear +
			   "	</a> "+
			   "</div>";
			
		// echo the titles block div for the titles submenu
		str += "<div id=\"" + thisyear + "_titles\" style=\"display:none;\">";
			
		// cycle through the list of images
		for (var imgk in exhibs[aid]["images"]) {
			
			var imgd = exhibs[aid]["images"][imgk];
			
			// ignore images from other years
			if (imgd["year"] != thisyear) continue;
			
			// choose parity for alternate line highlighting
			var title_style_clause = (par) ? " style=\"background-color:#f9f9f9;\" " : "";
			
			// switch parity
			tpar = !tpar;
			
			// prepare onclick string
			var link_click = "onclick=\"switchArtistImage( "+imgk+" );return false;\"";
			
			// print out the link
			str += 	"<div class=\"menuitem\" "+title_style_clause+" > "+
					"	<a class=\"oyear\" style=\"padding-left:10px;width:122px;\" "+link_click+" id=\"" + imgk + "_title\" "+
					"		href=\"?page=artists&a="+aid+"&yr="+thisyear+"&i="+imgd["id"]+"&ix="+imgk+"\"> "+
							imgd["image_title"] +
					"	</a> "+
					"</div>";
		}
		
		// end the titles block.
		str += "</div>";
	}
	
	// set the content
	work_cell.innerHTML = str;
}

function switchCBP( aid )
{
	// get the cell and artist
	var cbp = $('cbp_cell');
	var cad = exhibs[aid];
	var str = "";
	
	// define CV / Bio / Press onclick and link array
	var cbp_onclick = " onclick=\"link_popup(this, true, 600, 400);return false;\" ";
	
	var cbp_links = new Array();
	if (cad["biography"]) 		 cbp_links.push( "<a "+cbp_onclick+" target=\"_blank\" href=\"pages/bio.php?a="+aid+"\">Bio</a>" );
	if (cad["curriculum_vitae"]) cbp_links.push( "<a "+cbp_onclick+" target=\"_blank\" href=\"pages/cv.php?a="+aid+"\">CV</a>" );
	if (cad["press"]) 	 cbp_links.push( "<a "+cbp_onclick+" target=\"_blank\" href=\"pages/press.php?a="+aid+"\">Press</a>" );
	
	// echo the CBP links with relevant parity
	var par = true;
	for( var i in cbp_links ) {
		
		// decide which class to use and reverse $par
		var tclass = (par) ? "onmenuitem" : "menuitem";
		par = !par;
		
		str += '<div class="' + tclass + '">' + cbp_links[i] + '</div>';
	}
	
	// set the CBP contents.
	cbp.innerHTML = str;
}

function switchArtistImage( iid )
{
	// get number of images
	var num_imgs = exhibs[cExhib]["images"].length - 1;
	
	// set highlight
	highlightItem( iid, cImg, 'title' );
	
	// set meta data
	setMetadata( iid );
	
	// apply bounds
	cImg = iid;
	if (cImg<0) 		cImg = 0;
	if (cImg>=num_imgs)	cImg = num_imgs;
	
	// show the image
	showImage( cImg );
	
	// change the onclick handler and href for the image link.
	$( 'img_link' ).href = exhibs[cExhib]["images"][iid]["image"];
	$( 'img_link' ).onclick = function() { img_popup( exhibs[cExhib]["images"][iid]["id"], true, 500, 400);return false; };
	
	// switch year display
	switchExhibYear( exhibs[cExhib]["images"][cImg]["year"] );	
}
function highlightItem( newID, oldID, theName )
{
	var oldEl = $( oldID +'_'+theName );
	if (oldEl) oldEl.className = "oyear";
	
	var newEl = $( newID +'_'+theName );
	if (newEl) newEl.className = "tyear";
}
function setMetadata( iid )
{
	var ma = new Array();
	var cid = exhibs[cExhib]["images"][iid];
	
	// collate the array.
	if (cid["medium"]) 		ma.push( cid["medium"] );
	if (cid["dimensions"]) 	ma.push( cid["dimensions"] );
	if (cid["year"]) 		ma.push( cid["year"] );
			
	// implode meta-array
	var meta_str = ma.join("<br /> <br />");		
	
	// set it
	$( 'img_metadata' ).innerHTML = meta_str;
	
}
function switchExhib( xid )
{
	var el_data = $('exhib_data_div');
	var el_c_img = $('current_img');
	var el_num_imgs = $('num_imgs');
	
	// switch the highlights on the exhibition menu
	removeHighlight( 'exhib_'+cExhib );
	addHighlight( 'exhib_'+xid );
	
	// set this as the current exhibition
	cExhib = xid;
	
	/************* begin setting data field **********/
	var data_str = "";
	// append artists' names
	var artists = exhibs[xid]["artist_array"];
	for (aid in artists) {
		data_str += "<div><a class=\"menu\" href=\"?page=artists&a=" + artists[aid]["id"] + "\"><b>" + artists[aid]["name"] + "</b></a>";
	}
	
	// append dates
	data_str += "<br /><b>" + exhibs[xid]["exhibition_dates"] + "</b>";
	
	// append press link if necessary
	if (exhibs[xid]["exhibition_details"].length>0) data_str += '<br /><br /><a style="padding:0;width:207px;" href="javascript:launch(\'pages/more.php?t=exhibitions&f=exhibition_details&id=' + xid +'\',600,400);">Click here for the full press release.</a>';
	
	// set it
	el_data.innerHTML = data_str;
	
	/************* begin setting image data **********/
	el_c_img.innerHTML = 1;
	el_num_imgs.innerHTML = exhibs[xid]["images"].length;
	// switch to the first one
	cImg = 0;
	showImage(0);
}

function switchImage( dir )
{
	// apply direction
	cImg += dir;
	// get number of images
	var num_imgs = exhibs[cExhib]["images"].length - 1;
	
	// apply bounds
	if (cImg<0) 		cImg = 0;
	if (cImg>=num_imgs)	cImg = num_imgs;
	
	// show the image
	showImage( cImg );
}

function showImage( iid )
{
	var num_imgs = exhibs[cExhib]["images"].length - 1;
	var el_left = $('left_arrow');
	var el_right = $('right_arrow');
	var el_image = $('exhibition_img');
	var el_image_link = $('img_link');
	var el_c_img = $('current_img');
	
	// show the controls
	$('x_image_controls').style.display = 'block';
	
	// show the image and change the click link
	el_image.src = exhibs[cExhib]["images"][iid]["image"];
	el_image.oSrc = exhibs[cExhib]["images"][iid]["image"];
	el_image_link.setAttribute("rel", exhibs[cExhib]["images"][iid]["id"]);
	el_image_link.href = "?page=artists&artist=" + exhibs[cExhib]["images"][iid]["artist_id"] + "&image=" + exhibs[cExhib]["images"][iid]["id"];
	
	// hide/show the left right buttons
	if (iid<1) 			el_left.style.visibility = 'hidden';
	else 				el_left.style.visibility = 'visible';
	if (iid>=num_imgs) 	el_right.style.visibility = 'hidden';
	else 				el_right.style.visibility = 'visible';
	
	// show the count
	el_c_img.innerHTML = iid + 1;
}

function addHighlight( el )
{
	var elt = $(el);
	if (!elt) return;
	elt.className = 'tyear';
}
function removeHighlight( el )
{
	var elt = $(el);
	if (!elt) return;
	elt.className = 'oyear';
}
