
// Pre-load info 

var works_path="/images20/works/";
var path="images20/";
var display_image_fname;
var thediv;
var total_valid_items = 0;
			
	var x_title = new Array(12);
	var x_year = new Array(12);
	var x_materials = new Array(12);
	var x_desc = new Array(12);
	var x_collection = new Array(12);
	var x_artist = new Array(12);

if(document.images){

  var names = new Array(5)
    names[0] = "movements";
    names[1] = "artists";
    names[2] = "critics";
    names[3] = "inside";
    names[4] = "currentevents";
    
  var ext;
  
  var buttons = new Array(15)
    
    for( x=0; x<15; x+=3 ){
    ext = ".gif";
    buttons[x] = new Image()
    buttons[x].src = path + "btn_" + names[x/3] + "_off" + ext;
    buttons[x + 1] = new Image()
    buttons[x + 1].src = path + "btn_" + names[x/3] + "_ovr" + ext;
    buttons[x + 2] = new Image()
    buttons[x + 2].src = path + "btn_" + names[x/3] + "_on" + ext;
    }


	
	
  var arrows = new Array(4)
    arrows[0] = "big_left";
    arrows[1] = "big_right";
    arrows[2] = "small_left";
    arrows[3] = "small_right";

    
  
  var db_buttons = new Array(8)
    
    for( x=0; x<8; x+=2 ){
    ext = ".gif";
    db_buttons[x] = new Image()
    db_buttons[x].src = path + "arw_" + arrows[x/2] + "_off" + ext;
    db_buttons[x + 1] = new Image()
    db_buttons[x + 1].src = path + "arw_" + arrows[x/2] + "_on" + ext;
    }
}


///////////////////////////////////////////////////////////////////////////////
function zoom_artist (func_artist) {
	if (artist != "none") {
		thediv=document.getElementById(artist + '_big');
		if (thediv.style.display == "none") {
			thediv.style.display = "";
		} else {
			thediv.style.display = "none";
		}
		
		thediv=document.getElementById(artist + '_small');
		if (thediv.style.display == "none") {
			thediv.style.display = "";
		} else {
			thediv.style.display = "none";
		}
		
		if (func_artist != artist) {
			thediv=document.getElementById(func_artist + '_small');
			if (thediv.style.display == "none") {
				thediv.style.display = "";
			} else {
				thediv.style.display = "none";
			}
			
			thediv=document.getElementById(func_artist + '_big');
			if (thediv.style.display == "none") {
				thediv.style.display = "";
			} else {
				thediv.style.display = "none";
			}
			artist = func_artist;
		} else {
			artist="none";
		}
	} else {
	
		thediv=document.getElementById(func_artist + '_small');
		if (thediv.style.display == "none") {
			thediv.style.display = "";
		} else {
			thediv.style.display = "none";
		}
		
		thediv=document.getElementById(func_artist + '_big');
		if (thediv.style.display == "none") {
			thediv.style.display = "";
		} else {
			thediv.style.display = "none";
		}
		artist = func_artist;
	}
	return false;
}
	
function clicker(func_image_num){

artwork_num = func_image_num;

	// Reload iFrame with new artwork_num
	frames['PopUp'].location.href = 'z_popup.html?artist=' + artist + '&artwork_num=' + artwork_num + '&artist_name=' + encodeURIComponent(artist_name);

	// Display the hidden iframe
	thediv=document.getElementById('displaybox');

	if(thediv.style.display == "none"){
		thediv.style.display = "";

	}else{
		thediv.style.display = "none";
	}
	return false;

}



// Functions




function flip(name, x){
  if(document.images)
    document [name].src = buttons[x].src
}//end of flip

function flip_arrow(name, x){
  if(document.images)
    document [name].src = db_buttons[x].src
}//end of flip_arrow

function flip_img(name, img){
  if(document.images)
    document [name].src = path + img;

}//end of flip_arrow





function pic_go(num) {

	
	if (artwork_num > 6)
		artwork_num = num + 6;
	else
		artwork_num = num;
	
	update_db();
	
}

function go_to_first() {
	artwork_num = 6;
	update_db();
	artwork_num = 1;
	update_db();
}

function pic_next() {

	if (total_valid_items != artwork_num) {
		artwork_num = artwork_num + 1;
		update_db();
	}
}


function pic_previous() {

	if (artwork_num != 1) {
		artwork_num = artwork_num - 1;
 		update_db();
	}
	
}

function trim (string) {
	return string.replace(/(^\s+)|(\s+$)/g, "");
}


