var pe = "";
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}


sfHover = function() {
	var sfEls = document.getElementById("nav1").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+="sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp("sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


function spryMyLinks() 
	{
	var myDiv = document.getElementById('bionav');
	var aLinks=myDiv.getElementsByTagName('a');
	for(var i=0;i<aLinks.length;i++) 
		{
			var myCurrentItem = aLinks[i];
			//aLinks[i].setAttribute('onclick', 'pe.loadContent(this.href);return false;');
			if( myCurrentItem.attachEvent )
				{
				   myCurrentItem.setAttribute('name', myCurrentItem.href);
				   myCurrentItem.href = 'javascript: pe.loadContent("' + myCurrentItem.href + '")';				   
				} 
			else 
				{
				   myCurrentItem.setAttribute('name', myCurrentItem.href);
				   myCurrentItem.setAttribute('onClick', 'pe.loadContent(this.href);return false;');
				}
			
		}
	}

function spryMyNav()
{
if(document.getElementById('bionav'))
	{
	pe = new Spry.Widget.HTMLPanel("bioArea");	
	var mybDiv = document.getElementById('bionav');
	var bLinks=mybDiv.getElementsByTagName('a');
	var myFirstItem = bLinks[0];
	spryMyLinks();
	if ( (top.location.href) == (window.location.href) )
		{
		pe.loadContent(myFirstItem.name);
		}
	}
}



var buffer = false;
var loaded = false;
var aG = [];
function loadGallery(){
aG.push({
	totalW: totalW,
	totalH: totalH,
	path: path
});
$(function(){
	if(loaded) return false;
	loaded = true;
	for(var p in aG){
		var gSlideshow = $('div.slideshow:eq('+p+')')
		gSlideshow.data('mP',p);
		gSlideshow.height(aG[p].totalH);
		$.ajax({
			url: aG[p].path+'myimages.xml',
			dataType: 'xml',
			context: gSlideshow,
			success: function(data){
				var slideshow = $(this);
				var mP = slideshow.data('mP');
				var dataArray = [];
				$(data).find('data').each(function(){
					dataArray.push({img:$(this).attr('img'),imgcaption:$(this).attr('imgcaption')});
				});
				slideshow.append(
					'<img sr'+'c="'+aG[mP].path
					+dataArray[0].img
					+'" imagelabel="'
					+dataArray[0].imgcaption
					+'" style="position: absolute;display: none; ">'
				);
				dataArray.shift();
				var loaded = false;
				/* As soon as the first image is loaded, we may begin */
				slideshow.find('img:first').load(function(){
					if(loaded) return false;
					loaded = true;
					for(var i in dataArray){
						slideshow.append(
							'<img sr'+'c="'+aG[mP].path
							+dataArray[i].img
							+'" imagelabel="'
							+dataArray[i].imgcaption
							+'" style="position: absolute;display: none; ">'
						);
					}
					/* Variables */
					var transitionTime = 5000;
					
					/* Show First Image / Gallery / Hide Play Button */
					var n = slideshow.find('img:first');
					$('.descriptionText').html(n.attr('imageLabel').replace(/ /g,'')==''?' ':n.attr('imageLabel'));
					n.show();
					if(buffer){
						$('.buffer').css({
							'height':n.height(),
							'width':n.width(),
							'margin-left' : (aG[mP].totalW-n.width())/2,
							'margin-top' : (aG[mP].totalH-n.height())/2
						}).fadeOut(1000,function(){
							n.css('z-index','5');
							$('.buffer').show();
						});
						n.css({
							position : 'absolute',
							'margin-left' : (aG[mP].totalW-$(this).width())/2,
							'margin-top' : (aG[mP].totalH-$(this).height())/2
						});
					}
					
					$('.play').hide();
					var animating = false;
					var nextSlide = function(next){
						/* Make sure we're not already in transition */
						if(animating) return false;
						animating = true;
						var f = slideshow.find('img:visible');
						if(next){
							var n = slideshow.find('img:visible').prev('img');
							if(n.length==0) n = slideshow.find('img:last');
						}else{
							var n = slideshow.find('img:visible').next('img');
							if(n.length==0) n = slideshow.find('img:first');
						}
						/* Make sure the next image is loaded before proceeding (don't worry, the loop event will re-fire) */
						if(!n[0].complete) return false;
						
						/* Buffered Animation */
						if(buffer){
							$('.buffer').css({
								'z-index':'4',
								'height':f.height(),
								'width':f.width(),
								'margin-left' : (aG[mP].totalW-f.width())/2,
								'margin-top' : (aG[mP].totalH-f.height())/2
							});
							f.css('z-index','5').fadeOut(600,function(){
								$('.buffer').animate({
									'height':n.height(),
									'width':n.width(),
									'margin-left' : (aG[mP].totalW-n.width())/2,
									'margin-top' : (aG[mP].totalH-n.height())/2
								},300,'swing',function(){
									n.css({
										position : 'absolute',
										'margin-left' : (aG[mP].totalW-$(this).width())/2,
										'margin-top' : (aG[mP].totalH-$(this).height())/2,
										'z-index' : 3
									}).show();
									animating=false;
									/* Alter Text */
									$('.descriptionText').html(n.attr('imageLabel').replace(/ /g,'')==''?' ':n.attr('imageLabel'));
									
									$('.buffer').fadeOut(1000,function(){
										n.css('z-index','5');
										$('.buffer').show();
									});
								});
							});
						}else{
							/* Non Buffered Animation */
							f.css('z-index',5);
							n.css('z-index',4).show();
							f.fadeOut(1500,function(){
								animating=false;
								/* Alter Text */
								$('.descriptionText').html(n.attr('imageLabel').replace(/ /g,'')==''?' ':n.attr('imageLabel'));
							},'swing');
						}
					};
					var looping=true;
					var t = setTimeout(function(){beginLoop();},transitionTime);
					var beginLoop = function(){
						nextSlide();
						t = setTimeout(function(){beginLoop();},transitionTime);
					};
					var play = function(){
						clearTimeout(t);
						$('.play').hide();
						$('.pause').show();
						beginLoop();
					};
					var pause = function(){
						clearTimeout(t);
						$('.play').show();
						$('.pause').hide();
					};
					$('.play').click(play);
					$('.pause').click(pause);
					$('.back').click(function(){nextSlide(true);play();});
					$('.next').click(function(){nextSlide();play();});
				/* If the image is loaded from cache, the load event doesn't fire, this checks if it's already loaded and the load event missed */
				}).each(function(){
					if(this.complete) $(this).trigger("load");
				});
			}
		});
	}
});
}
