var xml = null;
var paintings = [];
var lastHash = '';

$(document).ready(function() {
	if(location.pathname.match('artist')) {
		changeArtistView();
	}
	
	if(location.pathname.match('gallery')) {
		loadGallery();
	}
	
	drawSideShadow();
	$(window).resize(function() { drawSideShadow(); });
});










function changeArtistView(obj) {
	
	$("#artist_bio div").hide();
	
	
	if(!obj) {
		var showId = window.location.hash.substring(0);
	}
	else {
		var showId = obj.hash;
	}
	
	
	if(!showId || showId == "") {
		$("#statement_text").show();
	}
	else {
		$(showId+"_text").show();
	}
	
	
}







function makePaintingObject(node) {
	var painting = {};
	painting._url = node.eq(0).text();
	painting._title = node.eq(1).text();
	painting._dimensions = node.eq(2).text();
	painting._media = node.eq(3).text();
	painting._date = node.eq(4).text();
	
	return painting;
}


function loadGallery() {
	
	$.get("./scripts/library.xml", function(data) {

		// create a temp array of the sorted xml data
		var temp = [];
		$("gallery",data).children().each(function(i) {
			temp[$(this).attr('order')] = makePaintingObject($(this).children());
		});
		
		// push all valid entries from the temp array into paintings array
		$(temp).each(function() {
			if (this._url != undefined) {
				paintings.push(this);
			}
		});
		
		
		// create html strings for the paintings link
		var icons = "";
		$(paintings).each(function(i) {
			icons += "<a href='#"+i+"' id='art"+i+"' class='gallery_icon'></a>";
		});
		
		// place the links into the html page and bind click events to them
		$("#gallery_icons").html(icons);
		$("#gallery_icons a").bind('click', function() {
			changeGalleryView(this);
		});
		
		
		
		// if the url has a hash value load up the painting associated with the hash value
		if(location.hash) {
			changeGalleryView();
		}
		else {
			$("#gallery_pic").html("click the squares to the left to view the paintings.");
			$("#gallery_pic").css("background-image","none");
		}
		
		
		// start the check hash loop to see if the hash has changed
		// this allows the back and forward buttons in the browser to work with hashes
		checkHashChange();
	});
	
}


function changeGalleryView(obj) {
	
	$("#gallery_pic").html("loading...");
	$("#gallery_pic").css('background-image', 'url(images/ajax-loader2.gif)');
	
	if(!obj) {
		var imageId = window.location.hash.substring(1);
	}
	else {
		var imageId = obj.hash.substring(1);
	}
	
	$(".gallery_icon").removeClass('gallery_icon_highlight');
	$("#art"+imageId).addClass('gallery_icon_highlight');
	
	var painting = paintings[imageId];
	
	var url = "gallery/" + painting._url;
	var title = painting._title;
	var dimensions = painting._dimensions;
	var media = painting._media;
	var date = painting._date;
	
	var painting = new Image;
	painting.onload = function() {
		$("#gallery_pic").html("");
		$("#gallery_pic").css('background-image', "url("+url+")");
		$("#painting_title").html(title);
		$("#painting_dimensions").html(dimensions);
		$("#painting_media").html(media);
		$("#painting_date").html(date);
		lastHash = imageId;
	};
	painting.src = url;
	
}


function checkHashChange() {
	currentHash = window.location.hash.substring(1);
	if (currentHash != lastHash) {
		if (currentHash == '') {
			$(".gallery_icon").removeClass('gallery_icon_highlight');
			var text = "click the squares to the left to view the paintings.";
			$("#gallery_pic").html(text);
			$("#gallery_pic").css('background-image', 'none');
		}
		else {
			$("#art"+currentHash).click();
		}
	}
	
	lastHash = window.location.hash.substring(1);
	var t = setTimeout('checkHashChange()',200);
}







function drawSideShadow() {
	
	if($.browser.msie) {
		var pageHeight = $("#page").height() + "px";
		
		$("#right_center_shadow").height(pageHeight);
	}
	
	
}