function update_db() {

	var var_img;
	var content='';
	var i;
	if (artwork_num < 7) {
		if (total_valid_items > 6) {
			for (i=0;i<6;i++) {
				content+='<td id="db_pnt_' + (i+1) + '" width="110px" align="center" valign="center" height="110px">';
				content+='<a onclick="pic_go(' + (i+1) + ');"><img src="/images20/pnt/pnt_' + artist + '_' + (i+1) +'.jpg" border="0" id="db_pnt_img_' + (i+1) + '"></a></td>';
			}
		}
		else {
			for (i=0;i<total_valid_items;i++) {
				content+='<td id="db_pnt_' + (i+1) + '" width="110px" align="center" valign="center" height="110px">';
				content+='<a onclick="pic_go(' + (i+1) + ');"><img src="/images20/pnt/pnt_' + artist + '_' + (i+1) +'.jpg" border="0" id="db_pnt_img_' + (i+1) + '"></a></td>';
			}
			for (i=total_valid_items;i<6;i++) {
				content+='<td id="db_pnt_' + (i+1) + '" width="110px" align="center" valign="center" height="110px">';
				content+='<img src="/images20/pnt/pnt_black.jpg"></td>';
			}
		}
	}
	else {
		if (total_valid_items > 6) {
			for (i=0;i<total_valid_items-6;i++) {
				content+='<td id="db_pnt_' + (i+1) + '" width="110px" align="center" valign="center" height="110px">';
				content+='<a onclick="pic_go(' + (i+1) + ');"><img src="/images20/pnt/pnt_' + artist + '_' + (i+7) +'.jpg" border="0" id="db_pnt_img_' + (i+1) + '"></a></td>';
			}
			for (i=total_valid_items-6;i<6;i++) {
				content+='<td id="db_pnt_' + (i+1) + '" width="110px" align="center" valign="center" height="110px">';
				content+='<img src="/images20/pnt/pnt_black.jpg"></td>';
			}
		}
	}
	$('#artworks').html(content);
 
	// Change Main Image 
	var var_main_image = document.getElementById('db_main_image');
	var_main_image.src = works_path + artist + '_' + artwork_num + '.jpg';
	
	// Change cur image #
	thediv = document.getElementById('db_img_cur_num');
	thediv.innerHTML = artwork_num;
	

	// Change captions
	var info=trim(x_title[artwork_num-1]);
	if (x_artist[artwork_num-1]) info+=', ' + trim(x_artist[artwork_num-1]);
	else info+=', ' + trim(artist_name);
	if (trim(x_year[artwork_num-1])!='') info+=', ' + trim(x_year[artwork_num-1]);
	if (trim(x_collection[artwork_num-1])!='') info+=', ' + trim(x_collection[artwork_num-1]);
	$('#db_img_info').html(info);
	$('#db_img_desc').html(x_desc[artwork_num-1]);
	$('#db_img_materials').html(x_materials[artwork_num-1]);

	// Change borders to black
	thediv = document.getElementById('db_pnt_1');
	thediv.style.backgroundColor = "black";

	thediv = document.getElementById('db_pnt_2');
	thediv.style.backgroundColor = "black";

	thediv = document.getElementById('db_pnt_3');
	thediv.style.backgroundColor = "black";

	thediv = document.getElementById('db_pnt_4');
	thediv.style.backgroundColor = "black";

	thediv = document.getElementById('db_pnt_5');
	thediv.style.backgroundColor = "black";

	thediv = document.getElementById('db_pnt_6');
	thediv.style.backgroundColor = "black";

	var sm_img_to_highlight = artwork_num;
	if (sm_img_to_highlight > 6)
	  sm_img_to_highlight = sm_img_to_highlight - 6;

	// Turn on Border
	thediv = document.getElementById('db_pnt_' + sm_img_to_highlight);
	thediv.style.backgroundColor = "white";

	// Remove bad arrows	
	if (artwork_num == total_valid_items) {
		document ['big_right'].src = '/images20/arw_big_x.gif';
		document ['big_right'].onmouseout=function(){}
		document ['big_right'].onmouseover=function(){}
	}
	else {
		document ['big_right'].src = '/images20/arw_big_right_off.gif';
		document ['big_right'].onmouseout=function(){flip_arrow('big_right', 2);}
		document ['big_right'].onmouseover=function(){flip_arrow('big_right', 3);}
	}

	if (artwork_num == 1) {
		document ['big_left'].src = '/images20/arw_big_x.gif';
		document ['big_left'].onmouseout=function(){}
		document ['big_left'].onmouseover=function(){}
	}
	else {
		document ['big_left'].src = '/images20/arw_big_left_off.gif';
		document ['big_left'].onmouseout=function(){flip_arrow('big_left', 0);}
		document ['big_left'].onmouseover=function(){flip_arrow('big_left', 1);}
	}

	if (artwork_num < 7) {
		document ['small_left'].src = '/images20/arw_small_x.gif';
		document ['small_left'].onmouseout=function(){}
		document ['small_left'].onmouseover=function(){}
	}
	else {
		document ['small_left'].src = '/images20/arw_small_left_off.gif';
		document ['small_left'].onmouseout=function(){flip_arrow('small_left', 4);}
		document ['small_left'].onmouseover=function(){flip_arrow('small_left', 5);}
		document ['small_left'].onclick=function(){go_to_first();}
	}

	if (total_valid_items < 7) {
		document ['small_right'].src = '/images20/arw_small_x.gif';
		document ['small_right'].onmouseout=function(){}
		document ['small_right'].onmouseover=function(){}
	}
	else if (artwork_num < 7){
		document ['small_right'].src = '/images20/arw_small_right_off.gif';
		document ['small_right'].onmouseout=function(){flip_arrow('small_right', 6);}
		document ['small_right'].onmouseover=function(){flip_arrow('small_right', 7);}
	
	}
	else {
		document ['small_right'].src = '/images20/arw_small_x.gif';
		document ['small_right'].onmouseout=function(){}
		document ['small_right'].onmouseover=function(){}
	}

}

