// Modified 01/19/10 by Pamela

// Changed loading behavior to give priority to the large image


if (Spry) {
	var loader = new Spry.Utils.ImageLoader();
    var priority=1;
}


//Makes some adjustments for IE
function osCheck(){
	if (navigator.appName=="Microsoft Internet Explorer" && document.getElementById('MainNav'))
		{
		document.getElementById('MainNav').style.letterSpacing='10px';
		document.getElementById('ContentShadow').style.visibility='hidden';
		document.getElementById('BannerShadow').style.visibility='hidden';
		};
}


//Calls the IE adjustments
function commonPageLoad() {
	osCheck();
}


//Quickly fades out the glowing home page icon called on mouseout
function fadeOutIcon(image) {
var fadeAway = new Spry.Effect.Fade(image,{duration: 250, from: 100, to: 0, toggle: false});
fadeAway.start();
}

//Quickly fades in the glowing home page icon called on mouseover
function fadeInIcon(image) {
var fadeBack =  new Spry.Effect.Fade(image,{duration:250, from: 0, to: 100, toggle: false});
fadeBack.start();
}

// Set the frame height around the image
function setFrame (myPhoto){
	document.getElementById(myPhoto.id + "Frame").style.height = myPhoto.height + "px";
}
	

//*********************** Image-loading logic ************************************

// Called for each item in the default project page, and each item in a newly-clicked tab.
//Sets highest priority to the project that's currently visible
function preloadFirstGallery(dataSet, row, rowIndex) {
	var folder = dataSet.getURL().replace(/PhotoInfo.xml/,"");
	if (rowIndex == 0)//Gives the first large image highest priority
		loader.load(folder + "Large/" + row["@path"],myTab,priority + 4);

	loader.load(folder + "Small/" + row["@thumbpath"], "",priority + 3); //Loads current thumbnails second
	loader.load(folder + "Large/" + row["@path"], "",priority + 2); //Loads current large images third.
	return row;
}

function myTab(){
}



// Called once for each item in the non-visible project pages
function preloadGallery(dataSet, row, rowIndex) {
	var folder = dataSet.getURL();
	folder = folder.replace(/PhotoInfo.xml/,"");
	loader.load(folder + "Small/" + row["@thumbpath"], "",priority+1);
	loader.load(folder + "Large/" + row["@path"], "",priority);
	return row;
}


//Called when a tab is clicked;assigns the newly-visible project page higher priority than the last project page
function galleryLoaded(galleryElement,frameElement,dataSet) {
//	Setting the frame height is now done on load of the frame to ensure the image has loaded
    priority+=6;
    dataSet.filterData(preloadFirstGallery);
}



// Called when a new thumbnail is clicked; switches the main image to the new thumbnail.
function fadeSwapDetail(image, dataSet, rowID) {
	if (dataSet.curRowID != rowID) { // Only performs this function if the thumbnail is not the current thumbnail
		var fadeOut = false;
		var loaded = false;
		
		// Sets the frame height for the new image, and fades in the image and frame.
		function fadeIn() {
			dataSet.setCurrentRow(rowID);

        	var imgHeight= document.getElementById(image).height;
			document.getElementById(image+"Frame").style.height= imgHeight + "px";

			fadePhoto.start(); // Reverse the animation
        	fadeFrame.start(); // Reverse the animation
		}
		
		
		// Called when the main image is loaded; starts the fadeIn().
		function loadDone(elementObj, fadeObj) {
			if (fadeOut == true) 
				fadeIn();
			else
				loaded = true;
		}
		
		//Waits until the new image is loaded, then calls fadeIn().
		function fadeDone(elementObj, fadeObj) {
			if (loaded == true) 
				fadeIn();
			else
				fadeOut = true;
		}




		var folder = dataSet.getURL().replace(/PhotoInfo.xml/,"");
		var fadePhoto = new Spry.Effect.Fade(image,{duration: 200, from: 100, to: 0, toggle: true, finish:fadeDone});
		var fadeFrame = new Spry.Effect.Fade(image+"Frame",{duration: 200, from: 100, to: 0, toggle: true});
		var newRow = dataSet.getRowByID(rowID);

		
		priority++; // Makes the main image the top load priority; starts fading out the last image and its frame.
		loader.load(folder + "Large/" + newRow["@path"], loadDone, 4+priority);

		fadePhoto.start();
        fadeFrame.start();

	
	}
}