function loadXML(fileName) { 
      var xmlHttp;
	  try
	  {
		xmlHttp = new XMLHttpRequest();
	  }
	  catch(e)
	  {
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
		{
		  try 
		  { 
			xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
		  } 
		  catch (e) {}
		}
	  }
      xmlHttp.open('GET', fileName, false);
      xmlHttp.send(null); 
		return xmlHttp.responseXML;
}

// ON LOAD

function load_data() {

// XML stuff here

	if (artist != "none") {
			var xmlDoc = loadXML('xml_' + artist + '.xml');
			var xmlRoot = xmlDoc.documentElement;
			var cur_item;
			var items = xmlRoot.getElementsByTagName("item");
			var total_items=items.length;
			var j=0;
			for (var i=0; i < total_items; i++)
			{
				cur_item = items[i];
				
				// If "sort_order attribute exists", store item
				if  (cur_item.getElementsByTagName("sort_order")[0].hasChildNodes()) {

					if (cur_item.getElementsByTagName("title").item(0).childNodes.length) x_title[j] = cur_item.getElementsByTagName("title")[0].childNodes[0].nodeValue; 
					else x_title[j]='';
					if (cur_item.getElementsByTagName("year").item(0).childNodes.length) x_year[j] = cur_item.getElementsByTagName("year")[0].childNodes[0].nodeValue; 
					else x_year[j]='';
					if (cur_item.getElementsByTagName("materials").item(0).childNodes.length) x_materials[j] = cur_item.getElementsByTagName("materials")[0].childNodes[0].nodeValue; 
					else x_materials[j]='';
					if (cur_item.getElementsByTagName("desc").item(0).childNodes.length) x_desc[j] = cur_item.getElementsByTagName("desc")[0].childNodes[0].nodeValue;
					else x_desc[j]='';
					if (cur_item.getElementsByTagName("collection").item(0).childNodes.length) x_collection[j] = cur_item.getElementsByTagName("collection")[0].childNodes[0].nodeValue;
					else x_collection[j]='';
					if (cur_item.getElementsByTagName("artist").item(0)) {
						if (cur_item.getElementsByTagName("artist").item(0).childNodes.length) x_artist[j] = cur_item.getElementsByTagName("artist")[0].childNodes[0].nodeValue;
						else x_artist[j]='';
					}
					j++;
					total_valid_items++;
				}
			}
		 }
		
	// Change Total images #
	thediv = document.getElementById('db_img_total_num');
	thediv.innerHTML = total_valid_items;	
	
	// Populate image info
	update_db();
	
	// if more than 7 images, show small left arrow

}

var active_artist='';
var obj;

function zoom (func_artist) {
	if (active_artist=='') {
		active_artist=func_artist;
		obj=document.getElementById(func_artist + '_small');
		obj.style.display = "none";
		obj=document.getElementById(func_artist + '_big');
		obj.style.display = "";
	} 
	else {
		if (active_artist==func_artist) {
			obj=document.getElementById(func_artist + '_big');
			obj.style.display = "none";
			obj=document.getElementById(func_artist + '_small');
			obj.style.display = "";
			active_artist='';
		}
		else {
			obj=document.getElementById(active_artist + '_big');
			obj.style.display = "none";
			obj=document.getElementById(active_artist + '_small');
			obj.style.display = "";
			obj=document.getElementById(func_artist + '_small');
			obj.style.display = "none";
			obj=document.getElementById(func_artist + '_big');
			obj.style.display = "";
			active_artist=func_artist;
		}
	}
	return false;
}
